Added in a MoveConverter for loading multiple different kinds of moves into a generic Move List.
This commit is contained in:
parent
53b53fc57d
commit
8a1aaf4bb7
9 changed files with 85 additions and 29 deletions
15
Main.cs
15
Main.cs
|
|
@ -2,14 +2,16 @@
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using Microsoft.VisualBasic;
|
using Microsoft.VisualBasic;
|
||||||
|
using System.IO.MemoryMappedFiles;
|
||||||
|
|
||||||
class Program
|
class Program
|
||||||
{
|
{
|
||||||
static void Main()
|
static void Main()
|
||||||
{
|
{
|
||||||
List<Item> allItems = ItemLoader.LoadAllItems("data/items.json");
|
List<Item> allItems = ItemLoader.LoadAllItems("data/items.json");
|
||||||
|
List<Move> allMoves = MoveLoader.LoadAllMoves("data/moves.json");
|
||||||
|
|
||||||
Player user = new("Tanner", [], 100, 90, 20, 20);
|
Player user = new("Tanner", 100, 90, 20, 20, [], []);
|
||||||
user.DisplayStats();
|
user.DisplayStats();
|
||||||
|
|
||||||
user.Inventory.Add(allItems.FirstOrDefault(m => m.Name == "Super Potion"));
|
user.Inventory.Add(allItems.FirstOrDefault(m => m.Name == "Super Potion"));
|
||||||
|
|
@ -17,15 +19,6 @@ class Program
|
||||||
user.Inventory.Add(allItems.FirstOrDefault(m => m.Name == "Elixir"));
|
user.Inventory.Add(allItems.FirstOrDefault(m => m.Name == "Elixir"));
|
||||||
user.Inventory.Add(allItems.FirstOrDefault(m => m.Name == "Dagger"));
|
user.Inventory.Add(allItems.FirstOrDefault(m => m.Name == "Dagger"));
|
||||||
|
|
||||||
Console.WriteLine("Do you want to use your Potion? (Y/N)");
|
user.Moves.Add(allMoves.FirstOrDefault(m => m.Name == "Stab"));
|
||||||
string? input = Console.ReadLine().ToUpper();
|
|
||||||
Console.WriteLine("\n");
|
|
||||||
|
|
||||||
if (input == "Y")
|
|
||||||
{
|
|
||||||
user.UseHealingItem((HealingItem)user.Inventory.FirstOrDefault(m => m.Name == "Potion"));
|
|
||||||
}
|
|
||||||
|
|
||||||
user.DisplayStats();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -4,21 +4,31 @@
|
||||||
"Description": "A burst of fire that burns the enemy.",
|
"Description": "A burst of fire that burns the enemy.",
|
||||||
"Power": 40,
|
"Power": 40,
|
||||||
"ManaCost": 10,
|
"ManaCost": 10,
|
||||||
"Type": "Attack"
|
"Type": "MagicMove",
|
||||||
|
"Action": "Attack"
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"Name": "Heal",
|
"Name": "Heal",
|
||||||
"Description": "Restores a small amount of HP.",
|
"Description": "Restores a small amount of HP.",
|
||||||
"Power": 25,
|
"Power": 25,
|
||||||
"ManaCost": 5,
|
"ManaCost": 5,
|
||||||
"Type": "Heal"
|
"Type": "MagicMove",
|
||||||
|
"Action": "Healing"
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"Name": "Lightning Bolt",
|
"Name": "Lightning Bolt",
|
||||||
"Description": "A shocking magical strike.",
|
"Description": "A shocking magical strike.",
|
||||||
"Power": 50,
|
"Power": 50,
|
||||||
"ManaCost": 15,
|
"ManaCost": 15,
|
||||||
"Type": "Attack"
|
"Type": "MagicMove",
|
||||||
|
"Action": "Attack"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"Name": "Stab",
|
||||||
|
"Description": "A stab with a sharp object.",
|
||||||
|
"Power": 20,
|
||||||
|
"Type": "PhysicalMove",
|
||||||
|
"Action": "Attack"
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|
@ -13,7 +13,7 @@ using System.Reflection;
|
||||||
[assembly: System.Reflection.AssemblyCompanyAttribute("MiniRPG")]
|
[assembly: System.Reflection.AssemblyCompanyAttribute("MiniRPG")]
|
||||||
[assembly: System.Reflection.AssemblyConfigurationAttribute("Debug")]
|
[assembly: System.Reflection.AssemblyConfigurationAttribute("Debug")]
|
||||||
[assembly: System.Reflection.AssemblyFileVersionAttribute("1.0.0.0")]
|
[assembly: System.Reflection.AssemblyFileVersionAttribute("1.0.0.0")]
|
||||||
[assembly: System.Reflection.AssemblyInformationalVersionAttribute("1.0.0+4b4b59b9a31bfc76fb1db2b6e61846e0e23362d0")]
|
[assembly: System.Reflection.AssemblyInformationalVersionAttribute("1.0.0+53b53fc57d953f8a3b8a97ed56e18ac682c4379a")]
|
||||||
[assembly: System.Reflection.AssemblyProductAttribute("MiniRPG")]
|
[assembly: System.Reflection.AssemblyProductAttribute("MiniRPG")]
|
||||||
[assembly: System.Reflection.AssemblyTitleAttribute("MiniRPG")]
|
[assembly: System.Reflection.AssemblyTitleAttribute("MiniRPG")]
|
||||||
[assembly: System.Reflection.AssemblyVersionAttribute("1.0.0.0")]
|
[assembly: System.Reflection.AssemblyVersionAttribute("1.0.0.0")]
|
||||||
|
|
|
||||||
|
|
@ -1 +1 @@
|
||||||
2f9f95c4d52ac0c3165163d219ccd0cd9cb08d53b12c9beccc5292d27ab9163e
|
d3a1cc43ff9983679cb5442bcc4c2923f62c186209b30538fb7445848d6c5fa5
|
||||||
|
|
|
||||||
40
scripts/loaders/MoveConverter.cs
Normal file
40
scripts/loaders/MoveConverter.cs
Normal file
|
|
@ -0,0 +1,40 @@
|
||||||
|
using Newtonsoft.Json;
|
||||||
|
using Newtonsoft.Json.Linq;
|
||||||
|
|
||||||
|
public class MoveConverter : JsonConverter<Move>
|
||||||
|
{
|
||||||
|
//Overrides the ReadJson function.
|
||||||
|
public override Move ReadJson(JsonReader reader, Type objectType, Move? existingValue, bool hasExistingValue, JsonSerializer serializer)
|
||||||
|
{
|
||||||
|
//Load the raw JSON object from the reader.
|
||||||
|
JObject obj = JObject.Load(reader);
|
||||||
|
|
||||||
|
// Read the "Type" field
|
||||||
|
string? type = obj["Type"]?.ToString();
|
||||||
|
|
||||||
|
//Create a temporary item object.
|
||||||
|
Move move;
|
||||||
|
|
||||||
|
//Uses the Type field to determine what kind of object to create, to allow for a mixed list.
|
||||||
|
switch (type)
|
||||||
|
{
|
||||||
|
case "PhysicalMove":
|
||||||
|
move = new PhysicalMove("", "", 0, "");
|
||||||
|
break;
|
||||||
|
|
||||||
|
default:
|
||||||
|
throw new JsonSerializationException($"Unknown item type: {type}");
|
||||||
|
}
|
||||||
|
|
||||||
|
// Populate the item with values from JSON
|
||||||
|
serializer.Populate(obj.CreateReader(), move);
|
||||||
|
return move;
|
||||||
|
}
|
||||||
|
|
||||||
|
//Overrides the default WriteJson method to allow for our different items.
|
||||||
|
public override void WriteJson(JsonWriter writer, Move? value, JsonSerializer serializer)
|
||||||
|
{
|
||||||
|
JObject obj = JObject.FromObject(value, serializer);
|
||||||
|
obj.WriteTo(writer);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -5,15 +5,20 @@ using Newtonsoft.Json;
|
||||||
|
|
||||||
public static class MoveLoader
|
public static class MoveLoader
|
||||||
{
|
{
|
||||||
public static List<Move> LoadMoves(string path)
|
public static List<Move> LoadAllMoves(string path)
|
||||||
{
|
{
|
||||||
if (!File.Exists(path)){
|
if (!File.Exists(path))
|
||||||
Console.WriteLine("Move data file not found!");
|
{
|
||||||
return new List<Move>();
|
return new List<Move>();
|
||||||
}
|
}
|
||||||
|
|
||||||
string json = File.ReadAllText(path);
|
string json = File.ReadAllText(path);
|
||||||
List<Move> moves = JsonConvert.DeserializeObject<List<Move>>(json) ?? new List<Move>();
|
|
||||||
return moves ?? new List<Move>();
|
var settings = new JsonSerializerSettings
|
||||||
|
{
|
||||||
|
Converters = { new MoveConverter() }
|
||||||
|
};
|
||||||
|
|
||||||
|
return JsonConvert.DeserializeObject<List<Move>>(json, settings) ?? new List<Move>();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -1,12 +1,12 @@
|
||||||
public class Player : Character
|
public class Player : Character
|
||||||
{
|
{
|
||||||
public List<Item> Inventory { get; set; }
|
public List<Item> Inventory { get; set; }
|
||||||
public List<Move> moves { get; set; }
|
public List<Move> Moves { get; set; }
|
||||||
|
|
||||||
public Player(string name, int maxhealth, int currenthealth, int maxmpoints, int currentmpoints, List<Item> inventory) : base(name, maxhealth, currenthealth, maxmpoints, currentmpoints)
|
public Player(string name, int maxhealth, int currenthealth, int maxmpoints, int currentmpoints, List<Item> inventory, List<Move> moves) : base(name, maxhealth, currenthealth, maxmpoints, currentmpoints)
|
||||||
{
|
{
|
||||||
Inventory = inventory;
|
Inventory = inventory;
|
||||||
|
Moves = moves;
|
||||||
}
|
}
|
||||||
|
|
||||||
//Returns the players inventory in a Dictionary.
|
//Returns the players inventory in a Dictionary.
|
||||||
|
|
|
||||||
|
|
@ -2,14 +2,13 @@ public class Move
|
||||||
{
|
{
|
||||||
public string Name { get; set; }
|
public string Name { get; set; }
|
||||||
public string Description { get; set; }
|
public string Description { get; set; }
|
||||||
public int Power { get; set; }
|
public virtual int Power { get; set; }
|
||||||
public int MPCost { get; set; }
|
public virtual int MPCost { get; set; }
|
||||||
public virtual string Type { get; set; }
|
public virtual string Type { get; set; }
|
||||||
|
public virtual string Action { get; set; }
|
||||||
|
|
||||||
public Move(string name, string description, int power, int mpcost){
|
public Move(string name, string description){
|
||||||
Name = name;
|
Name = name;
|
||||||
Description = description;
|
Description = description;
|
||||||
Power = power;
|
|
||||||
MPCost = mpcost;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,9 @@
|
||||||
|
public class PhysicalMove : Move
|
||||||
|
{
|
||||||
|
public override string Type => "PhysicalMove";
|
||||||
|
|
||||||
|
public PhysicalMove(string name, string description, int power, string action) : base(name, description)
|
||||||
|
{
|
||||||
|
Action = action;
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Add table
Reference in a new issue