2024-12-27 21:00:07 +01:00
|
|
|
extends CharacterBody3D
|
2025-02-05 06:33:48 +01:00
|
|
|
class_name Player
|
2024-12-27 21:00:07 +01:00
|
|
|
|
|
|
|
|
const SPEED = 150.0
|
2024-12-31 05:19:57 +01:00
|
|
|
const RUN_SPEED = 400.0
|
2024-12-27 21:00:07 +01:00
|
|
|
const JUMP_VELOCITY = 200
|
|
|
|
|
|
2025-02-05 06:33:48 +01:00
|
|
|
@export var state_machine: LimboHSM
|
|
|
|
|
@onready var idle_state: LimboState = $LimboHSM/Idle
|
|
|
|
|
@onready var move_state: LimboState = $LimboHSM/Move
|
|
|
|
|
@onready var jump_state: LimboState = $LimboHSM/Jump
|
|
|
|
|
@onready var fall_state: LimboState = $LimboHSM/Fall
|
|
|
|
|
@onready var pause_state: LimboState = $LimboHSM/Pause
|
2025-02-04 05:50:59 +01:00
|
|
|
|
2025-02-05 06:33:48 +01:00
|
|
|
var movement_input = Vector3.ZERO
|
2024-12-28 01:58:19 +01:00
|
|
|
var last_direction:= Vector3.ONE
|
2025-01-02 12:09:30 +01:00
|
|
|
@onready var camera: Camera3D = $Camera3D
|
2025-02-05 06:33:48 +01:00
|
|
|
|
|
|
|
|
var armed = false
|
|
|
|
|
var can_pause = true
|
|
|
|
|
|
|
|
|
|
func _ready():
|
|
|
|
|
state_machine = $LimboHSM
|
|
|
|
|
_initialize_state_machine()
|
|
|
|
|
GDRx.on_physics_step()\
|
|
|
|
|
.subscribe(check_buffered_jump).dispose_with(self)
|
|
|
|
|
GDRx.on_physics_step()\
|
|
|
|
|
.subscribe(check_coyote_jump).dispose_with(self)
|
|
|
|
|
GDRx.on_physics_step()\
|
|
|
|
|
.subscribe(handle_jump).dispose_with(self)
|
|
|
|
|
GDRx.on_physics_step()\
|
|
|
|
|
.subscribe(handle_move).dispose_with(self)
|
|
|
|
|
|
|
|
|
|
func _initialize_state_machine():
|
|
|
|
|
state_machine.add_transition(idle_state, move_state, "to_move")
|
|
|
|
|
state_machine.add_transition(move_state, idle_state, "to_idle")
|
|
|
|
|
state_machine.add_transition(move_state, jump_state, "to_jump")
|
|
|
|
|
state_machine.add_transition(idle_state, jump_state, "to_jump")
|
|
|
|
|
state_machine.add_transition(jump_state, fall_state, "to_fall")
|
|
|
|
|
state_machine.add_transition(state_machine.ANYSTATE, pause_state, "to_pause")
|
|
|
|
|
|
|
|
|
|
state_machine.initial_state = idle_state
|
|
|
|
|
state_machine.initialize(self)
|
|
|
|
|
state_machine.set_active(true)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func apply_movement(delta: float) -> void:
|
2024-12-27 21:00:07 +01:00
|
|
|
# Add the gravity.
|
|
|
|
|
if not is_on_floor():
|
|
|
|
|
velocity += get_gravity() * delta
|
2024-12-31 05:19:57 +01:00
|
|
|
var k = RUN_SPEED if Input.is_key_pressed(KEY_SHIFT) else SPEED
|
|
|
|
|
self.velocity.x = delta * k * Input.get_axis("move_left", "move_right")
|
|
|
|
|
self.velocity.z = delta * k * Input.get_axis("move_forward", "move_back")
|
|
|
|
|
|
2025-02-05 06:33:48 +01:00
|
|
|
if Input.is_key_pressed(KEY_E):
|
|
|
|
|
var s: SpineSprite = $MeshInstance3D/SubViewport/SpineSprite
|
|
|
|
|
var a = s.get_animation_state()
|
|
|
|
|
a.set_animation("holding/shoot", false, 3)
|
|
|
|
|
shoot_gun()
|
|
|
|
|
armed = !armed
|
2024-12-31 05:19:57 +01:00
|
|
|
Messagebus.DEBUG_XYZ.emit(self.global_transform.origin)
|
2025-02-05 06:33:48 +01:00
|
|
|
move_and_slide()
|
|
|
|
|
|
|
|
|
|
func shoot_gun():
|
|
|
|
|
var space_state = get_world_3d().direct_space_state
|
2024-12-27 21:00:07 +01:00
|
|
|
|
2025-02-05 06:33:48 +01:00
|
|
|
# use global coordinates, not local to node
|
|
|
|
|
var start = self.to_global(Vector3(0, 0, 0))
|
|
|
|
|
var end = self.to_global(Vector3(50, 0, 0)) * sign(last_direction.x)
|
|
|
|
|
line(start, end, Color.BLACK, 4)
|
|
|
|
|
var query = PhysicsRayQueryParameters3D.create(start, end)
|
|
|
|
|
query.exclude = [self]
|
|
|
|
|
var result = space_state.intersect_ray(query)
|
|
|
|
|
print(result)
|
|
|
|
|
|
|
|
|
|
func line(pos1: Vector3, pos2: Vector3, color = Color.BLACK, persist_ms = 0):
|
|
|
|
|
var mesh_instance := MeshInstance3D.new()
|
|
|
|
|
var immediate_mesh := ImmediateMesh.new()
|
|
|
|
|
var material := ORMMaterial3D.new()
|
|
|
|
|
|
|
|
|
|
mesh_instance.mesh = immediate_mesh
|
|
|
|
|
mesh_instance.cast_shadow = GeometryInstance3D.SHADOW_CASTING_SETTING_OFF
|
|
|
|
|
|
|
|
|
|
immediate_mesh.surface_begin(Mesh.PRIMITIVE_LINES, material)
|
|
|
|
|
immediate_mesh.surface_add_vertex(pos1)
|
|
|
|
|
immediate_mesh.surface_add_vertex(pos1-Vector3(0,0,0.2))
|
|
|
|
|
immediate_mesh.surface_add_vertex(pos2)
|
|
|
|
|
immediate_mesh.surface_add_vertex(pos2-Vector3(0,0,0.2))
|
|
|
|
|
immediate_mesh.surface_end()
|
|
|
|
|
|
|
|
|
|
material.shading_mode = BaseMaterial3D.SHADING_MODE_UNSHADED
|
|
|
|
|
material.albedo_color = color
|
|
|
|
|
|
|
|
|
|
return await final_cleanup(mesh_instance, persist_ms)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func final_cleanup(mesh_instance: MeshInstance3D, persist_ms: float):
|
|
|
|
|
get_tree().get_root().add_child(mesh_instance)
|
|
|
|
|
if persist_ms == 1:
|
|
|
|
|
await get_tree().physics_frame
|
|
|
|
|
mesh_instance.queue_free()
|
|
|
|
|
elif persist_ms > 0:
|
|
|
|
|
await get_tree().create_timer(persist_ms).timeout
|
|
|
|
|
mesh_instance.queue_free()
|
|
|
|
|
else:
|
|
|
|
|
return mesh_instance
|
|
|
|
|
|
|
|
|
|
func apply_jump() -> void:
|
|
|
|
|
pass
|
|
|
|
|
#move_and_slide()
|
|
|
|
|
|
|
|
|
|
func check_coyote_jump(delta) -> void:
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
func check_buffered_jump(delta) -> void:
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
func handle_jump(delta) -> void:
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
func handle_move(delta) -> void:
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
func check_jump_input(delta) -> void:
|
|
|
|
|
check_coyote_jump(delta)
|
|
|
|
|
check_buffered_jump(delta)
|
2025-01-02 14:05:21 +01:00
|
|
|
if Input.is_action_just_pressed("jump") and is_on_floor():
|
2025-02-05 06:33:48 +01:00
|
|
|
state_machine.dispatch("to_jump")
|
2024-12-27 21:00:07 +01:00
|
|
|
self.velocity.y = delta * JUMP_VELOCITY
|
2025-02-05 06:33:48 +01:00
|
|
|
|
|
|
|
|
func _physics_process(delta: float) -> void:
|
2025-01-02 14:05:21 +01:00
|
|
|
var i = Input.get_axis("move_left", "move_right")
|
|
|
|
|
if snapped(i, 1):
|
2025-02-05 06:33:48 +01:00
|
|
|
self.last_direction = Vector3(sign(i), 1, 1)
|
2024-12-31 05:46:18 +01:00
|
|
|
var t: Material = $MeshInstance3D.material_override
|
2025-02-05 06:33:48 +01:00
|
|
|
t.uv1_scale = self.last_direction
|
2025-01-02 12:09:30 +01:00
|
|
|
# handle camera
|
|
|
|
|
camera.position.x = lerp(camera.position.x, position.x, 0.08)
|
|
|
|
|
camera.position.y = lerp(camera.position.y, position.y, 0.08) + 0.07
|
|
|
|
|
camera.position.z = position.z + 4
|
|
|
|
|
|
2025-02-04 05:50:59 +01:00
|
|
|
func _unhandled_input(_event: InputEvent) -> void:
|
2025-01-02 14:05:21 +01:00
|
|
|
if Input.is_action_pressed("zoom_in"):
|
|
|
|
|
camera.fov -= 1 * (5 if Input.is_key_pressed(KEY_SHIFT) else 1)
|
|
|
|
|
camera.size -= 1 * (0.2 if Input.is_key_pressed(KEY_SHIFT) else 0.05)
|
|
|
|
|
if Input.is_action_pressed("zoom_out"):
|
|
|
|
|
camera.fov += 1 * (5 if Input.is_key_pressed(KEY_SHIFT) else 1)
|
|
|
|
|
camera.size += 1 * (0.2 if Input.is_key_pressed(KEY_SHIFT) else 0.05)
|