From 7f2aed65f77ab7477f53a5844ad031c6d603df8a Mon Sep 17 00:00:00 2001 From: Syndamia Date: Tue, 16 Apr 2019 16:13:26 +0300 Subject: Added class Weapons (custom weapon damage, name, char, ...), made combat system work with any mob, added weapons list to class mob, optimized screen printing, made class constructors for all classes, taking damage is a bit slower, mob walking is a bit slower (and less likely) --- Bitspace/Bitspace/Biome.cs | 19 ++++- Bitspace/Bitspace/Bitspace.csproj | 1 + Bitspace/Bitspace/Mob.cs | 70 ++++++++++++++---- Bitspace/Bitspace/Program.cs | 147 ++++++++++++++++++-------------------- Bitspace/Bitspace/Weapon.cs | 73 +++++++++++++++++++ 5 files changed, 218 insertions(+), 92 deletions(-) create mode 100644 Bitspace/Bitspace/Weapon.cs (limited to 'Bitspace') diff --git a/Bitspace/Bitspace/Biome.cs b/Bitspace/Bitspace/Biome.cs index 2713f80..653cb2c 100644 --- a/Bitspace/Bitspace/Biome.cs +++ b/Bitspace/Bitspace/Biome.cs @@ -11,8 +11,8 @@ namespace Game private string name; private char floor; //floor literally what the mobs "step on" private ConsoleColor color; - private ushort damage; - private bool suffocate; + private ushort damage; + private bool suffocate; //if true it will firstly drain the air var and then will drain health public string Name { @@ -43,5 +43,20 @@ namespace Game set { suffocate = value; } get { return suffocate; } } + + public Biome(string name, char floor, ConsoleColor color) : this(name, floor, color, 0, false) + { } + + public Biome(string name, char floor, ConsoleColor color, ushort damage) : this(name, floor, color, damage, false) + { } + + public Biome(string name, char floor, ConsoleColor color, ushort damage, bool suffocate) + { + Name = name; + Floor = floor; + Color = color; + Damage = damage; + Suffocate = suffocate; + } } } diff --git a/Bitspace/Bitspace/Bitspace.csproj b/Bitspace/Bitspace/Bitspace.csproj index bec126c..2e4673a 100644 --- a/Bitspace/Bitspace/Bitspace.csproj +++ b/Bitspace/Bitspace/Bitspace.csproj @@ -48,6 +48,7 @@ + diff --git a/Bitspace/Bitspace/Mob.cs b/Bitspace/Bitspace/Mob.cs index dc51a04..cece13f 100644 --- a/Bitspace/Bitspace/Mob.cs +++ b/Bitspace/Bitspace/Mob.cs @@ -8,12 +8,13 @@ namespace Game { class Mob { - //Mobile objects = Mobs - private bool alive; + public bool Alive { set; get; } private ushort health; private ushort maxHealth; + private List weaponsList; + private ushort armour; private ushort maxArmour; @@ -34,18 +35,9 @@ namespace Game Console.Write(Body); } - public void PrintStats() //prints the mob's stats, i.e. health, armour, lung capacity, only used for player + public string Stats() //prints the mob's stats, i.e. health, armour, lung capacity, only used for player { - Console.WriteLine(); - Console.ForegroundColor = ConsoleColor.Gray; - Console.WriteLine($"Health: {Health} | Armour: {Armour} | Air: {new string('O', LungCapacity)}"); - Console.WriteLine($"X: {XPos} | Y: {YPos}"); - } - - public bool Alive - { - set { alive = value; } - get { return alive; } + return $"Health: {Health} | Armour: {Armour} | Air: {new string('O', LungCapacity)}\r\nX: {XPos} | Y: {YPos}"; } public ushort Health @@ -116,5 +108,57 @@ namespace Game set { yPos = value; } get { return yPos; } } + + public List WeaponsList + { + set { weaponsList = value; } + get { return weaponsList; } + } + + public Mob(string name, char body, ConsoleColor color, ushort health) : this(name, body, color, health, 0, 0, health, 0, 0, 0, 0, true) + { } + + public Mob(string name, char body, ConsoleColor color, ushort health, int xPos, int yPos) : this(name, body, color, health, 0, 0, health, 0, 0, xPos, yPos, true) + { } + + public Mob(string name, char body, ConsoleColor color, ushort health, ushort armour) : this(name, body, color, health, armour, 0, health, armour, 0, 0, 0, true) + { } + + public Mob(string name, char body, ConsoleColor color, ushort health, ushort armour, int xPos, int yPos) : this(name, body, color, health, armour, 0, health, armour, 0, xPos, yPos, true) + { } + + public Mob(string name, char body, ConsoleColor color, ushort health, byte lungCapacity) : this(name, body, color, health, 0, lungCapacity, health, 0, lungCapacity, 0, 0, true) + { WeaponsList = new List(); } + + public Mob(string name, char body, ConsoleColor color, ushort health, byte lungCapacity, int xPos, int yPos) : this(name, body, color, health, 0, lungCapacity, health, 0, lungCapacity, xPos, yPos, true) + { } + + public Mob(string name, char body, ConsoleColor color, ushort health, ushort armour, byte lungCapacity) : this(name, body, color, health, armour, lungCapacity, health, armour, lungCapacity, 0, 0, true) + { } + + public Mob(string name, char body, ConsoleColor color, ushort health, ushort armour, byte lungCapacity, int xPos, int yPos) : this(name, body, color, health, armour, lungCapacity, health, armour, lungCapacity, xPos, yPos, true) + { } + + public Mob(string name, char body, ConsoleColor color, ushort health, ushort armour, byte lungCapacity, ushort maxHealth, ushort maxArmour, byte maxLungCapacity) : this(name, body, color, health, armour, lungCapacity, maxHealth, maxArmour, maxLungCapacity, 0, 0, true) + { } + + public Mob(string name, char body, ConsoleColor color, ushort health, ushort armour, byte lungCapacity, ushort maxHealth, ushort maxArmour, byte maxLungCapacity, int xPos, int yPos) : this(name, body, color, health, armour, lungCapacity, maxHealth, maxArmour, maxLungCapacity, xPos, yPos, true) + { } + + public Mob(string name, char body, ConsoleColor color, ushort health, ushort armour, byte lungCapacity, ushort maxHealth, ushort maxArmour, byte maxLungCapacity, int xPos, int yPos, bool alive) + { + Alive = alive; + Health = health; + MaxHealth = maxHealth; + Armour = armour; + MaxArmour = maxArmour; + LungCapacity = lungCapacity; + MaxLungCapacity = maxLungCapacity; + Name = name; + Body = body; + Color = color; + XPos = xPos; + YPos = yPos; + } } } diff --git a/Bitspace/Bitspace/Program.cs b/Bitspace/Bitspace/Program.cs index 1e8e842..c119aee 100644 --- a/Bitspace/Bitspace/Program.cs +++ b/Bitspace/Bitspace/Program.cs @@ -12,29 +12,36 @@ namespace Game //collection of all biomes, WARNING: biomes that do damage must be ON TOP of biomes that do not private static Dictionary biomeDict = new Dictionary { - { "Water", new Biome { Name = "Water",Floor = '-', Color = ConsoleColor.Blue, Damage = 1 , Suffocate = true } }, - { "Grass land", new Biome { Name = "Grass land",Floor = ',', Color = ConsoleColor.Green} }, - { "Rocks", new Biome { Name = "Rocks",Floor = '.', Color = ConsoleColor.Gray} } + { "Water", new Biome("Water", '-', ConsoleColor.Blue, 1, true) }, + { "Grass land", new Biome("Grass land", ',', ConsoleColor.Green) }, + { "Rocks", new Biome("Rocks", '.', ConsoleColor.Gray) } }; //collection of all mobs private static Dictionary mobDict = new Dictionary { - { "Player", new Mob {Name = "Player", Color = ConsoleColor.White, Body = '@', Alive = true, MaxHealth = 100, Health = 100, MaxLungCapacity = 10, LungCapacity = 10} }, - { "Smiley", new Mob {Name = "Smiley", Color = ConsoleColor.Magenta, Body = 'U', Alive = true, MaxHealth = 5, Health = 5, MaxLungCapacity = 5, LungCapacity = 5} }, - { "Smiley1", new Mob {Name = "Smiley1", Color = ConsoleColor.Magenta, Body = 'U', Alive = true, MaxHealth = 5, Health = 5, MaxLungCapacity = 5, LungCapacity = 5} }, - { "Smiley2", new Mob {Name = "Smiley2", Color = ConsoleColor.Magenta, Body = 'U', Alive = true, MaxHealth = 5, Health = 5, MaxLungCapacity = 5, LungCapacity = 5} } + { "Player", new Mob("Player", '@', ConsoleColor.White, 100, 10) }, + { "Smiley", new Mob("Smiley", 'U', ConsoleColor.Magenta, 5, 5) }, + { "Smiley1", new Mob("Smiley1", 'U', ConsoleColor.Magenta, 5, 5) }, + { "Smiley2", new Mob("Smiley1", 'U', ConsoleColor.Magenta, 5, 5) } + }; + + private static Dictionary weaponDict = new Dictionary() + { + { "Stick", new Weapon("Stick", 1, '-', '|') } }; static void Main(string[] args) { Console.BackgroundColor = ConsoleColor.Black; int s = MapSize(); - + + mobDict["Player"].WeaponsList = new List { weaponDict["Stick"]}; //temporary, just for tests string[] map = new string[s]; MapGenerate(map); + ushort gameTick = 0; while (mobDict["Player"].Alive == true) { if (Console.KeyAvailable) // checkes if the player has pressed any key @@ -49,90 +56,66 @@ namespace Game case ConsoleKey.A: if (mobDict["Player"].XPos > 0) mobDict["Player"].XPos--; break; case ConsoleKey.D: if (mobDict["Player"].XPos < map.Length * 2 - 1) mobDict["Player"].XPos++; break; - case ConsoleKey.UpArrow: DamageDealing(map, ConsoleKey.UpArrow); break; - case ConsoleKey.DownArrow: DamageDealing(map, ConsoleKey.DownArrow); break; - case ConsoleKey.LeftArrow: DamageDealing(map, ConsoleKey.LeftArrow); break; - case ConsoleKey.RightArrow: DamageDealing(map, ConsoleKey.RightArrow); break; + case ConsoleKey.UpArrow: mobDict["Player"].WeaponsList.First().Direction = 3; break; + case ConsoleKey.DownArrow: mobDict["Player"].WeaponsList.First().Direction = 4; break; + case ConsoleKey.LeftArrow: mobDict["Player"].WeaponsList.First().Direction = 1; break; + case ConsoleKey.RightArrow: mobDict["Player"].WeaponsList.First().Direction = 2; break; } } - //chooses random movement for mobs (that arent player) - var rndDirection = new Random(); - foreach (var mob in mobDict.Values.Where(m => m.Name != "Player" && m.Alive == true)) + if(gameTick == 1) { - switch (rndDirection.Next(6)) + //chooses random movement for mobs (that arent player) + var rndDirection = new Random(); + foreach (var mob in mobDict.Values.Where(m => m.Name != "Player" && m.Alive == true)) { - case 1: if (mob.YPos > 0) mob.YPos--; break; - case 2: if (mob.YPos < map.Length - 1) mob.YPos++; break; - case 3: if (mob.XPos > 0) mob.XPos--; break; - case 4: if (mob.XPos < map.Length * 2 - 1) mob.XPos++; break; - case 5: break; //this is for the mob to do nothing, so it can just stay where it is + switch (rndDirection.Next(10)) + { + case 1: if (mob.YPos > 0) mob.YPos--; break; + case 2: if (mob.YPos < map.Length - 1) mob.YPos++; break; + case 3: if (mob.XPos > 0) mob.XPos--; break; + case 4: if (mob.XPos < map.Length * 2 - 1) mob.XPos++; break; + case 5: break; //this is for the mob to do nothing, so it can just stay where it is + } } } - //damage-regeneation mechanism, at the moment you only get damaged in water, program crashes when player dies - foreach (var mob in mobDict.Values.Where(m => m.Alive == true)) + if (gameTick == 2) { - foreach(var biome in biomeDict.Values) + //damage-regeneation mechanism, at the moment you only get damaged in water, program crashes when player dies + foreach (var mob in mobDict.Values.Where(m => m.Alive == true)) { - if (biome.Floor == map[mob.YPos][mob.XPos] && biome.Damage > 0) + foreach (var biome in biomeDict.Values) { - if (mob.LungCapacity > 0 && biome.Suffocate) mob.LungCapacity--; - else mob.Health -= biome.Damage; + if (biome.Floor == map[mob.YPos][mob.XPos] && biome.Damage > 0) + { + if (mob.LungCapacity > 0 && biome.Suffocate) mob.LungCapacity--; + else mob.Health -= biome.Damage; - break; + break; + } + else + { + if (mob.LungCapacity < mob.MaxLungCapacity) mob.LungCapacity++; + if (mob.Health < mob.MaxHealth) mob.Health++; + } } - else - { - if (mob.LungCapacity < mob.MaxLungCapacity) mob.LungCapacity++; - if (mob.Health < mob.MaxHealth) mob.Health++; - } + + if (mob.Health == 0) mob.Alive = false; } - if (mob.Health == 0) mob.Alive = false; + gameTick = 0; } ReWriteScreen(map); Thread.Sleep(80); + gameTick++; } Console.WriteLine("GAME OVER. YOU DIED."); ConsoleKey endProgram = Console.ReadKey().Key; } - private static void DamageDealing(string[] map, ConsoleKey direction) - { - int x = mobDict["Player"].XPos; - int y = mobDict["Player"].YPos; - char weapon = '.'; - - switch (direction) - { - case ConsoleKey.UpArrow: if (y > 0) { y--; weapon = '|'; } break; - case ConsoleKey.DownArrow: if (y < map.Length - 1) { y++; weapon = '|'; } break; - case ConsoleKey.LeftArrow: if (x > 0) { x--; weapon = '-'; } break; - case ConsoleKey.RightArrow: if (x < map.Length * 2 - 1) { x++; weapon = '-'; } break; - } - - if(weapon != '.') - { - Console.SetCursorPosition(x, y); - Console.Write(weapon); - - List damagedMobs = mobDict.Values.Where(m => m.Alive == true && m.XPos == x && m.YPos == y).Select(m => m.Name).ToList(); - if (damagedMobs.Count > 0) - { - foreach (var dMob in damagedMobs) - { - mobDict[dMob].Health -= 1; - if (mobDict[dMob].Health == 0) mobDict[dMob].Alive = false; - } - } - } - - Thread.Sleep(80); - } - private static void MapGenerate(string[] map) { Random rnd = new Random(); @@ -160,20 +143,30 @@ namespace Game Console.ForegroundColor = biomeDict.Values.ToList().Find(x => x.Floor == map[i].Last()).Color; Console.WriteLine(map[i].TrimStart(map[i].First())); - } - //Prints mobs in their positions - foreach(var mob in mobDict.Values.Where(m => m.Alive == true)) - { - mob.Print(); - } + //If an alive mob is at the same line as the cycle is, print it + foreach (var mob in mobDict.Values.Where(m => m.YPos == i && m.Alive == true)) + { + mob.Print(); - //Prints the player status, i.e. health, armour, lung capacity (it's made with a string of "bubbles") - Console.SetCursorPosition(0, map.Length + 1); - mobDict["Player"].PrintStats(); + if (mob.WeaponsList.Count > 0) + { + foreach (var weapon in mob.WeaponsList) + { + + weapon.Print(mobDict["Player"].XPos, mobDict["Player"].YPos, map.Length * 2, map.Length); - //Console.WriteLine(biomeDict.Values.ToList().Find(b => map[mobDict["Player"].YPos][mobDict["Player"].XPos] == b.Floor).Name); + if (weapon.Direction != 4) weapon.Direction = 0; + } + } + } + Console.SetCursorPosition(0, i + 1); + } + //Prints the player status, i.e. health, armour, lung capacity (it's made with a string of "bubbles") + Console.SetCursorPosition(0, map.Length + 1); //sets cursor at the very end + Console.ForegroundColor = ConsoleColor.Gray; + Console.WriteLine(mobDict["Player"].Stats()); Console.CursorVisible = false; } diff --git a/Bitspace/Bitspace/Weapon.cs b/Bitspace/Bitspace/Weapon.cs new file mode 100644 index 0000000..4ecd62c --- /dev/null +++ b/Bitspace/Bitspace/Weapon.cs @@ -0,0 +1,73 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Game +{ + class Weapon + { + private string name; + private ushort damage; + + private byte direction; + + private char skinLeft; + private char skinRight; + private char skinUp; + private char skinDown; + + public void Print(int xPos, int yPos, int maxX, int maxY) + { + char swordChar = SwordSwing(direction); + + if(swordChar != '\0') + { + switch (direction) + { + case 1: if (xPos > 0) { Console.SetCursorPosition(xPos - 1, yPos); Console.Write(swordChar); } break; + case 2: if (xPos < maxX - 1) { Console.SetCursorPosition(xPos + 1, yPos); Console.Write(swordChar); } break; + case 3: if (yPos > 0) { Console.SetCursorPosition(xPos, yPos - 1); Console.Write(swordChar); } break; + case 4: if (yPos < maxY - 1) { Console.SetCursorPosition(xPos, yPos + 1); Console.Write(swordChar); } break; + } + } + } + + private char SwordSwing(byte direction) + { + switch (direction) + { + case 1: return SkinLeft; //no need for break;, return works like a break + case 2: return SkinRight; + case 3: return SkinUp; + case 4: return SkinDown; + default: return '\0'; // \0 is the char equivalent of null (you can't just write null) + } + } + + public Weapon(string name, ushort damage, char tetraChar) : this(name, damage, tetraChar, tetraChar, tetraChar, tetraChar) + { } + + public Weapon(string name, ushort damage, char leftRight, char upDown) : this(name, damage, leftRight, leftRight, upDown, upDown) + { } + + public Weapon(string name, ushort damage, char skinLeft, char skinRight, char skinUp, char skinDown) + { + Damage = damage; + SkinLeft = skinLeft; + SkinRight = skinRight; + SkinUp = skinUp; + SkinDown = skinDown; + Name = name; + } + + public ushort Damage { get => damage; set => damage = value; } + public char SkinLeft { get => skinLeft; set => skinLeft = value; } + public char SkinRight { get => skinRight; set => skinRight = value; } + public char SkinUp { get => skinUp; set => skinUp = value; } + public char SkinDown { get => skinDown; set => skinDown = value; } + public byte Direction { get => direction; set => direction = value; } + public string Name { get => name; set => name = value; } + } +} -- cgit v1.2.3