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
+5 -2
View File
@@ -1,6 +1,9 @@
extends CharacterBody2D extends CharacterBody2D
@export var speed: int = 300 @export var speed: int = 300
@export var acceleration: float = 2
@export var HP: int = 2
var target: Node2D var target: Node2D
func _physics_process(delta): func _physics_process(delta):
@@ -13,7 +16,8 @@ func move_slime():
var distance_to_player: Vector2 var distance_to_player: Vector2
distance_to_player = target.global_position - global_position distance_to_player = target.global_position - global_position
var direction_normal: Vector2 = distance_to_player.normalized() var direction_normal: Vector2 = distance_to_player.normalized()
velocity = direction_normal * speed #velocity = direction_normal * speed
velocity = velocity.move_toward(direction_normal * speed, acceleration)
func animate_slime(): func animate_slime():
var normal_velocity: Vector2 = velocity.normalized() var normal_velocity: Vector2 = velocity.normalized()
@@ -26,7 +30,6 @@ func animate_slime():
elif normal_velocity.y < -0.707: elif normal_velocity.y < -0.707:
$AnimatedSprite2D.play("move_up") $AnimatedSprite2D.play("move_up")
func _on_player_detect_area_body_entered(body): func _on_player_detect_area_body_entered(body):
if body is Player: if body is Player:
target = body target = body
+13 -2
View File
@@ -4,6 +4,7 @@ class_name Player
@export var move_speed: float = 100 @export var move_speed: float = 100
@export var hp: int = 10 @export var hp: int = 10
@export var push_strength: int = 50 @export var push_strength: int = 50
@export var move_acceleration: float = 50
var is_attacking: bool = false var is_attacking: bool = false
# Called when the node enters the scene tree for the first time. # 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(): func move_player():
var move_vector: Vector2 = Input.get_vector("ui_left", "ui_right", "ui_up", "ui_down") 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: if velocity.x > 0:
$AnimatedSprite2D.play("move_right") $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") %HealthBar.play(str(SceneManager.player_hp) + "_hp")
if SceneManager.player_hp <= 0: if SceneManager.player_hp <= 0:
die() 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(): func die():
get_tree().call_deferred("reload_current_scene") get_tree().call_deferred("reload_current_scene")
SceneManager.player_hp = 3 SceneManager.player_hp = 3
func _on_sword_area_2d_body_entered(body: Node2D) -> void: 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(): func attack():
var player_animation: String = $AnimatedSprite2D.animation var player_animation: String = $AnimatedSprite2D.animation
+1 -2
View File
@@ -195,7 +195,7 @@ radius = 5.0
height = 10.0 height = 10.0
[sub_resource type="CircleShape2D" id="CircleShape2D_chgu2"] [sub_resource type="CircleShape2D" id="CircleShape2D_chgu2"]
radius = 4.0 radius = 6.0
[sub_resource type="RectangleShape2D" id="RectangleShape2D_chgu2"] [sub_resource type="RectangleShape2D" id="RectangleShape2D_chgu2"]
size = Vector2(8, 8) size = Vector2(8, 8)
@@ -419,7 +419,6 @@ position = Vector2(0, 3)
shape = SubResource("CapsuleShape2D_bqpwv") shape = SubResource("CapsuleShape2D_bqpwv")
[node name="HitBoxArea2D" type="Area2D" parent="." unique_id=1052547818] [node name="HitBoxArea2D" type="Area2D" parent="." unique_id=1052547818]
visible = false
collision_layer = 2 collision_layer = 2
collision_mask = 16 collision_mask = 16