diff options
Diffstat (limited to 'Bitspace')
| -rw-r--r-- | Bitspace/Bitspace/Biome.cs | 4 | ||||
| -rw-r--r-- | Bitspace/Bitspace/Mob.cs | 8 | ||||
| -rw-r--r-- | Bitspace/Bitspace/Program.cs | 210 | ||||
| -rw-r--r-- | Bitspace/Bitspace/Weapon.cs | 6 |
4 files changed, 111 insertions, 117 deletions
diff --git a/Bitspace/Bitspace/Biome.cs b/Bitspace/Bitspace/Biome.cs index 070c3fd..60c24f5 100644 --- a/Bitspace/Bitspace/Biome.cs +++ b/Bitspace/Bitspace/Biome.cs @@ -9,10 +9,10 @@ namespace Game class Biome { private string name; - private char floor; //floor literally what the mobs "step on" + private char floor; private ConsoleColor color; private ushort damage; - private bool suffocate; //if true it will firstly drain the air var and then will drain health + private bool suffocate; //if true it will firstly drain the air (lungCapacity) variable and then will drain health public string Name { diff --git a/Bitspace/Bitspace/Mob.cs b/Bitspace/Bitspace/Mob.cs index d898253..45a43bc 100644 --- a/Bitspace/Bitspace/Mob.cs +++ b/Bitspace/Bitspace/Mob.cs @@ -19,24 +19,24 @@ namespace Game private ushort armour; private ushort maxArmour; - private byte lungCapacity; //determines how long can the mob stay in the water before it "drowns" + private byte lungCapacity; private byte maxLungCapacity; private string name; - private char body; //the body is the character that represents the mob + private char body; private ConsoleColor color; private int xPos; private int yPos; - public void Print() //prints the mob's char in it's corresponding position + public void Print() { Console.SetCursorPosition(XPos, YPos); Console.ForegroundColor = Color; Console.Write(Body); } - public string Stats() //prints the mob's stats, i.e. health, armour, lung capacity, only used for player + public string Stats() { return $"{Name} | Health: {Health} | Armour: {Armour} | Air: {new string('O', LungCapacity)}\r\nX: {XPos} | Y: {YPos}"; } diff --git a/Bitspace/Bitspace/Program.cs b/Bitspace/Bitspace/Program.cs index 6606f0c..f0bbfa1 100644 --- a/Bitspace/Bitspace/Program.cs +++ b/Bitspace/Bitspace/Program.cs @@ -11,7 +11,6 @@ namespace Game { private static string[] map; - //collection of all biomes private static Dictionary<string, Biome> biomeDict = new Dictionary<string, Biome> { { "Water", new Biome("Water", '-', ConsoleColor.Blue, 1, true) }, @@ -20,7 +19,6 @@ namespace Game { "Rocks", new Biome("Rocks", '.', ConsoleColor.Gray) } }; - //collection of all mobs private static Dictionary<string, Mob> mobDict = new Dictionary<string, Mob> { { "Player", new Mob("Player", '@', ConsoleColor.White, 100, 10, 10) }, @@ -34,8 +32,7 @@ namespace Game { "Stick", new Weapon("Stick", 5, '-', '|') } }; - //these are all the player controls, these hold the value of which button for what is used - private static Dictionary<string, ConsoleKey> keysDict = new Dictionary<string, ConsoleKey>() + private static Dictionary<string, ConsoleKey> keyBindingsDict = new Dictionary<string, ConsoleKey>() { { "forward", ConsoleKey.W }, { "backward", ConsoleKey.S }, { "left", ConsoleKey.A }, { "right", ConsoleKey.D }, { "swingUp", ConsoleKey.UpArrow }, { "swingDown", ConsoleKey.DownArrow }, { "swingLeft", ConsoleKey.LeftArrow }, { "swingRight", ConsoleKey.RightArrow } @@ -47,42 +44,42 @@ namespace Game StartMenu(); int s = MapSize(); - mobDict["Player"].WeaponsList = new List<Weapon> { weaponDict["Stick"]}; //temporary, just for tests + //TODO: better weapon adding to mobs + mobDict["Player"].WeaponsList = new List<Weapon> { weaponDict["Stick"]}; map = new string[s]; - MapGenerate(map); + GenerateMap(map); - ulong gameTick = 0; //we use a var so that movement is as fast as possible, but taking damage, healing, drowning, ... is slower + ulong gameTick = 0; //this is so that stuff like taking damage, drowning, ... is a bit slower than the almost instant game refresh while (mobDict["Player"].Alive == true) { - if (Console.KeyAvailable) // checkes if the player has pressed any key + if (Console.KeyAvailable) { var keyPressed = Console.ReadKey().Key; - //changes player position and swings weapon depending on pressed key //we can't use switch, because it requires constant values, i.e. not variables, so with switch you can't customise button mappings - if (keyPressed == keysDict["forward"]) + if (keyPressed == keyBindingsDict["forward"]) { if (mobDict["Player"].YPos > 0) mobDict["Player"].YPos--; } - else if (keyPressed == keysDict["backward"]) + else if (keyPressed == keyBindingsDict["backward"]) { if (mobDict["Player"].YPos < map.Length - 1) mobDict["Player"].YPos++; } - else if (keyPressed == keysDict["left"]) + else if (keyPressed == keyBindingsDict["left"]) { if (mobDict["Player"].XPos > 0) mobDict["Player"].XPos--; } - else if (keyPressed == keysDict["right"]) + else if (keyPressed == keyBindingsDict["right"]) { if (mobDict["Player"].XPos < map.Length * 2 - 1) mobDict["Player"].XPos++; } - else if (keyPressed == keysDict["swingLeft"]) + else if (keyPressed == keyBindingsDict["swingLeft"]) { mobDict["Player"].WeaponsList.First().Direction = 1; } - else if (keyPressed == keysDict["swingRight"]) + else if (keyPressed == keyBindingsDict["swingRight"]) { mobDict["Player"].WeaponsList.First().Direction = 2; } - else if (keyPressed == keysDict["swingUp"]) + else if (keyPressed == keyBindingsDict["swingUp"]) { mobDict["Player"].WeaponsList.First().Direction = 3; } - else if (keyPressed == keysDict["swingDown"]) + else if (keyPressed == keyBindingsDict["swingDown"]) { mobDict["Player"].WeaponsList.First().Direction = 4; } } @@ -93,22 +90,7 @@ namespace Game if (gameTick % 3 == 0) { - //damage-regeneation mechanism from biomes - foreach (var mob in mobDict.Values.Where(m => m.Alive == true)) - { - var biome = biomeDict.Values.Where(b => b.Floor == map[mob.YPos][mob.XPos]).Single(); - - if(biome.Damage > 0) - { - if (biome.Suffocate && mob.LungCapacity > 0) mob.LungCapacity--; - else mob.Health -= biome.Damage; - } - else - { - if (mob.LungCapacity < mob.MaxLungCapacity) mob.LungCapacity++; - else if (mob.Health < mob.MaxHealth && gameTick % 10 == 0) mob.Health++; - } - } + DoBiomeDamage(gameTick); } if (gameTick % 60 == 0) @@ -122,9 +104,8 @@ namespace Game ReWriteScreen(map); gameTick++; - //mobs that have health <= 0 are "killed", i.e. var alive is false - foreach(var dead in mobDict.Values.Where(m => m.Health <= 0)) - { dead.Alive = false; } + foreach(var deadMob in mobDict.Values.Where(m => m.Health <= 0)) + { deadMob.Alive = false; } for(int i = 0; i < 34000000; i++) { } //replace of Thread.Sleep(80); because we haven't learned about thread stuff for now @@ -137,34 +118,35 @@ namespace Game private static void MobMovement() { - //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)) { - int rnd = rndDirection.Next(20); + int movementChooser = rndDirection.Next(20); //number is bigger than cases, so mob has a higher chance of staying at the same place rather than movning if (biomeDict.Values.Where(b => b.Floor == map[mob.YPos][mob.XPos]).Single().Damage > 0) { + //a lot of try catches, so the code goes through all direction options, even if the player is at a wall/corner + //x and y positions aren't set here because we have to check the mob position (not to repeat code) try { - if (biomeDict.Values.Where(b => b.Floor == map[mob.YPos][mob.XPos - 1]).Single().Damage == 0) rnd = 3; + if (biomeDict.Values.Where(b => b.Floor == map[mob.YPos][mob.XPos - 1]).Single().Damage == 0) movementChooser = 3; } catch { } try { - if (biomeDict.Values.Where(b => b.Floor == map[mob.YPos][mob.XPos + 1]).Single().Damage == 0) rnd = 4; + if (biomeDict.Values.Where(b => b.Floor == map[mob.YPos][mob.XPos + 1]).Single().Damage == 0) movementChooser = 4; } catch { } try { - if (biomeDict.Values.Where(b => b.Floor == map[mob.YPos - 1][mob.XPos]).Single().Damage == 0) rnd = 1; + if (biomeDict.Values.Where(b => b.Floor == map[mob.YPos - 1][mob.XPos]).Single().Damage == 0) movementChooser = 1; } catch { } try { - if (biomeDict.Values.Where(b => b.Floor == map[mob.YPos + 1][mob.XPos]).Single().Damage == 0) rnd = 2; + if (biomeDict.Values.Where(b => b.Floor == map[mob.YPos + 1][mob.XPos]).Single().Damage == 0) movementChooser = 2; } catch { } } - switch (rnd) //number is bigger than cases, so mob has a higher chance of staying at the same place rather than movning + switch (movementChooser) { case 1: if (mob.YPos > 0) mob.YPos--; break; case 2: if (mob.YPos < map.Length - 1) mob.YPos++; break; @@ -174,11 +156,29 @@ namespace Game } } + private static void DoBiomeDamage(ulong gameTick) + { + foreach (var mob in mobDict.Values.Where(m => m.Alive == true)) + { + var biome = biomeDict.Values.Where(b => b.Floor == map[mob.YPos][mob.XPos]).Single(); + + if (biome.Damage > 0) + { + if (biome.Suffocate && mob.LungCapacity > 0) mob.LungCapacity--; + else mob.Health -= biome.Damage; + } + else + { + if (mob.LungCapacity < mob.MaxLungCapacity) mob.LungCapacity++; + else if (mob.Health < mob.MaxHealth && gameTick % 10 == 0) mob.Health++; + } + } + } + private static void ReWriteScreen(string[] map) { Console.Clear(); - //Prints the map for (int i = 0; i < map.Length; i++) { //because a map line can be max 2 biomes, the first half is determined by the first char, and the second half by the last char @@ -190,10 +190,10 @@ namespace Game foreach (var mob in mobDict.Values.Where(m => m.Alive == true)) { - //this prints all mobs that are on the line that was just drawn + //this prints all mobs that are on the line that was just drawn, so that there is less cursor "jumping" if (mob.YPos == i) mob.Print(); - //prints weapon when the line under the mob is drawn (so that the weapon is not overwritten by the biome) or is on the last line + //prints weapon when the line under the mob is drawn (so that the weapon is not overwritten by the biome) or is on the last line, also for less cursor "jumping" if (mob.WeaponsList.Count > 0 && (i == mob.YPos + 1 || i == map.Length - 1)) { foreach (var weapon in mob.WeaponsList) @@ -206,7 +206,6 @@ namespace Game 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()); @@ -222,7 +221,7 @@ namespace Game private static void StartMenu() { Console.CursorVisible = false; - int y = 0; //tracks cursor position + int cursorYPos = 0; bool stop = false; while (!stop) @@ -234,7 +233,7 @@ namespace Game Console.WriteLine(" Settings"); Console.ForegroundColor = ConsoleColor.Red; - Console.SetCursorPosition(0, y); + Console.SetCursorPosition(0, cursorYPos); Console.Write('>'); Console.CursorVisible = false; @@ -242,20 +241,20 @@ namespace Game switch (keyPressed) { - case ConsoleKey.UpArrow: if (y == 1) y--; break; - case ConsoleKey.DownArrow: if (y == 0) y++; break; + case ConsoleKey.UpArrow: if (cursorYPos == 1) cursorYPos--; break; + case ConsoleKey.DownArrow: if (cursorYPos == 0) cursorYPos++; break; case ConsoleKey.Enter: stop = true; break; } } - if (y == 1) Settings(); + if (cursorYPos == 1) Settings(); } private static void Settings() { Mob player = mobDict["Player"]; bool stop = false; - int y = 1; // tracks cursor position + int cursorYPos = 1; // tracks cursor position while (!stop) { @@ -268,7 +267,7 @@ namespace Game Console.WriteLine(" Key Bindings Settings"); Console.WriteLine(" Back"); - Console.SetCursorPosition(0, y); + Console.SetCursorPosition(0, cursorYPos); Console.ForegroundColor = ConsoleColor.Red; Console.Write('>'); Console.CursorVisible = false; @@ -277,11 +276,11 @@ namespace Game switch (keyPressed) { - case ConsoleKey.UpArrow: if (y > 1) y--; break; - case ConsoleKey.DownArrow: if (y < 4) y++; break; + case ConsoleKey.UpArrow: if (cursorYPos > 1) cursorYPos--; break; + case ConsoleKey.DownArrow: if (cursorYPos < 4) cursorYPos++; break; case ConsoleKey.Enter: { - switch (y) + switch (cursorYPos) { case 1: { @@ -306,17 +305,17 @@ namespace Game } } - //this is not in switch, so when you exit key bindings and then exit settings, you go into the start menu, you don't continue with settings loop - if (y == 3) KeyBindings(); + //this is not in the switch, so when you exit key bindings and then exit settings, you go into the start menu, you don't continue in the settings loop + if (cursorYPos == 3) KeyBindings(); else StartMenu(); } private static void KeyBindings() { bool stop = false; - int y = 1; //tracks position of cursor - int x = 0; // used to change cursor position when changing a button - string key = null; //tracks which button should be changed + int cursorYPos = 1; + int cursosXPos = 0; //used to change cursor position when changing a button + string keyToChange = null; while (!stop) { @@ -324,17 +323,17 @@ namespace Game Console.ForegroundColor = ConsoleColor.Gray; Console.WriteLine("Key Bindings"); - Console.WriteLine($" Move forward: {keysDict["forward"]}"); - Console.WriteLine($" Move backward: {keysDict["backward"]}"); - Console.WriteLine($" Move left: {keysDict["left"]}"); - Console.WriteLine($" Move right: {keysDict["right"]}"); - Console.WriteLine($" Swing weapon up: {keysDict["swingUp"]}"); - Console.WriteLine($" Swing weapon down: {keysDict["swingDown"]}"); - Console.WriteLine($" Swing weapon left: {keysDict["swingLeft"]}"); - Console.WriteLine($" Swing weapon right: {keysDict["swingRight"]}"); + Console.WriteLine($" Move forward: {keyBindingsDict["forward"]}"); + Console.WriteLine($" Move backward: {keyBindingsDict["backward"]}"); + Console.WriteLine($" Move left: {keyBindingsDict["left"]}"); + Console.WriteLine($" Move right: {keyBindingsDict["right"]}"); + Console.WriteLine($" Swing weapon up: {keyBindingsDict["swingUp"]}"); + Console.WriteLine($" Swing weapon down: {keyBindingsDict["swingDown"]}"); + Console.WriteLine($" Swing weapon left: {keyBindingsDict["swingLeft"]}"); + Console.WriteLine($" Swing weapon right: {keyBindingsDict["swingRight"]}"); Console.WriteLine(" Back"); - Console.SetCursorPosition(0, y); + Console.SetCursorPosition(0, cursorYPos); Console.ForegroundColor = ConsoleColor.Red; Console.Write('>'); Console.CursorVisible = false; @@ -343,32 +342,32 @@ namespace Game switch (keyPressed) { - case ConsoleKey.UpArrow: if (y > 1) y--; break; - case ConsoleKey.DownArrow: if (y < 9) y++; break; + case ConsoleKey.UpArrow: if (cursorYPos > 1) cursorYPos--; break; + case ConsoleKey.DownArrow: if (cursorYPos < 9) cursorYPos++; break; case ConsoleKey.Enter: { - switch (y) + switch (cursorYPos) { - case 1: x = 15; key = "forward"; break; - case 2: x = 16; key = "backward"; break; - case 3: x = 12; key = "left"; break; - case 4: x = 13; key = "right"; break; - case 5: x = 18; key = "swingUp"; break; - case 6: x = 20; key = "swingDown"; break; - case 7: x = 20; key = "swingLeft"; break; - case 8: x = 21; key = "swingRight"; break; + case 1: cursosXPos = 15; keyToChange = "forward"; break; + case 2: cursosXPos = 16; keyToChange = "backward"; break; + case 3: cursosXPos = 12; keyToChange = "left"; break; + case 4: cursosXPos = 13; keyToChange = "right"; break; + case 5: cursosXPos = 18; keyToChange = "swingUp"; break; + case 6: cursosXPos = 20; keyToChange = "swingDown"; break; + case 7: cursosXPos = 20; keyToChange = "swingLeft"; break; + case 8: cursosXPos = 21; keyToChange = "swingRight"; break; case 9: stop = true; break; } - if (y != 9) + if (cursorYPos != 9) { - Console.SetCursorPosition(x, y); + Console.SetCursorPosition(cursosXPos, cursorYPos); Console.CursorVisible = true; - keysDict[key] = Console.ReadKey().Key; + keyBindingsDict[keyToChange] = Console.ReadKey().Key; Console.CursorVisible = false; - Console.SetCursorPosition(0, y); + Console.SetCursorPosition(0, cursorYPos); } } break; @@ -381,7 +380,7 @@ namespace Game private static int MapSize() { int size = 0; - int y = 1; //tracks cursor position + int cursorYPos = 1; while (size == 0) { @@ -393,7 +392,7 @@ namespace Game Console.WriteLine("-Medium (10 x 20)"); Console.WriteLine("-Large (20 x 40) !FLICKER!"); - Console.SetCursorPosition(0, y); + Console.SetCursorPosition(0, cursorYPos); Console.ForegroundColor = ConsoleColor.Red; Console.Write('>'); Console.CursorVisible = false; @@ -402,11 +401,11 @@ namespace Game switch (keyPressed) { - case ConsoleKey.UpArrow: if (y > 1) y--; break; - case ConsoleKey.DownArrow: if (y < 3) y++; break; + case ConsoleKey.UpArrow: if (cursorYPos > 1) cursorYPos--; break; + case ConsoleKey.DownArrow: if (cursorYPos < 3) cursorYPos++; break; case ConsoleKey.Enter: { - switch (y) + switch (cursorYPos) { case 1: size = 5; break; case 2: size = 10; break; @@ -419,43 +418,40 @@ namespace Game return size; } - private static void MapGenerate(string[] map) + private static void GenerateMap(string[] map) { - Random rnd = new Random(); + Random randomNumber = new Random(); //map is composed of rows and every row has 1 or 2 biomes in it for (int i = 0; i < map.Length; i++) { - Biome currBiome = RandomBiome(rnd); - map[i] = new string(currBiome.Floor, map.Length * 2 - rnd.Next(map.Length + 1)); + Biome currBiome = RandomBiome(randomNumber); + map[i] = new string(currBiome.Floor, map.Length * 2 - randomNumber.Next(map.Length + 1)); - currBiome = RandomBiome(rnd); + currBiome = RandomBiome(randomNumber); map[i] = map[i] + new string(currBiome.Floor, map.Length * 2 - map[i].Length); } } - private static Biome RandomBiome(Random rnd) + private static Biome RandomBiome(Random randomNumber) { Biome currBiome = new Biome(); - if (rnd.Next(4) > 0) // 3/4 chance if the biome will hurt the mob or not + if (randomNumber.Next(4) > 0) // 3/4 chance that the biome will not hurt the mob { - //from all biomes that do no damage, choose a random one of them - currBiome = biomeDict.Where(x => x.Value.Damage == 0).ElementAt(rnd.Next(biomeDict.Count(x => x.Value.Damage == 0))).Value; + currBiome = biomeDict.Where(x => x.Value.Damage == 0).ElementAt(randomNumber.Next(biomeDict.Count(x => x.Value.Damage == 0))).Value; } else { - if (rnd.Next(5) > 0) // 4/5 chance if the biome will suffocate and hurt - { - //for all biomes that hurt and suffocate, chose a random one of them + if (randomNumber.Next(5) > 0) // 4/5 chance that the biome will suffocate and hurt + { currBiome = biomeDict.Where(x => x.Value.Damage > 0 && x.Value.Suffocate) - .ElementAt(rnd.Next(biomeDict.Count(x => x.Value.Damage > 0 && x.Value.Suffocate))).Value; + .ElementAt(randomNumber.Next(biomeDict.Count(x => x.Value.Damage > 0 && x.Value.Suffocate))).Value; } else { - //for all biomes that hurt but don't suffocate, choose a random one of them currBiome = biomeDict.Where(x => x.Value.Damage > 0 && !x.Value.Suffocate) - .ElementAt(rnd.Next(biomeDict.Count(x => x.Value.Damage > 0 && !x.Value.Suffocate))).Value; + .ElementAt(randomNumber.Next(biomeDict.Count(x => x.Value.Damage > 0 && !x.Value.Suffocate))).Value; } } @@ -476,7 +472,7 @@ namespace Game Console.ForegroundColor = defaultColor; Console.WriteLine($" Default"); - //not using a cycle, because where the console colors are situated is a bit too advanced (this game's idea is to mostly train my knowledge) + //not using a cycle, because where the console colors are situated, is a bit too advanced (this game's idea is to mostly train my knowledge) Console.ForegroundColor = ConsoleColor.Black; Console.WriteLine($" Black"); diff --git a/Bitspace/Bitspace/Weapon.cs b/Bitspace/Bitspace/Weapon.cs index a00b6b3..c5056f7 100644 --- a/Bitspace/Bitspace/Weapon.cs +++ b/Bitspace/Bitspace/Weapon.cs @@ -43,7 +43,6 @@ namespace Game { if (SwordIsSwung()) { - //takes all mobs that are in the position of the weapon and remove health/armour points foreach (var mob in mobDict.Values.Where(m => m.XPos == xPos && m.YPos == yPos)) { if (mobDict["Player"] == fightingMob) mob.FightingWithPlayer = true; @@ -63,7 +62,6 @@ namespace Game return (SwordChar(direction) != '\0') && (yPos != -1) && (xPos != -1); } - //changes the inputed xPos and yPos variables private void SwordPosition(Mob mobWithWeapon, int maxX, int maxY) { xPos = -1; yPos = -1; @@ -81,11 +79,11 @@ namespace Game { switch (direction) { - case 1: return SkinLeft; //no need for break;, return works like a break + case 1: return SkinLeft; 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) + default: return '\0'; // \0 is the char equivalent of null } } |
