✨ Item pickup
This commit is contained in:
BIN
Assets/Items/OreIronItem.png
Normal file
BIN
Assets/Items/OreIronItem.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 887 KiB |
34
Assets/Items/OreIronItem.png.import
Normal file
34
Assets/Items/OreIronItem.png.import
Normal file
@@ -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
|
BIN
Assets/Items/StoneItem.png
Normal file
BIN
Assets/Items/StoneItem.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 699 KiB |
34
Assets/Items/StoneItem.png.import
Normal file
34
Assets/Items/StoneItem.png.import
Normal file
@@ -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
|
@@ -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,
|
||||
|
4
Data/ItemTextures.json
Normal file
4
Data/ItemTextures.json
Normal file
@@ -0,0 +1,4 @@
|
||||
{
|
||||
"stone": "res://Assets/Items/StoneItem.png",
|
||||
"ore_iron": "res://Assets/Items/OreIronItem.png"
|
||||
}
|
@@ -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
|
||||
|
19
Scenes/System/ItemPickup.tscn
Normal file
19
Scenes/System/ItemPickup.tscn
Normal file
@@ -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")
|
@@ -1,4 +1,5 @@
|
||||
using System;
|
||||
using AceFieldNewHorizon.Scripts.System;
|
||||
using Godot;
|
||||
|
||||
namespace AceFieldNewHorizon.Scripts.Entities;
|
||||
@@ -19,6 +20,8 @@ public partial class Player : CharacterBody2D
|
||||
[Export] public float ZoomDecay = 0.9f;
|
||||
[Export] public float ZoomSmoothing = 10.0f;
|
||||
|
||||
[Export] public ResourceManager Inventory;
|
||||
|
||||
private Camera2D _camera;
|
||||
private Vector2 _cameraTargetZoom = Vector2.One;
|
||||
private float _currentZoomSpeed;
|
||||
@@ -29,6 +32,8 @@ public partial class Player : CharacterBody2D
|
||||
{
|
||||
_camera = GetNode<Camera2D>("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)}");
|
||||
}
|
||||
}
|
@@ -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)
|
||||
{
|
||||
|
71
Scripts/System/ItemPickup.cs
Normal file
71
Scripts/System/ItemPickup.cs
Normal file
@@ -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>("Area2D");
|
||||
area.BodyEntered += OnBodyEntered;
|
||||
|
||||
_sprite = GetNode<Sprite2D>("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<Texture2D>(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
|
||||
}
|
||||
}
|
||||
}
|
1
Scripts/System/ItemPickup.cs.uid
Normal file
1
Scripts/System/ItemPickup.cs.uid
Normal file
@@ -0,0 +1 @@
|
||||
uid://qgcue2doj2lf
|
Reference in New Issue
Block a user