From 8bfe4412262b9426d8f5eb0a26c0cb90dfc43b19 Mon Sep 17 00:00:00 2001 From: Ruslan Ignatov Date: Mon, 10 Nov 2025 21:35:35 +0300 Subject: [PATCH] Added enemies --- game/controllers/enemy_controller.gd | 18 ++++++++++ game/controllers/enemy_controller.gd.uid | 1 + game/controllers/enemy_controller.tscn | 6 ++++ game/entities/ships/abstract_ship.gd | 29 +++++++++++++++ game/entities/ships/abstract_ship.tscn | 3 -- .../ships/enemies/abstract_enemy_ship.gd | 8 +++++ .../ships/enemies/abstract_enemy_ship.gd.uid | 1 + .../ships/enemies/abstract_enemy_ship.tscn | 18 ++++++++++ .../ships/enemies/heavy/heavy_enemy_ship.gd | 2 ++ .../enemies/heavy/heavy_enemy_ship.gd.uid | 1 + .../ships/enemies/heavy/heavy_enemy_ship.tscn | 32 +++++++++++++++++ .../ships/enemies/medium/medium_enemy_ship.gd | 2 ++ .../enemies/medium/medium_enemy_ship.gd.uid | 1 + .../enemies/medium/medium_enemy_ship.tscn | 31 ++++++++++++++++ .../ships/enemies/small/small_enemy_ship.gd | 2 ++ .../enemies/small/small_enemy_ship.gd.uid | 1 + .../ships/enemies/small/small_enemy_ship.tscn | 31 ++++++++++++++++ game/entities/ships/player/player_ship.gd | 8 +++++ .../ships/{ => player}/player_ship.gd.uid | 0 .../ships/{ => player}/player_ship.tscn | 9 +++-- game/entities/ships/player_ship.gd | 35 ------------------- game/passage.gd | 23 +++++++++++- 22 files changed, 221 insertions(+), 41 deletions(-) create mode 100644 game/controllers/enemy_controller.gd create mode 100644 game/controllers/enemy_controller.gd.uid create mode 100644 game/controllers/enemy_controller.tscn create mode 100644 game/entities/ships/enemies/abstract_enemy_ship.gd create mode 100644 game/entities/ships/enemies/abstract_enemy_ship.gd.uid create mode 100644 game/entities/ships/enemies/abstract_enemy_ship.tscn create mode 100644 game/entities/ships/enemies/heavy/heavy_enemy_ship.gd create mode 100644 game/entities/ships/enemies/heavy/heavy_enemy_ship.gd.uid create mode 100644 game/entities/ships/enemies/heavy/heavy_enemy_ship.tscn create mode 100644 game/entities/ships/enemies/medium/medium_enemy_ship.gd create mode 100644 game/entities/ships/enemies/medium/medium_enemy_ship.gd.uid create mode 100644 game/entities/ships/enemies/medium/medium_enemy_ship.tscn create mode 100644 game/entities/ships/enemies/small/small_enemy_ship.gd create mode 100644 game/entities/ships/enemies/small/small_enemy_ship.gd.uid create mode 100644 game/entities/ships/enemies/small/small_enemy_ship.tscn create mode 100644 game/entities/ships/player/player_ship.gd rename game/entities/ships/{ => player}/player_ship.gd.uid (100%) rename game/entities/ships/{ => player}/player_ship.tscn (88%) delete mode 100644 game/entities/ships/player_ship.gd diff --git a/game/controllers/enemy_controller.gd b/game/controllers/enemy_controller.gd new file mode 100644 index 0000000..e721cc8 --- /dev/null +++ b/game/controllers/enemy_controller.gd @@ -0,0 +1,18 @@ +class_name EnemyController +extends Node + +@warning_ignore("unused_signal") +signal accelerate(direction: Vector2, delta: float) + +@warning_ignore("unused_signal") +signal shoot(weapon_index: int) + + +@warning_ignore("unused_signal") +signal reload(weapon_index: int) + + +@warning_ignore("unused_parameter") +func _physics_process(delta: float) -> void: + for i in 10: + shoot.emit(i) diff --git a/game/controllers/enemy_controller.gd.uid b/game/controllers/enemy_controller.gd.uid new file mode 100644 index 0000000..7a1c938 --- /dev/null +++ b/game/controllers/enemy_controller.gd.uid @@ -0,0 +1 @@ +uid://bs8qqj6yepfln diff --git a/game/controllers/enemy_controller.tscn b/game/controllers/enemy_controller.tscn new file mode 100644 index 0000000..4fbbec8 --- /dev/null +++ b/game/controllers/enemy_controller.tscn @@ -0,0 +1,6 @@ +[gd_scene load_steps=2 format=3 uid="uid://bsqehbymixust"] + +[ext_resource type="Script" uid="uid://bs8qqj6yepfln" path="res://game/controllers/enemy_controller.gd" id="1_10a67"] + +[node name="EnemyController" type="Node"] +script = ExtResource("1_10a67") diff --git a/game/entities/ships/abstract_ship.gd b/game/entities/ships/abstract_ship.gd index 216874b..3f240e6 100644 --- a/game/entities/ships/abstract_ship.gd +++ b/game/entities/ships/abstract_ship.gd @@ -2,6 +2,26 @@ class_name AbstractShip extends CharacterBody2D +const CANNON = preload("res://game/entities/weapons/cannon/cannon.tscn") +const GATLING = preload("res://game/entities/weapons/gatling/gatling.tscn") +const LASER = preload("res://game/entities/weapons/laser/laser.tscn") +const LAUNCHER = preload("res://game/entities/weapons/launcher/launcher.tscn") +const MINELAYER = preload("res://game/entities/weapons/minelayer/minelayer.tscn") +const PLASMA = preload("res://game/entities/weapons/plasma/plasma.tscn") +const RAILGUN = preload("res://game/entities/weapons/railgun/railgun.tscn") +const SHRAPNEL = preload("res://game/entities/weapons/shrapnel/shrapnel.tscn") +const TESLA = preload("res://game/entities/weapons/tesla/tesla.tscn") + +const WEAPONS := [ + CANNON, GATLING, LASER, + LAUNCHER, MINELAYER, PLASMA, + RAILGUN, SHRAPNEL, TESLA, +] + + +signal destroyed + + @onready var sprite := $Sprite2D @onready var collision := $CollisionShape2D @@ -16,6 +36,14 @@ extends CharacterBody2D var _weapons : Array[AbstractWeapon] +func _ready() -> void: + for pos in weapon_positions: + var weapon : AbstractWeapon = WEAPONS.pick_random().instantiate() + weapon.position = pos + add_child(weapon) + _weapons.append(weapon) + + func _physics_process(_delta: float) -> void: var was_collided := move_and_slide() if was_collided: @@ -57,3 +85,4 @@ func _get_new_speed(accel: float, decel: float, current_speed: float) -> float: func _on_health_depleted() -> void: queue_free() + destroyed.emit() diff --git a/game/entities/ships/abstract_ship.tscn b/game/entities/ships/abstract_ship.tscn index a573dc5..f2c6c78 100644 --- a/game/entities/ships/abstract_ship.tscn +++ b/game/entities/ships/abstract_ship.tscn @@ -18,8 +18,5 @@ shape = SubResource("CircleShape2D_xxtvk") [node name="Health" type="Node" parent="."] script = ExtResource("2_n766o") -max_shield = 1000 -max_armor = 1000 -max_hull = 1000 [connection signal="depleted" from="Health" to="." method="_on_health_depleted"] diff --git a/game/entities/ships/enemies/abstract_enemy_ship.gd b/game/entities/ships/enemies/abstract_enemy_ship.gd new file mode 100644 index 0000000..43f6551 --- /dev/null +++ b/game/entities/ships/enemies/abstract_enemy_ship.gd @@ -0,0 +1,8 @@ +class_name AbstractEnemyShip +extends AbstractShip + + +func _ready() -> void: + super._ready() + for weapon in _weapons: + weapon.belonging = AbstractWeapon.Belonging.ENEMY diff --git a/game/entities/ships/enemies/abstract_enemy_ship.gd.uid b/game/entities/ships/enemies/abstract_enemy_ship.gd.uid new file mode 100644 index 0000000..fd1885a --- /dev/null +++ b/game/entities/ships/enemies/abstract_enemy_ship.gd.uid @@ -0,0 +1 @@ +uid://byicf1t0807pq diff --git a/game/entities/ships/enemies/abstract_enemy_ship.tscn b/game/entities/ships/enemies/abstract_enemy_ship.tscn new file mode 100644 index 0000000..29edeff --- /dev/null +++ b/game/entities/ships/enemies/abstract_enemy_ship.tscn @@ -0,0 +1,18 @@ +[gd_scene load_steps=4 format=3 uid="uid://dwsn0lf1e3578"] + +[ext_resource type="PackedScene" uid="uid://jvyagshykmgb" path="res://game/entities/ships/abstract_ship.tscn" id="1_28j6l"] +[ext_resource type="Script" uid="uid://byicf1t0807pq" path="res://game/entities/ships/enemies/abstract_enemy_ship.gd" id="2_fwvrd"] +[ext_resource type="Script" uid="uid://bs8qqj6yepfln" path="res://game/controllers/enemy_controller.gd" id="3_vfnhw"] + +[node name="AbstractEnemyShip" instance=ExtResource("1_28j6l")] +collision_layer = 4 +collision_mask = 0 +script = ExtResource("2_fwvrd") + +[node name="EnemyController" type="Node" parent="." index="3"] +script = ExtResource("3_vfnhw") +metadata/_custom_type_script = "uid://bs8qqj6yepfln" + +[connection signal="accelerate" from="EnemyController" to="." method="accelerate"] +[connection signal="reload" from="EnemyController" to="." method="reload"] +[connection signal="shoot" from="EnemyController" to="." method="shoot"] diff --git a/game/entities/ships/enemies/heavy/heavy_enemy_ship.gd b/game/entities/ships/enemies/heavy/heavy_enemy_ship.gd new file mode 100644 index 0000000..1ba0dac --- /dev/null +++ b/game/entities/ships/enemies/heavy/heavy_enemy_ship.gd @@ -0,0 +1,2 @@ +class_name HeavyEnemyShip +extends AbstractEnemyShip diff --git a/game/entities/ships/enemies/heavy/heavy_enemy_ship.gd.uid b/game/entities/ships/enemies/heavy/heavy_enemy_ship.gd.uid new file mode 100644 index 0000000..7cc2b28 --- /dev/null +++ b/game/entities/ships/enemies/heavy/heavy_enemy_ship.gd.uid @@ -0,0 +1 @@ +uid://cxomr1oojcrcl diff --git a/game/entities/ships/enemies/heavy/heavy_enemy_ship.tscn b/game/entities/ships/enemies/heavy/heavy_enemy_ship.tscn new file mode 100644 index 0000000..f78af0a --- /dev/null +++ b/game/entities/ships/enemies/heavy/heavy_enemy_ship.tscn @@ -0,0 +1,32 @@ +[gd_scene load_steps=5 format=3 uid="uid://d3epy8w15qmjm"] + +[ext_resource type="PackedScene" uid="uid://dwsn0lf1e3578" path="res://game/entities/ships/enemies/abstract_enemy_ship.tscn" id="1_wvvpj"] +[ext_resource type="Script" uid="uid://cxomr1oojcrcl" path="res://game/entities/ships/enemies/heavy/heavy_enemy_ship.gd" id="2_3umer"] + +[sub_resource type="PlaceholderTexture2D" id="PlaceholderTexture2D_cuapu"] +size = Vector2(64, 48) + +[sub_resource type="CapsuleShape2D" id="CapsuleShape2D_cuapu"] +radius = 23.0 +height = 62.0 + +[node name="HeavyEnemyShip" instance=ExtResource("1_wvvpj")] +script = ExtResource("2_3umer") +acceleration = 30 +deceleration = 18 +max_speed = 60 +weapon_positions = Array[Vector2]([Vector2(0, 12), Vector2(0, 0), Vector2(0, -12)]) + +[node name="Sprite2D" parent="." index="0"] +texture = SubResource("PlaceholderTexture2D_cuapu") + +[node name="CollisionShape2D" parent="." index="1"] +rotation = 1.5707964 +shape = SubResource("CapsuleShape2D_cuapu") + +[node name="Health" parent="." index="2"] +max_hull = 200 + +[connection signal="accelerate" from="EnemyController" to="." method="accelerate"] +[connection signal="reload" from="EnemyController" to="." method="reload"] +[connection signal="shoot" from="EnemyController" to="." method="shoot"] diff --git a/game/entities/ships/enemies/medium/medium_enemy_ship.gd b/game/entities/ships/enemies/medium/medium_enemy_ship.gd new file mode 100644 index 0000000..da8d026 --- /dev/null +++ b/game/entities/ships/enemies/medium/medium_enemy_ship.gd @@ -0,0 +1,2 @@ +class_name MediumEnemyShip +extends AbstractEnemyShip diff --git a/game/entities/ships/enemies/medium/medium_enemy_ship.gd.uid b/game/entities/ships/enemies/medium/medium_enemy_ship.gd.uid new file mode 100644 index 0000000..1a0ba5d --- /dev/null +++ b/game/entities/ships/enemies/medium/medium_enemy_ship.gd.uid @@ -0,0 +1 @@ +uid://dcpqrdvp4nk82 diff --git a/game/entities/ships/enemies/medium/medium_enemy_ship.tscn b/game/entities/ships/enemies/medium/medium_enemy_ship.tscn new file mode 100644 index 0000000..cf45ce5 --- /dev/null +++ b/game/entities/ships/enemies/medium/medium_enemy_ship.tscn @@ -0,0 +1,31 @@ +[gd_scene load_steps=5 format=3 uid="uid://cye5ndbh0ht7w"] + +[ext_resource type="PackedScene" uid="uid://dwsn0lf1e3578" path="res://game/entities/ships/enemies/abstract_enemy_ship.tscn" id="1_16owb"] +[ext_resource type="Script" uid="uid://dcpqrdvp4nk82" path="res://game/entities/ships/enemies/medium/medium_enemy_ship.gd" id="2_dgwiy"] + +[sub_resource type="PlaceholderTexture2D" id="PlaceholderTexture2D_4jmkv"] +size = Vector2(32, 48) + +[sub_resource type="CapsuleShape2D" id="CapsuleShape2D_4jmkv"] +radius = 15.0 +height = 46.0 + +[node name="MediumEnemyShip" instance=ExtResource("1_16owb")] +script = ExtResource("2_dgwiy") +acceleration = 92 +deceleration = 46 +max_speed = 92 +weapon_positions = Array[Vector2]([Vector2(0, 10), Vector2(0, -10)]) + +[node name="Sprite2D" parent="." index="0"] +texture = SubResource("PlaceholderTexture2D_4jmkv") + +[node name="CollisionShape2D" parent="." index="1"] +shape = SubResource("CapsuleShape2D_4jmkv") + +[node name="Health" parent="." index="2"] +max_hull = 100 + +[connection signal="accelerate" from="EnemyController" to="." method="accelerate"] +[connection signal="reload" from="EnemyController" to="." method="reload"] +[connection signal="shoot" from="EnemyController" to="." method="shoot"] diff --git a/game/entities/ships/enemies/small/small_enemy_ship.gd b/game/entities/ships/enemies/small/small_enemy_ship.gd new file mode 100644 index 0000000..f3ca3ad --- /dev/null +++ b/game/entities/ships/enemies/small/small_enemy_ship.gd @@ -0,0 +1,2 @@ +class_name SmallEnemyShip +extends AbstractEnemyShip diff --git a/game/entities/ships/enemies/small/small_enemy_ship.gd.uid b/game/entities/ships/enemies/small/small_enemy_ship.gd.uid new file mode 100644 index 0000000..0e76582 --- /dev/null +++ b/game/entities/ships/enemies/small/small_enemy_ship.gd.uid @@ -0,0 +1 @@ +uid://btcce3nflycrs diff --git a/game/entities/ships/enemies/small/small_enemy_ship.tscn b/game/entities/ships/enemies/small/small_enemy_ship.tscn new file mode 100644 index 0000000..bd6c3be --- /dev/null +++ b/game/entities/ships/enemies/small/small_enemy_ship.tscn @@ -0,0 +1,31 @@ +[gd_scene load_steps=5 format=3 uid="uid://cbf3kumeoqpba"] + +[ext_resource type="PackedScene" uid="uid://dwsn0lf1e3578" path="res://game/entities/ships/enemies/abstract_enemy_ship.tscn" id="1_lt1av"] +[ext_resource type="Script" uid="uid://btcce3nflycrs" path="res://game/entities/ships/enemies/small/small_enemy_ship.gd" id="2_bw04d"] + +[sub_resource type="PlaceholderTexture2D" id="PlaceholderTexture2D_bw04d"] +size = Vector2(32, 16) + +[sub_resource type="CapsuleShape2D" id="CapsuleShape2D_lopve"] +radius = 7.0 + +[node name="SmallEnemyShip" instance=ExtResource("1_lt1av")] +script = ExtResource("2_bw04d") +acceleration = 180 +deceleration = 80 +max_speed = 120 +weapon_positions = Array[Vector2]([Vector2(0, 0)]) + +[node name="Sprite2D" parent="." index="0"] +texture = SubResource("PlaceholderTexture2D_bw04d") + +[node name="CollisionShape2D" parent="." index="1"] +rotation = 1.5707964 +shape = SubResource("CapsuleShape2D_lopve") + +[node name="Health" parent="." index="2"] +max_hull = 50 + +[connection signal="accelerate" from="EnemyController" to="." method="accelerate"] +[connection signal="reload" from="EnemyController" to="." method="reload"] +[connection signal="shoot" from="EnemyController" to="." method="shoot"] diff --git a/game/entities/ships/player/player_ship.gd b/game/entities/ships/player/player_ship.gd new file mode 100644 index 0000000..08e03ac --- /dev/null +++ b/game/entities/ships/player/player_ship.gd @@ -0,0 +1,8 @@ +class_name PlayerShip +extends AbstractShip + + +func _ready() -> void: + super._ready() + for weapon in _weapons: + weapon.belonging = AbstractWeapon.Belonging.PLAYER diff --git a/game/entities/ships/player_ship.gd.uid b/game/entities/ships/player/player_ship.gd.uid similarity index 100% rename from game/entities/ships/player_ship.gd.uid rename to game/entities/ships/player/player_ship.gd.uid diff --git a/game/entities/ships/player_ship.tscn b/game/entities/ships/player/player_ship.tscn similarity index 88% rename from game/entities/ships/player_ship.tscn rename to game/entities/ships/player/player_ship.tscn index e781794..0f1ddcd 100644 --- a/game/entities/ships/player_ship.tscn +++ b/game/entities/ships/player/player_ship.tscn @@ -1,14 +1,15 @@ [gd_scene load_steps=6 format=3 uid="uid://br074cqcnul3d"] [ext_resource type="PackedScene" uid="uid://jvyagshykmgb" path="res://game/entities/ships/abstract_ship.tscn" id="1_6otxb"] -[ext_resource type="Script" uid="uid://ruxw1n03iq4i" path="res://game/entities/ships/player_ship.gd" id="2_625ti"] +[ext_resource type="Script" uid="uid://ruxw1n03iq4i" path="res://game/entities/ships/player/player_ship.gd" id="2_625ti"] [ext_resource type="Script" uid="uid://dgevigih7owxd" path="res://game/controllers/player_controller.gd" id="3_dj8f1"] [sub_resource type="PlaceholderTexture2D" id="PlaceholderTexture2D_dj8f1"] size = Vector2(48, 32) [sub_resource type="CapsuleShape2D" id="CapsuleShape2D_dj8f1"] -height = 20.0 +radius = 15.0 +height = 46.0 [node name="PlayerShip" instance=ExtResource("1_6otxb")] collision_layer = 3 @@ -22,8 +23,12 @@ weapon_positions = Array[Vector2]([Vector2(0, 8), Vector2(0, -8)]) texture = SubResource("PlaceholderTexture2D_dj8f1") [node name="CollisionShape2D" parent="." index="1"] +rotation = 1.5707964 shape = SubResource("CapsuleShape2D_dj8f1") +[node name="Health" parent="." index="2"] +max_hull = 1000 + [node name="PlayerController" type="Node" parent="." index="3"] script = ExtResource("3_dj8f1") metadata/_custom_type_script = "uid://dgevigih7owxd" diff --git a/game/entities/ships/player_ship.gd b/game/entities/ships/player_ship.gd deleted file mode 100644 index 8db78a9..0000000 --- a/game/entities/ships/player_ship.gd +++ /dev/null @@ -1,35 +0,0 @@ -class_name PlayerShip -extends AbstractShip - - -func _ready() -> void: - @warning_ignore("unused_local_constant") - const CANNON = preload("res://game/entities/weapons/cannon/cannon.tscn") - @warning_ignore("unused_local_constant") - const GATLING = preload("res://game/entities/weapons/gatling/gatling.tscn") - @warning_ignore("unused_local_constant") - const LASER = preload("res://game/entities/weapons/laser/laser.tscn") - @warning_ignore("unused_local_constant") - const LAUNCHER = preload("res://game/entities/weapons/launcher/launcher.tscn") - @warning_ignore("unused_local_constant") - const MINELAYER = preload("res://game/entities/weapons/minelayer/minelayer.tscn") - @warning_ignore("unused_local_constant") - const PLASMA = preload("res://game/entities/weapons/plasma/plasma.tscn") - @warning_ignore("unused_local_constant") - const RAILGUN = preload("res://game/entities/weapons/railgun/railgun.tscn") - @warning_ignore("unused_local_constant") - const SHRAPNEL = preload("res://game/entities/weapons/shrapnel/shrapnel.tscn") - @warning_ignore("unused_local_constant") - const TESLA = preload("res://game/entities/weapons/tesla/tesla.tscn") - - var weapons := [ - GATLING.instantiate(), - RAILGUN.instantiate(), - ] - - for index in weapons.size(): - var weapon : Node2D = weapons[index] - if index < weapon_positions.size(): - weapon.position = weapon_positions[index] - add_child(weapons[index]) - _weapons.append(weapon) diff --git a/game/passage.gd b/game/passage.gd index 07f5527..81d1fff 100644 --- a/game/passage.gd +++ b/game/passage.gd @@ -1,8 +1,29 @@ extends Node +const SMALL_ENEMY = preload("res://game/entities/ships/enemies/small/small_enemy_ship.tscn") +const MEDIUM_ENEMY = preload("res://game/entities/ships/enemies/medium/medium_enemy_ship.tscn") +const HEAVY_ENEMY = preload("res://game/entities/ships/enemies/heavy/heavy_enemy_ship.tscn") + +const PLAYER := preload("res://game/entities/ships/player/player_ship.tscn") + + func _ready() -> void: - const PLAYER := preload("res://game/entities/ships/player_ship.tscn") + _create_player() + _create_random_enemy() + + +func _create_player() -> void: var player : PlayerShip = PLAYER.instantiate() add_child(player) player.position = Vector2(100, 100) + player.destroyed.connect(_create_player) + + +func _create_random_enemy() -> void: + const ENEMIES := [ SMALL_ENEMY, MEDIUM_ENEMY, HEAVY_ENEMY ] + + var enemy : AbstractEnemyShip = ENEMIES.pick_random().instantiate() + add_child(enemy) + enemy.position = Vector2(550, 180) + enemy.destroyed.connect(_create_random_enemy)