Skip to content

Commit

Permalink
added info class
Browse files Browse the repository at this point in the history
  • Loading branch information
jossse69 committed Sep 23, 2023
1 parent 137b03b commit 90fe8e2
Show file tree
Hide file tree
Showing 6 changed files with 83 additions and 17 deletions.
3 changes: 3 additions & 0 deletions src/Fighter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ internal class Fighter
public int power;
public int defense;

public bool IsDead { get; set; }
public event Action? OnDeath; // Define the OnDeath event

public Fighter(int maxHP, int HP, int power, int defense)
Expand All @@ -15,6 +16,7 @@ public Fighter(int maxHP, int HP, int power, int defense)
this.HP = HP;
this.power = power;
this.defense = defense;
this.IsDead = false;
}

public void takeDamage(int damage, int power)
Expand Down Expand Up @@ -47,6 +49,7 @@ public void takeDamage(int damage, int power)
if (HP <= 0)
{
OnDeath?.Invoke(); // Invoke the OnDeath event when HP reaches zero or below
IsDead = true;
}
}
}
Expand Down
31 changes: 27 additions & 4 deletions src/GameObject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,24 +13,35 @@ internal class GameObject

public Fighter? fighter { get; set; }

public Info info { get; set; }

private readonly ColoredGlyph DEAD = new ColoredGlyph(Color.Crimson, Color.Black, '%');

public GameObject(ColoredGlyph appearance, Point position, IScreenSurface surface, Fighter? Fighter)
public GameObject(ColoredGlyph appearance, Point position, IScreenSurface surface, Fighter? Fighter, Info Info)
{
Appearance = appearance;
Position = position;
hostingSurface = surface;
fighter = null;
fighter = Fighter;
info = Info;
// attach the OnDeath event of the fighter
fighter.OnDeath += Death;

}

private void Death()
{
System.Console.WriteLine("this thing has died!");
// check if its was dead before this call
if (fighter.IsDead)
{
System.Console.WriteLine(info.name + " is smashed to bits!");
this.Appearance = new ColoredGlyph();
return;
}

System.Console.WriteLine(info.name + " died!");
Appearance = DEAD;
info.name = info.name + " corpse";
}

public void Draw()
Expand Down Expand Up @@ -70,6 +81,11 @@ public void MoveAndAttack(int dx, int dy, Map map, List<GameObject> objects)
continue;
}

// if the traget is a corpse, move it
if (obj.fighter.IsDead == true){
continue;
}

obj.fighter?.takeDamage(2, fighter.power);
blockedbyobject = true;
break;
Expand Down Expand Up @@ -97,7 +113,14 @@ public bool IsBlocked(int x, int y, Map map, List<GameObject> objects)
{
if (obj.Position == new Point(x, y))
{
return true;
// see if the object has a fighter, if yes, see if the fighter is dead
if (obj.fighter != null)
{
return !obj.fighter.IsDead;
}
else{
return true;
}
}
}
return false;
Expand Down
20 changes: 20 additions & 0 deletions src/Info.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
namespace MIST
{
internal class Info
{
public string name;

public string description;

public monsterType type;

public Info(string name, string description, monsterType type)
{
this.name = name;
this.description = description;
this.type = type;
}

}

}
30 changes: 18 additions & 12 deletions src/Map.cs
Original file line number Diff line number Diff line change
Expand Up @@ -166,15 +166,15 @@ private void CreateMonster(Map map, Rectangle room)
var monsterY = random.Next(room.Y, room.Y + room.Height);
if (map[monsterX, monsterY].TileType == TileType.Floor)
{
var monster = new GameObject(new ColoredGlyph(Color.White, Color.Black, '!'), new Point(monsterX, monsterY), map,new Fighter(1, 1, 1, 1));
var monster = new GameObject(new ColoredGlyph(Color.White, Color.Black, '!'), new Point(monsterX, monsterY), map,new Fighter(1, 1, 1, 1), new Info("Undefined", "Undefined", monsterType.UNDEFINED));
// 20% to be an smile, alse its a spider
if (random.Next(20) == 0)
{
monster = new GameObject(new ColoredGlyph(Color.LimeGreen, Color.Black, 'S'), new Point(monsterX, monsterY), map, new Fighter(15, 15, 2, 1));
monster = new GameObject(new ColoredGlyph(Color.LimeGreen, Color.Black, 'S'), new Point(monsterX, monsterY), map, new Fighter(15, 15, 2, 1), new Info("Smile", "a mass of gelatic green goo... that is alive, it digest any food it comes across.", monsterType.smlie));
}
else
{
monster = new GameObject(new ColoredGlyph(Color.Red, Color.Black, 's'), new Point(monsterX, monsterY), map, new Fighter(5, 5, 1, 1));
monster = new GameObject(new ColoredGlyph(Color.Red, Color.Black, 's'), new Point(monsterX, monsterY), map, new Fighter(5, 5, 1, 1), new Info("Spider", "a oddly big arachnid. its black and has a big skull-shaped symbol on it's abdomen, that probably means something.", monsterType.insect));
}
success = true;
// add it to the list
Expand Down Expand Up @@ -334,18 +334,24 @@ public override bool ProcessKeyboard(Keyboard keyboard)
var player = ScreenContainer.Instance.Player;
player.MoveAndAttack(dx, dy, this, _objects);
UpdateFOV(player.Position.X, player.Position.Y, radius: 5);
Draw();

// draw objects
foreach (var obj in _objects)
Draw();

// make list of objects to draw
var drawList = new List<GameObject>();
drawList.AddRange(_objects);

// sort so that dead objects are drawn last
drawList = drawList.OrderBy(obj => obj.fighter?.IsDead).ToList();

// draw the list
for (var obj = 0; obj < drawList.Count; obj++)
{
// is lit
if (_fov.BooleanResultView[obj.Position.X, obj.Position.Y] == true)
{
obj.Draw();
}
}
// if not in FOV, don't draw
if (_fov.BooleanResultView[drawList[obj].Position.X, drawList[obj].Position.Y] == false) continue;

drawList[obj].Draw();
}
player.Draw();
_moveTimer = 5;
}
Expand Down
14 changes: 14 additions & 0 deletions src/MonsterType.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
namespace MIST
{
public enum monsterType
{
UNDEFINED,
player,
smlie,
insect,
humanoid,
human,
undead,
}

}
2 changes: 1 addition & 1 deletion src/ScreenContainer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public ScreenContainer()
Map.IsFocused = true;

// add the player
Player = new GameObject(new ColoredGlyph(Color.White, Color.Black, '@'), Map.start, Map, new Fighter(10, 10, 3, 3));
Player = new GameObject(new ColoredGlyph(Color.White, Color.Black, '@'), Map.start, Map, new Fighter(10, 10, 3, 3), new Info("Player", "It's you!", monsterType.player));
Player.Draw();


Expand Down

0 comments on commit 90fe8e2

Please sign in to comment.