-
Notifications
You must be signed in to change notification settings - Fork 10
/
Light.gd
78 lines (67 loc) · 1.79 KB
/
Light.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
# This code manages a table light.
extends Sprite
var flashes = 0
var actual_time = 0
# By default, the light sprite is somewhat dark when it's "off" and fully bright when it's "on".
# You can override this in the inspector for a given light instance.
export var off_light = Color(1.0, 1.0, 1.0, 0.3)
export var on_light = Color(1.0, 1.0, 1.0, 1.0)
# Set the sprite to it's "off" shade to begin with.
func _ready():
set_modulate(off_light)
# Switch to the "on" shade.
func switch_on():
set_modulate(on_light)
$Timer.stop()
# Switch to the "off" shade.
func switch_off():
actual_time = 0
set_modulate(off_light)
$Timer.stop()
# Flash the light and then leave it on.
func flash_on():
set_modulate(on_light)
flashes = 6
$Timer.set_one_shot(false)
$Timer.start(0.2)
# Flash the light and then turn it off.
func flash_off():
if get_modulate() == off_light:
set_modulate(on_light)
flashes = 5
else:
set_modulate(off_light)
flashes = 6
$Timer.set_one_shot(false)
$Timer.start(0.2)
# Flash the light continuously.
# The first parameter is the duration of each on/off stage.
# The second (optional) parameter is the duration of the initial flash.
func flash(flash_time = 0.3, first_time = 0.0):
set_modulate(on_light)
flashes = -1
$Timer.set_one_shot(false)
if first_time:
actual_time = flash_time
$Timer.start(first_time)
else:
$Timer.start(flash_time)
# Flash the light only once, quickly.
func flash_once():
set_modulate(on_light)
flashes = -1
$Timer.set_one_shot(true)
$Timer.start(0.15)
# The timer manages transitions between on and off.
func _on_Timer_timeout():
if get_modulate() != off_light:
set_modulate(off_light)
else:
set_modulate(on_light)
if actual_time:
$Timer.start(actual_time)
actual_time = 0
if flashes > 0:
flashes -= 1
if flashes == 0:
$Timer.stop()