From a38f65c261e9d7b23d046cb76b5aeedc05c4b54d Mon Sep 17 00:00:00 2001 From: Daniel Weipert Date: Fri, 20 Sep 2024 22:12:44 +0200 Subject: next commit --- Enemies/Enemy.tscn | 8 ++++++++ Enemies/enemy.gd | 50 ++++++++++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 56 insertions(+), 2 deletions(-) (limited to 'Enemies') diff --git a/Enemies/Enemy.tscn b/Enemies/Enemy.tscn index 678b33a..2791e05 100644 --- a/Enemies/Enemy.tscn +++ b/Enemies/Enemy.tscn @@ -36,5 +36,13 @@ sprite_frames = SubResource("SpriteFrames_n2gm8") [node name="VisibleOnScreenNotifier2D" type="VisibleOnScreenNotifier2D" parent="."] process_mode = 3 +[node name="RayLeft" type="RayCast2D" parent="."] +target_position = Vector2(-9, 0) +collision_mask = 16 + +[node name="RayRight" type="RayCast2D" parent="."] +target_position = Vector2(9, 0) +collision_mask = 16 + [connection signal="screen_entered" from="VisibleOnScreenNotifier2D" to="." method="_on_visible_on_screen_notifier_2d_screen_entered"] [connection signal="screen_exited" from="VisibleOnScreenNotifier2D" to="." method="_on_visible_on_screen_notifier_2d_screen_exited"] diff --git a/Enemies/enemy.gd b/Enemies/enemy.gd index ab1ceac..428f6ca 100644 --- a/Enemies/enemy.gd +++ b/Enemies/enemy.gd @@ -1,8 +1,14 @@ +class_name Enemy extends CharacterBody2D const SPEED = 30 +var direction = -1 : + set(value): + direction = value + $AnimatedSprite2D.flip_h = direction > 0 + func _ready() -> void: process_mode = PROCESS_MODE_DISABLED @@ -14,14 +20,54 @@ func _physics_process(delta: float) -> void: if not is_on_floor(): velocity += get_gravity() * delta - velocity.x = -SPEED + velocity.x = direction * SPEED move_and_slide() + if get_last_slide_collision(): + if $RayLeft.is_colliding(): + direction = 1 + elif $RayRight.is_colliding(): + direction = -1 + + +func on_hit(projectile_position: Vector2): + var impact_direction = global_position - projectile_position + var impact_direction_x = sign(impact_direction.x) + + var effect_star_scene := preload("res://effect_star.tscn") + + var directions = [] + if randi() % 10 > 5: + directions = [Vector2(1, 1) * 8, Vector2(1, -1) * 8, Vector2(-1, -1) * 8, Vector2(-1, 1) * 8] + else: + directions = [ + Vector2(impact_direction_x, 1) * 16, + Vector2(impact_direction_x, -1) * 16, + Vector2(impact_direction_x, 0.25) * 16, + Vector2(impact_direction_x, -0.25) * 16, + ] + + for effect_direction in directions: + var effect_star := effect_star_scene.instantiate() + effect_star.global_position = global_position + get_tree().current_scene.add_child(effect_star) + var tween := get_tree().create_tween().set_ease(Tween.EASE_OUT) + tween.tween_property( + effect_star, + "global_position", + global_position + effect_direction, + 0.1 + ) + tween.tween_callback(func(): + effect_star.queue_free() + ).set_delay(0.2) + + queue_free() func _on_visible_on_screen_notifier_2d_screen_entered() -> void: process_mode = PROCESS_MODE_INHERIT - func _on_visible_on_screen_notifier_2d_screen_exited() -> void: + return process_mode = PROCESS_MODE_DISABLED -- cgit v1.2.3