-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtimer.lua
136 lines (113 loc) · 3.78 KB
/
timer.lua
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
-- timers
function make_ingame_timer(num_frames)
local self = {
frames = 0,
num_frames = num_frames,
}
local update = function()
if self.frames < self.num_frames then
self.frames += 1
end
end
local done = function()
return self.frames >= self.num_frames
end
local get_elapsed_ratio = function()
return self.frames / self.num_frames
end
return {
get_elapsed_ratio = get_elapsed_ratio,
update = update,
done = done
}
end
function make_ui_timer(on_shake, total_ticks)
local self = {
blinking = false,
blink_ticks = 0,
blink_period = 25,
real_time_ticks = nil,
shaking = false,
on_shake = on_shake,
total_ticks = total_ticks,
pos_x = 42,
pos_y = 10,
}
local get_timer_completion_ratio = function (current_tick_count)
return current_tick_count / self.total_ticks
end
local should_shake_time = function (current_tick_count)
if current_tick_count == 0 or current_tick_count == self.total_ticks then
return false
end
local shake_start = flr(self.total_ticks / 24)
local shake_duration_in_ticks = 10
return (current_tick_count % shake_start) < shake_duration_in_ticks
end
local set_blinking = function(blinking)
self.blinking = blinking
end
local hide_timer_for_blink = function ()
if not self.blinking then
return false
end
return self.blink_ticks >= self.blink_period
end
local as_datetime = function (time_in_mins)
local hrs_part = flr(time_in_mins / 60)
local fixed_point_mins_part = time_in_mins % 60
local mins_part = flr(fixed_point_mins_part)
local fixed_point_secs_part = (fixed_point_mins_part & 0x0000.ffff) * 60
local secs_part = flr(fixed_point_secs_part)
return { hrs = hrs_part, mins = mins_part, secs = secs_part }
end
local start_timer = function()
self.started = true
end
local update_timer = function (current_tick_count)
if self.blinking then
self.blink_ticks += 1
if self.blink_ticks > (self.blink_period * 1.5) then
self.blink_ticks = 0
end
end
local should_shake = should_shake_time(current_tick_count)
if should_shake and (not self.shaking) then
self.on_shake()
end
self.shaking = should_shake
end
local draw_timer = function(current_tick_count)
-- render the timer
if hide_timer_for_blink() then
return
end
local text_color = Colors.White
local text_pos_x = self.pos_x
local text_pos_y = self.pos_y
if self.shaking then
text_color = Colors.Yellow
-- add shake to timer
text_pos_x += rnd_incrange(-1, 1)
text_pos_y += rnd_incrange(-1, 1)
end
local time_elapsed_ratio = get_timer_completion_ratio(current_tick_count)
local ingame_total_minutes = (24 * 60)
local min_remaining = (1 - time_elapsed_ratio) * ingame_total_minutes
local time = as_datetime(min_remaining)
local timer_text = int_leftpad(time.hrs, 2) .. "H:" .. int_leftpad(time.mins, 2) .. "M:" .. int_leftpad(time.secs, 2) .. "S"
-- render the timer text with a gray drop shadow
print(timer_text, text_pos_x + 1, text_pos_y + 1, Colors.DarkGray)
print(timer_text, text_pos_x, text_pos_y, text_color)
end
local move_timer = function(x, y)
self.pos_x += x
self.pos_y += y
end
return {
set_blinking = set_blinking,
update = update_timer,
draw = draw_timer,
move = move_timer,
}
end