Added Player Framework and some commands

This commit is contained in:
2026-04-06 13:47:06 +02:00
parent 22108bb58d
commit 2fca1281a7
19 changed files with 333 additions and 2 deletions
+29
View File
@@ -0,0 +1,29 @@
// Item.cs
namespace TextRPG.PlayerSystem
{
public class Item
{
public string Name { get; set; }
public string Description { get; set; }
public int Value { get; set; } // e.g., healing power, sell price
public string Type { get; set; } // "consumable", "weapon", "key", etc.
public Item(string name, string description, int value = 0, string type = "misc")
{
Name = name;
Description = description;
Value = value;
Type = type;
}
// Example: use effect (could be expanded with a delegate or virtual method)
public virtual void Use(Player player)
{
if (Type == "consumable")
{
player.Heal(Value);
// In a full implementation, you would also remove the item after use
}
}
}
}
+1
View File
@@ -0,0 +1 @@
uid://dvyff657dfv87
+121
View File
@@ -0,0 +1,121 @@
// Player.cs - Complete working version
using Godot;
using System;
using System.Collections.Generic;
namespace TextRPG.PlayerSystem
{
public partial class Player : Node
{
// ---------- Singleton Pattern ----------
private static Player _instance;
public static Player Instance => _instance;
// ---------- Signals ----------
[Signal]
public delegate void HealthChangedEventHandler(int currentHealth, int maxHealth);
[Signal]
public delegate void PlayerDiedEventHandler();
[Signal]
public delegate void InventoryChangedEventHandler();
// ---------- Attributes ----------
public int MaxHealth { get; set; } = 100;
private int _currentHealth;
public int CurrentHealth
{
get => _currentHealth;
set
{
int newHealth = Math.Clamp(value, 0, MaxHealth);
if (_currentHealth != newHealth)
{
_currentHealth = newHealth;
EmitSignal(SignalName.HealthChanged, _currentHealth, MaxHealth);
if (_currentHealth <= 0)
EmitSignal(SignalName.PlayerDied);
}
}
}
public int Strength { get; set; } = 10;
public int Dexterity { get; set; } = 10;
public int Intelligence { get; set; } = 10;
// ---------- Inventory ----------
public List<Item> Inventory { get; private set; } = new List<Item>();
// ---------- Lifecycle ----------
public override void _EnterTree()
{
if (_instance != null)
{
GD.Print("Duplicate Player instance detected. Removing.");
QueueFree();
}
else
{
_instance = this;
GD.Print("Player singleton initialized.");
}
}
public override void _Ready()
{
_currentHealth = MaxHealth;
}
// ---------- Public Methods ----------
public void TakeDamage(int amount)
{
CurrentHealth -= amount;
}
public void Heal(int amount)
{
CurrentHealth += amount;
}
public void AddItem(Item item)
{
Inventory.Add(item);
EmitSignal(SignalName.InventoryChanged);
}
public bool RemoveItem(Item item)
{
bool removed = Inventory.Remove(item);
if (removed)
EmitSignal(SignalName.InventoryChanged);
return removed;
}
public bool HasItem(string itemName)
{
return Inventory.Exists(i => i.Name.Equals(itemName, StringComparison.OrdinalIgnoreCase));
}
public Item GetItem(string itemName)
{
return Inventory.Find(i => i.Name.Equals(itemName, StringComparison.OrdinalIgnoreCase));
}
public string GetStatusString()
{
return $"Health: {CurrentHealth}/{MaxHealth}\n" +
$"Strength: {Strength} Dexterity: {Dexterity} Intelligence: {Intelligence}\n" +
$"Inventory: {Inventory.Count} items";
}
public string GetInventoryString()
{
if (Inventory.Count == 0)
return "Your inventory is empty.";
var lines = new List<string> { "=== Inventory ===" };
foreach (var item in Inventory)
lines.Add($"- {item.Name}: {item.Description}");
return string.Join("\n", lines);
}
}
}
+1
View File
@@ -0,0 +1 @@
uid://cx4q1u5whnn7m