-
Notifications
You must be signed in to change notification settings - Fork 1
/
game.gd
141 lines (105 loc) · 2.48 KB
/
game.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
130
131
132
133
134
135
136
137
138
139
140
141
extends MultiMeshInstance
class Tile:
var v : bool
var life : int
var others : Array
func _init():
self.v = false
self.life = 0
self.others = Array()
func get_total() -> int:
var result : int = 0
for other in others:
result += other.get_value()
return result
func get_value():
if v:
return 1
return 0
class Map:
var data : Array
var next : Array
var empty : Tile
var w : int
var h : int
func _init(width : int, height : int):
self.empty = Tile.new()
self.w = width
self.h = height
data = Array()
next = Array()
for i in self.w * self.h:
data.append(Tile.new())
next.append(false)
var i = 0
for y in self.h:
for x in self.w:
var t = data[i]
t.others = [
at(x - 1, y - 1), at(x, y - 1), at(x + 1, y - 1),
at(x - 1, y), at(x + 1, y),
at(x - 1, y + 1), at(x, y + 1), at(x + 1, y + 1)]
i += 1
func get_index(x, y):
if x < 0 || x >= w || y < 0 || y >= h:
return -1
return x + y * w
func put(x, y, v):
var index = get_index(x, y)
if index == -1:
return
data[index].v = v
func at(x, y):
var index = get_index(x, y)
if index == -1:
return empty
return data[index]
func process():
for i in w * h:
var t = data[i]
var v = t.get_total()
if data[i].v:
next[i] = v == 2 || v == 3
else:
next[i] = v == 3
for i in w * h:
data[i].v = next[i]
if data[i].v:
data[i].life = 20
data[i].life = max(0, data[i].life - 1)
var map : Map
var started
func _ready():
started = OS.get_system_time_msecs()
map = Map.new(50, 50)
self.multimesh.instance_count = map.w * map.h
var l = int(sqrt(self.multimesh.instance_count))
var index : int = 0
for y in l:
for x in l:
self.multimesh.set_instance_transform(index, Transform(Basis(), Vector3(x, randi() % 10 * 0.1, y)))
self.multimesh.set_instance_color(index, Color(255, x, y))
index += 1
var counter : int
var frame : int
func _process(_delta):
frame += 1
if frame == 1000:
var elapsed = OS.get_system_time_msecs() - started
print("Result: ", elapsed)
get_tree().quit()
return
counter += 1
if counter == 10:
map.put(25, 25, true)
map.put(24, 25, true)
map.put(26, 25, true)
map.put(25, 24, true)
counter = 0
map.process()
for i in self.multimesh.instance_count:
var tile = map.data[i]
if tile.v:
self.multimesh.set_instance_color(i, Color(1.0, 0, 0))
else:
self.multimesh.set_instance_color(i, Color(0, tile.life / 50.0, 0))