2021-11-18 20:07:42 +00:00
|
|
|
class_name BaseTrackLevel
|
2021-11-12 23:03:35 +00:00
|
|
|
extends Spatial
|
|
|
|
|
2022-01-19 10:39:45 +00:00
|
|
|
const CAMERA_CONTROLLER = preload("res://player/cameras/camera_controller.gd")
|
2022-01-20 10:38:35 +00:00
|
|
|
const PLAYER_CONTROLLER = preload("res://player/vehicle_controller.gd")
|
2021-11-18 23:36:05 +00:00
|
|
|
|
2022-01-19 10:39:45 +00:00
|
|
|
var player_node: BuggedVehicle
|
2021-11-13 22:55:18 +00:00
|
|
|
var gui: Node
|
2022-01-19 10:39:45 +00:00
|
|
|
var camera_controller: CameraController
|
2022-01-20 10:38:35 +00:00
|
|
|
var player_controller: PlayerVehicleController
|
2022-01-20 15:50:44 +00:00
|
|
|
var ready = false
|
2021-11-12 23:03:35 +00:00
|
|
|
|
2021-11-18 23:36:05 +00:00
|
|
|
onready var track = $Track
|
|
|
|
|
2021-11-18 23:08:21 +00:00
|
|
|
|
2021-11-12 23:03:35 +00:00
|
|
|
func _ready() -> void:
|
2022-01-20 15:50:44 +00:00
|
|
|
if player_node != null:
|
|
|
|
_spawn_in_player()
|
|
|
|
ready = true
|
2021-11-12 23:03:35 +00:00
|
|
|
|
2021-11-18 23:08:21 +00:00
|
|
|
|
2022-01-19 10:39:45 +00:00
|
|
|
func spawn_player(player_node: BuggedVehicle, gui: Node) -> void:
|
2021-11-12 23:03:35 +00:00
|
|
|
self.player_node = player_node
|
2021-11-13 22:55:18 +00:00
|
|
|
self.gui = gui
|
2022-01-20 15:50:44 +00:00
|
|
|
if ready:
|
|
|
|
_spawn_in_player()
|
|
|
|
|
|
|
|
|
|
|
|
func spawn_vehicle(vehicle: BuggedVehicle) -> void:
|
2022-01-21 12:50:50 +00:00
|
|
|
vehicle.connect("position_updated", track, "_on_player_position_updated")
|
2022-01-20 15:50:44 +00:00
|
|
|
reset_player_to(track.get_furthest_checkpoint(), vehicle)
|
|
|
|
add_child(vehicle)
|
|
|
|
|
|
|
|
|
|
|
|
func remove_player(peer_id: String) -> void:
|
|
|
|
get_node(peer_id).queue_free()
|
2021-11-18 23:08:21 +00:00
|
|
|
|
|
|
|
|
|
|
|
func reset_player_to(node_to_reset_to: Node, player_node: BuggedVehicle) -> void:
|
2021-11-20 16:34:08 +00:00
|
|
|
player_node.reset_transform = node_to_reset_to.global_transform.looking_at(
|
|
|
|
node_to_reset_to.global_transform.translated(
|
|
|
|
node_to_reset_to.global_transform.basis.z
|
|
|
|
).origin,
|
|
|
|
node_to_reset_to.global_transform.basis.y
|
|
|
|
)
|
2022-01-20 19:52:50 +00:00
|
|
|
player_node.reset_transform.origin += node_to_reset_to.global_transform.basis.y * 2
|
2021-11-18 23:08:21 +00:00
|
|
|
|
|
|
|
|
2022-01-20 15:50:44 +00:00
|
|
|
func _spawn_in_player():
|
|
|
|
reset_player_to(track.get_furthest_checkpoint(), player_node)
|
2022-01-21 12:50:50 +00:00
|
|
|
player_node.connect("position_updated", track, "_on_player_position_updated")
|
2022-01-20 15:50:44 +00:00
|
|
|
add_child(player_node)
|
|
|
|
add_child(gui)
|
|
|
|
player_controller = PLAYER_CONTROLLER.new()
|
2022-01-21 22:06:33 +00:00
|
|
|
player_controller.input_sink_path = player_node.get_path()
|
2022-01-20 15:50:44 +00:00
|
|
|
player_node.add_child(player_controller)
|
|
|
|
camera_controller = CAMERA_CONTROLLER.new()
|
|
|
|
camera_controller.attach_cameras_to(player_node)
|
|
|
|
|
|
|
|
|
2021-11-20 16:44:35 +00:00
|
|
|
func _process(_delta: float) -> void:
|
|
|
|
if Input.is_action_just_released("reset_vehicle"):
|
2021-11-20 17:46:23 +00:00
|
|
|
reset_player_to(track.get_furthest_checkpoint(), player_node)
|
2021-11-20 16:44:35 +00:00
|
|
|
|
2022-01-19 10:39:45 +00:00
|
|
|
if Input.is_action_just_released("next_camera"):
|
|
|
|
camera_controller.next_camera()
|
|
|
|
|
2022-01-24 19:04:29 +00:00
|
|
|
camera_controller.update_camera(
|
|
|
|
(
|
|
|
|
Input.get_action_strength("turn_camera_right")
|
|
|
|
- Input.get_action_strength("turn_camera_left")
|
|
|
|
),
|
|
|
|
Input.get_action_strength("turn_camera_up") - Input.get_action_strength("turn_camera_down"),
|
|
|
|
Input.is_action_pressed("look_backwards")
|
|
|
|
)
|
|
|
|
|
2021-11-20 16:44:35 +00:00
|
|
|
|
2021-11-18 23:08:21 +00:00
|
|
|
func _on_ResetArea_body_entered(body: Node) -> void:
|
|
|
|
if body.get_groups().has("car"):
|
2021-11-20 17:46:23 +00:00
|
|
|
reset_player_to(track.get_furthest_checkpoint(), body)
|