Debian Codegraph @

CorsixTH/Lua/dialogs/watch.lua

raw
1 --[[ Copyright (c) 2009 Peter "Corsix" Cawley
2
3 Permission is hereby granted, free of charge, to any person obtaining a copy of
4 this software and associated documentation files (the "Software"), to deal in
5 the Software without restriction, including without limitation the rights to
6 use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
7 of the Software, and to permit persons to whom the Software is furnished to do
8 so, subject to the following conditions:
9
10 The above copyright notice and this permission notice shall be included in all
11 copies or substantial portions of the Software.
12
13 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
19 SOFTWARE. --]]
20
21 --! Timer / clock / watch / countdown dialog for emergencies / level start
22 --! The timer lasts approximately 100 days, split into 13 segments
23 class "UIWatch" (Window)
24
25 ---@type UIWatch
26 local UIWatch = _G["UIWatch"]
27
28 local TICK_DAYS = 100
29 local TICK_DAYS_EMERGENCY = 52
30 local TIMER_SEGMENTS = 13
31
32 --!param count_type (string) One of: "open_countdown" or "emergency" or "epidemic"
33 function UIWatch:UIWatch(ui, count_type)
34 self:Window()
35
36 local app = ui.app
37
38 self.esc_closes = false
39 self.modal_class = "open_countdown"
40 if count_type == "emergency" then
41 self.tick_rate = math.floor((TICK_DAYS_EMERGENCY * Date.hoursPerDay()) / TIMER_SEGMENTS)
42 self.tick_timer = self.tick_rate
43 else
44 self.tick_rate = math.floor((TICK_DAYS * Date.hoursPerDay()) / TIMER_SEGMENTS)
45 self.tick_timer = self.tick_rate -- Initialize tick timer
46 end
47 self.open_timer = 12
48 self.ui = ui
49 self.hospital = ui.hospital
50 self.width = 39
51 self.height = 79
52 self:setDefaultPosition(20, -100)
53 self.panel_sprites = app.gfx:loadSpriteTable("Data", "Watch01V", true)
54 self.epidemic = false
55 self.count_type = count_type
56 self.active_hover = false
57 -- For cycling the list of epidemic/emergency patients which index to use
58 self.current_index = nil
59 -- The last patient whose dialog was opened by clicking the timer
60 self.lastCycledPatient = nil
61
62 local end_sprite = (count_type == "epidemic") and 14 or 16
63 self.end_sprite = end_sprite
64
65 local tooltips = {
66 ["initial_opening"] = _S.tooltip.watch.hospital_opening,
67 ["emergency"] = _S.tooltip.watch.emergency,
68 ["epidemic"] = _S.tooltip.watch.epidemic,
69 }
70
71 if count_type == "epidemic" then
72 self.end_button = self:addPanel(end_sprite, 4, 0)
73 :makeButton(4, 0, 27, 28, end_sprite + 1, self.toggleVaccinationMode)
74 :setTooltip(tooltips[count_type])
75
76 self:addPanel(end_sprite, 4, 0)
77 .custom_draw = --[[persistable:epidemic_timer_button]] function(panel, canvas, x, y)
78 local s = TheApp.config.ui_scale
79 x = x + panel.x * s
80 y = y + panel.y * s
81 panel.window.panel_sprites:draw(canvas, panel.sprite_index, x, y, { scaleFactor = s })
82 if self.active_hover then
83 self.panel_sprites:draw(canvas, 15, x, y, { scaleFactor = s })
84 end
85 end
86
87 elseif count_type ~= "emergency" then
88 self.end_button = self:addPanel(end_sprite, 4, 0)
89 :makeButton(4, 0, 27, 28, end_sprite + 1, self.onCountdownEnd)
90 :setTooltip(tooltips[count_type])
91 end
92
93 local timer_sprite = 13
94 if count_type == "epidemic" or count_type == "emergency" then
95 self:addPanel(timer_sprite, 0, 28)
96 :setTooltip(tooltips[count_type])
97 :makeButton(timer_sprite, 0, 25, 50, timer_sprite,
98 self.scrollToTimerEventPatient, nil, self.cycleTimerEventPatient)
99 else
100 self:addPanel(timer_sprite, 0, 28):setTooltip(tooltips[count_type])
101 .custom_draw = --[[persistable:open_hospital_timer_button]] function(panel, canvas, x, y)
102 local s = TheApp.config.ui_scale
103 x = x + panel.x * s
104 y = y + panel.y * s
105 panel.window.panel_sprites:draw(canvas, panel.sprite_index, x, y, { scaleFactor = s })
106 if self.active_hover then
107 self.panel_sprites:draw(canvas, 17, x + 4 * s, y - 28 * s, { scaleFactor = s })
108 end
109 end
110 end
111 self:addPanel(1, 2, 47)
112 end
113
114 function UIWatch:onCountdownEnd()
115 self:close()
116 if self.count_type == "emergency" then
117 self.ui.hospital:resolveEmergency()
118 elseif self.count_type == "epidemic" then
119 local epidemic = self.hospital.epidemic
120 if epidemic then
121 epidemic:coverUpTimeIsUp()
122 end
123 elseif self.count_type == "initial_opening" then
124 self.hospital:open()
125 self.ui:playSound("fanfare.wav")
126 end
127 end
128
129 function UIWatch:onMouseMove(x, y, dx, dy)
130 local s = TheApp.config.ui_scale
131 self.active_hover = self:hoverTest(self.active_hover, x, y, 4 * s, 31 * s, 0, 29 * s)
132 return Window:onMouseMove(x, y, dx, dy)
133 end
134
135 function UIWatch:onWorldTick()
136 if self.tick_timer == 0 and self.open_timer >= 0 then -- Used for making a smooth animation
137 self.tick_timer = self.tick_rate
138 self.open_timer = self.open_timer - 1
139 if self.open_timer == 11 then
140 self:addPanel(2, 2, 47, nil, nil, true, DrawFlags.Nearest)
141 elseif self.open_timer == 0 then
142 self.panels[#self.panels].sprite_index = 0
143 elseif self.open_timer < 11 and self.open_timer > 0 then
144 self.panels[#self.panels].sprite_index = 13 - self.open_timer
145 if self.open_timer == 5 then
146 table.remove(self.panels, #self.panels - 1)
147 end
148 end
149 elseif self.open_timer == -1 then -- the timer is at 0 when it is completely red.
150 self:onCountdownEnd() -- Countdown terminated, so we open the hospital or ends the epidemic panic
151 else
152 self.tick_timer = self.tick_timer - 1
153 end
154 end
155
156 --[[! Toggles vaccination mode by toggling the button then
157 toggling the mode in the current epidemic.]]
158 function UIWatch:toggleVaccinationMode()
159 local epidemic = self.hospital.epidemic
160 self.end_button:toggle()
161 epidemic:toggleVaccinationMode()
162 end
163
164 --[[! During an emergency - Cycles through the patient dialogs of all the emergency patients
165 ! During an epidemic - Cycles to the first patient who is infected but not vaccinated]]
166 function UIWatch:cycleTimerEventPatient()
167 self.ui:playSound("camclick.wav")
168 local hospital = self.ui.hospital
169
170 if self.count_type == "emergency" then
171 local patients = hospital.emergency_patients
172
173 if #patients > 0 then
174 if not self.current_index or self.current_index == #patients then
175 self.current_index = 1
176 else
177 self.current_index = self.current_index + 1
178 end
179 self.lastCycledPatient = patients[self.current_index]
180 self.ui:addWindow(UIPatient(self.ui, self.lastCycledPatient))
181 end
182 else
183 for _, infected_patient in ipairs(hospital.epidemic.infected_patients) do
184 if not infected_patient.vaccinated and not infected_patient.cured then
185 self.lastCycledPatient = infected_patient
186 self.ui:addWindow(UIPatient(self.ui, self.lastCycledPatient))
187 break
188 end
189 end
190 end
191 end
192
193 --[[! While cycling through timer event patients (@see
194 UIWatch:cycleTimerEventPatient) scrolls the screen to centre on the selected patient
195 If a patient dialog is open that does not belong to the timer event, does nothing]]
196 function UIWatch:scrollToTimerEventPatient()
197 self.ui:playSound("camclick.wav")
198 local patient = self.lastCycledPatient
199 if patient then
200 -- Current open dialog
201 local current_patient_dialog = self.ui:getWindow(UIPatient)
202 if not current_patient_dialog then
203 -- Create the dialog but don't add it to the window
204 local patient_dialog = UIPatient(self.ui, patient)
205 patient_dialog:scrollToPatient()
206 elseif patient == current_patient_dialog.patient then
207 current_patient_dialog:scrollToPatient()
208 end
209 end
210 end
211
212 function UIWatch:afterLoad(old, new)
213 Window.afterLoad(self, old, new)
214 if old < 237 then
215 local pnl = self.panels[#self.panels]
216 if pnl.sprite_index >= 2 and pnl.sprite_index < 13 then
217 pnl.draw_flags = DrawFlags.Nearest
218 end
219 end
220 end