Visualization for ammo

This commit is contained in:
Marco 2025-03-05 18:55:30 +01:00
commit 07ab64a0bf
12 changed files with 252 additions and 102 deletions

View file

@ -19,7 +19,7 @@ _name = "Intro"
[sub_resource type="Resource" id="Resource_cfhv5"]
script = ExtResource("1_ov731")
EggIndex = 0
StartingEquipment = Array[ExtResource("2_bkci5")]([])
StartingEquipment = []
[sub_resource type="Resource" id="Resource_0k62o"]
script = ExtResource("2_tnajf")
@ -31,7 +31,7 @@ StartData = SubResource("Resource_cfhv5")
[sub_resource type="Resource" id="Resource_tpb7s"]
script = ExtResource("1_ov731")
EggIndex = 0
StartingEquipment = Array[ExtResource("2_bkci5")]([])
StartingEquipment = []
[sub_resource type="Resource" id="Resource_edoov"]
script = ExtResource("2_tnajf")
@ -43,7 +43,7 @@ StartData = SubResource("Resource_tpb7s")
[sub_resource type="Resource" id="Resource_1sw5g"]
script = ExtResource("1_ov731")
EggIndex = 255
StartingEquipment = Array[ExtResource("2_bkci5")]([])
StartingEquipment = []
[sub_resource type="Resource" id="Resource_47bot"]
script = ExtResource("2_tnajf")
@ -73,7 +73,7 @@ _name = "Rebel Base"
[sub_resource type="Resource" id="Resource_maxpt"]
script = ExtResource("1_ov731")
EggIndex = 0
StartingEquipment = Array[ExtResource("2_bkci5")]([])
StartingEquipment = []
[sub_resource type="Resource" id="Resource_pein5"]
script = ExtResource("2_tnajf")
@ -128,7 +128,7 @@ metadata/_custom_type_script = "uid://dnthdjrx78u6m"
[sub_resource type="Resource" id="Resource_7sue8"]
script = ExtResource("1_ov731")
EggIndex = 255
StartingEquipment = Array[ExtResource("2_bkci5")]([])
StartingEquipment = []
metadata/_custom_type_script = "uid://mja0rk7n2kln"
[sub_resource type="Resource" id="Resource_ognca"]

View file

@ -1,7 +1,8 @@
[gd_scene load_steps=21 format=3 uid="uid://dkwi1hu1bixoe"]
[gd_scene load_steps=22 format=3 uid="uid://dkwi1hu1bixoe"]
[ext_resource type="Script" uid="uid://baf6pxbvhqmjk" 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="PackedScene" uid="uid://cd3ee1adstcrw" path="res://Scenes/HUD/HudItemContainer.tscn" id="2_mattb"]
[ext_resource type="LabelSettings" uid="uid://buk3e7bbwmnv1" path="res://Resources/Styles/Hud_Text_Style.tres" id="2_x7kl7"]
[ext_resource type="Texture2D" uid="uid://dde36pgb8fbwc" path="res://Sprites/selector_big.png" id="3_hee3i"]
[ext_resource type="PackedScene" uid="uid://bybd38ivpb2ly" path="res://Scenes/HUD/pause_menu.tscn" id="3_hxmxt"]
@ -68,6 +69,7 @@ anti_aliasing = false
[node name="HUD" type="CanvasLayer" node_paths=PackedStringArray("_healthLabel", "_healthBar", "_shieldLabel", "_shieldBar", "_itemsContainer", "_gameOverPanel", "PauseMenuContainer", "DebugMenuHolder")]
process_mode = 3
script = ExtResource("1_m0hb0")
WeaponContainerTemplate = ExtResource("2_mattb")
_healthLabel = NodePath("VBoxContainer/HealthBarContainer/HealthLabel")
_healthBar = NodePath("VBoxContainer/HealthBarContainer/HealthBar")
_shieldLabel = NodePath("VBoxContainer/ShieldBarContainer/ShieldLabel")
@ -195,6 +197,7 @@ mouse_filter = 2
metadata/_edit_group_ = true
[node name="ItemList" type="ItemList" parent="Control"]
visible = false
layout_mode = 1
anchors_preset = 15
anchor_right = 1.0

