diff --git a/Assets/Items/OreIronItem.png b/Assets/Items/OreIronItem.png new file mode 100644 index 0000000..d210600 Binary files /dev/null and b/Assets/Items/OreIronItem.png differ diff --git a/Assets/Items/OreIronItem.png.import b/Assets/Items/OreIronItem.png.import new file mode 100644 index 0000000..0bd09eb --- /dev/null +++ b/Assets/Items/OreIronItem.png.import @@ -0,0 +1,34 @@ +[remap] + +importer="texture" +type="CompressedTexture2D" +uid="uid://caoyy241xsoer" +path="res://.godot/imported/OreIronItem.png-9b5b0a6a8d0e3616bac40fde1dc14f49.ctex" +metadata={ +"vram_texture": false +} + +[deps] + +source_file="res://Assets/Items/OreIronItem.png" +dest_files=["res://.godot/imported/OreIronItem.png-9b5b0a6a8d0e3616bac40fde1dc14f49.ctex"] + +[params] + +compress/mode=0 +compress/high_quality=false +compress/lossy_quality=0.7 +compress/hdr_compression=1 +compress/normal_map=0 +compress/channel_pack=0 +mipmaps/generate=false +mipmaps/limit=-1 +roughness/mode=0 +roughness/src_normal="" +process/fix_alpha_border=true +process/premult_alpha=false +process/normal_map_invert_y=false +process/hdr_as_srgb=false +process/hdr_clamp_exposure=false +process/size_limit=0 +detect_3d/compress_to=1 diff --git a/Assets/Items/StoneItem.png b/Assets/Items/StoneItem.png new file mode 100644 index 0000000..bf16446 Binary files /dev/null and b/Assets/Items/StoneItem.png differ diff --git a/Assets/Items/StoneItem.png.import b/Assets/Items/StoneItem.png.import new file mode 100644 index 0000000..c407218 --- /dev/null +++ b/Assets/Items/StoneItem.png.import @@ -0,0 +1,34 @@ +[remap] + +importer="texture" +type="CompressedTexture2D" +uid="uid://d3mn15csjooch" +path="res://.godot/imported/StoneItem.png-cc89310dd113aeb437107818aa1ce10c.ctex" +metadata={ +"vram_texture": false +} + +[deps] + +source_file="res://Assets/Items/StoneItem.png" +dest_files=["res://.godot/imported/StoneItem.png-cc89310dd113aeb437107818aa1ce10c.ctex"] + +[params] + +compress/mode=0 +compress/high_quality=false +compress/lossy_quality=0.7 +compress/hdr_compression=1 +compress/normal_map=0 +compress/channel_pack=0 +mipmaps/generate=false +mipmaps/limit=-1 +roughness/mode=0 +roughness/src_normal="" +process/fix_alpha_border=true +process/premult_alpha=false +process/normal_map_invert_y=false +process/hdr_as_srgb=false +process/hdr_clamp_exposure=false +process/size_limit=0 +detect_3d/compress_to=1 diff --git a/Data/Buildings.json b/Data/Buildings.json index 2bd562e..316b157 100644 --- a/Data/Buildings.json +++ b/Data/Buildings.json @@ -12,8 +12,8 @@ "miner": { "scene": "res://Scenes/Tiles/MinerTile.tscn", "cost": { - "wood": 10, - "stone": 3 + "stone": 10, + "ore_iron": 3 }, "durability": 200, "buildTime": 3.0, diff --git a/Data/ItemTextures.json b/Data/ItemTextures.json new file mode 100644 index 0000000..23d9edf --- /dev/null +++ b/Data/ItemTextures.json @@ -0,0 +1,4 @@ +{ + "stone": "res://Assets/Items/StoneItem.png", + "ore_iron": "res://Assets/Items/OreIronItem.png" +} diff --git a/Scenes/Root.tscn b/Scenes/Root.tscn index 0d73d55..2fd5ae9 100644 --- a/Scenes/Root.tscn +++ b/Scenes/Root.tscn @@ -1,4 +1,4 @@ -[gd_scene load_steps=7 format=3 uid="uid://c22aprj452aha"] +[gd_scene load_steps=8 format=3 uid="uid://c22aprj452aha"] [ext_resource type="Script" uid="uid://cudpc3w17mbsw" path="res://Scripts/System/GridManager.cs" id="1_knkkn"] [ext_resource type="Script" uid="uid://dfi2snip78eq6" path="res://Scripts/System/ResourceManager.cs" id="1_pl8e4"] @@ -6,6 +6,7 @@ [ext_resource type="Script" uid="uid://cugfbvw70clgd" path="res://Scripts/System/NaturalResourceGenerator.cs" id="2_oss8w"] [ext_resource type="Script" uid="uid://bx1wj7gn6vrqe" path="res://Scripts/System/PlacementManager.cs" id="2_sxhdm"] [ext_resource type="PackedScene" uid="uid://doxy60afddg1m" path="res://Scenes/Entities/Player.tscn" id="3_oss8w"] +[ext_resource type="PackedScene" uid="uid://xwkplaxmye3v" path="res://Scenes/System/ItemPickup.tscn" id="7_is6ib"] [node name="Root" type="Node2D"] @@ -29,5 +30,16 @@ Grid = NodePath("../GridSystem") Inventory = NodePath("../ResourceSystem") Registry = NodePath("../BuildingRegistry") -[node name="Player" parent="." instance=ExtResource("3_oss8w")] +[node name="Player" parent="." node_paths=PackedStringArray("Inventory") instance=ExtResource("3_oss8w")] scale = Vector2(0.35, 0.35) +Inventory = NodePath("../ResourceSystem") + +[node name="ItemPickup" parent="." instance=ExtResource("7_is6ib")] +position = Vector2(-496, -245) +ItemId = "stone" +Infinite = true + +[node name="ItemPickup2" parent="." instance=ExtResource("7_is6ib")] +position = Vector2(-495, 5) +ItemId = "ore_iron" +Infinite = true diff --git a/Scenes/System/ItemPickup.tscn b/Scenes/System/ItemPickup.tscn new file mode 100644 index 0000000..49762be --- /dev/null +++ b/Scenes/System/ItemPickup.tscn @@ -0,0 +1,19 @@ +[gd_scene load_steps=4 format=3 uid="uid://xwkplaxmye3v"] + +[ext_resource type="Texture2D" uid="uid://d3mn15csjooch" path="res://Assets/Items/StoneItem.png" id="1_4weev"] +[ext_resource type="Script" uid="uid://qgcue2doj2lf" path="res://Scripts/System/ItemPickup.cs" id="1_ps3kh"] + +[sub_resource type="RectangleShape2D" id="RectangleShape2D_4weev"] +size = Vector2(160, 160) + +[node name="ItemPickup" type="Node2D"] +script = ExtResource("1_ps3kh") + +[node name="Sprite2D" type="Sprite2D" parent="."] +scale = Vector2(0.05, 0.05) +texture = ExtResource("1_4weev") + +[node name="Area2D" type="Area2D" parent="."] + +[node name="CollisionShape2D" type="CollisionShape2D" parent="Area2D"] +shape = SubResource("RectangleShape2D_4weev") diff --git a/Scripts/Entities/Player.cs b/Scripts/Entities/Player.cs index fc57953..4eb532e 100644 --- a/Scripts/Entities/Player.cs +++ b/Scripts/Entities/Player.cs @@ -1,4 +1,5 @@ using System; +using AceFieldNewHorizon.Scripts.System; using Godot; namespace AceFieldNewHorizon.Scripts.Entities; @@ -18,6 +19,8 @@ public partial class Player : CharacterBody2D [Export] public float ZoomAcceleration = 0.05f; [Export] public float ZoomDecay = 0.9f; [Export] public float ZoomSmoothing = 10.0f; + + [Export] public ResourceManager Inventory; private Camera2D _camera; private Vector2 _cameraTargetZoom = Vector2.One; @@ -29,6 +32,8 @@ public partial class Player : CharacterBody2D { _camera = GetNode("Camera2D"); _cameraTargetZoom = _camera.Zoom; + + AddToGroup(ItemPickup.PickupGroupName); } public override void _Input(InputEvent @event) @@ -135,4 +140,10 @@ public partial class Player : CharacterBody2D // Apply the movement MoveAndSlide(); } + + public void AddItem(string itemId, int quantity) + { + Inventory.AddResource(itemId, quantity); + GD.Print($"[Player] Picked up {quantity} x {itemId}. Total: {Inventory.GetResourceAmount(itemId)}"); + } } \ No newline at end of file diff --git a/Scripts/System/BuildingRegistry.cs b/Scripts/System/BuildingRegistry.cs index 64b800a..3e0f05a 100644 --- a/Scripts/System/BuildingRegistry.cs +++ b/Scripts/System/BuildingRegistry.cs @@ -71,7 +71,7 @@ public partial class BuildingRegistry : Node return; } - var dict = (Godot.Collections.Dictionary)json.Data; + var dict = json.Data.AsGodotDictionary(); foreach (string key in dict.Keys) { diff --git a/Scripts/System/ItemPickup.cs b/Scripts/System/ItemPickup.cs new file mode 100644 index 0000000..990f694 --- /dev/null +++ b/Scripts/System/ItemPickup.cs @@ -0,0 +1,71 @@ +using Godot; + +namespace AceFieldNewHorizon.Scripts.System; + +public partial class ItemPickup : Node2D +{ + public const string PickupGroupName = "ItemPickupTarget"; + + [Export] public string ItemId { get; set; } = ""; + [Export] public int Quantity { get; set; } = 1; + [Export] public bool Infinite { get; set; } = false; + + private Sprite2D _sprite; + + // Called when the node enters the scene tree + public override void _Ready() + { + var area = GetNode("Area2D"); + area.BodyEntered += OnBodyEntered; + + _sprite = GetNode("Sprite2D"); + UpdateTexture(); + } + + private void UpdateTexture() + { + var file = FileAccess.Open("res://Data/ItemTextures.json", FileAccess.ModeFlags.Read); + if (file == null) + { + GD.PrintErr("Failed to open ItemTextures.json"); + return; + } + + var text = file.GetAsText(); + file.Close(); + + var json = new Json(); + var err = json.Parse(text); + if (err != Error.Ok) + { + GD.PrintErr(json.GetErrorMessage()); + return; + } + + var dict = json.Data.AsGodotDictionary(); + if (!dict.TryGetValue(ItemId, out var value)) + return; + + var texturePath = value.AsString(); + if (string.IsNullOrEmpty(texturePath)) + return; + + var texture = GD.Load(texturePath); + if (texture == null) + return; + + _sprite.Texture = texture; + } + + private void OnBodyEntered(Node body) + { + if (body.IsInGroup(PickupGroupName)) + { + if (body.HasMethod("AddItem")) + body.Call("AddItem", ItemId, Quantity); + + if (!Infinite) + QueueFree(); // remove the pickup from the world + } + } +} \ No newline at end of file diff --git a/Scripts/System/ItemPickup.cs.uid b/Scripts/System/ItemPickup.cs.uid new file mode 100644 index 0000000..2e620f9 --- /dev/null +++ b/Scripts/System/ItemPickup.cs.uid @@ -0,0 +1 @@ +uid://qgcue2doj2lf