aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSyndamia <kami02882@gmail.com>2019-05-01 18:26:15 +0300
committerSyndamia <kami02882@gmail.com>2019-05-01 18:26:15 +0300
commit5b6c70a242c37f142dee8642fde3303c7a82c34d (patch)
treef4f2d3c4fadcef7df4f292f1821c7f872e6cd685
parentfabfbff9133934a4fdacf3154107a139bbc85ab8 (diff)
downloadShower-5b6c70a242c37f142dee8642fde3303c7a82c34d.tar
Shower-5b6c70a242c37f142dee8642fde3303c7a82c34d.tar.gz
Shower-5b6c70a242c37f142dee8642fde3303c7a82c34d.zip
Certain items can now be placed and destroyed (not really, the system must be fixed), for now mobs can't walk over the placeable items, placed items and future ambient items are and will be stored in an overlay map (that is printed on top of the normal map)
-rw-r--r--Bitspace/Bitspace/Program.cs100
-rw-r--r--Bitspace/Bitspace/Weapon.cs93
2 files changed, 136 insertions, 57 deletions
diff --git a/Bitspace/Bitspace/Program.cs b/Bitspace/Bitspace/Program.cs
index 75891ca..bdc8612 100644
--- a/Bitspace/Bitspace/Program.cs
+++ b/Bitspace/Bitspace/Program.cs
@@ -10,6 +10,7 @@ namespace Game
class Program
{
private static string[] map;
+ private static StringBuilder[] objectMap;
private static Dictionary<string, Biome> biomeDict = new Dictionary<string, Biome>
{
@@ -29,8 +30,8 @@ namespace Game
private static Dictionary<string, Weapon> itemDict = new Dictionary<string, Weapon>()
{
- { "Stick", new Weapon("Stick", 5, '-', '|', 1) },
- { "Sling", new Weapon("Sling", 10, 'Y',ConsoleColor.Yellow, 112) }
+ { "Stick", new Weapon("Stick", 3, 5, 1, '-', '|', false, 1) },
+ { "Wall", new Weapon("Wall", 10, 3, 0, 'i', ConsoleColor.Yellow, true, 20) }
};
private static Dictionary<string, ConsoleKey> keyBindingsDict = new Dictionary<string, ConsoleKey>()
@@ -48,10 +49,11 @@ namespace Game
//TODO: better weapon adding to mobs
mobDict["Player"].ItemsList.Add(itemDict["Stick"]);
- mobDict["Player"].ItemsList.Add(itemDict["Sling"]);
+ mobDict["Player"].ItemsList.Add(itemDict["Wall"]);
mobDict["Player"].SelectedItem = 1;
map = new string[s];
+ objectMap = new StringBuilder[s];
GenerateMap(map);
ulong gameTick = 0; //this is so that stuff like taking damage, drowning, ... is a bit slower than the almost instant game refresh
@@ -60,19 +62,20 @@ namespace Game
if (Console.KeyAvailable)
{
var keyPressed = Console.ReadKey().Key;
+ int playerYPos = mobDict["Player"].YPos, playerXPos = mobDict["Player"].XPos;
//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 == keyBindingsDict["forward"])
- { if (mobDict["Player"].YPos > 0) mobDict["Player"].YPos--; }
+ { if (playerYPos > 0 && objectMap[playerYPos - 1][playerXPos] == '\0') mobDict["Player"].YPos--; }
else if (keyPressed == keyBindingsDict["backward"])
- { if (mobDict["Player"].YPos < map.Length - 1) mobDict["Player"].YPos++; }
+ { if (playerYPos < map.Length - 1 && objectMap[playerYPos + 1][playerXPos] == '\0') mobDict["Player"].YPos++; }
else if (keyPressed == keyBindingsDict["left"])
- { if (mobDict["Player"].XPos > 0) mobDict["Player"].XPos--; }
+ { if (playerXPos > 0 && objectMap[playerYPos][playerXPos - 1] == '\0') mobDict["Player"].XPos--; }
else if (keyPressed == keyBindingsDict["right"])
- { if (mobDict["Player"].XPos < map.Length * 2 - 1) mobDict["Player"].XPos++; }
+ { if (playerXPos < map.Length * 2 - 1 && objectMap[playerYPos][playerXPos + 1] == '\0') mobDict["Player"].XPos++; }
else if (keyPressed == keyBindingsDict["itemsLeft"])
{ if (mobDict["Player"].SelectedItem > 1) mobDict["Player"].SelectedItem--; }
@@ -109,10 +112,11 @@ namespace Game
if (gameTick % 60 == 0)
{
- foreach (var mob in mobDict.Values.Where(m => m.FightingWithPlayer && m.Alive))
- {
- mob.FightingWithPlayer = false;
- }
+ foreach (var mob in mobDict.Values.Where(m => m.FightingWithPlayer))
+ { mob.FightingWithPlayer = false; }
+
+ foreach (var breakable in itemDict.Values.Where(b => b.BrokenByPlayer))
+ { breakable.BrokenByPlayer = false; }
}
ReWriteScreen(map);
@@ -171,10 +175,10 @@ namespace Game
switch (movementChooser)
{
- 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 1: if (mob.YPos > 0 && objectMap[mob.YPos - 1][mob.XPos] == '\0') mob.YPos--; break;
+ case 2: if (mob.YPos < map.Length - 1 && objectMap[mob.YPos + 1][mob.XPos] == '\0') mob.YPos++; break;
+ case 3: if (mob.XPos > 0 && objectMap[mob.YPos][mob.XPos - 1] == '\0') mob.XPos--; break;
+ case 4: if (mob.XPos < map.Length * 2 - 1 && objectMap[mob.YPos][mob.XPos + 1] == '\0') mob.XPos++; break;
}
}
}
@@ -202,33 +206,68 @@ namespace Game
{
Console.Clear();
- for (int i = 0; i < map.Length; i++)
+ for (int row = 0; row < map.Length; row++)
{
//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
- Console.ForegroundColor = biomeDict.Values.ToList().Find(x => x.Floor == map[i].First()).Color;
- Console.Write(map[i].Substring(0, map[i].LastIndexOf(map[i].First()) + 1));
+ Console.ForegroundColor = biomeDict.Values.First(x => x.Floor == map[row].First()).Color;
+ Console.Write(map[row].Substring(0, map[row].LastIndexOf(map[row].First()) + 1));
- Console.ForegroundColor = biomeDict.Values.ToList().Find(x => x.Floor == map[i].Last()).Color;
- Console.WriteLine(map[i].TrimStart(map[i].First()));
+ Console.ForegroundColor = biomeDict.Values.First(x => x.Floor == map[row].Last()).Color;
+ Console.Write(map[row].TrimStart(map[row].First()));
+
+ for (int col = 0; col < objectMap.Length * 2; col++)
+ {
+ Console.SetCursorPosition(col, row);
+
+ if (objectMap[row][col] != '\0')
+ {
+ try
+ {
+ if (itemDict.Values.First(x => x.XPos == col && x.YPos == row).Health <= 0) objectMap[row][col] = '\0';
+ else throw new Exception();
+ }
+ catch (Exception)
+ {
+ Console.ForegroundColor = itemDict.Values.First(x => x.SkinUp == objectMap[row][col]).Color;
+ Console.Write(objectMap[row][col]);
+ };
+ }
+ }
+ Console.WriteLine();
foreach (var mob in mobDict.Values.Where(m => m.Alive == true))
{
//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();
+ if (mob.YPos == row) 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, also for less cursor "jumping"
- if (mob.ItemsList.Count > 0 && (i == mob.YPos + 1 || i == map.Length - 1))
+ if (mob.ItemsList.Count > 0 && (row == mob.YPos + 1 || row == map.Length - 1))
{
try
{
- var weapon = mob.ItemsList[mob.SelectedItem - 1];
+ var item = mob.ItemsList[mob.SelectedItem - 1];
+
+ if (!item.Placable) item.PrintAndDoDamage(mob, mobDict, itemDict, map.Length * 2, map.Length);
+
+ else if (item.Direction != null)
+ {
+ item.WeaponPosition(mob, map.Length * 2, map.Length);
+
+ if (objectMap[item.YPos][item.XPos] == '\0')
+ {
+ objectMap[item.YPos][item.XPos] = item.SkinUp;
+ item.Amount--;
- weapon.PrintAndDoDamage(mob, mobDict, map.Length * 2, map.Length);
- weapon.Direction = null;
- } catch (Exception) { }
+ if (item.Amount == 0) mob.ItemsList.Remove(item);
+ }
+ }
+
+ item.Direction = null;
+ }
+ catch (Exception) { }
}
}
- Console.SetCursorPosition(0, i + 1);
+ Console.SetCursorPosition(0, row + 1);
}
Console.SetCursorPosition(0, map.Length + 1); //sets cursor at the very end
@@ -241,6 +280,11 @@ namespace Game
Console.WriteLine(mob.Stats());
}
+ foreach(var breakable in itemDict.Values.Where(b => b.BrokenByPlayer))
+ {
+ Console.WriteLine(breakable.Stats());
+ }
+
Console.CursorVisible = false;
}
@@ -460,6 +504,8 @@ namespace Game
currBiome = RandomBiome(randomNumber);
map[i] = map[i] + new string(currBiome.Floor, map.Length * 2 - map[i].Length);
+
+ objectMap[i] = new StringBuilder(new string('\0', objectMap.Length * 2));
}
}
diff --git a/Bitspace/Bitspace/Weapon.cs b/Bitspace/Bitspace/Weapon.cs
index c5c6353..a1ab064 100644
--- a/Bitspace/Bitspace/Weapon.cs
+++ b/Bitspace/Bitspace/Weapon.cs
@@ -12,7 +12,11 @@ namespace Game
private int yPos;
private string name;
- private ushort damage;
+ private int health;
+
+ private ushort healthDamage;
+ private ushort breakDamage;
+ public bool BrokenByPlayer { get; set; }
private ConsoleColor color;
private string direction;
@@ -22,58 +26,79 @@ namespace Game
private char skinUp;
private char skinDown;
+ private bool placable;
private byte amount;
- public void PrintAndDoDamage(Mob fightingMob, Dictionary<string, Mob> mobDict, int maxX, int maxY)
+ public void PrintAndDoDamage(Mob fightingMob, Dictionary<string, Mob> mobDict, Dictionary<string, Weapon> itemsDict, int maxX, int maxY)
{
- SwordPosition(fightingMob, maxX, maxY);
- Print(maxX, maxY);
- DoDamage(mobDict, fightingMob);
+ Print(fightingMob, maxX, maxY);
+ DoHealthDamage(mobDict, fightingMob);
+ DoBreakDamage(itemsDict, fightingMob);
}
- private void Print(int maxX, int maxY)
+ private void Print(Mob fightingMob, int maxX, int maxY)
{
- if (SwordIsSwung())
+ WeaponPosition(fightingMob, maxX, maxY);
+
+ if (WeaponIsSwung())
{
Console.ForegroundColor = Color;
- Console.SetCursorPosition(xPos, yPos);
+ Console.SetCursorPosition(XPos, YPos);
Console.Write(SwordChar());
}
}
- private void DoDamage(Dictionary<string, Mob> mobDict, Mob fightingMob)
+ public string Stats()
{
- if (SwordIsSwung())
+ return $"{Name} | Health: {Health} | X: {XPos} | Y: {YPos}";
+ }
+
+ private void DoHealthDamage(Dictionary<string, Mob> mobDict, Mob fightingMob)
+ {
+ if (WeaponIsSwung())
{
- foreach (var mob in mobDict.Values.Where(m => m.XPos == xPos && m.YPos == yPos))
+ foreach (var mob in mobDict.Values.Where(m => m.XPos == XPos && m.YPos == YPos))
{
- if (mobDict["Player"] == fightingMob) mob.FightingWithPlayer = true;
+ if (fightingMob.Name == "Player") mob.FightingWithPlayer = true;
- for (ushort d = Damage; d > 0 && mob.Health > 0; d--)
+ for (ushort d = HealthDamage; d > 0 && mob.Health > 0; d--)
{
- if (Damage <= mob.Armour / 4) { } //does nothing, all damage that is below a fourth of the armour is nullified
- else if (mob.Armour > 0 && d >= Damage * 2 / 3) mob.Armour--;
+ if (HealthDamage <= mob.Armour / 4) { } //does nothing, all damage that is below a fourth of the armour is nullified
+ else if (mob.Armour > 0 && d >= HealthDamage * 2 / 3) mob.Armour--;
else mob.Health--;
}
}
}
}
- private bool SwordIsSwung()
+ private void DoBreakDamage(Dictionary<string, Weapon> itemsDict, Mob fightingMob)
+ {
+ if (WeaponIsSwung())
+ {
+ foreach (var obj in itemsDict.Values.Where(o => (o.XPos == XPos && o.YPos == YPos) && o.Health > 0 && o != fightingMob.ItemsList[fightingMob.SelectedItem - 1]))
+ {
+ if (fightingMob.Name == "Player") obj.BrokenByPlayer = true;
+
+ obj.Health -= breakDamage;
+ }
+ }
+ }
+
+ private bool WeaponIsSwung()
{
- return (SwordChar() != '\0') && (yPos != -1) && (xPos != -1);
+ return (SwordChar() != '\0') && (YPos != -1) && (XPos != -1);
}
- private void SwordPosition(Mob mobWithWeapon, int maxX, int maxY)
+ public void WeaponPosition(Mob mobWithWeapon, int maxX, int maxY)
{
- xPos = -1; yPos = -1;
+ XPos = -1; YPos = -1;
switch (Direction)
{
- case "left": if (mobWithWeapon.XPos > 0) xPos = mobWithWeapon.XPos - 1; yPos = mobWithWeapon.YPos; break;
- case "right": if (mobWithWeapon.XPos < maxX - 1) xPos = mobWithWeapon.XPos + 1; yPos = mobWithWeapon.YPos; break;
- case "up": if (mobWithWeapon.YPos > 0) yPos = mobWithWeapon.YPos - 1; xPos = mobWithWeapon.XPos; break;
- case "down": if (mobWithWeapon.YPos < maxY - 1) yPos = mobWithWeapon.YPos + 1; xPos = mobWithWeapon.XPos; break;
+ case "left": if (mobWithWeapon.XPos > 0) XPos = mobWithWeapon.XPos - 1; YPos = mobWithWeapon.YPos; break;
+ case "right": if (mobWithWeapon.XPos < maxX - 1) XPos = mobWithWeapon.XPos + 1; YPos = mobWithWeapon.YPos; break;
+ case "up": if (mobWithWeapon.YPos > 0) YPos = mobWithWeapon.YPos - 1; XPos = mobWithWeapon.XPos; break;
+ case "down": if (mobWithWeapon.YPos < maxY - 1) YPos = mobWithWeapon.YPos + 1; XPos = mobWithWeapon.XPos; break;
}
}
@@ -89,21 +114,23 @@ namespace Game
}
}
- public Weapon(string name, ushort damage, char tetraChar, byte amount) : this(name, damage, tetraChar, tetraChar, tetraChar, tetraChar, ConsoleColor.Gray, amount)
+ public Weapon(string name, int health, ushort healthDamage, ushort breakDamage, char tetraChar, bool placeable, byte amount) : this(name, health, healthDamage, breakDamage, tetraChar, tetraChar, tetraChar, tetraChar, ConsoleColor.Gray, placeable, amount)
{ }
- public Weapon(string name, ushort damage, char tetraChar, ConsoleColor color, byte amount) : this(name, damage, tetraChar, tetraChar, tetraChar, tetraChar, color, amount)
+ public Weapon(string name, int health, ushort healthDamage, ushort breakDamage, char tetraChar, ConsoleColor color, bool placeable, byte amount) : this(name, health, healthDamage, breakDamage, tetraChar, tetraChar, tetraChar, tetraChar, color, placeable, amount)
{ }
- public Weapon(string name, ushort damage, char leftRight, char upDown, byte amount) : this(name, damage, leftRight, leftRight, upDown, upDown, ConsoleColor.Gray, amount)
+ public Weapon(string name, int health, ushort healthDamage, ushort breakDamage, char leftRight, char upDown, bool placeable, byte amount) : this(name, health, healthDamage, breakDamage, leftRight, leftRight, upDown, upDown, ConsoleColor.Gray, placeable, amount)
{ }
- public Weapon(string name, ushort damage, char leftRight, char upDown, ConsoleColor color, byte amount) : this(name, damage, leftRight, leftRight, upDown, upDown, color, amount)
+ public Weapon(string name, int health, ushort healthDamage, ushort breakDamage, char leftRight, char upDown, ConsoleColor color, bool placeable, byte amount) : this(name, health, healthDamage, breakDamage, leftRight, leftRight, upDown, upDown, color, placeable, amount)
{ }
- public Weapon(string name, ushort damage, char skinLeft, char skinRight, char skinUp, char skinDown, ConsoleColor color, byte amount)
+ public Weapon(string name, int health, ushort healthDamage, ushort breakDamage, char skinLeft, char skinRight, char skinUp, char skinDown, ConsoleColor color, bool placable ,byte amount)
{
- Damage = damage;
+ Health = health;
+ HealthDamage = healthDamage;
+ BreakDamage = breakDamage;
SkinLeft = skinLeft;
SkinRight = skinRight;
SkinUp = skinUp;
@@ -111,9 +138,10 @@ namespace Game
Name = name;
Color = color;
Amount = amount;
+ Placable = placable;
}
- public ushort Damage { get => damage; set => damage = value; }
+ public ushort HealthDamage { get => healthDamage; set => healthDamage = 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; }
@@ -122,5 +150,10 @@ namespace Game
public string Name { get => name; set => name = value; }
public ConsoleColor Color { get => color; set => color = value; }
public byte Amount { get => amount; set => amount = value; }
+ public bool Placable { get => placable; set => placable = value; }
+ public int XPos { get => xPos; private set => xPos = value; }
+ public int YPos { get => yPos; private set => yPos = value; }
+ public int Health { get => health; set => health = value; }
+ public ushort BreakDamage { get => breakDamage; set => breakDamage = value; }
}
}