-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStandardRespawnCommand.as
181 lines (158 loc) · 4.71 KB
/
StandardRespawnCommand.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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
// REQUIRES:
//
// onRespawnCommand() to be called in onCommand()
//
// implementation of:
//
// bool canChangeClass( CBlob@ this, CBlob @caller )
//
// Tag: "change class sack inventory" - if you want players to have previous items stored in sack on class change
// Tag: "change class store inventory" - if you want players to store previous items in this respawn blob
#include "ClassSelectMenu.as"
void InitRespawnCommand(CBlob@ this)
{
this.addCommandID("class menu");
}
bool isInRadius(CBlob@ this, CBlob @caller)
{
return ((this.getPosition() - caller.getPosition()).Length() < this.getRadius() * 2.0f + caller.getRadius());
}
bool canChangeClass(CBlob@ this, CBlob@ blob)
{
Vec2f tl, br, _tl, _br;
this.getShape().getBoundingRect(tl, br);
blob.getShape().getBoundingRect(_tl, _br);
return br.x > _tl.x
&& br.y > _tl.y
&& _br.x > tl.x
&& _br.y > tl.y
&& !blob.hasTag("raider");
}
// default classes
void InitClasses(CBlob@ this)
{
AddIconToken("$builder_class_icon$", "GUI/MenuItems.png", Vec2f(32, 32), 8);
AddIconToken("$knight_class_icon$", "GUI/MenuItems.png", Vec2f(32, 32), 12);
AddIconToken("$archer_class_icon$", "GUI/MenuItems.png", Vec2f(32, 32), 16);
AddIconToken("$change_class$", "/GUI/InteractionIcons.png", Vec2f(32, 32), 12, 2);
addPlayerClass(this, "Builder", "$builder_class_icon$", "builder", "Build ALL the towers.");
addPlayerClass(this, "Knight", "$knight_class_icon$", "knight", "Hack and Slash.");
addPlayerClass(this, "Archer", "$archer_class_icon$", "archer", "The Ranged Advantage.");
}
void BuildRespawnMenuFor(CBlob@ this, CBlob @caller)
{
PlayerClass[]@ classes;
this.get("playerclasses", @classes);
if (caller !is null && caller.isMyPlayer() && classes !is null)
{
CGridMenu@ menu = CreateGridMenu(caller.getScreenPos() + Vec2f(24.0f, caller.getRadius() * 1.0f + 48.0f), this, Vec2f(classes.length * CLASS_BUTTON_SIZE, CLASS_BUTTON_SIZE), "Swap class");
if (menu !is null)
{
addClassesToMenu(this, menu, caller.getNetworkID());
}
}
}
void onRespawnCommand(CBlob@ this, u8 cmd, CBitStream @params)
{
switch (cmd)
{
case SpawnCmd::buildMenu:
{
{
// build menu for them
CBlob@ caller = getBlobByNetworkID(params.read_u16());
BuildRespawnMenuFor(this, caller);
}
}
break;
case SpawnCmd::changeClass:
{
if (getNet().isServer())
{
// build menu for them
CBlob@ caller = getBlobByNetworkID(params.read_u16());
if (caller !is null && canChangeClass(this, caller))
{
string classconfig = params.read_string();
CBlob @newBlob = server_CreateBlob(classconfig, caller.getTeamNum(), this.getRespawnPosition());
if (newBlob !is null)
{
// copy health and inventory
// make sack
CInventory @inv = caller.getInventory();
if (inv !is null)
{
if (this.hasTag("change class drop inventory"))
{
while (inv.getItemsCount() > 0)
{
CBlob @item = inv.getItem(0);
caller.server_PutOutInventory(item);
}
}
else if (this.hasTag("change class store inventory"))
{
if (this.getInventory() !is null)
{
caller.MoveInventoryTo(this);
}
else // find a storage
{
PutInvInStorage(caller);
}
}
else
{
// keep inventory if possible
caller.MoveInventoryTo(newBlob);
}
}
// set health to be same ratio
float healthratio = caller.getHealth() / caller.getInitialHealth();
newBlob.server_SetHealth(newBlob.getInitialHealth() * healthratio);
//copy air
if (caller.exists("air_count"))
{
newBlob.set_u8("air_count", caller.get_u8("air_count"));
newBlob.Sync("air_count", true);
}
//copy stun
if (caller.exists("knocked"))
{
newBlob.set_u8("knocked", caller.get_u8("knocked"));
newBlob.Sync("knocked", true);
}
// plug the soul
newBlob.server_SetPlayer(caller.getPlayer());
newBlob.setPosition(caller.getPosition());
// no extra immunity after class change
if (caller.exists("spawn immunity time"))
{
newBlob.set_u32("spawn immunity time", caller.get_u32("spawn immunity time"));
newBlob.Sync("spawn immunity time", true);
}
caller.Tag("switch class");
caller.server_SetPlayer(null);
caller.server_Die();
}
}
}
}
break;
}
//params.SetBitIndex( index );
}
void PutInvInStorage(CBlob@ blob)
{
CBlob@[] storages;
if (getBlobsByTag("storage", @storages))
for (uint step = 0; step < storages.length; ++step)
{
CBlob@ storage = storages[step];
if (storage.getTeamNum() == blob.getTeamNum())
{
blob.MoveInventoryTo(storage);
return;
}
}
}