89 lines
2.8 KiB
GDScript
89 lines
2.8 KiB
GDScript
extends RigidBody3D
|
|
# Initially I used a CharacterBody3D, however, I wanted the player to bounce off
|
|
# other objects in the environment and that would have required manual handling
|
|
# of collisions. So that's why we're using a RigidBody3D instead.
|
|
|
|
const MOVE_SPEED := 8.0
|
|
const ACCELLERATION := 30.0
|
|
const DECELLERATION := 40.0
|
|
const JUMP_SPEED := 4.0
|
|
const MAX_NUMBER_OF_JUMPS := 2
|
|
|
|
var mouse_sensitivity := 0.005
|
|
var rotation_x := 0.0
|
|
var rotation_y := 0.0
|
|
var cameraMoveMode := false
|
|
var current_number_of_jumps := 0
|
|
|
|
var jump_sound = preload("res://assets/audio/jump.ogg")
|
|
var audio_player = AudioStreamPlayer.new()
|
|
|
|
@export var camera_path: NodePath
|
|
@onready var cam: Camera3D = get_node(camera_path) if camera_path != NodePath("") else null
|
|
|
|
func _ready() -> void:
|
|
axis_lock_angular_x = true
|
|
axis_lock_angular_z = true
|
|
angular_damp = 6.0
|
|
contact_monitor = true
|
|
max_contacts_reported = 4
|
|
add_child(audio_player)
|
|
audio_player.stream = jump_sound
|
|
audio_player.volume_db = -20
|
|
|
|
func _integrate_forces(state):
|
|
# Input as 2D vector
|
|
var input2v := Input.get_vector("ui_left", "ui_right", "ui_up", "ui_down")
|
|
|
|
# Camera based movement
|
|
var forward := Vector3.FORWARD * -1.0
|
|
var right := Vector3.RIGHT
|
|
if cam:
|
|
forward = cam.global_transform.basis.z
|
|
right = cam.global_transform.basis.x
|
|
# project onto ground plane so looking up/down doesn't tilt movement
|
|
forward.y = 0.0
|
|
right.y = 0.0
|
|
if forward.length() > 0.0001:
|
|
forward = forward.normalized()
|
|
if right.length() > 0.0001:
|
|
right = right.normalized()
|
|
|
|
var dir := (right * input2v.x + forward * input2v.y).normalized()
|
|
var target_v := dir * MOVE_SPEED
|
|
|
|
var ax := ACCELLERATION if dir != Vector3.ZERO else DECELLERATION
|
|
linear_velocity.x = move_toward(linear_velocity.x, target_v.x, ax * state.step)
|
|
linear_velocity.z = move_toward(linear_velocity.z, target_v.z, ax * state.step)
|
|
|
|
# Jump Logic
|
|
var on_floor = false
|
|
for i in state.get_contact_count():
|
|
var normal = state.get_contact_local_normal(i)
|
|
if normal.y > 0.5:
|
|
on_floor = true
|
|
break
|
|
|
|
if Input.is_action_just_pressed("ui_accept") and (on_floor or current_number_of_jumps == 1):
|
|
current_number_of_jumps = (current_number_of_jumps + 1) % 2
|
|
linear_velocity.y = JUMP_SPEED
|
|
audio_player.play()
|
|
|
|
func _input(event):
|
|
if event is InputEventMouseButton:
|
|
if event.button_index == MOUSE_BUTTON_MIDDLE:
|
|
if event.pressed:
|
|
cameraMoveMode = true
|
|
Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED)
|
|
else:
|
|
cameraMoveMode = false
|
|
Input.set_mouse_mode(Input.MOUSE_MODE_VISIBLE)
|
|
|
|
if event is InputEventMouseMotion and cameraMoveMode:
|
|
rotation_x -= event.relative.y * mouse_sensitivity
|
|
rotation_y -= event.relative.x * mouse_sensitivity
|
|
rotation_x = clamp(rotation_x, deg_to_rad(-90), deg_to_rad(90)) # Prevent flipping
|
|
|
|
$Camera3D.rotation.x = rotation_x
|
|
rotation.y = rotation_y
|