Script que contiene todos los datos del jugador como la vida y las acciones que puede realizar, moverse, saltar, golpear
extends CharacterBody3D
var currentHealth : int = 10
var maxHealth : int = 10
var damage : int = 1
var gold : int = 0
var attackRate : float = 0.3
var lastAttackTime : int = 0
var moveSpeed : float = 5.0
var jumpForce : float = 5.0
var gravity : float = 20.0
var vel : Vector3 = Vector3()
@onready var camera = $CameraPivot
@onready var animacion = $PortaEspada/Animacion
@onready var attackRayCast = $RayCast3D
@onready var ui =$"../CanvasLayer/UI"
func _ready ():
ui.update_health_bar(currentHealth, maxHealth)
ui.update_gold_label(gold)
func _process(delta):
if Input.is_action_just_pressed("ataque"):
try_attack()
func try_attack ():
if Time.get_ticks_msec() - lastAttackTime < attackRate * 1000:
return
lastAttackTime = Time.get_ticks_msec()
animacion.stop()
animacion.play("attack")
if attackRayCast.is_colliding():
if attackRayCast.get_collider().has_method("take_damage"):
attackRayCast.get_collider().take_damage(damage)
func _physics_process(delta):
var input_dir = Input.get_vector( "mover_izquierda", "mover_derecha", "mover_delante", "mover_atras")
var direction = (transform.basis * Vector3(input_dir.x, 0, input_dir.y)).normalized()
velocity.x = direction.x * moveSpeed
velocity.z = direction.z * moveSpeed
if not is_on_floor():
velocity.y -= gravity * delta
if Input.is_action_pressed("saltar") and is_on_floor():
velocity.y = jumpForce
move_and_slide()
func aumenta_oro (cantidad):
gold += cantidad
ui.update_gold_label(gold)
func take_damage (damageToTake):
currentHealth -= damageToTake
ui.update_health_bar(currentHealth, maxHealth)
if currentHealth <= 0:
die()
func die():
get_tree().change_scene_to_file("res://death.tscn")