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
+1 -2
View File
@@ -195,7 +195,7 @@ radius = 5.0
height = 10.0
[sub_resource type="CircleShape2D" id="CircleShape2D_chgu2"]
radius = 4.0
radius = 6.0
[sub_resource type="RectangleShape2D" id="RectangleShape2D_chgu2"]
size = Vector2(8, 8)
@@ -419,7 +419,6 @@ position = Vector2(0, 3)
shape = SubResource("CapsuleShape2D_bqpwv")
[node name="HitBoxArea2D" type="Area2D" parent="." unique_id=1052547818]
visible = false
collision_layer = 2
collision_mask = 16