diff --git a/Main.cs b/Main.cs index 7bb1f7a..01107ea 100644 --- a/Main.cs +++ b/Main.cs @@ -2,14 +2,16 @@ using System.Linq; using System.Collections.Generic; using Microsoft.VisualBasic; +using System.IO.MemoryMappedFiles; class Program { static void Main() { List allItems = ItemLoader.LoadAllItems("data/items.json"); + List 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.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 == "Dagger")); - Console.WriteLine("Do you want to use your Potion? (Y/N)"); - string? input = Console.ReadLine().ToUpper(); - Console.WriteLine("\n"); - - if (input == "Y") - { - user.UseHealingItem((HealingItem)user.Inventory.FirstOrDefault(m => m.Name == "Potion")); - } - - user.DisplayStats(); + user.Moves.Add(allMoves.FirstOrDefault(m => m.Name == "Stab")); } } diff --git a/data/moves.json b/data/moves.json index 88f0891..9ed93ff 100644 --- a/data/moves.json +++ b/data/moves.json @@ -4,21 +4,31 @@ "Description": "A burst of fire that burns the enemy.", "Power": 40, "ManaCost": 10, - "Type": "Attack" + "Type": "MagicMove", + "Action": "Attack" }, { "Name": "Heal", "Description": "Restores a small amount of HP.", "Power": 25, "ManaCost": 5, - "Type": "Heal" + "Type": "MagicMove", + "Action": "Healing" }, { "Name": "Lightning Bolt", "Description": "A shocking magical strike.", "Power": 50, "ManaCost": 15, - "Type": "Attack" + "Type": "MagicMove", + "Action": "Attack" + }, + { + "Name": "Stab", + "Description": "A stab with a sharp object.", + "Power": 20, + "Type": "PhysicalMove", + "Action": "Attack" } ] \ No newline at end of file diff --git a/obj/Debug/net8.0/MiniRPG.AssemblyInfo.cs b/obj/Debug/net8.0/MiniRPG.AssemblyInfo.cs index ac59532..10224e6 100644 --- a/obj/Debug/net8.0/MiniRPG.AssemblyInfo.cs +++ b/obj/Debug/net8.0/MiniRPG.AssemblyInfo.cs @@ -13,7 +13,7 @@ using System.Reflection; [assembly: System.Reflection.AssemblyCompanyAttribute("MiniRPG")] [assembly: System.Reflection.AssemblyConfigurationAttribute("Debug")] [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.AssemblyTitleAttribute("MiniRPG")] [assembly: System.Reflection.AssemblyVersionAttribute("1.0.0.0")] diff --git a/obj/Debug/net8.0/MiniRPG.AssemblyInfoInputs.cache b/obj/Debug/net8.0/MiniRPG.AssemblyInfoInputs.cache index 05fc7b9..168ef4e 100644 --- a/obj/Debug/net8.0/MiniRPG.AssemblyInfoInputs.cache +++ b/obj/Debug/net8.0/MiniRPG.AssemblyInfoInputs.cache @@ -1 +1 @@ -2f9f95c4d52ac0c3165163d219ccd0cd9cb08d53b12c9beccc5292d27ab9163e +d3a1cc43ff9983679cb5442bcc4c2923f62c186209b30538fb7445848d6c5fa5 diff --git a/scripts/loaders/MoveConverter.cs b/scripts/loaders/MoveConverter.cs new file mode 100644 index 0000000..134c437 --- /dev/null +++ b/scripts/loaders/MoveConverter.cs @@ -0,0 +1,40 @@ +using Newtonsoft.Json; +using Newtonsoft.Json.Linq; + +public class MoveConverter : JsonConverter +{ + //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); + } +} diff --git a/scripts/loaders/MoveLoader.cs b/scripts/loaders/MoveLoader.cs index 3d0642c..54adf8b 100644 --- a/scripts/loaders/MoveLoader.cs +++ b/scripts/loaders/MoveLoader.cs @@ -5,15 +5,20 @@ using Newtonsoft.Json; public static class MoveLoader { - public static List LoadMoves(string path) + public static List LoadAllMoves(string path) { - if (!File.Exists(path)){ - Console.WriteLine("Move data file not found!"); + if (!File.Exists(path)) + { return new List(); } string json = File.ReadAllText(path); - List moves = JsonConvert.DeserializeObject>(json) ?? new List(); - return moves ?? new List(); + + var settings = new JsonSerializerSettings + { + Converters = { new MoveConverter() } + }; + + return JsonConvert.DeserializeObject>(json, settings) ?? new List(); } } \ No newline at end of file diff --git a/scripts/objects/characters/Player.cs b/scripts/objects/characters/Player.cs index 8ca1c26..dbb01a3 100644 --- a/scripts/objects/characters/Player.cs +++ b/scripts/objects/characters/Player.cs @@ -1,12 +1,12 @@ public class Player : Character { public List Inventory { get; set; } - public List moves { get; set; } + public List Moves { get; set; } - public Player(string name, int maxhealth, int currenthealth, int maxmpoints, int currentmpoints, List inventory) : base(name, maxhealth, currenthealth, maxmpoints, currentmpoints) + public Player(string name, int maxhealth, int currenthealth, int maxmpoints, int currentmpoints, List inventory, List moves) : base(name, maxhealth, currenthealth, maxmpoints, currentmpoints) { Inventory = inventory; - + Moves = moves; } //Returns the players inventory in a Dictionary. diff --git a/scripts/objects/moves/Move.cs b/scripts/objects/moves/Move.cs index cb15763..79f82c4 100644 --- a/scripts/objects/moves/Move.cs +++ b/scripts/objects/moves/Move.cs @@ -2,14 +2,13 @@ public class Move { public string Name { get; set; } public string Description { get; set; } - public int Power { get; set; } - public int MPCost { get; set; } + public virtual int Power { get; set; } + public virtual int MPCost { 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; Description = description; - Power = power; - MPCost = mpcost; } } diff --git a/scripts/objects/moves/PhysicalMove.cs b/scripts/objects/moves/PhysicalMove.cs index e69de29..cf43445 100644 --- a/scripts/objects/moves/PhysicalMove.cs +++ b/scripts/objects/moves/PhysicalMove.cs @@ -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; + } +}