View file

@ -0,0 +1,43 @@
[gd_scene load_steps=5 format=3 uid="uid://cd3ee1adstcrw"]
[ext_resource type="Script" uid="uid://chqjrv7wqk6ej" path="res://Scripts/UI/WeaponAmmoCounter.cs" id="1_pwei3"]
[ext_resource type="Texture2D" uid="uid://duwiasewxvcb5" path="res://Sprites/Items/Icicle_Gun.png" id="2_pqw4t"]
[ext_resource type="LabelSettings" uid="uid://buk3e7bbwmnv1" path="res://Resources/Styles/Hud_Text_Style.tres" id="3_a6rek"]
[ext_resource type="Texture2D" uid="uid://c5g0qg6u2gm6f" path="res://Sprites/Items/Ammo1.png" id="4_kbanm"]
[node name="HudItemContainer" type="Control" node_paths=PackedStringArray("Icon", "AmmoIcon", "TotalAmmoLabel", "LoadedAmmoLabel")]
layout_mode = 3
anchors_preset = 15
anchor_right = 1.0
anchor_bottom = 1.0
grow_horizontal = 2
grow_vertical = 2
script = ExtResource("1_pwei3")
Icon = NodePath("HBoxContainer/WeaponIcon")
AmmoIcon = NodePath("HBoxContainer/Ammo Icon")
TotalAmmoLabel = NodePath("HBoxContainer/TotalAmmo")
LoadedAmmoLabel = NodePath("HBoxContainer/LoadedAmmo")
[node name="HBoxContainer" type="HBoxContainer" parent="."]
layout_mode = 1
offset_right = 16.0
offset_bottom = 16.0
theme_override_constants/separation = 0
[node name="WeaponIcon" type="TextureRect" parent="HBoxContainer"]
layout_mode = 2
texture = ExtResource("2_pqw4t")
[node name="LoadedAmmo" type="Label" parent="HBoxContainer"]
layout_mode = 2
text = "999"
label_settings = ExtResource("3_a6rek")
[node name="Ammo Icon" type="TextureRect" parent="HBoxContainer"]
layout_mode = 2
texture = ExtResource("4_kbanm")
[node name="TotalAmmo" type="Label" parent="HBoxContainer"]
layout_mode = 2
text = "999"
label_settings = ExtResource("3_a6rek")

View file

@ -1,8 +1,7 @@
[gd_scene load_steps=51 format=4 uid="uid://bpnahwxmjgb6t"]
[gd_scene load_steps=50 format=4 uid="uid://bpnahwxmjgb6t"]
[ext_resource type="Script" uid="uid://doxmbokehw8ci" path="res://Scripts/GameManager.cs" id="1_qxyfr"]
[ext_resource type="PackedScene" uid="uid://c4pr2707hbeph" path="res://Scenes/Actors/fsm_player.tscn" id="2_ajkcn"]
[ext_resource type="Script" uid="uid://epnwjptvks3t" path="res://Scripts/Resources/LootItem.cs" id="3_covcf"]
[ext_resource type="Script" uid="uid://mja0rk7n2kln" path="res://Scripts/Resources/MapStartDataResource.cs" id="4_g48r8"]
[ext_resource type="TileSet" uid="uid://6k28roiljylj" path="res://Tilesets/factory_tileset.tres" id="5_2m462"]
[ext_resource type="Script" uid="uid://krean0uywtms" path="res://Scripts/TilemapAvoidance.cs" id="6_bnfdx"]
@ -38,7 +37,7 @@
[sub_resource type="Resource" id="Resource_6wo78"]
script = ExtResource("4_g48r8")
EggIndex = 0
StartingEquipment = Array[ExtResource("3_covcf")]([])
StartingEquipment = []
[sub_resource type="Resource" id="Resource_hppa0"]
script = ExtResource("14_wc2f3")

View file

