Description:
Add basic simulation. AKA Procgen, baby!
Commit status:
[Not Reviewed]
References:
Comments:
0 Commit comments 0 Inline Comments
Unresolved TODOs:
There are no unresolved TODOs
Add another comment

r28:cd33afae1ee2 -

@@ -10,6 +10,7
10 using SpriteFontPlus;
10 using SpriteFontPlus;
11 using isometricparkfna;
11 using isometricparkfna;
12 using System.Diagnostics;
12 using System.Diagnostics;
13 using static isometricparkfna.TileMap;
13
14
14 #if DEBUG
15 #if DEBUG
15 using ImGuiNET.SampleProgram.XNA;
16 using ImGuiNET.SampleProgram.XNA;
@@ -42,8 +43,8
42 private const int height = 640;
43 private const int height = 640;
43
44
44 //new tile stuff
45 //new tile stuff
45 int squaresAcross = 50;
46 int squaresAcross = 150;
46 int squaresDown = 50;
47 int squaresDown = 150;
47 int baseOffsetX = -14;
48 int baseOffsetX = -14;
48 int baseOffsetY = -14;
49 int baseOffsetY = -14;
49
50
@@ -92,7 +93,7
92 //gdm.SynchronizeWithVerticalRetrace = false;
93 //gdm.SynchronizeWithVerticalRetrace = false;
93 IsFixedTimeStep = false;
94 IsFixedTimeStep = false;
94
95
95 this.simulation = new Simulation(this.squaresAcross, this.squaresDown);
96 this.simulation = new Simulation(this.squaresAcross, this.squaresDown, 16.66667f*30);
96
97
97 foreach (List<Cell> row in this.simulation.map.cells)
98 foreach (List<Cell> row in this.simulation.map.cells)
98 {
99 {
@@ -344,7 +345,7
344 }
345 }
345
346
346
347
347
348 this.simulation.update(gameTime.ElapsedGameTime);
348
349
349 this.original_point = Vector2.Transform(new Vector2(mouseCur.X, mouseCur.Y), Matrix.Invert(camera.get_transformation(GraphicsDevice)));
350 this.original_point = Vector2.Transform(new Vector2(mouseCur.X, mouseCur.Y), Matrix.Invert(camera.get_transformation(GraphicsDevice)));
350
351
@@ -457,7 +458,7
457 SpriteEffects.None,
458 SpriteEffects.None,
458 depth);
459 depth);
459 }
460 }
460
461
461 }
462 }
462
463
463
464
@@ -1,4 +1,5
1 using System;
1 using System;
2 using static isometricparkfna.TileMap;
2
3
3 namespace isometricparkfna
4 namespace isometricparkfna
4 {
5 {
@@ -15,6 +16,13
15 private set;
16 private set;
16 }
17 }
17
18
19 public float Elapsed
20 {
21 get;
22
23 private set;
24 }
25
18 public DateTime DateTime
26 public DateTime DateTime
19 {
27 {
20 get;
28 get;
@@ -24,21 +32,80
24
32
25 public decimal money;
33 public decimal money;
26
34
35 public float millisecondsPerAdvance { get; private set; }
36
27 public TileMap map;
37 public TileMap map;
28
38
29 public Simulation(int width, int height)
39 public int ticksPerAdvance;
40
41 private float lastAdvance;
42
43 public Simulation(int width, int height, float millisecondsPerAdvance)
30 {
44 {
31 this.DateTime = new DateTime(START_YEAR, START_MONTH, START_DAY);
45 this.DateTime = new DateTime(START_YEAR, START_MONTH, START_DAY);
32
46
33 this.map = new TileMap(width, height);
47 this.map = new TileMap(width, height);
34 this.money = 1000;
48 this.money = 1000;
49 this.millisecondsPerAdvance = millisecondsPerAdvance;
35 }
50 }
36
51
37 public void advanceClock()
52 private void advanceSimulation()
38 {
53 {
39 this.Tick++;
54 Random random = new Random();
55 this.DateTime = this.DateTime.AddMonths(1);
56
57 foreach (Cell cell in this.map.iterate_cells())
58 {
59 if (random.NextDouble() > 0.9995f)
60 {
61 cell.hasTree = true;
62 }
63 }
64
65 foreach (Cell cell in this.map.iterate_cells_with_neighbors(4))
66 {
67 if (random.NextDouble() > 0.995f)
68 {
69 cell.hasTree = true;
70 }
71 }
40
72
41 this.DateTime.AddMonths(1);
73 foreach (Cell cell in this.map.iterate_cells_with_neighbors(7))
74 {
75 if (random.NextDouble() > 0.995f)
76 {
77 cell.hasTree = false;
78 }
79 }
80 }
81 public void update(TimeSpan deltaTime)
82 {
83 //this.Tick++;
84
85 this.Elapsed += deltaTime.Milliseconds;
86
87 int advancesToSimulate = (int)((this.Elapsed - this.lastAdvance) / this.millisecondsPerAdvance);
88
89 for (int i = 0; i < advancesToSimulate; i++)
90 {
91 this.advanceSimulation();
92
93 //Partial frames haven't been simulated so they're not counted as part of
94 //lastAdvance
95 //Example:
96 //We start at t=100 and simulate every 10 t. If we miss enough updates that
97 //it's t=125, we have 2.5 steps to simulate. However, we only want to simulate
98 //whole steps, so that means we'll simulate 2. But that means we've only simulated
99 //through t=120, so that's what we want to track in lastAdvance.
100 this.lastAdvance += advancesToSimulate * this.millisecondsPerAdvance;
101 }
102
103 /*
104 if ((this.Tick % this.millisecondsPerAdvance) == 0)
105 {
106 this.DateTime = this.DateTime.AddMonths(1);
107 }*/
108
42
109
43 }
110 }
44 }
111 }
@@ -90,16 +90,84
90 {
90 {
91 foreach (Cell cell in row)
91 foreach (Cell cell in row)
92 {
92 {
93 yield return cell;
93 yield return cell;
94 }
94 }
95 }
95 }
96 }
96 }
97
97
98
98 public Boolean inBounds(int x, int y)
99 }
99 {
100 return MathUtils.Between(x, 0, MapWidth - 1) && MathUtils.Between(y, 0, MapHeight - 1);
101 }
100
102
101 public class Cell
103 private System.Collections.IEnumerable iterate_neighbors(int x, int y)
102 {
104 {
103 public Boolean hasTree = false;
105 //iterates over neighbors (clockwise starting at noon/midnight)
106 if (inBounds(x, y + 1))
107 {
108 yield return this.cells[x - 1][y];
109 }
110 if (inBounds(x + 1, y + 1))
111 {
112 yield return this.cells[x + 1][y + 1];
113 }
114 if (inBounds(x + 1, y))
115 {
116 yield return this.cells[x + 1][y];
117 }
118 if (inBounds(x + 1, y - 1))
119 {
120 yield return this.cells[x + 1][y - 1];
121 }
122 if (inBounds(x, y - 1))
123 {
124 yield return this.cells[x][y - 1];
125 }
126 if (inBounds(x - 1, y-1))
127 {
128 yield return this.cells[x - 1][y-1];
129 }
130 if (inBounds(x - 1, y))
131 {
132 yield return this.cells[x - 1][y];
133 }
134 if (inBounds(x - 1, y + 1))
135 {
136 yield return this.cells[x - 1][y+1];
137 }
138 }
139
140 private int countNeighbors(int x, int y)
141 {
142 int count = 0;
143 foreach (Cell neighbor in this.iterate_neighbors(x, y))
144 {
145 count++;
146 }
147
148 return count;
149 }
150
151 public System.Collections.IEnumerable iterate_cells_with_neighbors(int neighbors)
152 {
153 for (int i = 0; i < MapHeight; i++)
154 {
155 List<Cell> newRow = new List<Cell>();
156 for (int j = 0; j < MapWidth; j++)
157 {
158 if (this.countNeighbors(i, j) >= neighbors)
159 {
160 yield return cells[i][j];
161 }
162 }
163 }
164
165
166 }
167
168 public class Cell
169 {
170 public Boolean hasTree = false;
171 }
104 }
172 }
105 }
173 } No newline at end of file
You need to be logged in to leave comments. Login now