godot: add initial game
This commit is contained in:
57
game/Player.gd
Normal file
57
game/Player.gd
Normal file
@@ -0,0 +1,57 @@
|
||||
extends CharacterBody3D
|
||||
|
||||
const SPEED = 5.0
|
||||
const JUMP_VELOCITY = 4.5
|
||||
const SENSITIVITY = 0.005
|
||||
|
||||
var gravity = ProjectSettings.get_setting("physics/3d/default_gravity")
|
||||
|
||||
@onready var head = $Head
|
||||
@onready var camera = $Head/Camera3D
|
||||
@onready var dash_timer = $DashTimer
|
||||
@onready var player_debug = $Head/Camera3D/PlayerDebug
|
||||
|
||||
func _ready() -> void:
|
||||
Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED)
|
||||
|
||||
func _unhandled_input(event: InputEvent) -> void:
|
||||
if event is InputEventMouseMotion:
|
||||
head.rotate_y(-event.relative.x * SENSITIVITY)
|
||||
camera.rotate_x(-event.relative.y * SENSITIVITY)
|
||||
camera.rotation.x = clamp(camera.rotation.x, deg_to_rad(-40), deg_to_rad(80))
|
||||
|
||||
if Input.is_action_just_pressed("ui_cancel"):
|
||||
Input.set_mouse_mode(Input.MOUSE_MODE_VISIBLE)
|
||||
|
||||
if Input.is_action_just_pressed("debug_overlay"):
|
||||
player_debug.visible = !player_debug.visible
|
||||
|
||||
func _physics_process(delta: float) -> void:
|
||||
if not is_on_floor():
|
||||
velocity.y -= gravity * delta
|
||||
|
||||
if Input.is_action_just_pressed("jump") and is_on_floor():
|
||||
velocity.y = JUMP_VELOCITY
|
||||
|
||||
var input_dir = Input.get_vector("move_left", "move_right", "move_forward", "move_backward")
|
||||
var direction = (head.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 = 0.0
|
||||
velocity.z = 0.0
|
||||
|
||||
if Input.is_action_just_pressed("dash") and dash_timer.is_stopped():
|
||||
velocity.x = velocity.x * 10
|
||||
velocity.z = velocity.z * 10
|
||||
dash_timer.start()
|
||||
|
||||
if player_debug.visible:
|
||||
player_debug.add_stat("vel_x", str(velocity.x))
|
||||
player_debug.add_stat("vel_y", str(velocity.y))
|
||||
player_debug.add_stat("vel_z", str(velocity.z))
|
||||
player_debug.add_stat("dash_cd", str(dash_timer.time_left))
|
||||
|
||||
move_and_slide()
|
Reference in New Issue
Block a user