open-strike-2/scripts/player/player_input_contrller.gd

160 lines
5.7 KiB
GDScript3
Raw Normal View History

# ---------------------------------------------------------------------
# This script is supposed to handle character movement logic
# ---------------------------------------------------------------------
class_name PlayerInput extends CharacterBody3D
@export_category("PlayerInput")
const SPEED = 5.0
const JUMP_VELOCITY = 4.5
var mouse_captured: bool = false
@onready var player_synchronizer: MultiplayerSynchronizer = $PlayerSynchronizer
@onready var first_view_camera_mount: Node3D = $FirstPersonCameraMount
@onready var first_view_camera: Camera3D = $FirstPersonCameraMount/Camera
@onready var model_mount: Node3D = $ModelMount
# -- This node is supposed to be spawned per player, and since each
# -- player has an id, it is used for giving a node a name. So we can
# -- use it here to tell a controlled node from the rest models
@onready var owner_id: int = str($".".name).to_int()
var multiplayer_id: int = 0
# -- Character state
var alive: bool = true
func _ready() -> void:
player_synchronizer.set_multiplayer_authority(owner_id)
multiplayer_id = multiplayer.get_unique_id()
# -- Separate logic for player and other models that are controlled
# -- by other players on the server
# -- TODO: If player is alive, it must not be able to see other
# -- cameras, but if player is dead, it must be able to
# -- switch between other players
if _is_current_player():
var err := _add_first_view_model()
if err != OK:
print("Error occured: " + str(err))
_enable_camera()
else:
var err := _add_world_model()
if err != OK:
print("Error occured: " + str(err))
_capture_mouse()
# -- Add a world model to the player, that should be seen by other players
# -- on the server
func _add_world_model() -> Error :
# -- TODO: It should not be hardcoded
var path := "res://scenes/characters/blue/dummy.tscn"
if not ResourceLoader.exists(path):
return ERR_DOES_NOT_EXIST
var scene: PackedScene = ResourceLoader.load(path)
if not scene.can_instantiate():
return ERR_CANT_OPEN
var node: Node3D = scene.instantiate()
model_mount.add_child(node)
return OK
# -- Add the first person view to pthe player
func _add_first_view_model() -> Error :
# -- TODO: It should not be hardcoded
var path := "res://scenes/characters/first_person_view.tscn"
if not ResourceLoader.exists(path):
return ERR_DOES_NOT_EXIST
var scene: PackedScene = ResourceLoader.load(path)
if not scene.can_instantiate():
return ERR_CANT_OPEN
var node: Node3D = scene.instantiate()
first_view_camera.add_child(node)
return OK
func _enable_camera():
first_view_camera.make_current()
func _capture_mouse() -> void:
Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED)
mouse_captured = true
func _release_mouse() -> void:
Input.set_mouse_mode(Input.MOUSE_MODE_VISIBLE)
mouse_captured = false
# -- Does the peer own the node
func _is_current_player() -> bool:
if owner_id == multiplayer_id:
return true
return false
# ---------------------------------------------------------------------
# -- Input controller
# ---------------------------------------------------------------------
var move_dir: Vector2 # Input direction for movement
var look_dir: Vector2 # Input direction for look/aim
var walk_vel: Vector3 # Walking velocity
var grav_vel: Vector3 # Gravity velocity
var jump_vel: Vector3 # Jumping velocity
@export_range(0.1, 3.0, 0.1) var jump_height: float = 1 # m
@export_range(0.1, 3.0, 0.1, "or_greater") var camera_sens: float = 1
func _unhandled_input(event: InputEvent) -> void:
if _is_current_player():
if event is InputEventMouseMotion:
look_dir = event.relative * 0.001
if mouse_captured: _rotate_camera()
#if Input.is_action_just_pressed("jump"): jumping = true
if Input.is_action_just_pressed("exit"): get_tree().quit()
if Input.is_action_pressed("shot"): _shoot()
#if str($"..".name).to_int() == multiplayer.get_unique_id():
#if Input.is_action_just_pressed("shot"): $UpperTorso/ViewModelCamera.shot()
#if Input.is_action_just_pressed("reload"): $Body/UpperTorso/CameraMount/Camera.reload()
func _rotate_camera(sens_mod: float = 1.0) -> void:
#if str($"..".name).to_int() == multiplayer.get_unique_id():
rotation.y -= look_dir.x * camera_sens * sens_mod
first_view_camera_mount.rotation.x = clamp(first_view_camera_mount.rotation.x - look_dir.y * camera_sens * sens_mod, -1.5, 1.5)
func _physics_process(delta: float) -> void:
# Add the gravity.
if not is_on_floor():
velocity += get_gravity() * delta
# Handle jump.
if Input.is_action_just_pressed("jump") and is_on_floor():
velocity.y = JUMP_VELOCITY
# Get the input direction and handle the movement/deceleration.
# As good practice, you should replace UI actions with custom gameplay actions.
#var input_dir := Input.get_vector("ui_left", "ui_right", "ui_up", "ui_down")
# -- It shouldn't be possible to change direction during the jumps
if is_on_floor():
var input_dir := Input.get_vector("move_left", "move_right", "move_forward", "move_backwards")
var direction := (transform.basis * Vector3(input_dir.x, 0, input_dir.y)).normalized()
if direction:
velocity.x = direction.x * SPEED
velocity.z = direction.z * SPEED
else:
velocity.x = move_toward(velocity.x, 0, SPEED)
velocity.z = move_toward(velocity.z, 0, SPEED)
move_and_slide()
# -- TODO: It shouldn't be hardcoded
var bullet = load("res://scenes/weapon/bullet.tscn")
@onready var shooting_raycast: RayCast3D = $FirstPersonCameraMount/RayCast3D
@onready var bullet_starting_point: Node3D = $FirstPersonCameraMount/BulletStartingPoint
func _shoot():
print("shooting")
var root := get_tree().get_root()
#if !gun_anim.is_playing():
#gun_anim.play("Shoot")
var node: Node3D = bullet.instantiate()
node.position = bullet_starting_point.global_position
node.transform.basis = bullet_starting_point.global_transform.basis
root.add_child(node)