Skip to content

Commit

Permalink
added more mobs
Browse files Browse the repository at this point in the history
  • Loading branch information
jossse69 committed Aug 16, 2023
1 parent 85a0ae1 commit 898ef70
Show file tree
Hide file tree
Showing 8 changed files with 146 additions and 18 deletions.
30 changes: 30 additions & 0 deletions ColorConverter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using SFML.Graphics;

public class ColorConverter : JsonConverter<Color>
{
public override Color ReadJson(JsonReader reader, Type objectType, Color existingValue, bool hasExistingValue, JsonSerializer serializer)
{
var token = JToken.Load(reader);
var array = (JArray)token;

if (array.Count != 3)
throw new JsonSerializationException("Invalid color format. RGB values must be provided as an array of three integers.");

var r = array[0].Value<byte>();
var g = array[1].Value<byte>();
var b = array[2].Value<byte>();

return new Color(r, g, b);
}

public override void WriteJson(JsonWriter writer, Color value, JsonSerializer serializer)
{
writer.WriteStartArray();
writer.WriteValue(value.R);
writer.WriteValue(value.G);
writer.WriteValue(value.B);
writer.WriteEndArray();
}
}
4 changes: 2 additions & 2 deletions Entity.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public class Entity
public Color OutlineColor { get; set; }
public float OutlineThickness { get; set; }

private readonly Grid<char> grid;
public Grid<char> grid;
private SchedulingSystem schedulingSystem;
public Entity(Grid<char> grid, char symbol, Vector2i position, Color fillColor, Color backgroundColor, Color outlineColor, float outlineThickness = 0, Map? map = null)
{
Expand All @@ -26,7 +26,7 @@ public Entity(Grid<char> grid, char symbol, Vector2i position, Color fillColor,
OutlineThickness = outlineThickness;
}

public void draw()
public virtual void draw()
{
grid.SetCell(Position.X, Position.Y, Symbol, FillColor, BackgroundColor, OutlineColor, OutlineThickness);
}
Expand Down
1 change: 1 addition & 0 deletions LilRogue.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
<PackageReference Include="roguesharp" Version="4.2.0" />
<PackageReference Include="SFML.net" Version="2.5.1" />
</ItemGroup>
Expand Down
15 changes: 11 additions & 4 deletions Player.cs
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ public void tryToMove(int x, int y, Map map, List<Mob> mobs)
}
else
{
return;

}
}
}
Expand Down Expand Up @@ -182,9 +182,6 @@ public void Update()
if (HP <= 0)
{
isDead = true;
this.Symbol = '&';
this.BackgroundColor = Color.Black;
this.FillColor = Color.Red;
//wait a bit
deathwindowTimer.Start();
if (deathwindowTimer.ElapsedMilliseconds >= 1000)
Expand All @@ -194,5 +191,15 @@ public void Update()
}
}

public override void draw()
{
//if dead
if (HP <= 0)
{
return;
}

grid.SetCell(Position.X, Position.Y, Symbol, FillColor, BackgroundColor, OutlineColor, OutlineThickness);
}
}
}
28 changes: 25 additions & 3 deletions Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using SFML.Window;
using RogueSharp;
using RogueSharp.MapCreation;
using Newtonsoft.Json;

namespace LilRogue
{
Expand All @@ -21,6 +22,7 @@ static void Main(string[] args)

var window = new RenderWindow(new VideoMode((uint)screenWidth, (uint)screenHeight, 32), "Lil Rogue");

var RNG = new Random();
//set limit FPS
window.SetFramerateLimit(60);

Expand All @@ -44,12 +46,28 @@ static void Main(string[] args)
//mob array
var Mobs = new List<Mob>();

// add mobs
for (int i = 0; i < 2; i++)
string monstersjson = File.ReadAllText("data/monsters.json");
var settings = new JsonSerializerSettings();
settings.Converters.Add(new ColorConverter());
var monsters = JsonConvert.DeserializeObject<List<Mob>>(monstersjson, settings);

for (int i = 0; i < 5; i++)
{
var mob = new Mob(grid, 'M', FindWalkableCell(gameMap), Color.Black, Color.Red, Color.Black, 35 , 1);
//get a random monster
var monster = monsters[RNG.Next(0, monsters.Count)];
var pos = FindWalkableCell(gameMap);
var color = monster.color;
var BackgroundColor = monster.BackgroundColor;

Mob mob = new Mob(grid, monster.Symbol, pos, ConvertColor(BackgroundColor), ConvertColor(color), Color.Black, monster.HP, 0, monster.Type, monster.speed, monster.attack, monster.armor, schedulingSystem, gameMap);
Mobs.Add(mob);
}







var upStairs = new Entity(grid, '<', new Vector2i(upStairsPosition.X, upStairsPosition.Y), Color.Black, Color.Yellow, Color.White, 1, gameMap);
var downStairs = new Entity(grid, '>', new Vector2i(downStairsPosition.X, downStairsPosition.Y), Color.Black, Color.Yellow, Color.White, 1, gameMap);
Expand Down Expand Up @@ -145,6 +163,10 @@ static Vector2i FindWalkableCell(Map gameMap)
}
}
}
static Color ConvertColor(int[] ints)
{
return new Color((byte)ints[0], (byte)ints[1], (byte)ints[2]);
}
}
}
}
Binary file modified bin/Debug/net7.0/LilRogue.dll
Binary file not shown.
44 changes: 44 additions & 0 deletions data/monsters.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
[
{
"id": 0,
"name": "Rat",
"description": "A small, brown, rat. its eyes are red, and its appearance is is ugly. also, it is very fast and wants to kill you.",
"HP": 25,
"speed": 3,
"armor": 1,
"attack": 1,
"symbol": "r",
"color": [
153,
77,
51
],
"backgroundColor": [
0,
0,
0
],
"type": "animal"
},
{
"id": 1,
"name": "Zombie",
"description": "A undead (by some force) human body. unstoppable rage and hunger for human flesh is visible in its yellowish eyes. its skin is pale green and full of cuts and bruises.",
"HP": 50,
"speed": 2,
"armor": 2,
"attack": 2,
"symbol": "Z",
"color": [
100,
155,
100
],
"backgroundColor": [
0,
0,
0
],
"type": "undead"
}
]
42 changes: 33 additions & 9 deletions mob.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,32 @@ public class Mob : Entity
public int HP { get; set; }
public int MaxHP { get; set; }
public int armor { get; set; }
public int speed { get; set; }

