-
Notifications
You must be signed in to change notification settings - Fork 1
/
game_menu.as
143 lines (122 loc) · 4.59 KB
/
game_menu.as
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
137
138
139
140
141
142
143
/* game_menu - Custom entity for making a customisable text menu
Installation:-
- Place in scripts/maps
- Add
map_script game_menu
to your map cfg
OR
- Add
#include "game_menu"
to your main map script header
OR
- Create a trigger_script with these keys set in your map:
"classname" "trigger_script"
"m_iszScriptFile" "game_menu"
Usage:-
Triggering the game_menu entity will open the menu up to the player who activated this menu.
Keys follow this format:
"n:<option name>" "<targetname to trigger>"
n is the option number. All of the options in a menu page will be ordered based on their number.
A maximum of 7 options can fit in one page before the rest are put on the next page, so if you had an 8th option it would begin on page 2.
Example:
"1:Option A" "target_something"
"2:Plan B" "target_something_else"
"3:Test C" "target_another_thing"
...
"delay" - time delay before the menu closes automatically. By default this is 15 seconds, but, you can set 0 to make the menu not close.
"health" - You can set this value to have the menu start from a specific page rather than the first page.
Flags:-
Checking flag 1 will make the menu open for everyone.
- Outerbeast */
bool blGameMenuEntityRegistered = RegisterGameMenuEntity();
bool RegisterGameMenuEntity()
{
g_CustomEntityFuncs.RegisterCustomEntity( "game_menu", "game_menu" );
return g_CustomEntityFuncs.IsCustomEntity( "game_menu" );
}
final class game_menu : ScriptBaseEntity
{
private CTextMenu@ menu;
private array<array<string>> ARR_STR_OPTIONS;
private int iDelay = 15.0f;
private bool blMenuOpen;
private CScheduledFunction@ fnCloseMenu;
bool KeyValue(const string& in szKey, const string& in szValue)
{
if( szKey != "" && szValue != "" && ARR_STR_OPTIONS.find( { szKey, szValue } ) < 0 )
ARR_STR_OPTIONS.insertLast( { szKey, szValue } );
else if( szKey == "delay" )
iDelay = atoi( szValue );
else
return BaseClass.KeyValue( szKey, szValue );
return true;
}
void Spawn()
{
ARR_STR_OPTIONS.sort( function(a,b) { return atoi( a[0].Split( ":" )[0] ) < atoi( b[0].Split( ":" )[0] ); } );
SetupMenu();
}
bool SetupMenu()
{
@menu = CTextMenu( TextMenuPlayerSlotCallback( this.OptionSelected ) );
menu.SetTitle( "" + self.pev.message );
for( uint i = 0; i < ARR_STR_OPTIONS.length(); i++ )
{
if( ARR_STR_OPTIONS[i][0] == "" )
continue;
menu.AddItem( ARR_STR_OPTIONS[i][0].Split( ":" )[1], any( ARR_STR_OPTIONS[i][1] ) );
}
return menu.Register();
}
void OptionSelected(CTextMenu@ menu, CBasePlayer@ pPlayer, int iSlot, const CTextMenuItem@ pItem)
{
if( pPlayer is null || pItem is null || pItem.m_pUserData is null )
return;
string target;
pItem.m_pUserData.retrieve( target );
g_EntityFuncs.FireTargets( target, pPlayer, null, USE_TOGGLE );
self.pev.frags = iSlot;
self.SUB_UseTargets( pPlayer, USE_SET, float( iSlot ) );
blMenuOpen = false;
}
// This just updates the state, but can force a open menu to close
void CloseMenu(bool blForceClose)
{
blMenuOpen = false;
g_Scheduler.RemoveTimer( fnCloseMenu );
// Hardly open = close
if( blForceClose && blMenuOpen )
menu.Open( 1, 0 );
}
void Use(CBaseEntity@ pActivator, CBaseEntity@ pCaller, USE_TYPE useType, float flValue)
{
switch( useType )
{
case USE_KILL:
{
g_EntityFuncs.Remove( self );
return;
}
case USE_OFF:
iDelay = 1;
break;
case USE_TOGGLE:
iDelay = blMenuOpen ? 1 : iDelay;
break;
}
if( self.pev.SpawnFlagBitSet( 1 << 0 ) )// All players
menu.Open( iDelay, uint( self.pev.health ) );
else if( pActivator.IsPlayer() )
{
CBasePlayer@ pPlayer = cast<CBasePlayer@>( pActivator );
menu.Open( iDelay, uint( self.pev.health ), pPlayer );
}
blMenuOpen = true;
@fnCloseMenu = g_Scheduler.SetTimeout( "CloseMenu", float( iDelay ), false );
}
void UpdateOnRemove()
{
g_Scheduler.RemoveTimer( fnCloseMenu );
CloseMenu( true );
}
};