-
Notifications
You must be signed in to change notification settings - Fork 1
/
Board.gd
85 lines (70 loc) · 2.21 KB
/
Board.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
class_name Board
extends Reference
var flow_speed = 1
# Members
var pixels = []
var buffer = []
var resolution = Vector2()
func _init(resolution):
pixels = []
self.resolution = resolution
for i in range(resolution.x * resolution.y):
pixels.append(0)
func set_pixel(pos, value = 1):
if pos.x >= 0 and pos.x < resolution.x and pos.y >= 0 and pos.y < resolution.y:
pixels[conv(pos)] = min(32, pixels[conv(pos)] + value)
func get_pixel(pos):
if pos.x >= 0 and pos.x < resolution.x and pos.y >= 0 and pos.y < resolution.y:
return pixels[conv(pos)]
return 0
func clear_pixel(pos):
if pos.x > 0 and pos.x < resolution.x and pos.y > 0 and pos.y < resolution.y:
pixels[conv(pos)] = 0
func conv(pos):
return pos.y * resolution.x + pos.x
# Buffer methods
func _set_pixel_buffer(x, y, value):
#printt(x, y, value)
if 0 <= x and x < resolution.x and 0 <= y and y < resolution.y:
buffer[conv(Vector2(x, y))] += value
func _get_pixel_board_space(x, y):
if 0 <= x and x < resolution.x and 0 <= y and y < resolution.y:
return pixels[conv(Vector2(x, y))]
else:
return -1
var prev_tick = 0
func update():
var new_tick = OS.get_ticks_msec()
var delta_tick = new_tick - prev_tick
prev_tick = new_tick
print(delta_tick, "ms")
buffer = []
for p in pixels:
buffer.append(p)
for y in resolution.y:
for x in resolution.x:
var p = _get_pixel_board_space(x, y)
if p <= 0:
continue
var target_pixel_value = _get_pixel_board_space(x, y+1)
if target_pixel_value != -1 and target_pixel_value < p:
_set_pixel_buffer(x, y, -flow_speed)
_set_pixel_buffer(x, y+1, flow_speed)
else:
target_pixel_value = _get_pixel_board_space(x-1, y)
if target_pixel_value != -1 and target_pixel_value < p:
_set_pixel_buffer(x, y, -flow_speed)
_set_pixel_buffer(x-1, y, flow_speed)
else:
target_pixel_value = _get_pixel_board_space(x+1, y)
if target_pixel_value != -1 and target_pixel_value < p:
_set_pixel_buffer(x, y, -flow_speed)
_set_pixel_buffer(x+1, y, flow_speed)
pixels = buffer
func print_board():
var text = str(OS.get_ticks_msec() / 1000.0) + "\n"
for y in resolution.y:
for x in resolution.x:
text += "[" + str(get_pixel(Vector2(x, y))) + "], "
text += "\n"
print(text)