Added Player Framework and some commands
This commit is contained in:
parent
22108bb58d
commit
2fca1281a7
29
Scripts/PlayerSystem/Item.cs
Normal file
29
Scripts/PlayerSystem/Item.cs
Normal 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
Scripts/PlayerSystem/Item.cs.uid
Normal file
1
Scripts/PlayerSystem/Item.cs.uid
Normal file
@ -0,0 +1 @@
|
||||
uid://dvyff657dfv87
|
||||
121
Scripts/PlayerSystem/Player.cs
Normal file
121
Scripts/PlayerSystem/Player.cs
Normal 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
Scripts/PlayerSystem/Player.cs.uid
Normal file
1
Scripts/PlayerSystem/Player.cs.uid
Normal file
@ -0,0 +1 @@
|
||||
uid://cx4q1u5whnn7m
|
||||
@ -1,4 +1,4 @@
|
||||
<Project Sdk="Godot.NET.Sdk/4.6.1">
|
||||
<Project Sdk="Godot.NET.Sdk/4.6.2">
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net8.0</TargetFramework>
|
||||
<TargetFramework Condition=" '$(GodotTargetPlatform)' == 'android' ">net9.0</TargetFramework>
|
||||
|
||||
7
TextRPG.csproj.old
Normal file
7
TextRPG.csproj.old
Normal file
@ -0,0 +1,7 @@
|
||||
<Project Sdk="Godot.NET.Sdk/4.6.1">
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net8.0</TargetFramework>
|
||||
<TargetFramework Condition=" '$(GodotTargetPlatform)' == 'android' ">net9.0</TargetFramework>
|
||||
<EnableDynamicLoading>true</EnableDynamicLoading>
|
||||
</PropertyGroup>
|
||||
</Project>
|
||||
@ -15,6 +15,11 @@ namespace TextRPG.Commands
|
||||
RegisterCommand(new HelpCommand());
|
||||
RegisterCommand(new ClearCommand());
|
||||
RegisterCommand(new TimeCommand());
|
||||
RegisterCommand(new StatusCommand());
|
||||
RegisterCommand(new InventoryCommand());
|
||||
RegisterCommand(new TakeCommand());
|
||||
RegisterCommand(new DropCommand());
|
||||
RegisterCommand(new HealCommand());
|
||||
|
||||
}
|
||||
|
||||
|
||||
33
commands/DropCommand.cs
Normal file
33
commands/DropCommand.cs
Normal file
@ -0,0 +1,33 @@
|
||||
// DropCommand.cs
|
||||
using System;
|
||||
using TextRPG.PlayerSystem;
|
||||
|
||||
namespace TextRPG.Commands
|
||||
{
|
||||
public class DropCommand : Command
|
||||
{
|
||||
public override string Name => "drop";
|
||||
public override string Description => "Removes an item from your inventory.";
|
||||
public override string Usage => "drop <item_name>";
|
||||
|
||||
public override void Execute(string[] args, Action<string> outputCallback)
|
||||
{
|
||||
if (args.Length == 0)
|
||||
{
|
||||
outputCallback($"Usage: {Usage}");
|
||||
return;
|
||||
}
|
||||
|
||||
string itemName = string.Join(" ", args);
|
||||
var item = Player.Instance.GetItem(itemName);
|
||||
if (item == null)
|
||||
{
|
||||
outputCallback($"You don't have '{itemName}'.");
|
||||
return;
|
||||
}
|
||||
|
||||
Player.Instance.RemoveItem(item);
|
||||
outputCallback($"You dropped: {itemName}.");
|
||||
}
|
||||
}
|
||||
}
|
||||
1
commands/DropCommand.cs.uid
Normal file
1
commands/DropCommand.cs.uid
Normal file
@ -0,0 +1 @@
|
||||
uid://bfa36j6ca7bj6
|
||||
26
commands/HealCommand.cs
Normal file
26
commands/HealCommand.cs
Normal file
@ -0,0 +1,26 @@
|
||||
// HealCommand.cs
|
||||
using System;
|
||||
using TextRPG.PlayerSystem;
|
||||
|
||||
namespace TextRPG.Commands
|
||||
{
|
||||
public class HealCommand : Command
|
||||
{
|
||||
public override string Name => "heal";
|
||||
public override string Description => "Heals the player by a given amount (max 50).";
|
||||
public override string Usage => "heal <amount>";
|
||||
|
||||
public override void Execute(string[] args, Action<string> outputCallback)
|
||||
{
|
||||
if (args.Length == 0 || !int.TryParse(args[0], out int amount))
|
||||
{
|
||||
outputCallback($"Usage: {Usage}");
|
||||
return;
|
||||
}
|
||||
|
||||
amount = Math.Min(amount, 50);
|
||||
Player.Instance.Heal(amount);
|
||||
outputCallback($"You healed {amount} HP. Current health: {Player.Instance.CurrentHealth}/{Player.Instance.MaxHealth}");
|
||||
}
|
||||
}
|
||||
}
|
||||
1
commands/HealCommand.cs.uid
Normal file
1
commands/HealCommand.cs.uid
Normal file
@ -0,0 +1 @@
|
||||
uid://crubvxtr4u1ya
|
||||
18
commands/InventoryCommand.cs
Normal file
18
commands/InventoryCommand.cs
Normal file
@ -0,0 +1,18 @@
|
||||
// InventoryCommand.cs
|
||||
using System;
|
||||
using TextRPG.PlayerSystem;
|
||||
|
||||
namespace TextRPG.Commands
|
||||
{
|
||||
public class InventoryCommand : Command
|
||||
{
|
||||
public override string Name => "inventory";
|
||||
public override string Description => "Lists all items in your inventory.";
|
||||
public override string Usage => "inventory";
|
||||
|
||||
public override void Execute(string[] args, Action<string> outputCallback)
|
||||
{
|
||||
outputCallback(Player.Instance.GetInventoryString());
|
||||
}
|
||||
}
|
||||
}
|
||||
1
commands/InventoryCommand.cs.uid
Normal file
1
commands/InventoryCommand.cs.uid
Normal file
@ -0,0 +1 @@
|
||||
uid://c2m6vnugsmxgg
|
||||
24
commands/StatusCommand.cs
Normal file
24
commands/StatusCommand.cs
Normal file
@ -0,0 +1,24 @@
|
||||
using System;
|
||||
using TextRPG.PlayerSystem;
|
||||
|
||||
namespace TextRPG.Commands
|
||||
{
|
||||
public class StatusCommand : Command
|
||||
{
|
||||
public override string Name => "status";
|
||||
public override string Description => "Shows player status (health, attributes, inventory count).";
|
||||
public override string Usage => "status";
|
||||
|
||||
public override void Execute(string[] args, Action<string> outputCallback)
|
||||
{
|
||||
// Use the singleton instance directly
|
||||
var player = Player.Instance;
|
||||
if (player == null)
|
||||
{
|
||||
outputCallback("Player system not initialized.");
|
||||
return;
|
||||
}
|
||||
outputCallback(player.GetStatusString());
|
||||
}
|
||||
}
|
||||
}
|
||||
1
commands/StatusCommand.cs.uid
Normal file
1
commands/StatusCommand.cs.uid
Normal file
@ -0,0 +1 @@
|
||||
uid://c2ig5hu00nyql
|
||||
28
commands/TakeCommand.cs
Normal file
28
commands/TakeCommand.cs
Normal file
@ -0,0 +1,28 @@
|
||||
// TakeCommand.cs
|
||||
using System;
|
||||
using TextRPG.PlayerSystem;
|
||||
|
||||
namespace TextRPG.Commands
|
||||
{
|
||||
public class TakeCommand : Command
|
||||
{
|
||||
public override string Name => "take";
|
||||
public override string Description => "Picks up an item from the current location.";
|
||||
public override string Usage => "take <item_name>";
|
||||
|
||||
public override void Execute(string[] args, Action<string> outputCallback)
|
||||
{
|
||||
if (args.Length == 0)
|
||||
{
|
||||
outputCallback($"Usage: {Usage}");
|
||||
return;
|
||||
}
|
||||
|
||||
string itemName = string.Join(" ", args);
|
||||
// For demo, create a predefined item. In a real game, you'd fetch from the current room/zone.
|
||||
var item = new Item(itemName, "A mysterious object.", 10, "misc");
|
||||
Player.Instance.AddItem(item);
|
||||
outputCallback($"You picked up: {itemName}.");
|
||||
}
|
||||
}
|
||||
}
|
||||
1
commands/TakeCommand.cs.uid
Normal file
1
commands/TakeCommand.cs.uid
Normal file
@ -0,0 +1 @@
|
||||
uid://b6trmclox3caa
|
||||
@ -15,6 +15,10 @@ run/main_scene="uid://cpbeaf3bpe42v"
|
||||
config/features=PackedStringArray("4.6", "C#", "Forward Plus")
|
||||
config/icon="res://icon.svg"
|
||||
|
||||
[autoload]
|
||||
|
||||
Player="*uid://cx4q1u5whnn7m"
|
||||
|
||||
[dotnet]
|
||||
|
||||
project/assembly_name="TextRPG"
|
||||
|
||||
@ -1,10 +1,14 @@
|
||||
using Godot;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using TextRPG.Commands; // Add this to access ConsoleManager
|
||||
using TextRPG.Commands;
|
||||
using TextRPG.PlayerSystem;
|
||||
|
||||
|
||||
|
||||
public partial class ConsoleUI : CanvasLayer
|
||||
{
|
||||
private Player player;
|
||||
// UI Elements
|
||||
private RichTextLabel consoleOutput;
|
||||
private LineEdit consoleInput;
|
||||
@ -58,6 +62,31 @@ public partial class ConsoleUI : CanvasLayer
|
||||
|
||||
// Display welcome message
|
||||
DisplayWelcomeMessage();
|
||||
player = GetNode<Player>("/root/Player");
|
||||
if (player != null)
|
||||
{
|
||||
player.HealthChanged += OnPlayerHealthChanged;
|
||||
player.PlayerDied += OnPlayerDied;
|
||||
player.InventoryChanged += OnInventoryChanged;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private void OnPlayerHealthChanged(int current, int max)
|
||||
{
|
||||
AddToConsole($"[color=orange]Health changed: {current}/{max}[/color]");
|
||||
}
|
||||
|
||||
private void OnPlayerDied()
|
||||
{
|
||||
AddToConsole("[color=red]You have died... Game Over.[/color]");
|
||||
// Optionally disable input or reload
|
||||
}
|
||||
|
||||
private void OnInventoryChanged()
|
||||
{
|
||||
// Optional: show a subtle message or nothing – you decide
|
||||
// AddToConsole("[dim]Inventory updated.[/dim]");
|
||||
}
|
||||
|
||||
private void SetupConsole()
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user