TinyAdventure/objects/player.gd

82 lines
2.6 KiB
GDScript
Raw Normal View History

2024-07-20 09:49:49 +02:00
class_name Player
2024-07-09 22:33:38 +02:00
extends CharacterBody3D
2024-09-08 16:57:39 +02:00
const SPEED = 4.0
2024-07-09 22:33:38 +02:00
const JUMP_VELOCITY = 2.5
2024-09-08 16:57:39 +02:00
@onready var geometry:Node3D = %Geometry
@onready var actionable_detector = %ActionableDetector
2024-09-08 16:57:39 +02:00
@onready var animation_tree:AnimationTree = $Geometry/Rogue/AnimationTree
2024-07-09 22:33:38 +02:00
# Get the gravity from the project settings to be synced with RigidBody nodes.
var gravity = ProjectSettings.get_setting("physics/3d/default_gravity")
var inventory:Inventory = Inventory.new()
2024-09-08 16:57:39 +02:00
var look_direction:Vector3 = Vector3.BACK
2024-07-09 22:33:38 +02:00
signal trigger_message(message:String)
2024-09-08 16:57:39 +02:00
var interaction_state:bool = false
func _process(_delta):
animation_tree.set("parameters/conditions/attack", interaction_state)
if interaction_state:
interaction_state = false
2024-07-09 22:33:38 +02:00
func _physics_process(delta):
# Add the gravity.
if not is_on_floor():
velocity.y -= gravity * delta
# 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")
var direction = (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)
move_and_slide()
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:
look_direction = Vector3(ground_velocity.x, 0, ground_velocity.y).normalized()
elif direction.length_squared() > 0.1 * 0.1:
look_direction = direction
2024-07-12 11:27:10 +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)
geometry.look_at(position - look_direction, Vector3.UP)
2024-07-12 14:32:33 +02:00
2024-07-09 22:33:38 +02:00
func on_item_picked_up(item:Item):
emit_signal("trigger_message", "Picked up a " + item.name)
2024-07-12 14:32:33 +02:00
inventory.add_item(item)
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-09-08 16:57:39 +02:00
print("player position: " + str(position) + " actionables position: " + str(actionables[0].global_position))
look_direction = (actionables[0].global_position - position).normalized()
# geometry.look_at(actionables[0].global_position, Vector3.UP)
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"):
interaction_state = true
get_viewport().set_input_as_handled()
return