-
Notifications
You must be signed in to change notification settings - Fork 1
/
GridFog.gd
65 lines (55 loc) · 1.63 KB
/
GridFog.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
extends Node
var shown = {}
var max_illumination = 45
var tilemap = null
var timer = null
func _init(tiles, size):
tilemap = tiles
tiles.clear()
var topleft = tiles.world_to_map(size.position)
var bottomright = tiles.world_to_map(size.end)
for x in range(topleft.x, bottomright.x):
for y in range(topleft.y, bottomright.y):
tiles.set_cell(x, y, 0)
timer = Timer.new()
add_child(timer)
timer.connect("timeout", self, "periodic")
timer.set_wait_time(0.2)
timer.start()
set_physics_process(true)
func sum(arr):
var s = 0
for v in arr:
s += v
return s
func periodic():
for k in shown.keys():
for i in range(len(shown[k])):
shown[k][i] -= 1
if shown[k][i] < 0:
shown[k][i] = 0
if shown[k][0] + shown[k][1] == 0:
shown.erase(k)
func _physics_process(delta):
for k in shown.keys():
var total_illumination = sum(shown[k])
tilemap.set_cell(int(k.split(",")[0]), int(k.split(",")[1]), int(round((float(total_illumination)/max_illumination)*10)) )
func _ready():
pass
func illuminateTiles(center, radius, torch_index):
# index: 0 = player torch
# index: 1 = terrain torch
if tilemap != null && radius > 0:
for x in range(-radius, radius + 1):
for y in range(-radius, radius + 1):
var d2 = x * x + y * y
var r2 = radius * radius
if (d2 <= r2):
var X = int(round(center.x / 32 + x))
var Y = int(round(center.y / 32 + y))
var i = str(X) + "," + str(Y)
if ! (i in shown):
shown[i] = [0, 0]
shown[i][torch_index] = max_illumination - (d2/r2)*15
var total_illumination = sum(shown[i])
# tilemap.set_cell(X, Y, int(round((float(total_illumination)/max_illumination)*10)))