@ -215,7 +215,7 @@ SpawnMarkers = Dictionary[int, NodePath]({
2: NodePath("Factory Tilemaps/LevelProps/BossDebugTeleporterDestination"),
255: NodePath("Factory Tilemaps/Debug Room/DebugRoomStartPosition")
})
StartingEquipment = Array[ExtResource("6_8tdlb")]([ExtResource("4_swym2"), ExtResource("5_nqier")])
StartingEquipment = [ExtResource("4_swym2"), ExtResource("5_nqier")]
MapStartData = SubResource("Resource_6sau4")
metadata/_edit_lock_ = true
@ -971,7 +971,7 @@ position = Vector2(-2000, -736)
[node name="ControlPad8" parent="Factory Tilemaps/LevelProps" node_paths=PackedStringArray("Targets") instance=ExtResource("12_hfkf1")]
position = Vector2(-2027, -735)
Targets = [NodePath("../HorizontalForceField")]
Requirements = Array[ExtResource("6_8tdlb")]([ExtResource("84_ma1ta")])
Requirements = [ExtResource("84_ma1ta")]
[node name="Ammo6" parent="Factory Tilemaps/LevelProps" instance=ExtResource("34_17pjh")]
position = Vector2(-872, -220)
@ -998,6 +998,7 @@ position = Vector2(-1163, -812)
process_mode = 1
script = ExtResource("6_t8ide")
pixel_snap = false
enable_smoothing = false
[node name="CameraTarget" type="Node2D" parent="."]
position = Vector2(-808, -169)

View file

@ -81,8 +81,10 @@ public partial class GameManager : Node2D
if (_inventoryManager != null && _hud != null)
{
_inventoryManager.ItemAdded += (item, currentAmount) => _hud.AddInventoryItem(item, currentAmount);
_inventoryManager.ItemRemoved += (item, currentAmount) => _hud.RemoveInventoryItem(item, currentAmount);
//_inventoryManager.ItemAdded += (item, currentAmount) => _hud.AddInventoryItem(item, currentAmount);
//_inventoryManager.ItemRemoved += (item, currentAmount) => _hud.RemoveInventoryItem(item, currentAmount);
_inventoryManager.WeaponEquip += key => _hud.EquipWeapon(key);
}
PlayerRespawned += OnPlayerRespawned;

View file

