-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathRpgExample.cs
76 lines (70 loc) · 2.95 KB
/
RpgExample.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
// <copyright file="RpgExample.cs" company="Chris Muller">
// Copyright (c) Chris Muller. All rights reserved.
// </copyright>
namespace Examples {
using System.Numerics;
using MountainGoap;
using MountainGoapLogging;
/// <summary>
/// RPG example demo.
/// </summary>
internal static class RpgExample {
/// <summary>
/// Maximum X value of the world grid.
/// </summary>
internal static readonly int MaxX = 20;
/// <summary>
/// Maximum Y value of the world grid.
/// </summary>
internal static readonly int MaxY = 20;
/// <summary>
/// Runs the demo.
/// </summary>
internal static void Run() {
_ = new DefaultLogger(logToConsole: false, loggingFile: "rpg-example.log");
Random random = new();
List<Agent> agents = new();
List<Vector2> foodPositions = new();
var player = RpgCharacterFactory.Create(agents);
player.State["faction"] = "player";
agents.Add(player);
for (int i = 0; i < 20; i++) foodPositions.Add(new Vector2(random.Next(0, MaxX), random.Next(0, MaxY)));
for (int i = 0; i < 10; i++) {
var monster = RpgMonsterFactory.Create(agents, foodPositions);
monster.State["position"] = new Vector2(random.Next(0, MaxX), random.Next(0, MaxY));
agents.Add(monster);
}
for (int i = 0; i < 600; i++) {
foreach (var agent in agents) agent.Step(mode: StepMode.OneAction);
ProcessDeaths(agents);
PrintGrid(agents, foodPositions);
Thread.Sleep(200);
}
}
private static void PrintGrid(List<Agent> agents, List<Vector2> foodPositions) {
Console.SetCursorPosition(0, 0);
string[,] grid = new string[MaxX, MaxY];
for (int x = 0; x < MaxX; x++) {
for (int y = 0; y < MaxY; y++) {
grid[x, y] = " ";
}
}
foreach (var position in foodPositions) grid[(int)position.X, (int)position.Y] = "f";
agents.ForEach((agent) => {
if (agent.State["position"] is Vector2 position && agent.State["faction"] is string faction) {
if (faction == "player") grid[(int)position.X, (int)position.Y] = "@";
else grid[(int)position.X, (int)position.Y] = "g";
}
});
for (int x = 0; x < MaxX; x++) {
for (int y = 0; y < MaxY; y++) Console.Write(grid[x, y]);
Console.WriteLine();
}
}
private static void ProcessDeaths(List<Agent> agents) {
List<Agent> cullList = new();
foreach (var agent in agents) if (agent.State["hp"] is int hp && hp <= 0) cullList.Add(agent);
foreach (var agent in cullList) agents.Remove(agent);
}
}
}