Add different cameras

pull/1/head
Ensar Sarajčić 2022-01-19 11:39:45 +01:00
parent 618bfc1c61
commit 15d2c83ffc
12 changed files with 127 additions and 10 deletions

Binary file not shown.

View File

@ -0,0 +1,43 @@
class_name CameraController
extends Reference
const FOLLOW_CAMERA = preload("res://player/cameras/follow_camera.tscn")
const STATIC_CAMERA = preload("res://player/cameras/static_camera.tscn")
var _cameras = []
func attach_cameras_to(player_node: BuggedVehicle) -> void:
var cockpit_camera = STATIC_CAMERA.instance()
player_node.get_cockpit_position().add_child(cockpit_camera)
cockpit_camera.rotate(Vector3.UP, deg2rad(180))
var follow_camera = FOLLOW_CAMERA.instance()
follow_camera.global_transform = player_node.global_transform.translated(
-player_node.global_transform.basis.z * 100
)
follow_camera.follow_target_path = player_node.get_path()
player_node.get_parent().add_child(follow_camera)
var bumpera_camera = STATIC_CAMERA.instance()
player_node.get_bumper_position().add_child(bumpera_camera)
bumpera_camera.rotate(Vector3.UP, deg2rad(180))
var hood_camera = STATIC_CAMERA.instance()
player_node.get_hood_position().add_child(hood_camera)
hood_camera.rotate(Vector3.UP, deg2rad(180))
var static_follow_camera = STATIC_CAMERA.instance()
player_node.get_static_follow_position().add_child(static_follow_camera)
static_follow_camera.rotate(Vector3.UP, deg2rad(180))
_cameras = [follow_camera, cockpit_camera, hood_camera, bumpera_camera, static_follow_camera]
select_camera(GlobalSettings.selected_camera)
func select_camera(camera_index: int) -> void:
var select_index = camera_index
if camera_index < 0 or camera_index >= _cameras.size():
select_index = 0
GlobalSettings.selected_camera = select_index
_cameras[select_index].current = true
func next_camera() -> void:
select_camera(GlobalSettings.selected_camera + 1)

View File

@ -1,6 +1,6 @@
[gd_scene load_steps=2 format=2]
[ext_resource path="res://player/camera.gd" type="Script" id=1]
[ext_resource path="res://player/cameras/follow_camera.gd" type="Script" id=1]
[node name="camera" type="Camera"]
transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 5.88022, 23.0639 )

View File

@ -0,0 +1,4 @@
[gd_scene format=2]
[node name="camera" type="Camera"]
far = 1000.0

View File

@ -19,6 +19,11 @@ _global_script_classes=[ {
"language": "GDScript",
"path": "res://vehicles/vehicle.gd"
}, {
"base": "Reference",
"class": "CameraController",
"language": "GDScript",
"path": "res://player/cameras/camera_controller.gd"
}, {
"base": "Particles",
"class": "TireSmoke",
"language": "GDScript",
@ -32,6 +37,7 @@ _global_script_classes=[ {
_global_script_class_icons={
"BaseTrackLevel": "",
"BuggedVehicle": "",
"CameraController": "",
"TireSmoke": "",
"Track": ""
}
@ -124,6 +130,12 @@ reset_vehicle={
, Object(InputEventJoypadButton,"resource_local_to_scene":false,"resource_name":"","device":0,"button_index":3,"pressure":0.0,"pressed":false,"script":null)
]
}
next_camera={
"deadzone": 0.5,
"events": [ Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":0,"alt":false,"shift":false,"control":false,"meta":false,"command":false,"pressed":false,"scancode":70,"physical_scancode":0,"unicode":0,"echo":false,"script":null)
, Object(InputEventJoypadButton,"resource_local_to_scene":false,"resource_name":"","device":0,"button_index":2,"pressure":0.0,"pressed":false,"script":null)
]
}
[physics]

View File

@ -1,10 +1,11 @@
class_name BaseTrackLevel
extends Spatial
const CAMERA = preload("res://player/camera.tscn")
const CAMERA_CONTROLLER = preload("res://player/cameras/camera_controller.gd")
var player_node: Node
var player_node: BuggedVehicle
var gui: Node
var camera_controller: CameraController
onready var track = $Track
@ -13,15 +14,11 @@ func _ready() -> void:
reset_player_to(track.get_furthest_checkpoint(), player_node)
add_child(player_node)
add_child(gui)
var player_camera = CAMERA.instance()
player_camera.global_transform = player_node.global_transform.translated(
-player_node.global_transform.basis.z * 100
)
player_camera.follow_target_path = player_node.get_path()
add_child(player_camera)
camera_controller = CAMERA_CONTROLLER.new()
camera_controller.attach_cameras_to(player_node)
func spawn_player(player_node: Node, gui: Node) -> void:
func spawn_player(player_node: BuggedVehicle, gui: Node) -> void:
self.player_node = player_node
self.gui = gui
@ -40,6 +37,9 @@ func _process(_delta: float) -> void:
if Input.is_action_just_released("reset_vehicle"):
reset_player_to(track.get_furthest_checkpoint(), player_node)
if Input.is_action_just_released("next_camera"):
camera_controller.next_camera()
func _on_ResetArea_body_entered(body: Node) -> void:
if body.get_groups().has("car"):

View File

@ -2,3 +2,4 @@ extends Node
var auto_clutch: bool = false
var automatic_transmission: bool = true
var selected_camera: int = 0

View File

@ -139,3 +139,15 @@ transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, -0.621508, 0.0502438, 1.19336
[node name="fl_tire_smoke" parent="." index="12" instance=ExtResource( 3 )]
transform = Transform( 1, 0, 0, 0, 1, -1.50996e-07, 0, 1.50996e-07, 1, 0.621506, 0.0502438, 1.19336 )
[node name="cockpit" type="Position3D" parent="." index="13"]
transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, 0.159514, 0.993516, 0.421211 )
[node name="hood" type="Position3D" parent="." index="14"]
transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1.01256, 0.87905 )
[node name="bumper" type="Position3D" parent="." index="15"]
transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0.535274, 1.89154 )
[node name="static_follow" type="Position3D" parent="." index="16"]
transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1.59115, -3.01375 )

