-
Notifications
You must be signed in to change notification settings - Fork 1
/
GameManager.gd
129 lines (102 loc) · 4.04 KB
/
GameManager.gd
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
extends Node
var map:GridMap = null
var isRunning = false
var tick = 1.0 #secs
var tickEl = 0.0
var currentLayer = 0.0
var generation = 0
var mode = "3D"
signal on_tick
var cam_init_pos = Vector3()
func _ready():
self.map = get_tree().get_nodes_in_group("map")[0]
self.cam_init_pos = get_tree().get_nodes_in_group("camera")[0].translation
func _process(delta):
if(isRunning):
self.tickEl += delta
if(self.tickEl >= self.tick):
self.tickEl = 0.0
self.checkCurrentLayer()
if(self.mode != "2D"):
self.currentLayer += 1
self.generation += 1
emit_signal("on_tick")
func start():
self.isRunning = true
func stop():
self.isRunning = false
func clear():
self.map.clear()
self.currentLayer = 0
self.generation = 0
get_tree().get_nodes_in_group("camera")[0].translation = self.cam_init_pos
get_tree().get_nodes_in_group("camera")[0].look_at(Vector3(0,0,0), Vector3(0,1,0))
func checkCurrentLayer():
var cellsToAdd = []
if(self.mode == "2D"):
self.currentLayer = 0
for cell in self.map.get_used_cells():
if(cell.y == self.currentLayer):
var livingNeighbours = 0
var cellsAround = [
Vector3(cell.x-1, self.currentLayer, cell.z-1),
Vector3(cell.x-1, self.currentLayer, cell.z),
Vector3(cell.x-1, self.currentLayer, cell.z+1),
Vector3(cell.x, self.currentLayer, cell.z-1),
Vector3(cell.x, self.currentLayer, cell.z+1),
Vector3(cell.x+1, self.currentLayer, cell.z-1),
Vector3(cell.x+1, self.currentLayer, cell.z),
Vector3(cell.x+1, self.currentLayer, cell.z+1)
]
for cellAround in cellsAround:
if(self.map.get_cell_item(cellAround.x, cellAround.y, cellAround.z) != GridMap.INVALID_CELL_ITEM):
livingNeighbours += 1
if(livingNeighbours == 2 or livingNeighbours == 3):
#survived
cellsToAdd.append([cell.x, self.currentLayer+1, cell.z, 0])
for cellAround in cellsAround:
livingNeighbours = 0
if(self.map.get_cell_item(cellAround.x, cellAround.y, cellAround.z) == GridMap.INVALID_CELL_ITEM):
var cellsAround2 = [
Vector3(cellAround.x-1, self.currentLayer, cellAround.z-1),
Vector3(cellAround.x-1, self.currentLayer, cellAround.z),
Vector3(cellAround.x-1, self.currentLayer, cellAround.z+1),
Vector3(cellAround.x, self.currentLayer, cellAround.z-1),
Vector3(cellAround.x, self.currentLayer, cellAround.z+1),
Vector3(cellAround.x+1, self.currentLayer, cellAround.z-1),
Vector3(cellAround.x+1, self.currentLayer, cellAround.z),
Vector3(cellAround.x+1, self.currentLayer, cellAround.z+1)
]
for cellAround2 in cellsAround2:
if(self.map.get_cell_item(cellAround2.x, cellAround2.y, cellAround2.z) != GridMap.INVALID_CELL_ITEM):
livingNeighbours += 1
if(livingNeighbours == 3):
#birth
cellsToAdd.append([cellAround.x, self.currentLayer+1, cellAround.z, 1])
#if(cellsToAdd.size() > 0):
if(self.mode == "2D"):
self.map.clear()
for cellToAdd in cellsToAdd:
self.map.set_cell_item(cellToAdd[0], 0, cellToAdd[2], cellToAdd[3])
else:
for cellToAdd in cellsToAdd:
self.map.set_cell_item(cellToAdd[0], cellToAdd[1], cellToAdd[2], cellToAdd[3])
func load_pattern(pattern:Array):
self.map.clear()
var lineNum = 0
var rowNum = 0
for line in pattern:
for row in line:
if(row):
self.map.set_cell_item(lineNum, 0, rowNum, 0)
rowNum += 1
rowNum = 0
lineNum += 1
func populate_at(pos):
if(!self.isRunning):
pos.y = self.currentLayer
var map_pos = self.map.world_to_map(pos)
if(self.map.get_cell_item(map_pos.x, map_pos.y, map_pos.z) != GridMap.INVALID_CELL_ITEM):
self.map.set_cell_item(map_pos.x, map_pos.y, map_pos.z, -1)
else:
self.map.set_cell_item(map_pos.x, map_pos.y, map_pos.z, 0)