1
+ SecretCodeEntry = {};
2
+ -- TODO obfuscate the secret codes
3
+ SecretCodeEntry .codes = {
4
+ };
5
+ SecretCodeEntry .allowedControlStates = { Controller .PRESS_PRIMARY , Controller .PRESS_SECONDARY , Controller .PRESS_RIGHT , Controller .PRESS_LEFT , Controller .PRESS_UP , Controller .PRESS_DOWN };
6
+ SecretCodeEntry .defaultFirstEntrySoundContainer = CreateSoundContainer (" Funds Changed" , " Base.rte" );
7
+ SecretCodeEntry .defaultFirstEntrySoundContainer .Volume = 4 ;
8
+ SecretCodeEntry .defaultCorrectEntrySoundContainer = CreateSoundContainer (" Funds Changed" , " Base.rte" );
9
+ SecretCodeEntry .defaultCorrectEntrySoundContainer .Volume = 2 ;
10
+ SecretCodeEntry .defaultIncorrectEntrySoundContainer = CreateSoundContainer (" Error" , " Base.rte" );
11
+ SecretCodeEntry .defaultCodeCompletionSoundContainer = CreateSoundContainer (" Funds Changed" , " Base.rte" );
12
+ SecretCodeEntry .defaultCodeCompletionSoundContainer .Volume = 4 ;
13
+ SecretCodeEntry .data = {};
14
+
15
+ function SecretCodeEntry .Setup (callbackFunction , callbackSelfObject , codeSequenceOrCodeType , firstEntrySoundContainer , correctEntrySoundContainer , incorrectEntrySoundContainer , codeCompletionSoundContainer )
16
+ local activity = ActivityMan :GetActivity ();
17
+ if activity == nil then
18
+ print (" Secret Code Entry Error: You need to start an Activity before setting up code entry!" );
19
+ return ;
20
+ end
21
+
22
+ if callbackFunction == nil then
23
+ print (" Secret Code Entry Error: You need to specify the callback function to run upon successful code entry!" );
24
+ return ;
25
+ end
26
+
27
+ if codeSequenceOrCodeType == nil then
28
+ print (" Secret Code Entry Error: You need to specify a code sequence or code type!" );
29
+ return ;
30
+ end
31
+
32
+ if type (codeSequenceOrCodeType ) == " number" then
33
+ if codeSequenceOrCodeType < 1 or codeSequenceOrCodeType > # SecretCodeEntry .codes then
34
+ print (" Secret Code Entry Error: Code type must be between 1 and " .. tostring (# SecretCodeEntry .codes ));
35
+ return ;
36
+ end
37
+ codeSequenceOrCodeType = SecretCodeEntry .codes [codeSequenceOrCodeType ];
38
+ elseif type (codeSequenceOrCodeType ) == " table" then
39
+ for _ , codeSequenceControlState in pairs (codeSequenceOrCodeType ) do
40
+ local isAllowedControlState = false ;
41
+ for _ , allowedControlState in pairs (SecretCodeEntry .allowedControlStates ) do
42
+ if codeSequenceControlState == allowedControlState then
43
+ isAllowedControlState = true ;
44
+ break ;
45
+ end
46
+ end
47
+
48
+ if not isAllowedControlState then
49
+ print (" Secret Code Entry Error: Only the following Controller control states are supported: " .. table.concat (SecretCodeEntry .allowedControlStates , " , " ));
50
+ end
51
+ end
52
+ end
53
+
54
+ local secretCodeEntryDataTable = {}
55
+ secretCodeEntryDataTable .callbackFunction = callbackFunction ;
56
+ secretCodeEntryDataTable .callbackSelfObject = callbackSelfObject ;
57
+ secretCodeEntryDataTable .codeSequence = codeSequenceOrCodeType ;
58
+ secretCodeEntryDataTable .firstEntrySoundContainer = firstEntrySoundContainer or SecretCodeEntry .defaultFirstEntrySoundContainer ;
59
+ secretCodeEntryDataTable .correctEntrySoundContainer = correctEntrySoundContainer or SecretCodeEntry .defaultCorrectEntrySoundContainer ;
60
+ secretCodeEntryDataTable .incorrectEntrySoundContainer = incorrectEntrySoundContainer or SecretCodeEntry .defaultIncorrectEntrySoundContainer ;
61
+ secretCodeEntryDataTable .codeCompletionSoundContainer = codeCompletionSoundContainer or SecretCodeEntry .defaultCodeCompletionSoundContainer ;
62
+
63
+ secretCodeEntryDataTable .inputs = {};
64
+ for player = Activity .PLAYER_1 , Activity .MAXPLAYERCOUNT - 1 do
65
+ if activity :PlayerActive (player ) and activity :PlayerHuman (player ) then
66
+ secretCodeEntryDataTable .inputs [player ] = { controller = ActivityMan :GetActivity ():GetPlayerController (player ), currentStep = 0 };
67
+ end
68
+ end
69
+
70
+ SecretCodeEntry .data [# SecretCodeEntry .data + 1 ] = secretCodeEntryDataTable ;
71
+
72
+ return # SecretCodeEntry .data ;
73
+ end
74
+
75
+ function SecretCodeEntry .IsValid (secretCodeEntryDataIndex )
76
+ return secretCodeEntryDataIndex ~= nil and secretCodeEntryDataIndex > 0 and secretCodeEntryDataIndex <= # SecretCodeEntry .data ;
77
+ end
78
+
79
+ function SecretCodeEntry .Update (secretCodeEntryDataIndex )
80
+ if secretCodeEntryDataIndex == nil or secretCodeEntryDataIndex < 0 or secretCodeEntryDataIndex > # SecretCodeEntry .data then
81
+ print (" Secret Code Entry Error: The secret code entry index must be between 1 and " .. tostring (# SecretCodeEntry .data ) .. " . This index was returned by SecretCodeEntry.Setup." );
82
+ return ;
83
+ end
84
+
85
+ local secretCodeEntryData = SecretCodeEntry .data [secretCodeEntryDataIndex ];
86
+ local playersWhoCompletedCode = {};
87
+
88
+ if UInputMan :AnyPress () then
89
+ for player , inputData in pairs (secretCodeEntryData .inputs ) do
90
+ local expectedNextControlState = secretCodeEntryData .codeSequence [inputData .currentStep + 1 ];
91
+
92
+ local correctInputPressed = inputData .controller :IsState (expectedNextControlState );
93
+ local incorrectInputPressed = false ;
94
+ if inputData .currentStep > 0 then
95
+ for _ , controlState in ipairs (SecretCodeEntry .allowedControlStates ) do
96
+ if inputData .controller :IsState (controlState ) and controlState ~= expectedNextControlState then
97
+ incorrectInputPressed = true ;
98
+ break ;
99
+ end
100
+ end
101
+ end
102
+
103
+ local soundToPlay ;
104
+ if incorrectInputPressed then
105
+ soundToPlay = secretCodeEntryData .incorrectEntrySoundContainer ;
106
+ inputData .currentStep = 0 ;
107
+ elseif correctInputPressed then
108
+ soundToPlay = inputData .currentStep == 0 and secretCodeEntryData .firstEntrySoundContainer or (inputData .currentStep + 1 == # secretCodeEntryData .codeSequence and secretCodeEntryData .codeCompletionSoundContainer or secretCodeEntryData .correctEntrySoundContainer );
109
+ inputData .currentStep = inputData .currentStep + 1 ;
110
+ end
111
+ if soundToPlay then
112
+ soundToPlay :Play (CameraMan :GetScrollTarget (inputData .controller .Player ), inputData .controller .Player );
113
+ end
114
+
115
+ if inputData .currentStep == # secretCodeEntryData .codeSequence then
116
+ secretCodeEntryData .inputs [player ] = nil ;
117
+ playersWhoCompletedCode [# playersWhoCompletedCode + 1 ] = player ;
118
+ end
119
+ end
120
+ end
121
+
122
+ if # playersWhoCompletedCode > 0 then
123
+ secretCodeEntryData .callbackFunction (secretCodeEntryData .callbackSelfObject , playersWhoCompletedCode );
124
+ end
125
+ end
0 commit comments