153 lines
4.7 KiB
GDScript
153 lines
4.7 KiB
GDScript
extends CharacterBody2D
|
|
class_name Player
|
|
|
|
@export var move_speed: float = 100
|
|
@export var hp: int = 10
|
|
@export var push_strength: int = 50
|
|
@export var move_acceleration: float = 50
|
|
var is_attacking: bool = false
|
|
var can_interact: bool = false
|
|
|
|
# Called when the node enters the scene tree for the first time.
|
|
func _ready() -> void:
|
|
update_scrolls()
|
|
%HealthBar.play("3_hp")
|
|
var damage: float = 2
|
|
if SceneManager.player_next_scene_spawn_pos != Vector2(0, 0):
|
|
position = SceneManager.player_next_scene_spawn_pos
|
|
|
|
|
|
# Called every frame. 'delta' is the elapsed time since the previous frame.
|
|
func _physics_process(delta: float) -> void:
|
|
if SceneManager.player_hp <= 0:
|
|
return
|
|
|
|
if not is_attacking:
|
|
move_player()
|
|
push_blocks()
|
|
update_scrolls()
|
|
move_and_slide()
|
|
if Input.is_action_just_pressed("Interact") and not can_interact:
|
|
attack()
|
|
|
|
func move_player():
|
|
var move_vector: Vector2 = Input.get_vector("ui_left", "ui_right", "ui_up", "ui_down")
|
|
velocity = velocity.move_toward(move_vector * move_speed, move_acceleration)
|
|
|
|
if velocity.x > 0:
|
|
$AnimatedSprite2D.play("move_right")
|
|
$InteractArea2D.position = Vector2(5, 2)
|
|
elif velocity.x < 0:
|
|
$AnimatedSprite2D.play("move_left")
|
|
$InteractArea2D.position = Vector2(-5, 2)
|
|
elif velocity.y > 0:
|
|
$AnimatedSprite2D.play("move_down")
|
|
$InteractArea2D.position = Vector2(0, 8)
|
|
elif velocity.y < 0:
|
|
$AnimatedSprite2D.play("move_up")
|
|
$InteractArea2D.position = Vector2(0, -4)
|
|
else:
|
|
$AnimatedSprite2D.stop()
|
|
|
|
|
|
func push_blocks():
|
|
var collision: KinematicCollision2D = get_last_slide_collision()
|
|
if collision:
|
|
var collisision_node = collision.get_collider()
|
|
if collisision_node.is_in_group("pushable"):
|
|
var collision_normal: Vector2 = collision.get_normal()
|
|
collisision_node.apply_central_force(-collision_normal * push_strength)
|
|
|
|
func update_scrolls():
|
|
$CanvasLayer/Panel/Label.text = str(SceneManager.opened_chests.size())
|
|
|
|
func _on_area_2d_body_entered(body: Node2D) -> void:
|
|
if body.is_in_group("interactable"):
|
|
can_interact = true
|
|
body.can_interact = true
|
|
|
|
|
|
func _on_area_2d_body_exited(body: Node2D) -> void:
|
|
if body.is_in_group("interactable"):
|
|
can_interact = false
|
|
body.can_interact = false
|
|
|
|
|
|
func _on_hit_box_area_2d_body_entered(body: Node2D) -> void:
|
|
SceneManager.player_hp -= 1
|
|
%HealthBar.play(str(SceneManager.player_hp) + "_hp")
|
|
if SceneManager.player_hp <= 0:
|
|
die()
|
|
var distance_to_player: Vector2 = global_position - body.global_position
|
|
var normalize_knockback: Vector2 = distance_to_player.normalized()
|
|
print("distancetoplayer: ", distance_to_player)
|
|
velocity += normalize_knockback * 500
|
|
var flash_white_color: Color = Color(50, 50, 50)
|
|
modulate = flash_white_color
|
|
await get_tree().create_timer(0.2).timeout
|
|
var original_color: Color = Color(1, 1, 1)
|
|
modulate = original_color
|
|
|
|
|
|
func die():
|
|
$AnimatedSprite2D.play("death")
|
|
$DeathTimer.start()
|
|
|
|
func _on_sword_area_2d_body_entered(body: Node2D) -> void:
|
|
var distance_to_enemy: Vector2 = body.global_position - global_position
|
|
var knockback_direction: Vector2 = distance_to_enemy.normalized()
|
|
var knockback_strength = 150
|
|
body.velocity += knockback_direction * knockback_strength
|
|
body.HP -= 1
|
|
var flash_red_color: Color = Color(20, 0, 0)
|
|
body.modulate = flash_red_color
|
|
await get_tree().create_timer(0.2).timeout
|
|
var original_color: Color = Color(1, 1, 1)
|
|
body.modulate = original_color
|
|
if (body.HP <= 0):
|
|
body.queue_free()
|
|
|
|
func attack():
|
|
$Sword/SwordSFX.play()
|
|
if not $Sword/SwordTimer.is_stopped():
|
|
return
|
|
var player_animation: String = $AnimatedSprite2D.animation
|
|
if player_animation == "move_right":
|
|
$AnimatedSprite2D.play("attack_right")
|
|
$AnimationPlayer.play("attack_right")
|
|
if player_animation == "move_left":
|
|
$AnimatedSprite2D.play("attack_left")
|
|
$AnimationPlayer.play("attack_left")
|
|
if player_animation == "move_up":
|
|
$AnimatedSprite2D.play("attack_up")
|
|
$AnimationPlayer.play("attack_up")
|
|
if player_animation == "move_down":
|
|
$AnimatedSprite2D.play("attack_down")
|
|
$AnimationPlayer.play("attack_bottom")
|
|
|
|
is_attacking = true
|
|
velocity = Vector2(0,0 )
|
|
|
|
$Sword.visible = true
|
|
$Sword/SwordArea2D.monitoring = true
|
|
$Sword/SwordTimer.start()
|
|
|
|
func _on_sword_timer_timeout() -> void:
|
|
is_attacking = false
|
|
$Sword.visible = false
|
|
$Sword/SwordArea2D.monitoring = false
|
|
var player_animation: String = $AnimatedSprite2D.animation
|
|
if player_animation == "attack_right":
|
|
$AnimatedSprite2D.play("move_right")
|
|
if player_animation == "attack_left":
|
|
$AnimatedSprite2D.play("move_left")
|
|
if player_animation == "attack_up":
|
|
$AnimatedSprite2D.play("move_up")
|
|
if player_animation == "attack_down":
|
|
$AnimatedSprite2D.play("move_down")
|
|
|
|
|
|
func _on_death_timer_timeout() -> void:
|
|
get_tree().call_deferred("reload_current_scene")
|
|
SceneManager.player_hp = 3
|