## SPDX-License-Identifier: GPL-3.0-or-later ## Copyright (c) 2024 interstellardevelopment.org class_name Player extends CharacterBody3D const SPEED: float = 5.0 const JUMP_VELOCITY: float = 4.5 var activated: bool = false var freecam: bool = false @onready var camera: Camera3D = $Camera3D var camera_rotation: Vector2 = Vector2(0,0) func _ready() -> void: if name == Game.username: activated = true camera.current = true Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED) func _physics_process(delta: float) -> void: if activated: if !freecam: if not is_on_floor(): velocity += get_gravity() * delta if !freecam: if Input.is_action_just_pressed("jump") and is_on_floor(): velocity.y = JUMP_VELOCITY if freecam: if Input.is_action_pressed("jump"): velocity.y = JUMP_VELOCITY elif Input.is_action_pressed("descend"): velocity.y = -JUMP_VELOCITY else: velocity.y = 0 if Input.is_action_just_pressed("freecam"): freecam = !freecam ($CollisionShape3D as CollisionShape3D).disabled = !($CollisionShape3D as CollisionShape3D).disabled var input_dir: Vector2 = Input.get_vector("move_left", "move_right", "move_forwards", "move_backwards") var direction: Vector3 = (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) if move_and_slide(): pass var fps_counter: Label = $Label fps_counter.text = str(Engine.get_frames_per_second()) Networking.player_sync_call(position, rotation) func _input(event: InputEvent) -> void: if event is InputEventMouseMotion and activated: var mouse_event: InputEventMouseMotion = event camera_rotation = mouse_event.relative * 0.01 * 0.5 if camera.rotation.x <= 1.6 && camera_rotation.y <= 0: camera.rotate(Vector3.RIGHT, -camera_rotation.y) elif camera.rotation.x >= -1.6 && camera_rotation.y >= 0: camera.rotate(Vector3.RIGHT, -camera_rotation.y) if camera.rotation.x >= 1.6: camera.rotation.x = 1.6 elif camera.rotation.x <= -1.6: camera.rotation.x = -1.6 rotate(Vector3.DOWN, camera_rotation.x) # this way we can check for the player. func playerstub() -> void: pass