-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbsFlag.py
320 lines (262 loc) · 10.5 KB
/
bsFlag.py
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
import bs
class FlagFactory(object):
"""
category: Game Flow Classes
Wraps up media and other resources used by bs.Flags.
A single instance of this is shared between all flags
and can be retrieved via bs.Flag.getFactory().
Attributes:
flagMaterial
The bs.Material applied to all bs.Flags.
impactSound
The bs.Sound used when a bs.Flag hits the ground.
skidSound
The bs.Sound used when a bs.Flag skids along the ground.
noHitMaterial
A bs.Material that prevents contact with most objects;
applied to 'non-touchable' flags.
flagTexture
The bs.Texture for flags.
"""
def __init__(self):
"""
Instantiate a FlagFactory.
You shouldn't need to do this; call bs.Flag.getFactory() to get
a shared instance.
"""
self.flagMaterial = bs.Material()
self.flagMaterial.addActions(
conditions=(('weAreYoungerThan', 100),
'and',
('theyHaveMaterial', bs.getSharedObject(
'objectMaterial'))),
actions=(('modifyNodeCollision', 'collide', False)))
self.flagMaterial.addActions(
conditions=('theyHaveMaterial',
bs.getSharedObject('footingMaterial')),
actions=(('message', 'ourNode', 'atConnect', 'footing', 1),
('message', 'ourNode', 'atDisconnect', 'footing', -1)))
self.impactSound = bs.getSound('metalHit')
self.skidSound = bs.getSound('metalSkid')
self.flagMaterial.addActions(
conditions=('theyHaveMaterial',
bs.getSharedObject('footingMaterial')),
actions=(('impactSound', self.impactSound, 2, 5),
('skidSound', self.skidSound, 2, 5)))
self.noHitMaterial = bs.Material()
self.noHitMaterial.addActions(
conditions=(('theyHaveMaterial',
bs.getSharedObject('pickupMaterial')), 'or',
('theyHaveMaterial',
bs.getSharedObject('attackMaterial'))),
actions=(('modifyPartCollision', 'collide', False)))
# we also dont want anything moving it
self.noHitMaterial.addActions(
conditions=(('theyHaveMaterial',
bs.getSharedObject('objectMaterial')), 'or',
('theyDontHaveMaterial',
bs.getSharedObject('footingMaterial'))),
actions=(('modifyPartCollision', 'collide', False),
('modifyPartCollision', 'physical', False)))
self.flagTexture = bs.getTexture('flagColor')
class FlagPickedUpMessage(object):
"""
category: Message Classes
A bs.Flag has been picked up.
Attributes:
flag
The bs.Flag that has been picked up.
node
The bs.Node doing the picking up.
"""
def __init__(self, flag, node):
'Instantiate with given values.'
self.flag = flag
self.node = node
class FlagDeathMessage(object):
"""
category: Message Classes
A bs.Flag has died.
Attributes:
flag
The bs.Flag that died.
"""
def __init__(self, flag):
'Instantiate with given values.'
self.flag = flag
class FlagDroppedMessage(object):
"""
category: Message Classes
A bs.Flag has been dropped.
Attributes:
flag
The bs.Flag that was dropped.
node
The bs.Node that was holding it.
"""
def __init__(self, flag, node):
'Instantiate with given values.'
self.flag = flag
self.node = node
class Flag(bs.Actor):
"""
category: Game Flow Classes
A flag; used in games such as capture-the-flag or king-of-the-hill.
Can be stationary or carry-able by players.
"""
def __init__(
self, position=(0, 1, 0),
color=(1, 1, 1),
materials=[],
touchable=True, droppedTimeout=None):
"""
Instantiate a flag.
If 'touchable' is False, the flag will only touch terrain;
useful for things like king-of-the-hill where players should
not be moving the flag around.
'materials is a list of extra bs.Materials to apply to the flag.
If 'droppedTimeout' is provided (in seconds), the flag will die
after remaining untouched for that long once it has been moved
from its initial position.
"""
bs.Actor.__init__(self)
self._initialPosition = None
self._hasMoved = False
factory = self.getFactory()
if type(materials) is not list:
# in case they passed a tuple or whatnot..
materials = list(materials)
if not touchable:
materials = [factory.noHitMaterial]+materials
self.node = bs.newNode(
"flag",
attrs={'position': (position[0],
position[1] + 0.75, position[2]),
'colorTexture': factory.flagTexture, 'color': color,
'materials':
[bs.getSharedObject('objectMaterial'),
factory.flagMaterial] + materials},
delegate=self)
self._droppedTimeout = droppedTimeout
if self._droppedTimeout is not None:
self._count = self._droppedTimeout
self._tickTimer = bs.Timer(
1000, call=bs.WeakCall(self._tick),
repeat=True)
self._counter = bs.newNode(
'text', owner=self.node,
attrs={'inWorld': True, 'color': (1, 1, 1, 0.7),
'scale': 0.015, 'shadow': 0.5, 'flatness': 1.0,
'hAlign': 'center'})
else:
self._counter = None
self._heldCount = 0
@classmethod
def getFactory(cls):
"""
Returns a shared bs.FlagFactory object, creating it if necessary.
"""
activity = bs.getActivity()
try:
return activity._sharedFlagFactory
except Exception:
f = activity._sharedFlagFactory = FlagFactory()
return f
def _tick(self):
if self.node.exists():
# grab our initial position after one tick (in case we fall)
if self._initialPosition is None:
self._initialPosition = self.node.position
# keep track of when we first move; we don't count down
# until then
if not self._hasMoved:
t = self.node.position
if (max(abs(t[i] - self._initialPosition[i])
for i in range(3)) > 1.0):
self._hasMoved = True
if self._heldCount > 0 or not self._hasMoved:
self._count = self._droppedTimeout
self._counter.text = ''
else:
self._count -= 1
if self._count <= 10:
t = self.node.position
self._counter.position = (t[0], t[1]+1.3, t[2])
self._counter.text = str(self._count)
if self._count < 1:
self.handleMessage(bs.DieMessage())
else:
self._counter.text = ''
def _hideScoreText(self):
bs.animate(self._scoreText, 'scale', {0: self._scoreText.scale, 200: 0})
def setScoreText(self, text):
"""
Utility func to show a message over the flag; handy for scores.
"""
if not self.node.exists():
return
try:
exists = self._scoreText.exists()
except Exception:
exists = False
if not exists:
startScale = 0.0
math = bs.newNode('math', owner=self.node, attrs={
'input1': (0, 1.4, 0), 'operation': 'add'})
self.node.connectAttr('position', math, 'input2')
self._scoreText = bs.newNode('text',
owner=self.node,
attrs={'text': text,
'inWorld': True,
'scale': 0.02,
'shadow': 0.5,
'flatness': 1.0,
'hAlign': 'center'})
math.connectAttr('output', self._scoreText, 'position')
else:
startScale = self._scoreText.scale
self._scoreText.text = text
self._scoreText.color = bs.getSafeColor(self.node.color)
bs.animate(self._scoreText, 'scale', {0: startScale, 200: 0.02})
self._scoreTextHideTimer = bs.Timer(
1000, bs.WeakCall(self._hideScoreText))
def handleMessage(self, msg):
self._handleMessageSanityCheck()
if isinstance(msg, bs.DieMessage):
if self.node.exists():
self.node.delete()
if not msg.immediate:
self.getActivity().handleMessage(FlagDeathMessage(self))
elif isinstance(msg, bs.HitMessage):
self.node.handleMessage(
"impulse", msg.pos[0],
msg.pos[1],
msg.pos[2],
msg.velocity[0],
msg.velocity[1],
msg.velocity[2],
msg.magnitude, msg.velocityMagnitude, msg.radius, 0, msg.
forceDirection[0],
msg.forceDirection[1],
msg.forceDirection[2])
elif isinstance(msg, bs.OutOfBoundsMessage):
# we just kill ourselves when out-of-bounds.. would we ever not
# want this?..
self.handleMessage(bs.DieMessage(how='fall'))
elif isinstance(msg, bs.PickedUpMessage):
self._heldCount += 1
if self._heldCount == 1 and self._counter is not None:
self._counter.text = ''
a = self.getActivity()
if a is not None:
a.handleMessage(FlagPickedUpMessage(self, msg.node))
elif isinstance(msg, bs.DroppedMessage):
self._heldCount -= 1
if self._heldCount < 0:
print 'Flag held count < 0'
self._heldCount = 0
a = self.getActivity()
if a is not None:
a.handleMessage(FlagDroppedMessage(self, msg.node))
else:
bs.Actor.handleMessage(self, msg)