@ -16,6 +16,9 @@ public partial class Hud : CanvasLayer
[Export]
public PackedScene SelectorScene { get; set; }
[Export]
public PackedScene WeaponContainerTemplate { get; private set; }
[Export]
private Node2D _selector;
@ -45,7 +48,7 @@ public partial class Hud : CanvasLayer
[Export]
public Control DebugMenuHolder { get; set; }
private Dictionary<string, HudItem> _items = new();
private Dictionary<string, WeaponAmmoCounter> _items = new();
private PauseMenu _pauseMenu;
@ -128,63 +131,49 @@ public partial class Hud : CanvasLayer
if (!_items.TryGetValue(item.ItemKey, out var item1))
{
var hbox = new HBoxContainer();
_itemsContainer.AddChild(hbox);
var instance = new TextureRect();
instance.Texture = item.InventorySprite;
instance.StretchMode = TextureRect.StretchModeEnum.Keep;
// change transform size?
var instance = WeaponContainerTemplate.Instantiate<WeaponAmmoCounter>();
//var instance = item.HudItemScene.Instantiate<TextureRect>();
//_in.CallDeferred("add_child", instance);
hbox.AddChild(instance);
var hudItem = new HudItem()
{
Item = item,
Container = hbox,
//Amount = currentAmount
};
_itemsContainer.CallDeferred("add_child", instance);
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();
// }
// var hbox = new HBoxContainer();
// _itemsContainer.AddChild(hbox);
//
// var instance = new TextureRect();
// instance.Texture = item.InventorySprite;
// instance.StretchMode = TextureRect.StretchModeEnum.Keep;
// hbox.AddChild(instance);
label.LabelSettings = _labelSettings;
hbox.AddChild(label);
hudItem.Label = label;
}
// var hudItem = new HudItem()
// {
// Item = item,
// Container = hbox,
// //Amount = currentAmount
// };
_items.Add(item.ItemKey, hudItem);
// if (item.UiType == UiItemType.IconText)
// {
// var label = new Label();
// label.Text = currentAmount.ToString();
//
//
// label.LabelSettings = _labelSettings;
// hbox.AddChild(label);
//
// hudItem.Label = label;
// }
_items.Add(item.ItemKey, instance);
instance.Init(item);
}
else
{
if (item.UiType == UiItemType.IconText && item1.Label != null)
{
item1.Label.Text = currentAmount.ToString();
}
// nothing
// if (item.UiType == UiItemType.IconText && item1.Label != null)
// {
// item1.Label.Text = currentAmount.ToString();
// }
}
// TextureRect texture = new TextureRect();
@ -244,38 +233,38 @@ public partial class Hud : CanvasLayer
}
}
public void RemoveInventoryItem(string itemKey, int currentAmount)
{
if (_items.TryGetValue(itemKey, out var itm))
{
if (currentAmount <= 0)
{
itm.Container.QueueFree();
_items.Remove(itemKey);
}
else
{
if (itm.Item.UiType == UiItemType.IconText)
{
itm.Label.Text = currentAmount.ToString();
}
}
}
else
{
GD.Print($"Tried to remove item {itemKey} but it was not found");
}
// var containerItem = _items.FirstOrDefault(x => x.Item == item);
// if (containerItem == null)
// {
// GD.Print($"Tried to remove item {item.ItemName} but it was not found");
// return;
// }
//
// containerItem.Container.QueueFree();
}
// public void RemoveInventoryItem(string itemKey, int currentAmount)
// {
// if (_items.TryGetValue(itemKey, out var itm))
// {
// if (currentAmount <= 0)
// {
// itm.Container.QueueFree();
// _items.Remove(itemKey);
// }
// else
// {
// if (itm.Item.UiType == UiItemType.IconText)
// {
// itm.Label.Text = currentAmount.ToString();
// }
// }
// }
// else
// {
// GD.Print($"Tried to remove item {itemKey} but it was not found");
// }
//
// // var containerItem = _items.FirstOrDefault(x => x.Item == item);
// // if (containerItem == null)
// // {
// // GD.Print($"Tried to remove item {item.ItemName} but it was not found");
// // return;
// // }
// //
// // containerItem.Container.QueueFree();
//
// }
private void SpawnDebugMenu()
{
@ -309,4 +298,29 @@ public partial class Hud : CanvasLayer
//public int Amount { get; set; }
}
public void EquipWeapon(string itemKey)
{
if (_items.TryGetValue(itemKey, out var localItem))
{
// It's already in the hud, abort
GD.Print($"Item with key {itemKey} is already in hud, aborting.");
return;
}
if (!InventoryManager.Instance.TryGetItem(itemKey, out var item))
{
GD.Print($"Item with key {itemKey} not found in inventory when equipping in hud.");
return;
}
// Clear all items
foreach (var hudItem in _items)
{
hudItem.Value.QueueFree();
}
_items.Clear();
AddInventoryItem(item.Item, item.Count);
}
}

View file

