diff --git a/game/entities/player.gd b/game/controllers/player_controller.gd similarity index 53% rename from game/entities/player.gd rename to game/controllers/player_controller.gd index 69351f2..a2ca13f 100644 --- a/game/entities/player.gd +++ b/game/controllers/player_controller.gd @@ -1,34 +1,25 @@ +class_name PlayerController extends Node -@onready var ship := $Ship +signal accelerate(direction: Vector2, delta: float) + +signal shoot(weapon_index: int) + +signal reload(weapon_index: int) -var position : Vector2: - set(value): - ship.position = value - get: - return ship.position - - func _physics_process(delta: float) -> void: var input_direction := Input.get_vector("move_left", "move_right", "move_up", "move_down") - ship.accelerate(input_direction, delta) + accelerate.emit(input_direction, delta) - var weapons : Array[AbstractWeapon] = ship.weapons var weapon_actions := { 0: ["shoot_weapon_1", "reload_weapon_1"], 1: ["shoot_weapon_2", "reload_weapon_2"] } for index : int in weapon_actions: - if index >= weapons.size(): break - if Input.is_action_pressed(weapon_actions[index][0]): - ship.shoot(weapons[index]) + shoot.emit(index) if Input.is_action_pressed(weapon_actions[index][1]): - ship.reload(weapons[index]) - - -func _on_ship_destroyed() -> void: - queue_free() + reload.emit(index) diff --git a/game/controllers/player_controller.gd.uid b/game/controllers/player_controller.gd.uid new file mode 100644 index 0000000..e804a38 --- /dev/null +++ b/game/controllers/player_controller.gd.uid @@ -0,0 +1 @@ +uid://dgevigih7owxd diff --git a/game/controllers/player_controller.tscn b/game/controllers/player_controller.tscn new file mode 100644 index 0000000..4a3d3b7 --- /dev/null +++ b/game/controllers/player_controller.tscn @@ -0,0 +1,6 @@ +[gd_scene load_steps=2 format=3 uid="uid://dh1oj1w5wx4je"] + +[ext_resource type="Script" uid="uid://dgevigih7owxd" path="res://game/controllers/player_controller.gd" id="1_cu3ev"] + +[node name="PlayerController" type="Node"] +script = ExtResource("1_cu3ev") diff --git a/game/entities/player.gd.uid b/game/entities/player.gd.uid deleted file mode 100644 index ba7d986..0000000 --- a/game/entities/player.gd.uid +++ /dev/null @@ -1 +0,0 @@ -uid://c2uf62j1im13p diff --git a/game/entities/player.tscn b/game/entities/player.tscn deleted file mode 100644 index 739e625..0000000 --- a/game/entities/player.tscn +++ /dev/null @@ -1,17 +0,0 @@ -[gd_scene load_steps=3 format=3 uid="uid://bkv8y6n7chu3f"] - -[ext_resource type="Script" uid="uid://c2uf62j1im13p" path="res://game/entities/player.gd" id="1_3a8sv"] -[ext_resource type="PackedScene" uid="uid://jvyagshykmgb" path="res://game/entities/ship.tscn" id="2_ktqbp"] - -[node name="Player" type="Node"] -script = ExtResource("1_3a8sv") - -[node name="Ship" parent="." instance=ExtResource("2_ktqbp")] -collision_layer = 2 -collision_mask = 21 -size = Vector2(48, 32) -acceleration = 92 -deceleration = 46 -max_speed = 92 - -[connection signal="destroyed" from="Ship" to="." method="_on_ship_destroyed"] diff --git a/game/entities/ship.gd b/game/entities/ship.gd deleted file mode 100644 index 82dfafe..0000000 --- a/game/entities/ship.gd +++ /dev/null @@ -1,103 +0,0 @@ -extends CharacterBody2D - - -signal destroyed - - -@onready var sprite := $Sprite2D -@onready var collision := $CollisionShape2D - - -@export var size : Vector2: - set(value): - size = value - _update_texture_size() - _update_collision_shape() - get: - return size - -@export_range(0, 250) var acceleration : int = 0 -@export_range(0, 250) var deceleration : int = 0 -@export_range(0, 250) var max_speed : int = 0 - - -@onready var weapons : Array[AbstractWeapon]: - set(value): - pass - get: - return weapons - - -func _ready() -> void: - var texture := PlaceholderTexture2D.new() - sprite.texture = texture - - _update_texture_size() - _update_collision_shape() - - const GATLING = preload("res://game/entities/weapons/gatling/gatling.tscn") - const RAILGUN = preload("res://game/entities/weapons/railgun/railgun.tscn") - var weapons_by_offset := { - 8: GATLING.instantiate(), - -8: RAILGUN.instantiate(), - } - for offset : int in weapons_by_offset: - var weapon : Node2D = weapons_by_offset[offset] - weapon.position = Vector2(0, offset) - add_child(weapon) - weapons.append(weapon) - - -func _physics_process(_delta: float) -> void: - var was_collided := move_and_slide() - if was_collided: - var normal := get_wall_normal() - velocity -= normal.abs() * velocity - - -func accelerate(direction: Vector2, delta: float) -> void: - var accel : Vector2 = direction * acceleration * delta - var decel : float = deceleration * delta - - velocity.x = _get_new_speed(accel.x, decel, velocity.x) - velocity.y = _get_new_speed(accel.y, decel, velocity.y) - - if velocity.length() > max_speed: - velocity = velocity.normalized() * max_speed - - -func _get_new_speed(accel: float, decel: float, current_speed: float) -> float: - if is_zero_approx(accel): - if absf(current_speed) < decel: - return 0.0 - return current_speed + (decel if current_speed < 0 else -decel) - else: - return current_speed + accel - - -func shoot(weapon: Node) -> void: - if weapon in weapons: - weapon.shoot(velocity) - - -func reload(weapon: Node) -> void: - if weapon in weapons: - weapon.reload() - - -func _update_texture_size() -> void: - if sprite and sprite.texture: - sprite.texture.size = size - - -func _update_collision_shape() -> void: - if collision: - collision.shape.radius = 0.9 * minf(size.x, size.y)/2 - collision.shape.height = 0.9 * maxf(size.x, size.y) - collision.rotation = 0.0 if size.x < size.y else PI/2 - - - -func _on_health_depleted() -> void: - destroyed.emit() - queue_free() diff --git a/game/entities/ships/abstract_ship.gd b/game/entities/ships/abstract_ship.gd new file mode 100644 index 0000000..216874b --- /dev/null +++ b/game/entities/ships/abstract_ship.gd @@ -0,0 +1,59 @@ +class_name AbstractShip +extends CharacterBody2D + + +@onready var sprite := $Sprite2D +@onready var collision := $CollisionShape2D + + +@export_range(0, 250) var acceleration : int = 0 +@export_range(0, 250) var deceleration : int = 0 +@export_range(0, 250) var max_speed : int = 0 + +@export var weapon_positions: Array[Vector2] + + +var _weapons : Array[AbstractWeapon] + + +func _physics_process(_delta: float) -> void: + var was_collided := move_and_slide() + if was_collided: + var normal := get_wall_normal() + velocity -= normal.abs() * velocity + + +func accelerate(direction: Vector2, delta: float) -> void: + var accel : Vector2 = direction * acceleration * delta + var decel : float = deceleration * delta + + velocity.x = _get_new_speed(accel.x, decel, velocity.x) + velocity.y = _get_new_speed(accel.y, decel, velocity.y) + + if velocity.length() > max_speed: + velocity = velocity.normalized() * max_speed + + +func shoot(weapon_index: int) -> void: + if weapon_index >= _weapons.size(): return + + _weapons[weapon_index].shoot(velocity) + + +func reload(weapon_index: int) -> void: + if weapon_index >= _weapons.size(): return + + _weapons[weapon_index].reload() + + +func _get_new_speed(accel: float, decel: float, current_speed: float) -> float: + if is_zero_approx(accel): + if absf(current_speed) < decel: + return 0.0 + return current_speed + (decel if current_speed < 0 else -decel) + else: + return current_speed + accel + + +func _on_health_depleted() -> void: + queue_free() diff --git a/game/entities/ship.gd.uid b/game/entities/ships/abstract_ship.gd.uid similarity index 100% rename from game/entities/ship.gd.uid rename to game/entities/ships/abstract_ship.gd.uid diff --git a/game/entities/ship.tscn b/game/entities/ships/abstract_ship.tscn similarity index 74% rename from game/entities/ship.tscn rename to game/entities/ships/abstract_ship.tscn index 3d50d1d..a573dc5 100644 --- a/game/entities/ship.tscn +++ b/game/entities/ships/abstract_ship.tscn @@ -1,14 +1,12 @@ [gd_scene load_steps=4 format=3 uid="uid://jvyagshykmgb"] -[ext_resource type="Script" uid="uid://cesibaqtrgotl" path="res://game/entities/ship.gd" id="1_6isjb"] +[ext_resource type="Script" uid="uid://cesibaqtrgotl" path="res://game/entities/ships/abstract_ship.gd" id="1_6isjb"] [ext_resource type="Script" uid="uid://d3g4xbq45qmpr" path="res://game/health_system/health.gd" id="2_n766o"] -[sub_resource type="CapsuleShape2D" id="CapsuleShape2D_6isjb"] -height = 20.0 +[sub_resource type="CircleShape2D" id="CircleShape2D_xxtvk"] -[node name="Ship" type="CharacterBody2D"] +[node name="AbstractShip" type="CharacterBody2D"] disable_mode = 1 -collision_layer = 7 motion_mode = 1 wall_min_slide_angle = 3.1415927 script = ExtResource("1_6isjb") @@ -16,7 +14,7 @@ script = ExtResource("1_6isjb") [node name="Sprite2D" type="Sprite2D" parent="."] [node name="CollisionShape2D" type="CollisionShape2D" parent="."] -shape = SubResource("CapsuleShape2D_6isjb") +shape = SubResource("CircleShape2D_xxtvk") [node name="Health" type="Node" parent="."] script = ExtResource("2_n766o") diff --git a/game/entities/ships/player_ship.gd b/game/entities/ships/player_ship.gd new file mode 100644 index 0000000..8db78a9 --- /dev/null +++ b/game/entities/ships/player_ship.gd @@ -0,0 +1,35 @@ +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/entities/ships/player_ship.gd.uid b/game/entities/ships/player_ship.gd.uid new file mode 100644 index 0000000..77e1982 --- /dev/null +++ b/game/entities/ships/player_ship.gd.uid @@ -0,0 +1 @@ +uid://ruxw1n03iq4i diff --git a/game/entities/ships/player_ship.tscn b/game/entities/ships/player_ship.tscn new file mode 100644 index 0000000..e781794 --- /dev/null +++ b/game/entities/ships/player_ship.tscn @@ -0,0 +1,33 @@ +[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://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 + +[node name="PlayerShip" instance=ExtResource("1_6otxb")] +collision_layer = 3 +script = ExtResource("2_625ti") +acceleration = 92 +deceleration = 46 +max_speed = 92 +weapon_positions = Array[Vector2]([Vector2(0, 8), Vector2(0, -8)]) + +[node name="Sprite2D" parent="." index="0"] +texture = SubResource("PlaceholderTexture2D_dj8f1") + +[node name="CollisionShape2D" parent="." index="1"] +shape = SubResource("CapsuleShape2D_dj8f1") + +[node name="PlayerController" type="Node" parent="." index="3"] +script = ExtResource("3_dj8f1") +metadata/_custom_type_script = "uid://dgevigih7owxd" + +[connection signal="accelerate" from="PlayerController" to="." method="accelerate"] +[connection signal="reload" from="PlayerController" to="." method="reload"] +[connection signal="shoot" from="PlayerController" to="." method="shoot"] diff --git a/game/entities/weapons/cannon/cannon_projectile.tscn b/game/entities/weapons/cannon/cannon_projectile.tscn index d22007e..13af80e 100644 --- a/game/entities/weapons/cannon/cannon_projectile.tscn +++ b/game/entities/weapons/cannon/cannon_projectile.tscn @@ -18,6 +18,8 @@ region = Rect2(32, 16, 16, 16) radius = 2.0 [node name="CannonProjectile" instance=ExtResource("1_20qwt")] +collision_layer = 0 +collision_mask = 0 script = ExtResource("2_x3axw") damage = SubResource("Resource_bb01p") speed = 600 diff --git a/game/entities/weapons/gatling/gatling_projectile.tscn b/game/entities/weapons/gatling/gatling_projectile.tscn index 3ae0ea1..b887dd7 100644 --- a/game/entities/weapons/gatling/gatling_projectile.tscn +++ b/game/entities/weapons/gatling/gatling_projectile.tscn @@ -18,6 +18,8 @@ region = Rect2(0, 0, 16, 16) radius = 1.0 [node name="GatlingProjectile" instance=ExtResource("1_3tgt7")] +collision_layer = 0 +collision_mask = 0 script = ExtResource("2_hbgoq") damage = SubResource("Resource_ndegg") speed = 600 diff --git a/game/entities/weapons/laser/laser_projectile.tscn b/game/entities/weapons/laser/laser_projectile.tscn index 2c53475..8864f1d 100644 --- a/game/entities/weapons/laser/laser_projectile.tscn +++ b/game/entities/weapons/laser/laser_projectile.tscn @@ -16,6 +16,8 @@ size = Vector2(4, 4) radius = 1.0 [node name="LaserProjectile" instance=ExtResource("1_3a8fg")] +collision_layer = 0 +collision_mask = 0 script = ExtResource("2_je1a2") damage = SubResource("Resource_bytws") speed = 500 diff --git a/game/entities/weapons/launcher/launcher_projectile.tscn b/game/entities/weapons/launcher/launcher_projectile.tscn index e441f44..c56a234 100644 --- a/game/entities/weapons/launcher/launcher_projectile.tscn +++ b/game/entities/weapons/launcher/launcher_projectile.tscn @@ -47,6 +47,8 @@ radius = 1.0 height = 6.0 [node name="LauncherProjectile" instance=ExtResource("1_0mcat")] +collision_layer = 0 +collision_mask = 0 script = ExtResource("2_6hdsf") damage = SubResource("Resource_kxgpk") speed = 300 diff --git a/game/entities/weapons/minelayer/minelayer_projectile.gd b/game/entities/weapons/minelayer/minelayer_projectile.gd index 3863731..044ec76 100644 --- a/game/entities/weapons/minelayer/minelayer_projectile.gd +++ b/game/entities/weapons/minelayer/minelayer_projectile.gd @@ -2,18 +2,9 @@ extends AbstractProjectile @export var deceleration : int -@export var livetime : int -func _ready() -> void: - super._ready() - - var livetime_timer := Timer.new() - add_child(livetime_timer) - livetime_timer.wait_time = livetime - livetime_timer.one_shot = true - livetime_timer.timeout.connect(queue_free) - livetime_timer.start() +@onready var livetime_timer := $LivetimeTimer func _physics_process(delta: float) -> void: @@ -27,3 +18,7 @@ func _process_acceleration(delta: float) -> void: _velocity -= _velocity.normalized() * current_deceleration else: _velocity = Vector2.ZERO + + +func _on_livetime_timer_timeout() -> void: + queue_free() diff --git a/game/entities/weapons/minelayer/minelayer_projectile.tscn b/game/entities/weapons/minelayer/minelayer_projectile.tscn index e7e898e..ceb50e6 100644 --- a/game/entities/weapons/minelayer/minelayer_projectile.tscn +++ b/game/entities/weapons/minelayer/minelayer_projectile.tscn @@ -18,9 +18,10 @@ region = Rect2(0, 16, 16, 16) radius = 7.0 [node name="MinelayerProjectile" instance=ExtResource("1_ufc4r")] +collision_layer = 0 +collision_mask = 0 script = ExtResource("2_hwwfa") deceleration = 100 -livetime = 60 damage = SubResource("Resource_px1i2") speed = 200 @@ -29,3 +30,10 @@ texture = SubResource("AtlasTexture_ckqco") [node name="CollisionShape2D" parent="." index="1"] shape = SubResource("CircleShape2D_ufc4r") + +[node name="LivetimeTimer" type="Timer" parent="." index="3"] +wait_time = 60.0 +one_shot = true +autostart = true + +[connection signal="timeout" from="LivetimeTimer" to="." method="_on_livetime_timer_timeout"] diff --git a/game/entities/weapons/plasma/plasma_projectile.tscn b/game/entities/weapons/plasma/plasma_projectile.tscn index d2d0d31..e34fcb2 100644 --- a/game/entities/weapons/plasma/plasma_projectile.tscn +++ b/game/entities/weapons/plasma/plasma_projectile.tscn @@ -10,13 +10,15 @@ value = 20 metadata/_custom_type_script = "uid://c27v705giygv4" [sub_resource type="PlaceholderTexture2D" id="PlaceholderTexture2D_dlvdm"] -size = Vector2(4, 6) +size = Vector2(6, 4) [sub_resource type="CapsuleShape2D" id="CapsuleShape2D_5enq5"] radius = 1.0 height = 4.0 [node name="PlasmaProjectile" instance=ExtResource("1_x58hw")] +collision_layer = 0 +collision_mask = 0 script = ExtResource("2_0deih") damage = SubResource("Resource_5enq5") speed = 450 diff --git a/game/entities/weapons/railgun/railgun_projectile.tscn b/game/entities/weapons/railgun/railgun_projectile.tscn index d26ec60..a850b4a 100644 --- a/game/entities/weapons/railgun/railgun_projectile.tscn +++ b/game/entities/weapons/railgun/railgun_projectile.tscn @@ -23,6 +23,8 @@ radius = 1.0 height = 4.0 [node name="RailgunProjectile" instance=ExtResource("1_rfd1j")] +collision_layer = 0 +collision_mask = 0 script = ExtResource("1_hycpq") damage = SubResource("Resource_u82jm") speed = 900 diff --git a/game/entities/weapons/shrapnel/shrapnel_projectile.tscn b/game/entities/weapons/shrapnel/shrapnel_projectile.tscn index bd5a1f0..6cf5b9b 100644 --- a/game/entities/weapons/shrapnel/shrapnel_projectile.tscn +++ b/game/entities/weapons/shrapnel/shrapnel_projectile.tscn @@ -18,6 +18,8 @@ region = Rect2(48, 0, 16, 16) radius = 1.0 [node name="ShrapnelProjectile" instance=ExtResource("1_yu2c6")] +collision_layer = 0 +collision_mask = 0 script = ExtResource("2_2jiy6") max_distance = 350 damage = SubResource("Resource_klguu") diff --git a/game/entities/weapons/tesla/tesla_projectile.tscn b/game/entities/weapons/tesla/tesla_projectile.tscn index ff1a945..49e002a 100644 --- a/game/entities/weapons/tesla/tesla_projectile.tscn +++ b/game/entities/weapons/tesla/tesla_projectile.tscn @@ -16,6 +16,8 @@ size = Vector2(12, 12) radius = 5.0 [node name="TeslaProjectile" instance=ExtResource("1_1oexk")] +collision_layer = 0 +collision_mask = 0 script = ExtResource("2_q73is") damage = SubResource("Resource_1121u") speed = 900 diff --git a/game/passage.gd b/game/passage.gd index 2688b97..07f5527 100644 --- a/game/passage.gd +++ b/game/passage.gd @@ -2,6 +2,7 @@ extends Node func _ready() -> void: - var player : Node = load("res://game/entities/player.tscn").instantiate() + const PLAYER := preload("res://game/entities/ships/player_ship.tscn") + var player : PlayerShip = PLAYER.instantiate() add_child(player) player.position = Vector2(100, 100)