diff --git a/Scenes/Enemy.tscn b/Scenes/Enemy.tscn index 33471e2b..3ab60a92 100644 --- a/Scenes/Enemy.tscn +++ b/Scenes/Enemy.tscn @@ -1,8 +1,9 @@ -[gd_scene load_steps=8 format=3 uid="uid://v8s3kubgb2qg"] +[gd_scene load_steps=9 format=3 uid="uid://v8s3kubgb2qg"] [ext_resource type="Texture2D" uid="uid://b4ynnb14mb4uq" path="res://Sprites/Reisen.png" id="1_4w8mj"] [ext_resource type="Script" path="res://Scripts/Enemy.cs" id="1_lpwdj"] [ext_resource type="PackedScene" uid="uid://cuixq5ex0j40h" path="res://Scenes/enemyBullet.tscn" id="2_ogldd"] +[ext_resource type="PackedScene" uid="uid://crry0rgk7a8sm" path="res://Scenes/Weapons/BaseWeapon.tscn" id="4_2k1dv"] [sub_resource type="AtlasTexture" id="AtlasTexture_2brqc"] atlas = ExtResource("1_4w8mj") @@ -17,12 +18,11 @@ radius = 4.0 [sub_resource type="CircleShape2D" id="CircleShape2D_v711r"] radius = 85.0529 -[node name="Enemy" type="Area2D" groups=["Destroyable"]] +[node name="Enemy" type="Area2D" node_paths=PackedStringArray("EquippedWeapon") groups=["Destroyable"]] collision_layer = 16 collision_mask = 9 script = ExtResource("1_lpwdj") -BulletScene = ExtResource("2_ogldd") -BulletSpeed = 50.0 +EquippedWeapon = NodePath("Weapon") metadata/_edit_group_ = true [node name="Sprite2D" type="Sprite2D" parent="."] @@ -48,6 +48,11 @@ shape = SubResource("CircleShape2D_v711r") wait_time = 0.4 one_shot = true +[node name="Weapon" parent="." instance=ExtResource("4_2k1dv")] +BulletScene = ExtResource("2_ogldd") +BulletCapacity = 4 +BulletSpeed = 50.0 + [connection signal="area_entered" from="." to="." method="_on_area_entered"] [connection signal="area_entered" from="PlayerDetection" to="." method="_on_player_detection_area_entered"] [connection signal="area_exited" from="PlayerDetection" to="." method="_on_player_detection_area_exited"] diff --git a/Scenes/Weapons/BaseWeapon.tscn b/Scenes/Weapons/BaseWeapon.tscn new file mode 100644 index 00000000..61cab89d --- /dev/null +++ b/Scenes/Weapons/BaseWeapon.tscn @@ -0,0 +1,12 @@ +[gd_scene load_steps=2 format=3 uid="uid://crry0rgk7a8sm"] + +[ext_resource type="Script" path="res://Scripts/Weapon.cs" id="1_f5iec"] + +[node name="Weapon" type="Sprite2D"] +script = ExtResource("1_f5iec") + +[node name="Muzzle" type="Marker2D" parent="."] +position = Vector2(5, 0) + +[node name="ShootTimer" type="Timer" parent="."] +one_shot = true diff --git a/Scenes/player.tscn b/Scenes/player.tscn index a88e208c..9f927856 100644 --- a/Scenes/player.tscn +++ b/Scenes/player.tscn @@ -1,4 +1,4 @@ -[gd_scene load_steps=27 format=3 uid="uid://bghghp5ep4w2j"] +[gd_scene load_steps=28 format=3 uid="uid://bghghp5ep4w2j"] [ext_resource type="Script" path="res://Scripts/PlayerMovement.cs" id="1_m27vu"] [ext_resource type="Texture2D" uid="uid://la06powu57hu" path="res://Sprites/Cirno_Big.png" id="2_bwf6x"] @@ -8,6 +8,7 @@ [ext_resource type="Script" path="res://Scenes/CameraTarget.gd" id="5_cxvyt"] [ext_resource type="PackedScene" uid="uid://cfb3nsay84xdb" path="res://Scenes/crosshair.tscn" id="6_l43rf"] [ext_resource type="Script" path="res://Scenes/InteractionController.cs" id="7_uvgjg"] +[ext_resource type="PackedScene" uid="uid://crry0rgk7a8sm" path="res://Scenes/Weapons/BaseWeapon.tscn" id="9_wblq0"] [sub_resource type="RectangleShape2D" id="RectangleShape2D_ai4rh"] size = Vector2(6, 6) @@ -141,7 +142,7 @@ radius = 17.2627 [sub_resource type="CircleShape2D" id="CircleShape2D_e6woi"] radius = 1.41421 -[node name="Player" type="CharacterBody2D" node_paths=PackedStringArray("Muzzle") groups=["Destroyable", "player"]] +[node name="Player" type="CharacterBody2D" node_paths=PackedStringArray("Muzzle", "EquippedWeapon") groups=["Destroyable", "player"]] collision_layer = 2 collision_mask = 99 script = ExtResource("1_m27vu") @@ -152,7 +153,7 @@ SelectorScene = ExtResource("3_8wt6s") GameOverScene = "res://Scenes/GameOver.tscn" Muzzle = NodePath("Muzzle") Health = 32.0 -RateOfFire = 0.1 +EquippedWeapon = NodePath("Weapon") metadata/_edit_group_ = true [node name="CollisionShape2D" type="CollisionShape2D" parent="."] @@ -205,5 +206,12 @@ collision_mask = 128 shape = SubResource("CircleShape2D_e6woi") debug_color = Color(1, 0.00817797, 0.0443347, 0.42) +[node name="Weapon" parent="." instance=ExtResource("9_wblq0")] +BulletScene = ExtResource("2_ov36d") +RateOfFire = 0.1 +BulletCapacity = 100 +ReloadTime = 0.4 +BulletSpeed = 300.0 + [connection signal="area_entered" from="InteractionController" to="." method="_on_interaction_controller_area_entered"] [connection signal="area_entered" from="DamageHitBox" to="." method="_on_damage_hit_box_area_entered"] diff --git a/Scripts/Enemy.cs b/Scripts/Enemy.cs index b833d1bb..aa940ed7 100644 --- a/Scripts/Enemy.cs +++ b/Scripts/Enemy.cs @@ -8,34 +8,19 @@ public partial class Enemy : Area2D, IDestructible private InteractionController _cachedPlayer; private EnemyState _currentState = EnemyState.Idle; - [Export] - public PackedScene BulletScene { get; set; } - [Export] public float Health = 4f; - [Export] public double RateOfFire = 0.4f; - - [Export] public double ReloadTime = 1.0f; - - [Export] public int BulletCount = 4; - - [Export] public float BulletSpeed = 100f; + [Export] public Weapon EquippedWeapon; private float _currentHealth = 0f; private bool _isDestroyed = false; - - private Timer _cooldownTimer; - - [DebugGUIPrint] - private int _ammo = 0; + // Called when the node enters the scene tree for the first time. public override void _Ready() { _currentHealth = Health; - _ammo = BulletCount; - _cooldownTimer = GetNode("./ShootTimer"); } // Called every frame. 'delta' is the elapsed time since the previous frame. @@ -72,7 +57,7 @@ public partial class Enemy : Area2D, IDestructible return; } - if (_cooldownTimer.IsStopped() && IsPlayerInSight()) + if (IsPlayerInSight()) { Shoot(); } @@ -80,26 +65,32 @@ public partial class Enemy : Area2D, IDestructible private void Shoot() { - // SHOOT - var bullet = this.CreateChild(BulletScene); - // var bullet = BulletScene.Instantiate(); - // Owner.AddChild(bullet); - // bullet.Transform = this.GlobalTransform; - // bullet.Position = this.Position; - bullet.SetDirection((_cachedPlayer.GlobalPosition - this.GlobalPosition).Normalized()); - bullet.Speed = BulletSpeed; + if (EquippedWeapon == null) return; - _ammo -= 1; - - if (_ammo <= 0) - { - _ammo = BulletCount; - _cooldownTimer.Start(ReloadTime); - } - else - { - _cooldownTimer.Start(RateOfFire); - } + EquippedWeapon.ShootDirection = (_cachedPlayer.GlobalPosition - this.GlobalPosition).Normalized(); + + EquippedWeapon.Shoot(); + + // // SHOOT + // var bullet = this.CreateChild(BulletScene); + // // var bullet = BulletScene.Instantiate(); + // // Owner.AddChild(bullet); + // // bullet.Transform = this.GlobalTransform; + // // bullet.Position = this.Position; + // bullet.SetDirection((_cachedPlayer.GlobalPosition - this.GlobalPosition).Normalized()); + // bullet.Speed = BulletSpeed; + // + // _ammo -= 1; + // + // if (_ammo <= 0) + // { + // _ammo = BulletCount; + // _cooldownTimer.Start(ReloadTime); + // } + // else + // { + // _cooldownTimer.Start(RateOfFire); + // } } private bool IsPlayerInSight() diff --git a/Scripts/PlayerMovement.cs b/Scripts/PlayerMovement.cs index d6521358..c45e208b 100644 --- a/Scripts/PlayerMovement.cs +++ b/Scripts/PlayerMovement.cs @@ -11,9 +11,6 @@ public partial class PlayerMovement : CharacterBody2D, IDestructible [Export] public float CrosshairDistance { get; set; } = 10f; - [Export] - public PackedScene BulletScene { get; set; } - [Export] public PackedScene SelectorScene { get; set; } @@ -35,17 +32,13 @@ public partial class PlayerMovement : CharacterBody2D, IDestructible private Sprite2D _crosshair; - private Timer _cooldownTimer; - [Export] public float Health = 4f; + + [Export] public Weapon EquippedWeapon; [DebugGUIPrint] private float _currentHealth = 0f; - [Export] public double RateOfFire = 0.4f; - - [Export] public float BulletSpeed = 300f; - private bool _isDestroyed = false; public override void _Ready() @@ -54,7 +47,6 @@ public partial class PlayerMovement : CharacterBody2D, IDestructible _animatedSprite = GetNode("./Smoothing2D/AnimatedSprite2D"); _crosshair = GetNode("./Smoothing2D/Crosshair"); - _cooldownTimer = GetNode("./ShootTimer"); _movementDirection = Vector2.Zero; _facingDirection = Vector2.Zero; @@ -100,13 +92,18 @@ public partial class PlayerMovement : CharacterBody2D, IDestructible private void HandleShoot() { - if (!Input.IsActionJustPressed("shoot")) return; - //Debug.WriteLine("Shoot"); - var bullet = BulletScene.Instantiate(); - Owner.AddChild(bullet); - bullet.Transform = Muzzle.GlobalTransform; - bullet.Position = this.Position; - bullet.SetDirection(this._facingDirection); + if (EquippedWeapon == null) return; + if (!Input.IsActionPressed("shoot")) return; + + EquippedWeapon.ShootDirection = this._facingDirection; + EquippedWeapon.Shoot(); + + // //Debug.WriteLine("Shoot"); + // var bullet = BulletScene.Instantiate(); + // Owner.AddChild(bullet); + // bullet.Transform = Muzzle.GlobalTransform; + // bullet.Position = this.Position; + // bullet.SetDirection(this._facingDirection); } private void SetAnimation() diff --git a/Scripts/Weapon.cs b/Scripts/Weapon.cs new file mode 100644 index 00000000..8c1e805d --- /dev/null +++ b/Scripts/Weapon.cs @@ -0,0 +1,84 @@ +using Godot; +using System; +using Cirno.Scripts; + +public partial class Weapon : Node2D +{ + + [Export] + public PackedScene BulletScene { get; set; } + + [Export] + public Marker2D Muzzle { get; set; } + + [Export] public double RateOfFire = 0.4f; + + [Export] public int BulletCapacity = 20; + + [Export] public double ReloadTime = 1.0f; + + [Export] public float BulletSpeed = 100f; + + [Export] public bool AutoReload = true; + + [Export] public bool InfiniteAmmo = true; + public int Ammo { get; set; } = 0; + + public int LoadedAmmo { get; private set; } + + public Vector2 ShootDirection { get; set; } = Vector2.Zero; + + private Timer _cooldownTimer; + + private Marker2D _muzzle; + + // Called when the node enters the scene tree for the first time. + public override void _Ready() + { + _muzzle = GetNode("./Muzzle"); + _cooldownTimer = GetNode("./ShootTimer"); + } + + public void Reload() + { + _cooldownTimer.Start(ReloadTime); + + if (InfiniteAmmo) + { + LoadedAmmo = BulletCapacity; + } + else + { + // TODO: Calculate subtraction, etc + LoadedAmmo = BulletCapacity; + } + } + + public void Shoot() + { + // Waiting on reload or Rate of Fire cooldown? + if (!_cooldownTimer.IsStopped()) + { + return; + } + + // Out of ammo? + if (LoadedAmmo <= 0) + { + if (AutoReload) + { + Reload(); + } + return; + } + + // TODO: Shoot at muzzle position, need to provide a way to turn it, on a radius? + var bullet = this.CreateChild(BulletScene); + bullet.SetDirection(ShootDirection); + bullet.Speed = BulletSpeed; + + LoadedAmmo -= 1; + + _cooldownTimer.Start(RateOfFire); + } +}