public int attack { get; set; }

public char Symbol { get; set; }
public int[] color { get; set; }
public Color TrueColor { get; set; }
public int[] BackgroundColor { get; set; }
public Color TrueBackgroundColor { get; set; }
public string Type { get; set; }
public Grid<char> grid1 { get; set; }
public Mob(Grid<char> grid, char symbol, Vector2i position, Color fillColor, Color backgroundColor, Color outlineColor, int maxHP = 100, float outlineThickness = 0)
public Mob(Grid<char> grid, char symbol, Vector2i position, Color fillColor, Color backgroundColor, Color outlineColor, int maxHP = 100, float outlineThickness = 0, string type = "null", int Speed = 1, int Attack = 0, int Armor = 0, SchedulingSystem schedulingSystem = null, Map map = null)
: base(grid, symbol, position, fillColor, backgroundColor, outlineColor, outlineThickness)
{
MaxHP = maxHP;
HP = MaxHP;
grid1 = grid;
armor = Armor;
speed = Speed;
attack = Attack;
Type = type;
Symbol = symbol;
color = new int[] { fillColor.R, fillColor.G, fillColor.B };
TrueColor = new Color((byte)color[0], (byte)color[1], (byte)color[2]);
BackgroundColor = new int[] { backgroundColor.R, backgroundColor.G, backgroundColor.B };
TrueBackgroundColor = new Color((byte)BackgroundColor[0], (byte)BackgroundColor[1], (byte)BackgroundColor[2]);
}

public Vector2i GoToLocation(int x, int y, Map map)
Expand Down Expand Up @@ -58,10 +77,6 @@ public void Update(Map map, Player player, SchedulingSystem schedulingSystem, Li
var lastSeenPlayerPosition = new Vector2i(-1, -1);
if (HP <= 0 || HP > MaxHP)
{
// set char to a bloody red "&"
this.Symbol = '&';
this.BackgroundColor = Color.Black;
this.FillColor = Color.Red;

return;
}
Expand All @@ -79,7 +94,7 @@ public void Update(Map map, Player player, SchedulingSystem schedulingSystem, Li

if (state == "chase"){

for (int i = 0; i < schedulingSystem.calculateTimeSteps(schedulingSystem.time, schedulingSystem.time + 1, 2); i++)
for (int i = 0; i < schedulingSystem.calculateTimeSteps(schedulingSystem.time, schedulingSystem.time + 6, speed * 2); i++)
{
if (CanbeSeen(map))
{
Expand All @@ -91,8 +106,7 @@ public void Update(Map map, Player player, SchedulingSystem schedulingSystem, Li
foreach (var mob in mobs)
if (ToGoalPosition.X == player.Position.X && ToGoalPosition.Y == player.Position.Y)
{

player.TakeDamage(4.25);
player.TakeDamage(attack);
break;
}
else if (ToGoalPosition.X == mob.Position.X && ToGoalPosition.Y == mob.Position.Y) //blocked by another mob
Expand All @@ -119,7 +133,7 @@ public void Update(Map map, Player player, SchedulingSystem schedulingSystem, Li
wanderpos = map.GetRandomCell();
}

for (int i = 0; i < schedulingSystem.calculateTimeSteps(schedulingSystem.time, schedulingSystem.time + 1, 1); i++)
for (int i = 0; i < schedulingSystem.calculateTimeSteps(schedulingSystem.time, schedulingSystem.time + 6, speed); i++)
{
ToGoalPosition = GoToLocation(wanderpos.X, wanderpos.Y, map);

Expand Down Expand Up @@ -152,6 +166,16 @@ public void TakeDamage(double damage)
this.HP -= (int) Math.Round(Math.Clamp(finalDMG * 1.25, 0, 999999), 1);

}
public override void draw()
{
//if dead
if (HP <= 0)
{
return;
}

grid1.SetCell(Position.X, Position.Y, Symbol, TrueColor, TrueBackgroundColor, Color.Black, 1);
}

}
}

0 comments on commit 898ef70

Please sign in to comment.