✨ Item pickup
This commit is contained in:
@@ -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>("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