summaryrefslogtreecommitdiff
path: root/Scenes/Entities/Enemies
diff options
context:
space:
mode:
authorDaniel Weipert <code@drogueronin.de>2023-08-10 11:48:05 +0200
committerDaniel Weipert <code@drogueronin.de>2023-08-10 11:48:05 +0200
commit46556d864b9685c3b09a0038f5de83966fe7ff94 (patch)
treec68082eacd35559e14565d1598dd694972fb8e0e /Scenes/Entities/Enemies
Initial commit
Diffstat (limited to 'Scenes/Entities/Enemies')
-rw-r--r--Scenes/Entities/Enemies/Enemy.gd81
-rw-r--r--Scenes/Entities/Enemies/Enemy.tscn91
2 files changed, 172 insertions, 0 deletions
diff --git a/Scenes/Entities/Enemies/Enemy.gd b/Scenes/Entities/Enemies/Enemy.gd
new file mode 100644
index 0000000..5b72c04
--- /dev/null
+++ b/Scenes/Entities/Enemies/Enemy.gd
@@ -0,0 +1,81 @@
+extends CharacterBody2D
+
+
+const SPEED = 30
+
+const DIRECTIONS = [Vector2.UP, Vector2.RIGHT, Vector2.DOWN, Vector2.LEFT]
+var CURRENT_DIRECTION = Vector2.UP
+
+@export var health: int = 4
+
+var is_invincible = false
+
+
+func _ready():
+ add_to_group("enemies")
+
+ var collision_area = Utilities.Collision.Area.new(self, $CollisionShape2D)
+ collision_area.connect("collided", Callable(self, "_collide"))
+ add_child(collision_area)
+
+ $Label.text = str(self.health)
+
+
+func _physics_process(delta):
+ if CURRENT_DIRECTION == Vector2.UP:
+ velocity.y -= SPEED
+ $AnimatedSprite2D.play("up")
+ elif CURRENT_DIRECTION == Vector2.DOWN:
+ velocity.y += SPEED
+ $AnimatedSprite2D.play("down")
+ elif CURRENT_DIRECTION == Vector2.LEFT:
+ velocity.x -= SPEED
+ $AnimatedSprite2D.flip_h = true
+ $AnimatedSprite2D.play("left")
+ elif CURRENT_DIRECTION == Vector2.RIGHT:
+ velocity.x += SPEED
+ $AnimatedSprite2D.flip_h = false
+ $AnimatedSprite2D.play("right")
+
+ move_and_collide(velocity * delta)
+ velocity = velocity.lerp(Vector2(0, 0), 1)
+
+
+func take_damage(amount):
+ if self.is_invincible:
+ return
+
+ self.set_invincibility()
+
+ self.health -= amount
+
+ if self.health == 0:
+ queue_free()
+
+ $Label.text = str(self.health)
+
+
+func _collide(area: Area2D):
+ if area.is_in_group("explosions"):
+ self.take_damage(4)
+
+
+func _on_movement_timer_timeout():
+ var directions = self.DIRECTIONS.duplicate()
+
+ directions.remove_at(directions.find(CURRENT_DIRECTION))
+ directions.shuffle()
+
+ CURRENT_DIRECTION = directions[0]
+
+ $MovementTimer.start()
+
+
+func set_invincibility():
+ $InvincibilityTimer.start()
+ $AnimatedSprite2D.set_modulate(Color(10, 1, 1, 1))
+ self.is_invincible = true
+
+func _on_invincibility_timer_timeout():
+ $AnimatedSprite2D.set_modulate(Color(1, 1, 1, 1))
+ self.is_invincible = false
diff --git a/Scenes/Entities/Enemies/Enemy.tscn b/Scenes/Entities/Enemies/Enemy.tscn
new file mode 100644
index 0000000..f78d0e5
--- /dev/null
+++ b/Scenes/Entities/Enemies/Enemy.tscn
@@ -0,0 +1,91 @@
+[gd_scene load_steps=9 format=3 uid="uid://dgyk1sged38ct"]
+
+[ext_resource type="Script" path="res://Scenes/Entities/Enemies/Enemy.gd" id="1_k16kd"]
+[ext_resource type="Texture2D" uid="uid://5dk0c1kpvdgs" path="res://Assets/bomb_party_v4.png" id="2_cjc2s"]
+
+[sub_resource type="RectangleShape2D" id="RectangleShape2D_gw5gm"]
+size = Vector2(11, 11.5)
+
+[sub_resource type="AtlasTexture" id="AtlasTexture_t7fm8"]
+atlas = ExtResource("2_cjc2s")
+region = Rect2(16, 224, 16, 16)
+
+[sub_resource type="AtlasTexture" id="AtlasTexture_pyhot"]
+atlas = ExtResource("2_cjc2s")
+region = Rect2(64, 224, 16, 16)
+
+[sub_resource type="AtlasTexture" id="AtlasTexture_aj58r"]
+atlas = ExtResource("2_cjc2s")
+region = Rect2(64, 224, 16, 16)
+
+[sub_resource type="AtlasTexture" id="AtlasTexture_spvfr"]
+atlas = ExtResource("2_cjc2s")
+region = Rect2(0, 224, 16, 16)
+
+[sub_resource type="SpriteFrames" id="SpriteFrames_t5cld"]
+animations = [{
+"frames": [{
+"duration": 1.0,
+"texture": SubResource("AtlasTexture_t7fm8")
+}],
+"loop": true,
+"name": &"down",
+"speed": 5.0
+}, {
+"frames": [{
+"duration": 1.0,
+"texture": SubResource("AtlasTexture_pyhot")
+}],
+"loop": true,
+"name": &"left",
+"speed": 5.0
+}, {
+"frames": [{
+"duration": 1.0,
+"texture": SubResource("AtlasTexture_aj58r")
+}],
+"loop": true,
+"name": &"right",
+"speed": 5.0
+}, {
+"frames": [{
+"duration": 1.0,
+"texture": SubResource("AtlasTexture_spvfr")
+}],
+"loop": true,
+"name": &"up",
+"speed": 5.0
+}]
+
+[node name="Enemy" type="CharacterBody2D"]
+collision_layer = 16
+collision_mask = 62
+script = ExtResource("1_k16kd")
+
+[node name="CollisionShape2D" type="CollisionShape2D" parent="."]
+position = Vector2(0.5, -0.25)
+shape = SubResource("RectangleShape2D_gw5gm")
+metadata/_edit_lock_ = true
+
+[node name="AnimatedSprite2D" type="AnimatedSprite2D" parent="."]
+sprite_frames = SubResource("SpriteFrames_t5cld")
+animation = &"down"
+
+[node name="Label" type="Label" parent="."]
+offset_left = -5.0
+offset_top = -20.0
+offset_right = 16.0
+offset_bottom = 6.0
+scale = Vector2(0.5, 0.5)
+text = "hp"
+
+[node name="MovementTimer" type="Timer" parent="."]
+wait_time = 3.0
+autostart = true
+
+[node name="InvincibilityTimer" type="Timer" parent="."]
+wait_time = 1.5
+one_shot = true
+
+[connection signal="timeout" from="MovementTimer" to="." method="_on_movement_timer_timeout"]
+[connection signal="timeout" from="InvincibilityTimer" to="." method="_on_invincibility_timer_timeout"]