diff options
Diffstat (limited to 'Enemies/enemy.gd')
-rw-r--r-- | Enemies/enemy.gd | 50 |
1 files changed, 48 insertions, 2 deletions
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 |