From 5eb7b578bccce7acb70e678e799f9a53290a3ff3 Mon Sep 17 00:00:00 2001 From: Marco Date: Tue, 11 Feb 2025 11:50:45 +0100 Subject: [PATCH] Ammo UI system --- Cirno.sln.DotSettings.user | 2 + Resources/Items/Ammo1.tres | 10 ++-- Resources/Items/Ammo1.tscn | 11 ---- Resources/Items/IceShotgun.tres | 18 ++++++ Resources/Items/IcicleGun.tres | 18 ++++++ Resources/Weapons/Ice_Shotgun_Weapon.tres | 23 ++++++++ Resources/Weapons/IcicleGun.tres | 4 +- Scenes/HUD/HUD.tscn | 14 +---- Scenes/HUD/Items/ice_shotgun_huditem.tscn | 7 +++ Scenes/Interactable.cs | 2 +- Scenes/Items/IcicleGun.tscn | 6 +- Scenes/Weapons/enemy_weapon_base.tscn | 2 - Scenes/player.tscn | 3 +- Scenes/test.tscn | 17 +++++- Scripts/GameManager.cs | 70 +++++++++++++++++++---- Scripts/Hud.cs | 38 ++++++++++-- Scripts/InventoryManager.cs | 48 +++++++--------- Scripts/ItemTypes.cs | 5 +- Scripts/PlayerMovement.cs | 11 +++- Scripts/Resources/LootItem.cs | 1 + Scripts/Resources/WeaponResource.cs | 1 + Scripts/Weapon.cs | 20 +++++-- 22 files changed, 241 insertions(+), 90 deletions(-) delete mode 100644 Resources/Items/Ammo1.tscn create mode 100644 Resources/Items/IceShotgun.tres create mode 100644 Resources/Items/IcicleGun.tres create mode 100644 Resources/Weapons/Ice_Shotgun_Weapon.tres create mode 100644 Scenes/HUD/Items/ice_shotgun_huditem.tscn diff --git a/Cirno.sln.DotSettings.user b/Cirno.sln.DotSettings.user index 55102c91..0be9c13e 100644 --- a/Cirno.sln.DotSettings.user +++ b/Cirno.sln.DotSettings.user @@ -3,7 +3,9 @@ ForceIncluded ForceIncluded ForceIncluded + ForceIncluded ForceIncluded ForceIncluded ForceIncluded + ForceIncluded ForceIncluded \ No newline at end of file diff --git a/Resources/Items/Ammo1.tres b/Resources/Items/Ammo1.tres index 2812043a..0a991c18 100644 --- a/Resources/Items/Ammo1.tres +++ b/Resources/Items/Ammo1.tres @@ -1,19 +1,21 @@ -[gd_resource type="Resource" script_class="LootItem" load_steps=4 format=3 uid="uid://ct1fa2huvy34n"] +[gd_resource type="Resource" script_class="LootItem" load_steps=5 format=3 uid="uid://ct1fa2huvy34n"] [ext_resource type="Script" path="res://Scripts/Resources/LootItem.cs" id="1_31o2l"] [ext_resource type="Texture2D" uid="uid://c5g0qg6u2gm6f" path="res://Sprites/Items/Ammo1.png" id="1_bpftr"] +[ext_resource type="PackedScene" uid="uid://cjv4yahpb0h2l" path="res://Scenes/HUD/Items/ammo.tscn" id="1_qkp4b"] [sub_resource type="AtlasTexture" id="AtlasTexture_3y0hf"] atlas = ExtResource("1_bpftr") [resource] script = ExtResource("1_31o2l") -ItemName = "" -ItemKey = "" +ItemName = "Ammo 1" +ItemKey = "Ammo1" Item = 3 Amount = 100 Max = 800 PickupIfMaxed = false ConsumeOnUse = true -UiType = 0 +UiType = 2 InventorySprite = SubResource("AtlasTexture_3y0hf") +HudItemScene = ExtResource("1_qkp4b") diff --git a/Resources/Items/Ammo1.tscn b/Resources/Items/Ammo1.tscn deleted file mode 100644 index 401314f7..00000000 --- a/Resources/Items/Ammo1.tscn +++ /dev/null @@ -1,11 +0,0 @@ -[gd_scene load_steps=4 format=3 uid="uid://j1pbuvw2kduo"] - -[ext_resource type="PackedScene" uid="uid://dxs3ks2ucaxl4" path="res://Scenes/Items/Red_Keycard.tscn" id="1_v0ux4"] -[ext_resource type="Resource" uid="uid://ct1fa2huvy34n" path="res://Resources/Items/Ammo1.tres" id="2_2eoli"] -[ext_resource type="SpriteFrames" uid="uid://pmwatoyt2ase" path="res://Resources/Sprites/Ammo_Inventory_Sprite.tres" id="2_8g7mw"] - -[node name="Ammo1" instance=ExtResource("1_v0ux4")] -LootTable = [ExtResource("2_2eoli")] - -[node name="AnimatedSprite2D" parent="." index="1"] -sprite_frames = ExtResource("2_8g7mw") diff --git a/Resources/Items/IceShotgun.tres b/Resources/Items/IceShotgun.tres new file mode 100644 index 00000000..a4d00203 --- /dev/null +++ b/Resources/Items/IceShotgun.tres @@ -0,0 +1,18 @@ +[gd_resource type="Resource" script_class="LootItem" load_steps=4 format=3 uid="uid://dau0s8ob7qnpc"] + +[ext_resource type="PackedScene" uid="uid://bqb3gbnrkqewt" path="res://Scenes/HUD/Items/ice_shotgun_huditem.tscn" id="1_13ven"] +[ext_resource type="Resource" uid="uid://cyntngidhcbkc" path="res://Resources/Weapons/Ice_Shotgun_Weapon.tres" id="2_3le6e"] +[ext_resource type="Script" path="res://Scripts/Resources/LootItem.cs" id="3_cjfxs"] + +[resource] +script = ExtResource("3_cjfxs") +ItemName = "Ice Shotgun" +ItemKey = "IceShotgun" +Item = 9 +WeaponData = ExtResource("2_3le6e") +Amount = 1 +Max = 1 +PickupIfMaxed = false +ConsumeOnUse = false +UiType = 1 +HudItemScene = ExtResource("1_13ven") diff --git a/Resources/Items/IcicleGun.tres b/Resources/Items/IcicleGun.tres new file mode 100644 index 00000000..ee925646 --- /dev/null +++ b/Resources/Items/IcicleGun.tres @@ -0,0 +1,18 @@ +[gd_resource type="Resource" script_class="LootItem" load_steps=4 format=3 uid="uid://cs3ihltcn2166"] + +[ext_resource type="Resource" uid="uid://b8apu0l5fm4k" path="res://Resources/Weapons/IcicleGun.tres" id="1_itajb"] +[ext_resource type="PackedScene" uid="uid://cicthr1v5v4s2" path="res://Scenes/HUD/Items/icicle_gun.tscn" id="1_lfbsh"] +[ext_resource type="Script" path="res://Scripts/Resources/LootItem.cs" id="2_hy2hk"] + +[resource] +script = ExtResource("2_hy2hk") +ItemName = "Icicle Gun" +ItemKey = "IcicleGun" +Item = 9 +WeaponData = ExtResource("1_itajb") +Amount = 1 +Max = 1 +PickupIfMaxed = false +ConsumeOnUse = false +UiType = 1 +HudItemScene = ExtResource("1_lfbsh") diff --git a/Resources/Weapons/Ice_Shotgun_Weapon.tres b/Resources/Weapons/Ice_Shotgun_Weapon.tres new file mode 100644 index 00000000..fdc0efec --- /dev/null +++ b/Resources/Weapons/Ice_Shotgun_Weapon.tres @@ -0,0 +1,23 @@ +[gd_resource type="Resource" script_class="WeaponResource" load_steps=3 format=3 uid="uid://cyntngidhcbkc"] + +[ext_resource type="PackedScene" uid="uid://b1qnfiuokpvsr" path="res://Scenes/Weapons/bullet.tscn" id="1_puh43"] +[ext_resource type="Script" path="res://Scripts/Resources/WeaponResource.cs" id="2_utbbo"] + +[resource] +script = ExtResource("2_utbbo") +Name = "Ice Shotgun" +BulletScene = ExtResource("1_puh43") +RateOfFire = 0.1 +BulletCapacity = 4 +ReloadTime = 0.8 +AutoReload = true +InfiniteAmmo = false +BulletsPerShot = 3 +SpreadAngle = 45.0 +RandomSpread = 0.0 +AmmoKey = "Ammo1" +BulletSpeed = 300.0 +BulletDamage = 1.0 +_rotationOffset = 0.0 +owner = 1 +_timeModifiers = null diff --git a/Resources/Weapons/IcicleGun.tres b/Resources/Weapons/IcicleGun.tres index 544a3bb1..60a71c14 100644 --- a/Resources/Weapons/IcicleGun.tres +++ b/Resources/Weapons/IcicleGun.tres @@ -15,9 +15,9 @@ InfiniteAmmo = true BulletsPerShot = 1 SpreadAngle = 0.0 RandomSpread = 0.0 +AmmoKey = "" BulletSpeed = 300.0 -bulletPerShotCount = 1 +BulletDamage = 1.0 _rotationOffset = 0.0 -spread = 0.0 owner = 1 _timeModifiers = null diff --git a/Scenes/HUD/HUD.tscn b/Scenes/HUD/HUD.tscn index 2bf86c8d..163dc9b7 100644 --- a/Scenes/HUD/HUD.tscn +++ b/Scenes/HUD/HUD.tscn @@ -1,9 +1,8 @@ -[gd_scene load_steps=8 format=3 uid="uid://dkwi1hu1bixoe"] +[gd_scene load_steps=7 format=3 uid="uid://dkwi1hu1bixoe"] [ext_resource type="Script" path="res://Scripts/Hud.cs" id="1_m0hb0"] [ext_resource type="FontFile" uid="uid://ccm3u37q1hvux" path="res://fonts/Xolonium-Regular.ttf" id="2_0xmx2"] [ext_resource type="Texture2D" uid="uid://dde36pgb8fbwc" path="res://Sprites/selector_big.png" id="3_hee3i"] -[ext_resource type="PackedScene" uid="uid://cicthr1v5v4s2" path="res://Scenes/HUD/Items/icicle_gun.tscn" id="4_m333p"] [sub_resource type="AtlasTexture" id="AtlasTexture_h626b"] atlas = ExtResource("3_hee3i") @@ -81,14 +80,3 @@ text = "100/100" [node name="ItemsContainer" type="VBoxContainer" parent="VBoxContainer"] layout_mode = 2 - -[node name="HBoxContainer" type="HBoxContainer" parent="VBoxContainer/ItemsContainer"] -layout_mode = 2 - -[node name="IcicleGun" parent="VBoxContainer/ItemsContainer/HBoxContainer" instance=ExtResource("4_m333p")] -layout_mode = 2 - -[node name="Label" type="Label" parent="VBoxContainer/ItemsContainer/HBoxContainer"] -layout_mode = 2 -theme_override_font_sizes/font_size = 10 -text = "100/300" diff --git a/Scenes/HUD/Items/ice_shotgun_huditem.tscn b/Scenes/HUD/Items/ice_shotgun_huditem.tscn new file mode 100644 index 00000000..1098961e --- /dev/null +++ b/Scenes/HUD/Items/ice_shotgun_huditem.tscn @@ -0,0 +1,7 @@ +[gd_scene load_steps=2 format=3 uid="uid://bqb3gbnrkqewt"] + +[ext_resource type="Texture2D" uid="uid://b46usr10jhmev" path="res://Sprites/Items/Ice_Shotgun.png" id="1_1dd2l"] + +[node name="IceShotgun" type="TextureRect"] +texture = ExtResource("1_1dd2l") +stretch_mode = 2 diff --git a/Scenes/Interactable.cs b/Scenes/Interactable.cs index 8c32f22a..45ba1c67 100644 --- a/Scenes/Interactable.cs +++ b/Scenes/Interactable.cs @@ -21,7 +21,7 @@ public partial class Interactable : Area2D { if (Requirements.Any()) { - if (_inventoryManager.HasItems(Requirements.Select(x => x.Item).ToList())) + if (_inventoryManager.HasItems(Requirements.Select(x => x.ItemKey).ToList())) { GD.Print($"Requirements for activation of {this.Name} successfully met: {string.Join(",", Requirements.Select(x => x.Item))} "); return true; diff --git a/Scenes/Items/IcicleGun.tscn b/Scenes/Items/IcicleGun.tscn index a5e7835f..4f594498 100644 --- a/Scenes/Items/IcicleGun.tscn +++ b/Scenes/Items/IcicleGun.tscn @@ -1,10 +1,14 @@ -[gd_scene load_steps=3 format=3 uid="uid://q7hau0tl3vsr"] +[gd_scene load_steps=5 format=3 uid="uid://q7hau0tl3vsr"] [ext_resource type="Texture2D" uid="uid://duwiasewxvcb5" path="res://Sprites/Items/Icicle_Gun.png" id="1_exv8g"] +[ext_resource type="Script" path="res://Scripts/Interactables/ItemPickup.cs" id="1_rdvi1"] +[ext_resource type="Resource" uid="uid://cs3ihltcn2166" path="res://Resources/Items/IcicleGun.tres" id="2_6qt07"] [sub_resource type="CircleShape2D" id="CircleShape2D_jtwy2"] [node name="IcicleGun" type="Area2D"] +script = ExtResource("1_rdvi1") +LootTable = [ExtResource("2_6qt07")] [node name="Sprite2D" type="Sprite2D" parent="."] texture = ExtResource("1_exv8g") diff --git a/Scenes/Weapons/enemy_weapon_base.tscn b/Scenes/Weapons/enemy_weapon_base.tscn index 60b626e6..8e60b216 100644 --- a/Scenes/Weapons/enemy_weapon_base.tscn +++ b/Scenes/Weapons/enemy_weapon_base.tscn @@ -5,5 +5,3 @@ [node name="EnemyWeapon" instance=ExtResource("1_60orr")] BulletScene = ExtResource("2_dyoou") -BulletCapacity = 4 -BulletSpeed = 50.0 diff --git a/Scenes/player.tscn b/Scenes/player.tscn index a87c0546..dc455591 100644 --- a/Scenes/player.tscn +++ b/Scenes/player.tscn @@ -143,7 +143,7 @@ radius = 17.2627 [sub_resource type="CircleShape2D" id="CircleShape2D_e6woi"] radius = 1.0 -[node name="Player" type="CharacterBody2D" node_paths=PackedStringArray("Muzzle", "EquippedWeapon") groups=["Destroyable", "player"]] +[node name="Player" type="CharacterBody2D" node_paths=PackedStringArray("Muzzle") groups=["Destroyable", "player"]] y_sort_enabled = true collision_layer = 2 collision_mask = 99 @@ -154,7 +154,6 @@ CrosshairDistance = 20.0 SelectorScene = ExtResource("3_8wt6s") GameOverScene = "res://Scenes/GameOver.tscn" Muzzle = NodePath("Muzzle") -EquippedWeapon = NodePath("Weapon") metadata/_edit_group_ = true [node name="CollisionShape2D" type="CollisionShape2D" parent="."] diff --git a/Scenes/test.tscn b/Scenes/test.tscn index b4170faa..ee8560a6 100644 --- a/Scenes/test.tscn +++ b/Scenes/test.tscn @@ -1,10 +1,14 @@ -[gd_scene load_steps=40 format=4 uid="uid://bv451a8wgty4u"] +[gd_scene load_steps=44 format=4 uid="uid://bv451a8wgty4u"] [ext_resource type="Script" path="res://Scripts/GameManager.cs" id="1_8tmoj"] [ext_resource type="PackedScene" uid="uid://bghghp5ep4w2j" path="res://Scenes/player.tscn" id="2_8mh54"] +[ext_resource type="PackedScene" uid="uid://crry0rgk7a8sm" path="res://Scenes/Weapons/BaseWeapon.tscn" id="3_2wlhc"] [ext_resource type="TileSet" uid="uid://6k28roiljylj" path="res://Tilesets/factory_tileset.tres" id="3_kgs2x"] [ext_resource type="Script" path="res://Scripts/TilemapAvoidance.cs" id="4_p1fl3"] [ext_resource type="PackedScene" uid="uid://rp4jhx0tuh24" path="res://Scenes/fragola.tscn" id="4_s7wq6"] +[ext_resource type="Resource" uid="uid://ct1fa2huvy34n" path="res://Resources/Items/Ammo1.tres" id="4_swym2"] +[ext_resource type="Resource" uid="uid://dau0s8ob7qnpc" path="res://Resources/Items/IceShotgun.tres" id="5_frf65"] +[ext_resource type="Resource" uid="uid://cs3ihltcn2166" path="res://Resources/Items/IcicleGun.tres" id="5_nqier"] [ext_resource type="PackedScene" uid="uid://uaf5r6cd71hu" path="res://Scenes/Props/LargeTank.tscn" id="6_nkauc"] [ext_resource type="Script" path="res://Scenes/CameraController.gd" id="6_t8ide"] [ext_resource type="PackedScene" uid="uid://crmwuwhltmdq7" path="res://Scenes/Props/MediumTank.tscn" id="7_lmrl1"] @@ -47,6 +51,8 @@ position = Vector2(38, 39) script = ExtResource("1_8tmoj") PlayerTemplate = ExtResource("2_8mh54") PlayerSpawnMarker = NodePath("PlayerStartPosition") +WeaponTemplate = ExtResource("3_2wlhc") +StartingEquipment = Array[Object]([ExtResource("4_swym2"), ExtResource("5_frf65"), ExtResource("5_nqier")]) [node name="Factory Tilemaps" type="Node2D" parent="."] @@ -333,3 +339,12 @@ _trackName = "computer2" [node name="Ammo1" parent="." instance=ExtResource("34_17pjh")] position = Vector2(-792, -378) + +[node name="Ammo2" parent="." instance=ExtResource("34_17pjh")] +position = Vector2(-808, -378) + +[node name="Ammo3" parent="." instance=ExtResource("34_17pjh")] +position = Vector2(-808, -400) + +[node name="Ammo4" parent="." instance=ExtResource("34_17pjh")] +position = Vector2(-791, -401) diff --git a/Scripts/GameManager.cs b/Scripts/GameManager.cs index 14104f1c..41e85487 100644 --- a/Scripts/GameManager.cs +++ b/Scripts/GameManager.cs @@ -1,6 +1,10 @@ using Godot; using System; +using System.Linq; +using System.Threading.Tasks; using Cirno.Scripts; +using Cirno.Scripts.Resources; +using Godot.Collections; public partial class GameManager : Node2D { @@ -22,6 +26,10 @@ public partial class GameManager : Node2D [Export] public Marker2D PlayerSpawnMarker { get; set; } + [Export] public PackedScene WeaponTemplate { get; private set; } + + [Export] public Array StartingEquipment { get; private set; } + private InventoryManager _inventoryManager { get; set; } //private AlarmManager _alarmManager { get; set; } @@ -52,25 +60,15 @@ public partial class GameManager : Node2D SpawnBulletsContainer(); - if (PlayerSpawnMarker != null) - { - SpawnPlayer(); - } - if (_inventoryManager != null && _hud != null) { _inventoryManager.ItemAdded += (item, currentAmount) => _hud.AddInventoryItem(item, currentAmount); _inventoryManager.ItemRemoved += (item, currentAmount) => _hud.RemoveInventoryItem(item, currentAmount); } - if (_player != null && _hud != null) - { - _player.HealthChanged += (newHealth, maxHealth) => _hud.UpdateHealth(newHealth, maxHealth); - - _player.InteractableAreaEntered += (interactable) => _hud.UpdateInteractable(interactable); - } - GameState = GameState.Playing; + + _ = DelayPlayerSpawn(); } // Called every frame. 'delta' is the elapsed time since the previous frame. @@ -82,6 +80,25 @@ public partial class GameManager : Node2D } } + private async Task DelayPlayerSpawn() + { + await Task.Delay(500); + + if (PlayerSpawnMarker != null) + { + SpawnPlayer(); + + SpawnWeapons(); + } + + if (_player != null && _hud != null) + { + _player.HealthChanged += (newHealth, maxHealth) => _hud.UpdateHealth(newHealth, maxHealth); + + _player.InteractableAreaEntered += (interactable) => _hud.UpdateInteractable(interactable); + } + } + public void SpawnPlayer() { if (_player != null) return; @@ -99,6 +116,35 @@ public partial class GameManager : Node2D _cameraTarget.GlobalPosition = _player.Position; } } + + private void SpawnWeapons() + { + if (!StartingEquipment.Any()) + { + GD.Print("No items to spawn on Player"); + return; + } + + foreach (var startingItem in StartingEquipment) + { + switch (startingItem.Item) + { + case ItemTypes.Weapon: + if (WeaponTemplate == null) + { + GD.Print("Could not spawn weapon because template is null"); + break; + } + var weapon = _player.CreateChild(WeaponTemplate); + weapon.WeaponData = startingItem.WeaponData; + + _player.EquippedWeapon ??= weapon; + break; + } + + _inventoryManager.AddItem(startingItem); + } + } private void SpawnBulletsContainer() { diff --git a/Scripts/Hud.cs b/Scripts/Hud.cs index d3cb5b98..9a434df0 100644 --- a/Scripts/Hud.cs +++ b/Scripts/Hud.cs @@ -2,6 +2,7 @@ using Godot; using System; using System.Collections.Generic; using System.Linq; +using Cirno.Scripts; using Cirno.Scripts.Resources; public partial class Hud : CanvasLayer @@ -82,12 +83,33 @@ public partial class Hud : CanvasLayer { Item = item, Container = hbox, + //Amount = currentAmount }; if (item.UiType == UiItemType.IconText) { var label = new Label(); label.Text = currentAmount.ToString(); + // if (item.Item == ItemTypes.Weapon && item.WeaponData != null) + // { + // // Show ammo instead of item count + // var ammoItem = _items.GetValueOrDefault(item.WeaponData.AmmoKey); + // + // if (ammoItem != null) + // { + // label.Text = "0"; + // } + // else + // { + // label.Text = ammoItem. + // } + // + // } + // else + // { + // label.Text = currentAmount.ToString(); + // } + label.LabelSettings = new LabelSettings() { FontSize = 8 @@ -116,24 +138,26 @@ public partial class Hud : CanvasLayer // _itemsContainer.AddChild(texture); } - public void RemoveInventoryItem(LootItem item, int currentAmount) + public void RemoveInventoryItem(string itemKey, int currentAmount) { - - if (_items.TryGetValue(item.ItemKey, out var itm)) + if (_items.TryGetValue(itemKey, out var itm)) { if (currentAmount <= 0) { itm.Container.QueueFree(); - _items.Remove(item.ItemKey); + _items.Remove(itemKey); } else { - itm.Label.Text = currentAmount.ToString(); + if (itm.Item.UiType == UiItemType.IconText) + { + itm.Label.Text = currentAmount.ToString(); + } } } else { - GD.Print($"Tried to remove item {item.ItemName} {item.ItemKey} but it was not found"); + GD.Print($"Tried to remove item {itemKey} but it was not found"); } // var containerItem = _items.FirstOrDefault(x => x.Item == item); @@ -152,5 +176,7 @@ public partial class Hud : CanvasLayer public LootItem Item { get; set; } public HBoxContainer Container { get; set; } public Label Label { get; set; } + + //public int Amount { get; set; } } } diff --git a/Scripts/InventoryManager.cs b/Scripts/InventoryManager.cs index b193adfa..f9f988f8 100644 --- a/Scripts/InventoryManager.cs +++ b/Scripts/InventoryManager.cs @@ -9,8 +9,6 @@ public partial class InventoryManager : Node2D { public bool RedKeycard { get; set; } - private List _items = new List(); - private Dictionary _itemsDict = new Dictionary(); public Dictionary Items => _itemsDict; @@ -19,7 +17,7 @@ public partial class InventoryManager : Node2D public delegate void ItemAddedEventHandler(LootItem item, int currentAmount); [Signal] - public delegate void ItemRemovedEventHandler(LootItem item, int currentAmount); + public delegate void ItemRemovedEventHandler(string itemKey, int currentAmount); // Called when the node enters the scene tree for the first time. public override void _Ready() @@ -30,38 +28,34 @@ public partial class InventoryManager : Node2D public override void _Process(double delta) { } - - public bool HasItems(List items) + + public bool HasItems(List itemKeys) { - return items.Aggregate(false, (current, item) => current || HasItem(item)); + return itemKeys.Aggregate(false, (current, item) => current || GetItemCount(item) > 0); } - public bool HasItem(ItemTypes type) + public int GetItemCount(string itemKey) { - return _items.Any(x => x.Item == type && x.Amount > 0); + return _itemsDict.TryGetValue(itemKey, out var itm) ? itm.Count : 0; } - public int RemoveItem(LootItem item, int amount) + public int RemoveItem(string itemKey, int amount) { - if (_itemsDict.ContainsKey(item.ItemKey)) + if (!_itemsDict.TryGetValue(itemKey, out var itm)) return 0; + + int removed = Math.Min(itm.Count, amount); + + itm.Count -= amount; + + if (itm.Count <= 0) { - var itm = _itemsDict[item.ItemKey]; - - int removed = 0; - - itm.Count -= amount; - - if (itm.Count <= 0) - { - _itemsDict.Remove(item.ItemKey); - } - - EmitSignal(nameof(ItemRemoved), item, item.Amount); - - return itm.Count; + _itemsDict.Remove(itemKey); } + + EmitSignal(nameof(ItemRemoved), itemKey, itm.Count); + + return removed; - return 0; } public bool AddItem(LootItem item) @@ -81,7 +75,7 @@ public partial class InventoryManager : Node2D } else { - if (itm.Count < item.Amount) + if (itm.Count < item.Max) { itm.Count += item.Amount; } @@ -96,7 +90,7 @@ public partial class InventoryManager : Node2D return false; } } - EmitSignal(nameof(ItemAdded), item, itm.Count, item.Amount); + EmitSignal(nameof(ItemAdded), item, itm.Count); } return true; diff --git a/Scripts/ItemTypes.cs b/Scripts/ItemTypes.cs index 1c8c8dbe..a63ae28e 100644 --- a/Scripts/ItemTypes.cs +++ b/Scripts/ItemTypes.cs @@ -7,8 +7,9 @@ public enum ItemTypes KeycardGreen, Ammo, Medkit, - CrabBomb, + FrogBomb, Bomb, Mine, - Battery + Battery, + Weapon } \ No newline at end of file diff --git a/Scripts/PlayerMovement.cs b/Scripts/PlayerMovement.cs index 60b50cbd..11aaeb97 100644 --- a/Scripts/PlayerMovement.cs +++ b/Scripts/PlayerMovement.cs @@ -1,7 +1,10 @@ using Godot; using System; using System.Diagnostics; +using System.Linq; using Cirno.Scripts; +using Cirno.Scripts.Resources; +using Godot.Collections; public partial class PlayerMovement : CharacterBody2D, IDestructible { @@ -41,13 +44,15 @@ public partial class PlayerMovement : CharacterBody2D, IDestructible [Export] public float MaxHealth = 32f; - [Export] public Weapon EquippedWeapon; + public Weapon EquippedWeapon { get; set; } private float _currentHealth = 0f; private bool _isDestroyed = false; private GameManager _gameManager; + + private InventoryManager _inventoryManager; [Export] public Sprite2D HitboxSprite { get; set; } @@ -94,6 +99,8 @@ public partial class PlayerMovement : CharacterBody2D, IDestructible _gameManager = this.GetGameManager(); //GetNode("/root/GameScene"); + _inventoryManager = this.GetInventoryManager(); + _gameManager.GameStateChange += GameManagerOnGameStateChange; if (SelectorScene != null) @@ -102,7 +109,7 @@ public partial class PlayerMovement : CharacterBody2D, IDestructible _selector.Visible = false; } } - + private void GameManagerOnGameStateChange(GameState state) { switch (state) diff --git a/Scripts/Resources/LootItem.cs b/Scripts/Resources/LootItem.cs index 92671364..e4bf7481 100644 --- a/Scripts/Resources/LootItem.cs +++ b/Scripts/Resources/LootItem.cs @@ -8,6 +8,7 @@ public partial class LootItem : Resource [Export] public string ItemName { get; set; } [Export] public string ItemKey { get; set; } [Export] public ItemTypes Item; + [Export] public WeaponResource WeaponData { get; set; } [Export] public int Amount; [Export] public int Max; [Export] public bool PickupIfMaxed; diff --git a/Scripts/Resources/WeaponResource.cs b/Scripts/Resources/WeaponResource.cs index fd11012e..4946f95b 100644 --- a/Scripts/Resources/WeaponResource.cs +++ b/Scripts/Resources/WeaponResource.cs @@ -30,6 +30,7 @@ public partial class WeaponResource : Resource #region Bullet spawn data + [Export] public string AmmoKey; [Export] public float BulletSpeed = 100f; [Export] public float BulletDamage = 1; [Export] private float _rotationOffset = 0f; diff --git a/Scripts/Weapon.cs b/Scripts/Weapon.cs index ab36e988..8cd4cf25 100644 --- a/Scripts/Weapon.cs +++ b/Scripts/Weapon.cs @@ -30,6 +30,8 @@ public partial class Weapon : Node2D private Node2D _bulletsContainer; private GameManager _gameManager; + + private InventoryManager _inventoryManager; // Called when the node enters the scene tree for the first time. public override void _Ready() @@ -37,21 +39,31 @@ public partial class Weapon : Node2D _muzzle = GetNode("./Muzzle"); _cooldownTimer = GetNode("./ShootTimer"); - _gameManager = GetNode("/root/GameScene"); + _gameManager = this.GetGameManager(); + _inventoryManager = this.GetInventoryManager(); } public void Reload() { _cooldownTimer.Start(WeaponData.ReloadTime); - if (WeaponData.InfiniteAmmo) + if (WeaponData.InfiniteAmmo || string.IsNullOrWhiteSpace(WeaponData.AmmoKey)) { LoadedAmmo = WeaponData.BulletCapacity; } else { - // TODO: Calculate subtraction, etc - LoadedAmmo = WeaponData.BulletCapacity; + // if (_inventoryManager.GetItemCount(WeaponData.AmmoKey) <= 0) return; + var ammoToLoad = _inventoryManager.RemoveItem(WeaponData.AmmoKey, WeaponData.BulletCapacity); + + if (ammoToLoad > 0) + { + LoadedAmmo = ammoToLoad; + } + else + { + GD.Print("Out of ammo"); + } } }