feat: add knockback functionality; add the hp to the slime;

This commit is contained in:
2026-09-24 22:22:42 +02:00
parent 2aad0915e3
commit 20c127e11d
3 changed files with 19 additions and 6 deletions
+13 -2
View File
@@ -4,6 +4,7 @@ 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
# Called when the node enters the scene tree for the first time.
@@ -27,7 +28,7 @@ func _physics_process(delta: float) -> void:
func move_player():
var move_vector: Vector2 = Input.get_vector("ui_left", "ui_right", "ui_up", "ui_down")
velocity = move_vector * move_speed
velocity = velocity.move_toward(move_vector * move_speed, move_acceleration)
if velocity.x > 0:
$AnimatedSprite2D.play("move_right")
@@ -71,13 +72,23 @@ func _on_hit_box_area_2d_body_entered(body: Node2D) -> void:
%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
func die():
get_tree().call_deferred("reload_current_scene")
SceneManager.player_hp = 3
func _on_sword_area_2d_body_entered(body: Node2D) -> void:
body.queue_free()
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
if (body.HP <= 0):
body.queue_free()
func attack():
var player_animation: String = $AnimatedSprite2D.animation