diff --git a/scenes/Ships/Modules/Shields/shield.tscn b/scenes/Ships/Modules/Shields/shield.tscn index eb5a1ac..a5ad0d1 100644 --- a/scenes/Ships/Modules/Shields/shield.tscn +++ b/scenes/Ships/Modules/Shields/shield.tscn @@ -4,8 +4,8 @@ [node name="Shield" type="Node2D" node_paths=PackedStringArray("shield_recharge_timer", "laser_recharge_timer")] script = ExtResource("1_sexol") -shield_recharge_timer = NodePath("") -laser_recharge_timer = NodePath("") +shield_recharge_timer = NodePath("ShieldTimer") +laser_recharge_timer = NodePath("LaserTimer") [node name="ShieldTimer" type="Timer" parent="."] wait_time = 5.0 diff --git a/scenes/Ships/Modules/Weapons/weapon.tscn b/scenes/Ships/Modules/Weapons/weapon.tscn new file mode 100644 index 0000000..5b5fddd --- /dev/null +++ b/scenes/Ships/Modules/Weapons/weapon.tscn @@ -0,0 +1,16 @@ +[gd_scene load_steps=3 format=3 uid="uid://ryy1tdrxmjav"] + +[ext_resource type="Script" path="res://scripts/Ship/weapon.gd" id="1_ga8vg"] +[ext_resource type="PackedScene" uid="uid://djmoij5kuou3j" path="res://scenes/projectiles/Laser.tscn" id="2_qo148"] + +[node name="Weapon" type="Node2D" node_paths=PackedStringArray("shoot_timer", "spawner_points")] +script = ExtResource("1_ga8vg") +projectile = ExtResource("2_qo148") +shoot_timer = NodePath("ShootTimer") +spawner_points = [NodePath("Spawner")] + +[node name="ShootTimer" type="Timer" parent="."] +wait_time = 0.25 +one_shot = true + +[node name="Spawner" type="Node2D" parent="."] diff --git a/scenes/Ships/player_ship.tscn b/scenes/Ships/player_ship.tscn index 46ce514..9b1b2d6 100644 --- a/scenes/Ships/player_ship.tscn +++ b/scenes/Ships/player_ship.tscn @@ -1,21 +1,13 @@ -[gd_scene load_steps=5 format=3 uid="uid://dok3i8u5t1ka4"] +[gd_scene load_steps=4 format=3 uid="uid://dok3i8u5t1ka4"] [ext_resource type="PackedScene" uid="uid://bjkshql8ut6hk" path="res://scenes/Ships/ship.tscn" id="1_6x7bu"] -[ext_resource type="Shader" path="res://shaders/ship.gdshader" id="2_dbojl"] [ext_resource type="Script" path="res://scripts/Ship/player_input_controller.gd" id="3_0e84a"] - -[sub_resource type="ShaderMaterial" id="ShaderMaterial_u1mh3"] -resource_local_to_scene = true -resource_name = "Shield Material" -shader = ExtResource("2_dbojl") -shader_parameter/wave_centers = PackedVector2Array() -shader_parameter/wave_distances = PackedFloat32Array(-1) -shader_parameter/color = Color(1, 0, 0, 1) -shader_parameter/max_distance = 20.0 -shader_parameter/bublic_size = 20.0 +[ext_resource type="PackedScene" uid="uid://ryy1tdrxmjav" path="res://scenes/Ships/Modules/Weapons/weapon.tscn" id="4_fy1be"] [node name="PlayerShip" instance=ExtResource("1_6x7bu")] -material = SubResource("ShaderMaterial_u1mh3") -[node name="InputController" type="Node2D" parent="." index="3"] +[node name="InputController" type="Node2D" parent="." index="0"] script = ExtResource("3_0e84a") + +[node name="Weapon" parent="Weapons" index="0" instance=ExtResource("4_fy1be")] +action_id = "primary" diff --git a/scenes/Ships/ship.tscn b/scenes/Ships/ship.tscn index 0e92311..9a654f3 100644 --- a/scenes/Ships/ship.tscn +++ b/scenes/Ships/ship.tscn @@ -9,7 +9,9 @@ [node name="Ship" type="Node2D"] script = ExtResource("2_n14ml") -[node name="Hull" parent="." instance=ExtResource("3_upgeg")] +[node name="HullHolder" type="Node" parent="."] + +[node name="Hull" parent="HullHolder" instance=ExtResource("3_upgeg")] [node name="Engine" parent="." instance=ExtResource("4_adr14")] diff --git a/scenes/debug.tscn b/scenes/debug.tscn index 1d6b4c9..bd64003 100644 --- a/scenes/debug.tscn +++ b/scenes/debug.tscn @@ -1,7 +1,8 @@ -[gd_scene load_steps=5 format=3 uid="uid://ckdttdkoo1t0s"] +[gd_scene load_steps=6 format=3 uid="uid://ckdttdkoo1t0s"] [ext_resource type="PackedScene" uid="uid://dok3i8u5t1ka4" path="res://scenes/Ships/player_ship.tscn" id="1_b1l1c"] [ext_resource type="Shader" path="res://shaders/ship.gdshader" id="2_15op6"] +[ext_resource type="Script" path="res://scripts/misc/ProjectilesContainer.gd" id="3_amh71"] [sub_resource type="GDScript" id="GDScript_6w8qb"] resource_name = "gogo" @@ -83,12 +84,12 @@ func check_for_item(modules : Array[String]) -> Array[String]: # Called every frame. 'delta' is the elapsed time since the previous frame. -func _process(delta): +func _process(_delta): if Input.is_action_just_released(\"pause\"): get_tree().change_scene_to_file(\"res://scenes/MainMenu.tscn\") " -[sub_resource type="ShaderMaterial" id="ShaderMaterial_634ti"] +[sub_resource type="ShaderMaterial" id="ShaderMaterial_m15k8"] resource_local_to_scene = true resource_name = "Shield Material" shader = ExtResource("2_15op6") @@ -117,6 +118,9 @@ vertical_alignment = 1 autowrap_mode = 2 [node name="PlayerShip" parent="." instance=ExtResource("1_b1l1c")] -material = SubResource("ShaderMaterial_634ti") +material = SubResource("ShaderMaterial_m15k8") position = Vector2(608, 352) scale = Vector2(0.5, 0.5) + +[node name="ProjectileContainer" type="Node2D" parent="."] +script = ExtResource("3_amh71") diff --git a/scripts/Ship/hull.gd b/scripts/Ship/hull.gd index 90e91bb..7117f3b 100644 --- a/scripts/Ship/hull.gd +++ b/scripts/Ship/hull.gd @@ -2,8 +2,8 @@ extends RigidBody2D class_name Hull -## Shortcut to get_parent() -@onready var ship: Ship = get_parent() +## Shortcut to get_parent().get_parent() +@onready var ship: Ship = get_parent().get_parent() ## Hull ID to use with the Game.get_module func @export var id: String = "hull" @@ -36,3 +36,8 @@ func add_ammunition(which: String, value: float) -> bool: if ammunition[which] > max_ammunition[which]: ammunition[which] = max_ammunition[which] return true + +## Update ship's position and rotation +func _physics_process(_delta): + ship.position = position + ship.rotation = rotation diff --git a/scripts/Ship/player_input_controller.gd b/scripts/Ship/player_input_controller.gd index 2ca7a87..68e434e 100644 --- a/scripts/Ship/player_input_controller.gd +++ b/scripts/Ship/player_input_controller.gd @@ -6,3 +6,9 @@ extends Node2D func _physics_process(_delta) -> void: ship.engine.acceleration_axis = Input.get_axis("deccelerate", "accelerate") ship.engine.rotation_axis = Input.get_axis("rotateleft", "rotateright") + for weapon in ship.weapons.list: + match weapon.action_id: + "primary": + weapon.shoot_request = Input.get_action_strength("shootprimary") + "secondary": + weapon.shoot_request = Input.get_action_strength("shootsecondary") diff --git a/scripts/Ship/ship.gd b/scripts/Ship/ship.gd index 5ad5404..2a8673e 100644 --- a/scripts/Ship/ship.gd +++ b/scripts/Ship/ship.gd @@ -10,7 +10,7 @@ signal destroyed ## Reference to the engine module @onready var engine: ShipEngine = $Engine ## Reference to the hull module -@onready var hull: Hull = $Hull +@onready var hull: Hull = $HullHolder/Hull ## Reference to the shield module @onready var shield: Shield = $Shield ## Reference to weapons node @@ -20,6 +20,7 @@ signal destroyed var faction : Game.Faction = Game.Faction.Player func _ready() -> void: + hull.global_position += global_position destroyed.connect(destroy) ## Reset all required variables diff --git a/scripts/Ship/weapon.gd b/scripts/Ship/weapon.gd new file mode 100644 index 0000000..430f94a --- /dev/null +++ b/scripts/Ship/weapon.gd @@ -0,0 +1,51 @@ +extends Node2D + +class_name Weapon + +## Weapon ID to be used with Game.get_module() +@export var id: String = "weapon" +## Variable to use with input controller to determine which weapon to shoot +@export var action_id: String = "null" +## Projectile scene which will be created on shoot() +@export var projectile: PackedScene +## Applies additional angle (in degrees) while shooting +@export var spread: float = 0 +## Ammo type that will be consumed +@export var ammo_type: String = "n/a" +## How much ammo will be consumet each shoot() call +@export var ammo_consumption: float = 0 +## Timer to make cooldown between shots +@export var shoot_timer: Timer +## Points where projectiles will spawn +@export var spawner_points: Array[Node2D] +## Shortcut to get_parent().get_parent() +@onready var ship: Ship = get_parent().get_parent() +## Spread, converted to radians +var deviation: float = deg_to_rad(spread) +## Input variable to determine will the ship shoot +var shoot_request: bool = false + +## Emits when shoot() is called +signal weapon_shooted + +func _ready() -> void: + randomize() + +func _process(_delta) -> void: + var can_shoot = ship.hull.ammunition[ammo_type] >= ammo_consumption and shoot_timer.is_stopped() + if can_shoot and shoot_request: + shoot() + ship.hull.add_ammunition(ammo_type, -ammo_consumption) + shoot_timer.start() + if ammo_type == "Laser Energy": + ship.shield.laser_recharge_timer.start() + +func shoot() -> void: + for spawner in spawner_points: + var projectile_instance = projectile.instantiate() + ProjectileContainer.instance.add_child(projectile_instance) + projectile_instance.global_position = spawner.global_position + projectile_instance.global_rotation = spawner.global_rotation + randf_range(-deviation/2, deviation/2) + projectile_instance.faction = ship.faction + projectile_instance.modulate = ship.modulate + weapon_shooted.emit() diff --git a/scripts/Ship/weapons.gd b/scripts/Ship/weapons.gd index 2fa7365..1091ed0 100644 --- a/scripts/Ship/weapons.gd +++ b/scripts/Ship/weapons.gd @@ -3,23 +3,30 @@ extends Node2D # TODO: implement add_weapon ## Shortcut to get_parent() -@onready var ship = get_parent() +@onready var ship: Ship = get_parent() ## List of weapons -var list: Array[Weapon] = get_children() as Array[Weapon] +@onready var list: Array[Node] = get_children() as Array[Node] ## Updates list with actual children of this node func update_weapon_list() -> void: - list = get_children() as Array[Weapon] + list = get_children() as Array[Node] -## Removes weapon with given ID and returns true if successful -func remove_weapon(id: String) -> bool: +## Removes weapon with given ID +func remove_weapon(id: String) -> void: for weapon in list: if weapon.id == id: list.erase(weapon) weapon.free() - return true - return false + break + update_weapon_list() ## Adds weapon with given ID to list -func add_weapon(id: String) -> void: +func add_weapon(_id: String) -> void: pass + +## Returns a reference to weapon with given id if it exists, otherwise returns null +func get_weapon(id: String) -> Weapon: + for weapon in list: + if weapon.id == id: + return weapon as Weapon + return null diff --git a/scripts/game.gd b/scripts/game.gd index bcbf8bf..30b1f64 100644 --- a/scripts/game.gd +++ b/scripts/game.gd @@ -4,7 +4,7 @@ class_name Game enum ITEM_TYPE {VALUABLE, WEAPON, HULL, SHIELD, ENGINE, AMMUNITION} enum AMMO_TYPE {NULL, LASER_ENERGY, ROCKETS} enum BASE_TYPE {POWER, MINING, FOOD, TRADING, MODULE} -enum Faction {Player, Peaceful, Neutral, Aggressive} +enum Faction {None, Player, Peaceful, Neutral, Aggressive} const DEFAULT_ITEM = preload("res://items/test_item.tres") const DEFAULT_WEAPON = preload("res://scenes/weapons/presets/SingleLaserMk1.tscn") diff --git a/scripts/objects/projectile.gd b/scripts/objects/projectile.gd index 726c06e..24e548a 100644 --- a/scripts/objects/projectile.gd +++ b/scripts/objects/projectile.gd @@ -8,7 +8,7 @@ class_name Projectile @export var collider : Area2D @export var lifetime : float = 10 -var faction = "none" +var faction: Game.Faction = Game.Faction.None var destination_angle : float var target : Node2D = self @@ -34,10 +34,10 @@ func _physics_process(delta): func _on_collision(body): match body.collision_layer: 1: - if body.faction != faction: + if body.ship.faction != faction: if target != self: target.queue_free() - body.shield.deal_damage(damage,global_position) + body.ship.shield.deal_damage(damage) queue_free() 2: if target != self: diff --git a/scripts/objects/weapon.gd b/scripts/objects/weapon.gd index e9ef596..f939130 100644 --- a/scripts/objects/weapon.gd +++ b/scripts/objects/weapon.gd @@ -1,6 +1,6 @@ extends Node2D -class_name Weapon +# class_name Weapon @export var projectile : PackedScene @export var spread : float = 0