diff options
author | Daniel Weipert <git@mail.dweipert.de> | 2024-09-28 15:27:57 +0200 |
---|---|---|
committer | Daniel Weipert <git@mail.dweipert.de> | 2024-09-28 15:27:57 +0200 |
commit | 6032b3dc82ccbb3710061059c90b7db40df19673 (patch) | |
tree | a99682fb23bcd8a1ddd01f4f137bee2d46fa4b1f /Enemies | |
parent | a38f65c261e9d7b23d046cb76b5aeedc05c4b54d (diff) |
Diffstat (limited to 'Enemies')
-rw-r--r-- | Enemies/FlyingEnemy.tscn | 48 | ||||
-rw-r--r-- | Enemies/WalkingEnemy.tscn (renamed from Enemies/Enemy.tscn) | 9 | ||||
-rw-r--r-- | Enemies/enemy.gd | 16 | ||||
-rw-r--r-- | Enemies/flying_enemy.gd | 30 | ||||
-rw-r--r-- | Enemies/walking_enemy.gd | 40 |
5 files changed, 137 insertions, 6 deletions
diff --git a/Enemies/FlyingEnemy.tscn b/Enemies/FlyingEnemy.tscn new file mode 100644 index 0000000..0389300 --- /dev/null +++ b/Enemies/FlyingEnemy.tscn @@ -0,0 +1,48 @@ +[gd_scene load_steps=6 format=3 uid="uid://bsqgvrwpn3ll5"] + +[ext_resource type="Script" path="res://Enemies/flying_enemy.gd" id="1_6lct6"] +[ext_resource type="Texture2D" uid="uid://c62o6lkel0hm" path="res://Assets/Enemies/enemy-flying-01.png" id="2_xlcih"] +[ext_resource type="Texture2D" uid="uid://cmh7xpn5isthn" path="res://Assets/Enemies/enemy-flying-02.png" id="3_nmmbb"] + +[sub_resource type="CircleShape2D" id="CircleShape2D_p646n"] +radius = 8.0 + +[sub_resource type="SpriteFrames" id="SpriteFrames_n2gm8"] +animations = [{ +"frames": [{ +"duration": 1.0, +"texture": ExtResource("2_xlcih") +}, { +"duration": 1.0, +"texture": ExtResource("3_nmmbb") +}], +"loop": true, +"name": &"default", +"speed": 4.0 +}] + +[node name="FlyingEnemy" type="CharacterBody2D" groups=["enemy"]] +collision_layer = 2 +collision_mask = 17 +script = ExtResource("1_6lct6") + +[node name="CollisionShape2D" type="CollisionShape2D" parent="."] +shape = SubResource("CircleShape2D_p646n") + +[node name="AnimatedSprite2D" type="AnimatedSprite2D" parent="."] +texture_filter = 1 +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.tscn b/Enemies/WalkingEnemy.tscn index 2791e05..28ce169 100644 --- a/Enemies/Enemy.tscn +++ b/Enemies/WalkingEnemy.tscn @@ -1,7 +1,7 @@ [gd_scene load_steps=6 format=3 uid="uid://6d20yrqejxs8"] +[ext_resource type="Script" path="res://Enemies/walking_enemy.gd" id="1_kngny"] [ext_resource type="Texture2D" uid="uid://cduruhn8mecgn" path="res://Assets/Enemies/enemy-walk-01.png" id="1_v3aim"] -[ext_resource type="Script" path="res://Enemies/enemy.gd" id="1_x68ej"] [ext_resource type="Texture2D" uid="uid://ysaejkoo1bnw" path="res://Assets/Enemies/enemy-walk-02.png" id="2_madb5"] [sub_resource type="CircleShape2D" id="CircleShape2D_p646n"] @@ -21,10 +21,10 @@ animations = [{ "speed": 4.0 }] -[node name="Enemy" type="CharacterBody2D" groups=["enemy"]] +[node name="WalkingEnemy" type="CharacterBody2D" groups=["enemy"]] collision_layer = 2 collision_mask = 17 -script = ExtResource("1_x68ej") +script = ExtResource("1_kngny") [node name="CollisionShape2D" type="CollisionShape2D" parent="."] shape = SubResource("CircleShape2D_p646n") @@ -44,5 +44,8 @@ collision_mask = 16 target_position = Vector2(9, 0) collision_mask = 16 +[node name="BackForthTimer" type="Timer" parent="."] + [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"] +[connection signal="timeout" from="BackForthTimer" to="." method="_on_back_forth_timer_timeout"] diff --git a/Enemies/enemy.gd b/Enemies/enemy.gd index 428f6ca..eed482d 100644 --- a/Enemies/enemy.gd +++ b/Enemies/enemy.gd @@ -2,15 +2,22 @@ class_name Enemy extends CharacterBody2D -const SPEED = 30 +@export var speed: float = 30.0 var direction = -1 : set(value): direction = value $AnimatedSprite2D.flip_h = direction > 0 +@onready var disable_timer = Timer.new() + func _ready() -> void: + disable_timer.wait_time = 3.0 + disable_timer.one_shot = true + disable_timer.timeout.connect(_on_disable_timer_timeout) + add_child(disable_timer) + process_mode = PROCESS_MODE_DISABLED @@ -20,7 +27,7 @@ func _physics_process(delta: float) -> void: if not is_on_floor(): velocity += get_gravity() * delta - velocity.x = direction * SPEED + velocity.x = direction * speed move_and_slide() if get_last_slide_collision(): @@ -66,8 +73,11 @@ func on_hit(projectile_position: Vector2): func _on_visible_on_screen_notifier_2d_screen_entered() -> void: + disable_timer.stop() process_mode = PROCESS_MODE_INHERIT func _on_visible_on_screen_notifier_2d_screen_exited() -> void: - return + disable_timer.start() + +func _on_disable_timer_timeout(): process_mode = PROCESS_MODE_DISABLED diff --git a/Enemies/flying_enemy.gd b/Enemies/flying_enemy.gd new file mode 100644 index 0000000..701affc --- /dev/null +++ b/Enemies/flying_enemy.gd @@ -0,0 +1,30 @@ +extends Enemy + + +@export var angular_speed = 3.0 +var alpha = 0 + +enum MovementType { + UP_DOWN, + FORWARD, +} +@export var movement_type: MovementType = MovementType.UP_DOWN + + +func _physics_process(delta: float) -> void: + $AnimatedSprite2D.play() + + if movement_type == MovementType.UP_DOWN: + velocity.y = sin(alpha) * 32 + elif movement_type == MovementType.FORWARD: + velocity.x = direction * speed + velocity.y = sin(alpha) * 16 + + alpha += delta * angular_speed + + move_and_slide() + if get_last_slide_collision(): + if $RayLeft.is_colliding(): + direction = 1 + elif $RayRight.is_colliding(): + direction = -1 diff --git a/Enemies/walking_enemy.gd b/Enemies/walking_enemy.gd new file mode 100644 index 0000000..be36e2d --- /dev/null +++ b/Enemies/walking_enemy.gd @@ -0,0 +1,40 @@ +extends Enemy + + +@export var acceleration: float = 10.0 + +enum MovementType { + BACK_FORTH, + FORWARD, +} +@export var movement_type: MovementType = MovementType.BACK_FORTH + +@export var back_forth_time: float = 1.0 + + +func _ready() -> void: + super._ready() + + if movement_type == MovementType.BACK_FORTH: + $BackForthTimer.wait_time = back_forth_time + $BackForthTimer.start() + + +func _physics_process(delta: float) -> void: + $AnimatedSprite2D.play() + + if not is_on_floor(): + velocity += get_gravity() * delta + + velocity.x = move_toward(velocity.x, direction * speed, acceleration) + + move_and_slide() + if get_last_slide_collision(): + if $RayLeft.is_colliding(): + direction = 1 + elif $RayRight.is_colliding(): + direction = -1 + + +func _on_back_forth_timer_timeout() -> void: + direction = -direction |