@ -25,6 +25,12 @@ public partial class InventoryManager : Node2D
[Signal]
public delegate void ItemUsedEventHandler(LootItem itemKey, int totalCount);
[Signal]
public delegate void TotalAmmoChangedEventHandler(StringName ammoKey, int count);
[Signal]
public delegate void LoadedAmmoChangedEventHandler(StringName weaponKey, int count);
public override void _Ready()
{
@ -36,6 +42,11 @@ public partial class InventoryManager : Node2D
{
}
public bool TryGetItem(string key, out ItemContainer item)
{
return _itemsDict.TryGetValue(key, out item);
}
public bool HasItems(List<string> itemKeys)
{
return itemKeys.Aggregate(false, (current, item) => current || GetItemCount(item) > 0);
@ -59,8 +70,13 @@ public partial class InventoryManager : Node2D
_itemsDict.Remove(itemKey);
}
EmitSignal(nameof(ItemRemoved), itemKey, itm.Count);
EmitSignal(SignalName.ItemRemoved, itemKey, itm.Count);
if (itm.Item.Item is ItemTypes.Ammo)
{
EmitSignal(SignalName.TotalAmmoChanged, itemKey, itm.Count);
}
return removed;
}
@ -78,7 +94,7 @@ public partial class InventoryManager : Node2D
});
GD.Print($"Added new ({item.ItemKey}) {item.Item} x{item.Amount}");
EmitSignal(nameof(ItemAdded), item, item.Amount);
EmitSignal(SignalName.ItemAdded, item, item.Amount);
}
else
{
@ -97,7 +113,7 @@ public partial class InventoryManager : Node2D
return false;
}
}
EmitSignal(nameof(ItemAdded), item, itm.Count);
EmitSignal(SignalName.ItemAdded, item, itm.Count);
}
return true;
@ -141,6 +157,11 @@ public partial class InventoryManager : Node2D
return true;
}
public void NotifyLoadedAmmoChange(string weaponDataItemKey, int loadedAmmo)
{
EmitSignal(SignalName.LoadedAmmoChanged, weaponDataItemKey, loadedAmmo);
}
}
public class ItemContainer

View file

@ -0,0 +1,51 @@
using Cirno.Scripts.Resources;
using Godot;
namespace Cirno.Scripts.UI;
public partial class WeaponAmmoCounter : Control
{
public LootItem Item { get; private set; }
[Export]
public TextureRect Icon { get; private set; }
[Export]
public TextureRect AmmoIcon { get; private set; }
[Export]
public Label TotalAmmoLabel { get; private set; }
[Export]
public Label LoadedAmmoLabel { get; private set; }
public void Init(LootItem item)
{
Item = item;
InventoryManager.Instance.LoadedAmmoChanged += (weaponKey, count) =>
{
if (weaponKey != Item.WeaponData.ItemKey) return;
LoadedAmmoLabel.Text = count.ToString();
};
if (string.IsNullOrWhiteSpace(item.WeaponData.AmmoKey))
{
TotalAmmoLabel.Hide();
AmmoIcon.Hide();
return;
}
UpdateCounter();
// Register this only if there's ammo
InventoryManager.Instance.TotalAmmoChanged += (ammoKey, count) =>
{
if (ammoKey != Item.WeaponData.AmmoKey) return;
TotalAmmoLabel.Text = count.ToString();
};
}
private void UpdateCounter()
{
TotalAmmoLabel.Text = InventoryManager.Instance.GetItemCount(Item.WeaponData.AmmoKey).ToString();
}
}

View file

@ -0,0 +1 @@
uid://chqjrv7wqk6ej

View file

@ -19,7 +19,16 @@ public partial class Weapon : Node2D
public int Ammo { get; set; } = 0;
public int LoadedAmmo { get; private set; }
private int _loadedAmmo;
public int LoadedAmmo
{
get => _loadedAmmo;
private set
{
_loadedAmmo = value;
_inventoryManager?.NotifyLoadedAmmoChange(WeaponData?.ItemKey, _loadedAmmo);
}
}
public Vector2 ShootDirection { get; set; } = Vector2.Zero;
@ -61,7 +70,7 @@ public partial class Weapon : Node2D
{
// if (_inventoryManager.GetItemCount(WeaponData.AmmoKey) <= 0) return;
var ammoToLoad = _inventoryManager.RemoveItem(WeaponData.AmmoKey, WeaponData.BulletCapacity);
if (ammoToLoad > 0)
{
LoadedAmmo = ammoToLoad;
@ -127,7 +136,13 @@ public partial class Weapon : Node2D
}
LoadedAmmo -= 1;
//_inventoryManager.NotifyLoadedAmmoChange(WeaponData.ItemKey, LoadedAmmo);
// if (!string.IsNullOrWhiteSpace(WeaponData?.AmmoKey))
// {
// // Notify hud to decrease weapon
//
// }
_cooldownTimer.Start(WeaponData.RateOfFire);
_cooldownTimer.Start(WeaponData?.RateOfFire ?? 0);
}
}