Skip to content
This repository was archived by the owner on Jan 5, 2024. It is now read-only.

Commit b287aa6

Browse files
authored
Merge pull request #166 from cortex-command-community/secrets-and-speedruns
Secrets and speedruns
2 parents da77f5b + e5a8cb4 commit b287aa6

File tree

6 files changed

+482
-71
lines changed

6 files changed

+482
-71
lines changed
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
--[[
2+
How To Use the Activity Speedrun Helper:
3+
1. In your Activity's Create function, call ActivitySpeedrunHelper.Setup. The first argument should be self, the second should be the function you want to run when then player enters speedrun mode (see the SignalHunt Activity for an example).
4+
This will return speedrun data, which you should keep, since you'll need to use it elsewhere.
5+
2. In your Activity's Update function, call ActivitySpeedrunHelper.CheckForSpeedrun, until the speedrun has begun. The only argument is the speedrun data which was returned by Setup.
6+
This will return true if the speedrun has begun or nothing went wrong, or false if something went wrong. If it returns false, you should delete your speedrun data, since it will no longer work.
7+
3. When you want to mark a speedrun as completed, call ActivitySpeedrunHelper.CompleteSpeedrun. The only argument is the speedrun data which was returned by Setup.
8+
9+
To check whether a run is active, call ActivitySpeedrunHelper.SpeedrunActive. The only argument is the speedrun data which was returned by Setup.
10+
To check whether a run is completed, call ActivitySpeedrunHelper.SpeedrunCompleted. The only argument is the speedrun data which was returned by Setup.
11+
To get duration of a run (active or completed), formatted in seconds and milliseconds, call ActivitySpeedrunHelper.GetSpeedrunDuration. The only argument is the speedrun data which was returned by Setup.
12+
--]]
13+
14+
require("Scripts/Shared/SecretCodeEntry");
15+
16+
ActivitySpeedrunHelper = {};
17+
18+
function ActivitySpeedrunHelper.Setup(activityLuaObject, activitySpeedrunInitializationCallbackFunction)
19+
if activityLuaObject.HumanCount == 1 then
20+
local speedrunData = {};
21+
22+
speedrunData.activitySpeedrunInitializationCallbackFunction = activitySpeedrunInitializationCallbackFunction;
23+
speedrunData.activityLuaObject = activityLuaObject;
24+
speedrunData.secretCodeEntryDataIndex = SecretCodeEntry.Setup(ActivitySpeedrunHelper.InitializeSpeedrun, speedrunData, 1);
25+
26+
speedrunData.speedrunActive = false;
27+
speedrunData.speedrunTimer = Timer();
28+
speedrunData.speedrunCompletionTime = -1;
29+
30+
return speedrunData;
31+
end
32+
end
33+
34+
function ActivitySpeedrunHelper:CheckForSpeedrun()
35+
if self.speedrunActive then
36+
return true;
37+
end
38+
39+
if not SecretCodeEntry.IsValid(self.secretCodeEntryDataIndex) then
40+
return false;
41+
end
42+
43+
SecretCodeEntry.Update(self.secretCodeEntryDataIndex);
44+
45+
return true;
46+
end
47+
48+
function ActivitySpeedrunHelper:InitializeSpeedrun()
49+
self.speedrunActive = true;
50+
if self.activitySpeedrunInitializationCallbackFunction ~= nil then
51+
self.activitySpeedrunInitializationCallbackFunction(self.activityLuaObject);
52+
end
53+
end
54+
55+
function ActivitySpeedrunHelper:CompleteSpeedrun()
56+
self.speedrunCompletionTime = self.speedrunTimer.ElapsedRealTimeMS;
57+
end
58+
59+
function ActivitySpeedrunHelper:SpeedrunActive()
60+
return self.speedrunActive;
61+
end
62+
63+
function ActivitySpeedrunHelper:SpeedrunCompleted()
64+
return self.speedrunCompletionTime > -1;
65+
end
66+
67+
function ActivitySpeedrunHelper:GetSpeedrunDuration()
68+
if self.speedrunCompletionTime == -1 then
69+
return "Speedrun Duration: " .. RoundFloatToPrecision(self.speedrunTimer.ElapsedRealTimeMS / 1000, 3, 1) .. " seconds";
70+
else
71+
return "Speedrun Completed In: " .. RoundFloatToPrecision(self.speedrunCompletionTime / 1000, 3, 1) .. " seconds";
72+
end
73+
end

0 commit comments

Comments
 (0)