1local ns = {}
2setmetatable(ns, {__index = _G})
3battle_with_dark_soldiers_script = ns;
4setfenv(1, ns);
5
6local Battle = nil
7local Script = nil
8local DialogueManager = nil
9local start_timer = nil
10
11local dialogue1_done = false;
12
13local time_left_header_text = nil
14local time_left_text = nil
15
16local header_color = vt_video.Color(1.0, 1.0, 1.0, 1.0);
17local text_color = vt_video.Color(1.0, 1.0, 1.0, 1.0);
18-- Makes the time glow when low
19local pulse_green_blue = 0.4;
20local pulse_up = false;
21
22function Initialize(battle_instance)
23    Battle = battle_instance;
24    Script = Battle:GetScriptSupervisor();
25
26    local text = nil
27    local dialogue = nil
28
29    DialogueManager = Battle:GetDialogueSupervisor();
30
31    -- Add all speakers for the dialogues to be added
32    DialogueManager:AddSpeaker("Kalya", vt_system.Translate("Kalya"), "data/entities/portraits/kalya.png");
33    DialogueManager:AddSpeaker("Soldier", vt_system.Translate("Soldier"), "data/entities/portraits/npcs/dark_soldier.png");
34
35    dialogue = vt_common.Dialogue.Create(DialogueManager, "The dark soldier tells them to surrender");
36    text = vt_system.Translate("I found you! My comrades will be here in no time. Surrender now, or you'll be punished!");
37    dialogue:AddLine(text, "Soldier");
38    text = vt_system.Translate("Don't listen to him! We must get rid of him before reinforcements arrive or we're doomed!");
39    dialogue:AddLine(text, "Kalya");
40
41    -- Construct a timer so we can start the dialogue a couple seconds after the battle begins
42    start_timer = vt_system.SystemTimer(100, 0);
43
44    -- 3 minutes before spawning more soldiers...
45    timer_before_soldiers = vt_system.SystemTimer(180000, 0);
46    timer_before_soldiers:EnableManualUpdate();
47    time_left_text = Script:CreateText("3:00", vt_video.TextStyle("text22"));
48    time_left_header_text = Script:CreateText(vt_system.Translate("Time left:"), vt_video.TextStyle("text22"));
49
50    dialogue1_done = false;
51end
52
53function Restart()
54    dialogue1_done = false;
55
56    start_timer:Reset();
57
58    timer_before_soldiers:Reset();
59    pulse_green_blue = 0.4;
60    pulse_up = false;
61    text_color:SetColor(1.0, 1.0, 1.0, 1.0);
62end
63
64
65function Update()
66
67    start_timer:Update();
68
69    -- get time expired
70    local time_expired = SystemManager:GetUpdateTime();
71
72    -- Wait until the initial battle sequence ends to begin running the dialogue start timer
73    if ((start_timer:IsInitial() == true) and (Battle:GetState() ~= vt_battle.BattleMode.BATTLE_STATE_INITIAL)) then
74        start_timer:Run();
75    end
76
77    -- If battle is won or lost, don't update the script
78    if (Battle:GetState() == vt_battle.BattleMode.BATTLE_STATE_VICTORY
79        or Battle:GetState() == vt_battle.BattleMode.BATTLE_STATE_DEFEAT
80        or Battle:GetState() == vt_battle.BattleMode.BATTLE_STATE_EXITING) then
81        return;
82    end
83
84    -- Wait for the battle to start
85    if (start_timer:IsFinished() == false) then
86        return;
87    elseif (timer_before_soldiers:IsRunning() == false) then
88        timer_before_soldiers:Run();
89    end
90
91    -- If the dialogue has not been seen yet, check if its time to start it
92    if (DialogueManager:IsDialogueActive() == true) then
93        return;
94    end
95
96    Battle:SetSceneMode(false);
97
98    if (dialogue1_done == false) then
99        DialogueManager:StartDialogue("The dark soldier tells them to surrender");
100        Battle:SetSceneMode(true);
101        dialogue1_done = true;
102    end
103
104    -- Update the timer text
105    _UpdateTimer();
106
107end
108
109function _UpdateTimer()
110    -- The timer is manually updated
111    timer_before_soldiers:Update();
112
113    -- Compute the text
114    local time_left = timer_before_soldiers:TimeLeft();
115    local minutes_text = math.floor(time_left / 60000);
116    local seconds_text = math.fmod(time_left, 60000);
117    seconds_text = math.floor(seconds_text / 1000); -- Keep only the two first digits.
118
119    if (seconds_text < 10) then
120        time_left_text:SetText(minutes_text .. ":0" .. seconds_text);
121    else
122        time_left_text:SetText(minutes_text .. ":" .. seconds_text);
123    end
124
125    -- Update the color according to the time left
126    if (minutes_text > 0) then
127        return;
128    end
129    -- Becomes yellow between :59 and :31
130    text_color:SetColor(1.0, 1.0, 0.4, 1.0);
131    if (minutes_text > 0 or seconds_text > 30) then
132        return;
133    end
134
135    -- Becomes red between :30 and :16
136    text_color:SetColor(1.0, 0.4, 0.4, 1.0);
137    if (minutes_text > 0 or seconds_text > 15) then
138        return;
139    end
140
141    -- Red pulse until 0:00
142    local time_expired = SystemManager:GetUpdateTime();
143
144    if (pulse_up == true) then
145        pulse_green_blue = pulse_green_blue + 0.0025 * time_expired;
146    elseif (pulse_up == false) then
147        pulse_green_blue = pulse_green_blue - 0.0025 * time_expired;
148    end
149
150    if (pulse_green_blue >= 0.4) then
151        pulse_up = false;
152    elseif (pulse_green_blue <= 0.0) then
153        pulse_up = true;
154    end
155
156    text_color:SetColor(1.0, pulse_green_blue, pulse_green_blue, 1.0);
157
158    if (minutes_text > 0 or seconds_text > 0) then
159        return;
160    end
161
162    -- Reset timer in the end, but add two soldiers:
163    Battle:AddEnemy(9);
164    Battle:AddEnemy(9);
165    timer_before_soldiers:Reset();
166    timer_before_soldiers:Run();
167end
168
169function DrawPostEffects()
170    if (dialogue1_done == true) then
171        -- Draw timer
172        VideoManager:Move(80.0, 80.0);
173        time_left_header_text:Draw(header_color);
174        VideoManager:Move(100.0, 100.0);
175        time_left_text:Draw(text_color);
176    end
177end
178