-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGameOfLife.cs
More file actions
177 lines (151 loc) · 4.37 KB
/
GameOfLife.cs
File metadata and controls
177 lines (151 loc) · 4.37 KB
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class GameOfLife : MonoBehaviour
{
private static int SCREEN_WIDTH = 64; // 1024 - pixels
private static int SCREEN_HEIGHT = 48; // 768 - pixels
public float speed = 0.1f;
private float timer = 0;
Cell[,] grid = new Cell[SCREEN_WIDTH, SCREEN_HEIGHT];
void Start()
{
PlaceCells();
}
void Update()
{
float simSpeed = PlayerPrefs.GetFloat("SimulationSpeed");
if(timer >= simSpeed)
{
timer = 0;
CountNeighbors();
PopulationControl();
}
else
{
timer += Time.deltaTime;
}
}
void PlaceCells()
{
for (int y = 0; y < SCREEN_HEIGHT; y++)
{
for (int x = 0; x < SCREEN_WIDTH; x++)
{
Cell cell = Instantiate(Resources.Load("Prefabs/Cell", typeof(Cell)), new Vector2(x, y), Quaternion.identity) as Cell;
grid[x, y] = cell;
grid[x, y].SetAlive(RandomAliveCell());
}
}
}
void CountNeighbors()
{
for(int y = 0; y < SCREEN_HEIGHT; y++)
{
for(int x = 0; x < SCREEN_WIDTH; x++)
{
int numNeighbors = 0;
// North
if(y+1 < SCREEN_HEIGHT)
{
if (grid[x, y+1].isAlive)
{
numNeighbors++;
}
}
// East
if(x+1 < SCREEN_WIDTH)
{
if (grid[x+1, y].isAlive)
{
numNeighbors++;
}
}
// South
if(y-1 >= 0)
{
if (grid[x, y - 1].isAlive)
{
numNeighbors++;
}
}
// West
if(x-1 >= 0)
{
if (grid[x-1, y].isAlive)
{
numNeighbors++;
}
}
// North-East
if(x+1 < SCREEN_WIDTH && y+1 < SCREEN_HEIGHT)
{
if (grid[x+1, y+1].isAlive)
{
numNeighbors++;
}
}
// North-West
if(x-1 >= 0 && y+1 < SCREEN_HEIGHT)
{
if (grid[x-1, y+1].isAlive)
{
numNeighbors++;
}
}
// South-East
if(x+1 < SCREEN_WIDTH && y-1 >= 0)
{
if (grid[x+1, y-1].isAlive)
{
numNeighbors++;
}
}
// South-West
if(x-1 >= 0 && y-1 >= 0)
{
if (grid[x-1, y-1].isAlive)
{
numNeighbors++;
}
}
grid[x, y].neighbors = numNeighbors;
}
}
}
void PopulationControl()
{
for(int y = 0; y < SCREEN_HEIGHT; y++)
{
for(int x = 0; x < SCREEN_WIDTH; x++)
{
if (grid[x, y].isAlive)
{
// Cell is alive
if (grid[x, y].neighbors != 2 && grid[x, y].neighbors != 3)
{
grid[x, y].SetAlive(false);
}
}
else
{
// Cell is dead
if (grid[x, y].neighbors == 3)
{
grid[x, y].SetAlive(true);
}
}
}
}
}
bool RandomAliveCell()
{
float s_percentage = PlayerPrefs.GetFloat("SpawnPercentage");
int rand = UnityEngine.Random.Range(0, 100);
if(rand <= s_percentage)
{
return true;
}
return false;
}
}