2024-07-20 09:49:49 +02:00
|
|
|
class_name Player
|
2024-07-09 22:33:38 +02:00
|
|
|
extends CharacterBody3D
|
|
|
|
|
2024-09-10 22:26:26 +02:00
|
|
|
const SPEED = 3.0
|
2024-10-11 23:50:17 +02:00
|
|
|
const JUMP_VELOCITY = 3.5
|
2024-07-09 22:33:38 +02:00
|
|
|
|
2024-09-08 16:57:39 +02:00
|
|
|
@onready var geometry:Node3D = %Geometry
|
2024-07-10 22:52:28 +02:00
|
|
|
@onready var actionable_detector = %ActionableDetector
|
2024-11-01 11:51:59 +01:00
|
|
|
@onready var animation_tree:AnimationTree = $Geometry/AnimationTree
|
2024-09-10 22:26:26 +02:00
|
|
|
@onready var build_location:Node3D = %BuildLocation
|
2024-11-01 11:51:59 +01:00
|
|
|
@onready var right_hand_attachement:HandTool = %RightHandAttachement
|
2024-11-01 18:07:23 +01:00
|
|
|
|
|
|
|
var tool_damage:bool = false :
|
2024-11-01 11:51:59 +01:00
|
|
|
get:
|
|
|
|
return tool_damage
|
|
|
|
set(value):
|
|
|
|
tool_damage = value
|
|
|
|
|
|
|
|
if right_hand_attachement != null and right_hand_attachement.tool_hitbox:
|
|
|
|
right_hand_attachement.tool_hitbox.monitorable = bool(tool_damage)
|
2024-07-10 22:52:28 +02:00
|
|
|
|
2024-07-09 22:33:38 +02:00
|
|
|
var gravity = ProjectSettings.get_setting("physics/3d/default_gravity")
|
|
|
|
var inventory:Inventory = Inventory.new()
|
2024-09-14 12:45:42 +02:00
|
|
|
var selected_tool_slot_index:int = 0
|
2024-10-18 12:01:15 +02:00
|
|
|
var current_tool:ItemResource = null
|
2024-07-09 22:33:38 +02:00
|
|
|
|
2024-11-15 11:05:01 +01:00
|
|
|
var _current_look_direction:Vector3 = Vector3.BACK
|
|
|
|
var _target_look_direction:Vector3 = Vector3.BACK
|
|
|
|
var _look_angle_damper:SpringDamper = SpringDamper.new(0, 4, 0.06, 0.003)
|
2024-07-10 22:52:28 +02:00
|
|
|
|
2024-11-01 18:07:23 +01:00
|
|
|
var is_input_blocked:bool = false
|
2024-07-09 22:33:38 +02:00
|
|
|
|
2024-10-20 22:27:27 +02:00
|
|
|
signal trigger_message(message:String)
|
2024-09-08 16:57:39 +02:00
|
|
|
|
|
|
|
func _process(_delta):
|
2024-10-20 22:27:27 +02:00
|
|
|
if animation_tree.get("parameters/playback").get_current_node() == "Interact":
|
|
|
|
animation_tree.set("parameters/conditions/interact", false)
|
2024-07-09 22:33:38 +02:00
|
|
|
|
2024-10-20 22:27:27 +02:00
|
|
|
func _handle_input() -> void:
|
2024-07-09 22:33:38 +02:00
|
|
|
# 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("walk_left", "walk_right", "walk_forward", "walk_back")
|
2024-11-15 11:05:01 +01:00
|
|
|
var direction = Vector3(input_dir.x, 0, input_dir.y).normalized()
|
|
|
|
if direction:
|
|
|
|
velocity.x = direction.x * SPEED
|
|
|
|
velocity.z = direction.z * SPEED
|
2024-07-09 22:33:38 +02:00
|
|
|
else:
|
|
|
|
velocity.x = move_toward(velocity.x, 0, SPEED)
|
|
|
|
velocity.z = move_toward(velocity.z, 0, SPEED)
|
|
|
|
|
2024-10-11 23:50:17 +02:00
|
|
|
if is_on_floor() and Input.is_action_just_pressed("jump"):
|
|
|
|
velocity.y = JUMP_VELOCITY
|
|
|
|
|
2024-10-20 22:27:27 +02:00
|
|
|
func _physics_process(delta):
|
|
|
|
# Add the gravity.
|
|
|
|
if not is_on_floor():
|
|
|
|
velocity.y -= gravity * delta
|
|
|
|
|
2024-11-01 18:07:23 +01:00
|
|
|
if not is_input_blocked:
|
2024-10-20 22:27:27 +02:00
|
|
|
_handle_input()
|
|
|
|
|
2024-07-09 22:33:38 +02:00
|
|
|
move_and_slide()
|
2024-07-10 22:52:28 +02:00
|
|
|
|
2024-07-12 11:59:33 +02:00
|
|
|
var ground_velocity:Vector2 = Vector2(velocity.x, velocity.z)
|
2024-09-08 16:57:39 +02:00
|
|
|
var is_moving:bool = ground_velocity.length_squared() > 0.1 * 0.1
|
2024-07-12 11:59:33 +02:00
|
|
|
|
2024-09-08 16:57:39 +02:00
|
|
|
if is_moving:
|
2024-11-15 11:05:01 +01:00
|
|
|
_target_look_direction = Vector3(velocity.x, 0, velocity.z).normalized()
|
|
|
|
|
|
|
|
_current_look_direction = basis.z
|
|
|
|
#_target_look_direction = _current_look_direction
|
2024-07-12 11:27:10 +02:00
|
|
|
|
2024-11-15 11:05:01 +01:00
|
|
|
update_look_direction(delta)
|
2024-09-08 21:11:02 +02:00
|
|
|
|
2024-09-08 16:57:39 +02:00
|
|
|
animation_tree.set("parameters/conditions/running", is_moving)
|
|
|
|
animation_tree.set("parameters/conditions/idle", not is_moving)
|
2024-11-15 11:05:01 +01:00
|
|
|
|
|
|
|
func init_look_target_direction() -> void:
|
|
|
|
_target_look_direction = basis.z
|
|
|
|
|
|
|
|
func update_look_direction(delta:float) -> void:
|
|
|
|
var current_look_angle:float = _current_look_direction.signed_angle_to(Vector3.BACK, Vector3.UP)
|
|
|
|
var target_look_angle:float = _target_look_direction.signed_angle_to(Vector3.BACK, Vector3.UP)
|
|
|
|
if target_look_angle - current_look_angle > PI:
|
|
|
|
current_look_angle = current_look_angle + PI * 2
|
|
|
|
elif current_look_angle - target_look_angle > PI:
|
|
|
|
current_look_angle = current_look_angle - PI * 2
|
|
|
|
|
|
|
|
var damped_look_angle:float = _look_angle_damper.calc(current_look_angle, target_look_angle, delta)
|
|
|
|
|
|
|
|
_current_look_direction = Vector3(sin(-damped_look_angle), 0, cos(-damped_look_angle))
|
|
|
|
self.basis = Basis.looking_at(-_current_look_direction)
|
|
|
|
|
2024-07-12 14:32:33 +02:00
|
|
|
|
2024-10-04 17:25:17 +02:00
|
|
|
func set_tool_slot_index(value:int) -> void:
|
|
|
|
if inventory == null:
|
|
|
|
return
|
|
|
|
|
|
|
|
selected_tool_slot_index = value
|
|
|
|
set_right_hand_item(inventory.get_tool_item_stacks()[selected_tool_slot_index].item)
|
2024-07-09 22:33:38 +02:00
|
|
|
|
2024-10-18 12:01:15 +02:00
|
|
|
func on_item_picked_up(item:ItemResource):
|
2024-10-11 18:40:40 +02:00
|
|
|
trigger_message.emit("Picked up a " + item.name)
|
2024-07-12 14:32:33 +02:00
|
|
|
inventory.add_item(item)
|
2024-07-10 22:52:28 +02:00
|
|
|
|
|
|
|
|
2024-09-08 22:06:34 +02:00
|
|
|
func get_actionable_global_transform() -> Vector3:
|
2024-09-10 22:26:26 +02:00
|
|
|
return build_location.global_position
|
2024-09-08 22:06:34 +02:00
|
|
|
|
2024-09-14 12:45:42 +02:00
|
|
|
|
2024-10-18 12:01:15 +02:00
|
|
|
func set_right_hand_item(item:ItemResource) -> void:
|
2024-09-27 18:09:30 +02:00
|
|
|
current_tool = null
|
2024-09-27 11:46:31 +02:00
|
|
|
|
2024-11-01 11:51:59 +01:00
|
|
|
right_hand_attachement.set_tool(item.scene.instantiate())
|
|
|
|
|
2024-09-27 11:46:31 +02:00
|
|
|
if item != null and item.is_tool:
|
2024-09-27 18:09:30 +02:00
|
|
|
current_tool = item
|
2024-09-27 11:46:31 +02:00
|
|
|
|
2024-09-30 18:02:20 +02:00
|
|
|
animation_tree.set("parameters/Interact/conditions/tool_action_slice", false)
|
|
|
|
animation_tree.set("parameters/Interact/conditions/tool_action_chop", false)
|
|
|
|
|
|
|
|
match item.tool_action:
|
|
|
|
1:
|
|
|
|
animation_tree.set("parameters/Interact/conditions/tool_action_chop", true)
|
|
|
|
2:
|
|
|
|
animation_tree.set("parameters/Interact/conditions/tool_action_slice", true)
|
|
|
|
|
2024-09-27 18:09:30 +02:00
|
|
|
func has_build_tool_active() -> bool:
|
2024-10-02 17:33:11 +02:00
|
|
|
if current_tool and current_tool.resource_path == "res://data/items/hammer.tres":
|
2024-09-27 18:09:30 +02:00
|
|
|
return true
|
|
|
|
|
|
|
|
return false
|
2024-09-27 11:46:31 +02:00
|
|
|
|
2024-09-27 18:09:30 +02:00
|
|
|
func handle_tool_slot_input_events(_event:InputEvent) -> bool:
|
2024-09-14 12:45:42 +02:00
|
|
|
var key_event:InputEventKey = _event as InputEventKey
|
|
|
|
|
|
|
|
if key_event and key_event.is_released():
|
|
|
|
if key_event.keycode >= KEY_1 and key_event.keycode <= KEY_9:
|
2024-10-04 17:25:17 +02:00
|
|
|
set_tool_slot_index(key_event.keycode - KEY_1)
|
2024-09-14 12:45:42 +02:00
|
|
|
return true
|
|
|
|
|
|
|
|
if _event.is_action_released("item_next"):
|
2024-10-04 17:25:17 +02:00
|
|
|
set_tool_slot_index((selected_tool_slot_index + 1) % (inventory.get_tool_item_stacks().size()))
|
2024-09-14 12:45:42 +02:00
|
|
|
return true
|
|
|
|
|
|
|
|
if _event.is_action_released("item_prev"):
|
|
|
|
var tool_slot_size = inventory.get_tool_item_stacks().size()
|
2024-10-04 17:25:17 +02:00
|
|
|
set_tool_slot_index((selected_tool_slot_index + tool_slot_size - 1) % (tool_slot_size))
|
2024-09-14 12:45:42 +02:00
|
|
|
return true
|
|
|
|
|
|
|
|
return false
|
|
|
|
|
2024-07-10 22:52:28 +02:00
|
|
|
func _unhandled_input(_event: InputEvent) -> void:
|
|
|
|
if Input.is_action_just_pressed("ui_accept"):
|
|
|
|
var actionables = actionable_detector.get_overlapping_areas()
|
|
|
|
if actionables.size() > 0:
|
2024-11-15 11:05:01 +01:00
|
|
|
_target_look_direction = (actionables[0].global_position - position).normalized()
|
2024-08-16 00:24:27 +02:00
|
|
|
actionables[0].action()
|
2024-09-08 16:57:39 +02:00
|
|
|
|
|
|
|
get_viewport().set_input_as_handled()
|
|
|
|
return
|
|
|
|
|
|
|
|
if Input.is_action_just_pressed("interaction"):
|
2024-10-20 22:27:27 +02:00
|
|
|
animation_tree.set("parameters/conditions/interact", true)
|
2024-09-08 16:57:39 +02:00
|
|
|
|
|
|
|
get_viewport().set_input_as_handled()
|
|
|
|
return
|