View File

@ -139,3 +139,15 @@ transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, 0.950753, -0.0380455, -1.41438
[node name="rr_tire_smoke" parent="." index="12" instance=ExtResource( 2 )]
transform = Transform( 1, 0, 0, 0, 1, -1.50996e-07, 0, 1.50996e-07, 1, -0.950755, -0.038045, -1.41438 )
[node name="cockpit" type="Position3D" parent="." index="13"]
transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1.36101, 0 )
[node name="hood" type="Position3D" parent="." index="14"]
transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1.31056, 0.894584 )
[node name="bumper" type="Position3D" parent="." index="15"]
transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0.811448, 1.79879 )
[node name="static_follow" type="Position3D" parent="." index="16"]
transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 2.15146, -4.84184 )

View File

@ -138,3 +138,15 @@ transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, 1.18159, 0.117865, -1.93644 )
[node name="rr_tire_smoke" parent="." index="12" instance=ExtResource( 1 )]
transform = Transform( 1, 0, 0, 0, 1, -1.50996e-07, 0, 1.50996e-07, 1, -1.02536, 0.101852, -1.93766 )
[node name="cockpit" type="Position3D" parent="." index="13"]
transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, 0.265389, 1.1411, 0.337731 )
[node name="hood" type="Position3D" parent="." index="14"]
transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1.00873, 0.756477 )
[node name="bumper" type="Position3D" parent="." index="15"]
transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0.605405, 1.5324 )
[node name="static_follow" type="Position3D" parent="." index="16"]
transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1.88984, -4.78212 )

View File

@ -47,6 +47,11 @@ onready var flsmoke: TireSmoke = $fl_tire_smoke
onready var rrsmoke: TireSmoke = $rr_tire_smoke
onready var rlsmoke: TireSmoke = $rl_tire_smoke
onready var cockpit: Position3D = $cockpit
onready var hood: Position3D = $hood
onready var bumper: Position3D = $bumper
onready var static_follow: Position3D = $static_follow
onready var engine_sound_player: AudioStreamPlayer3D = $engine_sound_player
onready var engine_sound_playback: AudioStreamPlayback = $engine_sound_player.get_stream_playback()
@ -66,6 +71,22 @@ func _ready():
engine_sound_player.play()
func get_cockpit_position() -> Node:
return cockpit
func get_hood_position() -> Node:
return hood
func get_bumper_position() -> Node:
return bumper
func get_static_follow_position() -> Node:
return static_follow
func _integrate_forces(state: PhysicsDirectBodyState) -> void:
if reset_transform != Transform.IDENTITY:
state.linear_velocity = Vector3.ZERO