-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.py
761 lines (614 loc) · 29.3 KB
/
client.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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
import math, os, time
import ogre.renderer.OGRE as ogre
import ogre.gui.CEGUI as CEGUI
# Another fix for CEGUI to ensure we get a working parser..
if os.name == 'nt':
CEGUI.System.setDefaultXMLParserName("ExpatParser")
else:
CEGUI.System.setDefaultXMLParserName("TinyXMLParser")
import ogre.sound.OgreAL as OgreAL
import ode
import ogre.io.OIS as OIS
from engine import Engine
from guiobjects import *
import objects
import networkclient
import gamenet
from encode import timer
import console
def cegui_reldim ( x ) :
return CEGUI.UDim((x),0)
class Application(object):
debugText = ""
def __init__(self):
self.frameListener = None
self.root = None
self.camera = None
self.renderWindow = None
self.sceneManager = None
self.world = None
self.debug = True
self.debugNetworkTime = 0
self.debugChatTime = 0
def go(self):
"Starts the rendering loop."
if not self._setUp():
return
self.root.startRendering()
def _setUp(self):
"""This sets up the ogre application, and returns false if the user
hits "cancel" in the dialog box."""
self.root = ogre.Root(os.path.join(os.getcwd(), 'plugins.cfg'))
self.root.setFrameSmoothingPeriod(5.0)
self._setUpResources()
if not self._configure():
return False
self._chooseSceneManager()
self._createWorld()
self._createCamera()
self._createViewports()
ogre.TextureManager.getSingleton().setDefaultNumMipmaps (5)
self._createResourceListener()
self._loadResources()
self._createScene()
self._createFrameListener()
self._createConsole()
return True
def _createConsole(self):
self.console = console.Console(self.root)
self.console.show()
def _setUpResources(self):
"""This sets up Ogre's resources, which are required to be in
resources.cfg."""
config = ogre.ConfigFile()
config.load('resources.cfg' )
seci = config.getSectionIterator()
while seci.hasMoreElements():
SectionName = seci.peekNextKey()
Section = seci.getNext()
for item in Section:
ogre.ResourceGroupManager.getSingleton().\
addResourceLocation(item.value, item.key, SectionName)
def _createResourceListener(self):
"""This method is here if you want to add a resource listener to check
the status of resources loading."""
pass
def _createWorld ( self ):
""" this should be overridden when supporting the OgreRefApp framework. Also note you
will have to override __createCamera"""
pass
def _loadResources(self):
ogre.ResourceGroupManager.getSingleton().initialiseAllResourceGroups()
def _configure(self):
carryOn = self.root.showConfigDialog()
if carryOn:
self.renderWindow = self.root.initialise(True, "Assault Vector")
return carryOn
def _chooseSceneManager(self):
self.sceneManager = self.root.createSceneManager(ogre.ST_GENERIC,"ExampleSMInstance")
def convertOISMouseButtonToCegui( buttonID):
if buttonID ==0:
return CEGUI.LeftButton
elif buttonID ==1:
return CEGUI.RightButton
elif buttonID ==2:
return CEGUI.MiddleButton
elif buttonID ==3:
return CEGUI.X1Button
else:
return CEGUI.LeftButton
class GameKeyListener(OIS.KeyListener):
def __init__(self, game):
OIS.KeyListener.__init__(self)
self.game = game
self.keyToRepeat = {}
self.timeUntilRepeat = 0.4
self.timeBetweenRepeats = 0.03
self.chatAlpha = 0.3
self.chatPosition = CEGUI.UDim(0.8, 0)
self.console = console.Console()
def frameEnded(self, time, keyboard):
chat = CEGUI.WindowManager.getSingleton().getWindow("TextWindow")
# TODO: Moderate speed of effects with time
chat.setAlpha((chat.getAlpha()*3 + self.chatAlpha)/4)
chat.setYPosition((chat.getYPosition()*CEGUI.UDim(3, 0) + self.chatPosition)*CEGUI.UDim(0.25, 0))
if len(self.keyToRepeat) != 0:
key = self.keyToRepeat['key']
self.keyToRepeat['time'] += time
if self.keyToRepeat['mode'] == 'waiting' and \
self.keyToRepeat['time'] > self.timeUntilRepeat:
self.keyToRepeat['mode'] = 'repeating'
self.keyToRepeat['time'] -= self.timeUntilRepeat
elif self.keyToRepeat['mode'] == 'repeating':
while self.keyToRepeat['time'] > self.timeBetweenRepeats:
self.keyToRepeat['time'] -= self.timeBetweenRepeats
CEGUI.System.getSingleton().injectKeyDown( key )
CEGUI.System.getSingleton().injectChar( self.keyToRepeat['text'] )
return True # Keep running
def keyPressed(self, arg):
print "key pressed", arg.text
self.keyToRepeat = {'mode':'waiting', 'time':0.0, 'text':arg.text, 'key':arg.key}
CEGUI.System.getSingleton().injectKeyDown( arg.key )
CEGUI.System.getSingleton().injectChar( arg.text )
self.console.keyPressed(arg)
def keyReleased(self, arg):
if len(self.keyToRepeat) != 0 and arg.key == self.keyToRepeat['key']:
self.keyToRepeat = {}
CEGUI.System.getSingleton().injectKeyUp( arg.key )
static = CEGUI.WindowManager.getSingleton().getWindow("TextWindow/Static")
chat = CEGUI.WindowManager.getSingleton().getWindow("TextWindow")
editBox = CEGUI.WindowManager.getSingleton().getWindow("TextWindow/Editbox1")
if arg.key == OIS.KC_F1:
print "Technique:", self.game.sceneManager.getShadowTechnique()
if self.game.sceneManager.getShadowTechnique() == ogre.SHADOWTYPE_STENCIL_ADDITIVE:
self.game.sceneManager.setShadowTechnique(ogre.SHADOWTYPE_NONE)
else:
self.game.sceneManager.setShadowTechnique(ogre.SHADOWTYPE_STENCIL_ADDITIVE)
if arg.key == OIS.KC_F2:
self.game.sceneManager.setShowDebugShadows(not self.game.sceneManager.getShowDebugShadows())
if arg.key == OIS.KC_RETURN and editBox.hasInputFocus():
self.game.sendText()
editBox.deactivate() # Remove focus from editbox
chat.disable()
self.chatAlpha = 0.3
if arg.key == OIS.KC_T and \
(chat.isDisabled() or (not chat.isDisabled() and not editBox.hasInputFocus())):
chat.enable()
self.chatAlpha = 0.9
self.chatPosition = CEGUI.UDim(0.8, 0)
if editBox.getText().c_str().startswith("team:"):
editBox.setText(editBox.getText().c_str()[5:])
editBox.setCaratIndex(editBox.getCaratIndex() - 5)
editBox.activate()
if arg.key == OIS.KC_Y and \
(chat.isDisabled() or (not chat.isDisabled() and not editBox.hasInputFocus())):
chat.enable()
self.chatAlpha = 0.9
self.chatPosition = CEGUI.UDim(0.8, 0)
if not editBox.getText().c_str().startswith("team:"):
editBox.setText("team:" + editBox.getText().c_str())
editBox.setCaratIndex(editBox.getCaratIndex() + 5)
editBox.activate()
if arg.key == OIS.KC_ESCAPE and editBox.hasInputFocus():
editBox.deactivate()
chat.disable()
self.chatAlpha = 0.3
if arg.key == OIS.KC_F12:
if self.chatPosition == CEGUI.UDim(0.8, 0):
self.chatPosition = CEGUI.UDim(1.0, 0)
editBox.deactivate()
else:
self.chatPosition = CEGUI.UDim(0.8, 0)
chat.disable()
self.chatAlpha = 0.3
class GameJoystickListener(OIS.JoyStickListener):
def __init__(self, game):
self.game = game
OIS.JoyStickListener.__init__( self)
def povMoved( self, event, pov ):
pass
def axisMoved( self, event, axis ):
if axis == 2 or axis == 3:
y = float(event.get_state().mAxes[2].abs)/32767
x = float(event.get_state().mAxes[3].abs)/32767
print x, y
size = self.game.mouse.getMouseState()
CEGUI.System.getSingleton().injectMousePosition(
x*size.width/2 + size.width/2,
y*size.height/2 + size.height/2)
self.game.camera.setDirection(x*0.39,-y*0.39,-1)
def sliderMoved( self, event, sliderID ):
pass
def buttonPressed( self, event, button ):
print button
pass
def buttonReleased( self, event, button ):
pass
class GameMouseListener(OIS.MouseListener):
def __init__(self, game):
self.game = game
OIS.MouseListener.__init__( self)
def mouseMoved( self, arg ):
self.game.camera.yaw(- arg.get_state().X.rel * 0.0012)
self.game.camera.pitch(- arg.get_state().Y.rel * 0.0022)
d = self.game.camera.getDirection()
# TODO: Swing camera on circular path away from directly in front of character?
maxMouseLook = 0.39
angle = 0
if d.x != 0.0:
angle = math.atan2(d.y,d.x)
maxY = math.sin(angle)*maxMouseLook
maxX = math.cos(angle)*maxMouseLook
if math.fabs(d.x) >= math.fabs(maxX):
d.x = maxX
if math.fabs(d.y) >= math.fabs(maxY):
d.y = maxY
self.game.camera.setDirection(d)
CEGUI.System.getSingleton().injectMousePosition( \
((d.x + maxMouseLook)/(maxMouseLook*2))*arg.get_state().width, \
((1.0 - ((d.y + maxMouseLook))/(maxMouseLook*2)))*arg.get_state().height )
def mousePressed( self, arg, id ):
pass
#CEGUI.System.getSingleton().injectMouseButtonDown(convertOISMouseButtonToCegui(id))
def mouseReleased( self, arg, id ):
pass
#CEGUI.System.getSingleton().injectMouseButtonUp(convertOISMouseButtonToCegui(id))
class FrameListener(ogre.FrameListener, ogre.WindowEventListener):
"""A default frame listener, which takes care of basic mouse and keyboard
input."""
def __init__(self, game, renderWindow, camera):
ogre.FrameListener.__init__(self)
ogre.WindowEventListener.__init__(self)
self.game = game
self.camera = camera
self.renderWindow = renderWindow
self.statisticsOn = True
self.numScreenShots = 0
self.timeUntilNextToggle = 0
self.sceneDetailIndex = 0
self.moveScale = 0.0
self.rotationScale = 0.0
self.translateVector = ogre.Vector3(0.0,0.0,0.0)
self.filtering = ogre.TFO_BILINEAR
self.moveSpeed = 100.0
self.rotationSpeed = 8.0
self.displayCameraDetails = False
self.MenuMode = False # lets understand a simple menu function
self._setupInput()
self.keylistener = GameKeyListener(self.game)
self.BufferedKeyboard.setEventCallback(self.keylistener)
self.mouselistener = GameMouseListener(self.game)
self.Mouse.setEventCallback(self.mouselistener)
if self.Joy:
self.joysticklistener = GameJoystickListener(self.game)
self.Joy.setEventCallback(self.joysticklistener)
self.lastUpdate = time.time()
def __del__ (self ):
ogre.WindowEventUtilities.removeWindowEventListener(self.renderWindow, self)
self.windowClosed(self.renderWindow)
def _setupInput(self):
windowHnd = self.renderWindow.getCustomAttributeInt("WINDOW")
self.InputManager = \
OIS.createPythonInputSystem([("WINDOW",str(windowHnd))])
#Create all devices (We only catch joystick exceptions here, as, most people have Key/Mouse)
#self.Keyboard = self.InputManager.createInputObjectKeyboard( OIS.OISKeyboard, False )
self.BufferedKeyboard = self.InputManager.createInputObjectKeyboard( OIS.OISKeyboard, True )
self.Mouse = self.InputManager.createInputObjectMouse( OIS.OISMouse, True )
try :
self.Joy = self.InputManager.createInputObjectJoyStick( OIS.OISJoyStick, True )
except:
self.Joy = False
#Set initial mouse clipping size
self.windowResized(self.renderWindow)
#Register as a Window listener
ogre.WindowEventUtilities.addWindowEventListener(self.renderWindow, self);
def setMenuMode(self, mode):
self.MenuMode = mode
def _UpdateSimulation( self, frameEvent ):
# create a real version of this to update the simulation
pass
def windowResized (self, rw):
#[width, height, depth, left, top] = rw.getMetrics() # Note the wrapped function as default needs unsigned int's
ms = self.Mouse.getMouseState()
ms.width = rw.getWidth()
ms.height = rw.getHeight()
def windowClosed(self, rw):
#Only close for window that created OIS (mWindow)
if( rw == self.renderWindow ):
if( self.InputManager ):
self.InputManager.destroyInputObjectMouse( self.Mouse )
self.InputManager.destroyInputObjectKeyboard( self.BufferedKeyboard )
if self.Joy:
self.InputManager.destroyInputObjectJoyStick( self.Joy )
OIS.InputManager.destroyInputSystem(self.InputManager)
self.InputManager=None
def frameStarted(self, frameEvent):
if(self.renderWindow.isClosed()):
return False
##Need to capture/update each device - this will also trigger any listeners
self.BufferedKeyboard.capture()
#self.Keyboard.capture()
self.Mouse.capture()
if( self.Joy ):
self.Joy.capture()
return True
def frameEnded(self, frameEvent):
keepGoing = True
curTime = time.time()
frameTime = (curTime - self.lastUpdate)
keepGoing = keepGoing and self.keylistener.frameEnded(frameTime, self.BufferedKeyboard)
keepGoing = keepGoing and self.game.frameEnded(frameTime, self.BufferedKeyboard, self.Mouse, self.Joy)
self.lastUpdate = curTime
return keepGoing
class Client(Application, Engine):
def __init__(self, autoConnect = False):
Application.__init__(self)
Engine.__init__(self)
#ip, port = "cradle.dyndns.org", "10001"
ip, port = "127.0.0.1", "10001"
if not autoConnect:
address = raw_input("server ('%s:%s') :> " % (ip, port))
if address != "":
split = address.split(":")
ip = split[0]
if len(split) == 2:
port = split[1]
self.chat = gamenet.NetCode("cradle", "cradle.dyndns.org", "AV", "enter", "-".join([ip, port]))
self.chat.registerMessageListener(self.messageListener)
self.timeBetweenChatUpdates = 0.5
self.timeUntilNextChatUpdate = 0.0
self.network = networkclient.NetworkClient(ip, int(port))
self.timeBetweenNetworkUpdates = 0.02
self.timeUntilNextNetworkUpdate = 0.0
self.serverRoundTripTime = 0.0
self.lastServerUpdate = time.time()
self.player = None
def sendText(self):
e = CEGUI.WindowManager.getSingleton().getWindow("TextWindow/Editbox1")
self.chat.sendMessage(e.getText().c_str())
e.setText("")
def messageListener(self, name, message):
if message.startswith("/me") and len(message[3:].strip()) > 0 :
self.appendText("*" + name + " " + message[3:].strip())
else:
self.appendText(name + ": " + message)
def appendText(self, text):
st = CEGUI.WindowManager.getSingleton().getWindow("TextWindow/Static")
currentText = st.getText()
currentText += "\n"
currentText += text
st.setText(currentText)
def _createCamera(self):
self.camera = self.sceneManager.createCamera("Player1Cam")
self.camera.setPosition(0,25,75)
self.camera.lookAt(0,25,0)
self.camera.nearClipDistance = 5
def _createViewports(self):
self.viewPort = self.renderWindow.addViewport(self.camera,1)
self.viewPort.setDimensions(0.0, 0.0, 1.0, 1.0)
self.viewPort.setBackgroundColour((0.0, 0.0, 0.0, 1.0))
self.camera.aspectRatio = self.viewPort.actualWidth / self.viewPort.actualHeight
self.viewPort.setOverlaysEnabled(True)
def createStaticObject(self, size):
return StaticObject(self, "s%s" % len(self.statics), size=size)
def createBulletObject(self, name, direction, velocity, damage):
return BulletObject(self, name, direction, velocity, damage)
def createShrapnelObject(self, name, direction, velocity, damage):
return ShrapnelObject(self, name, direction, velocity, damage)
def createGrenadeObject(self, name, direction , velocity, damage):
return GrenadeObject(self, name, direction, velocity, damage)
def createLaserObject(self, name, direction, velocity, damage):
return LaserObject(self, name, direction, velocity, damage)
def createPerson(self, name):
return Person(self, name)
def createStaticTriangleMesh(self, ent, space):
vertdata=[]
facedata=[]
node = ent.parentNode
for i in range(ent.mesh.numSubMeshes):
#if not ent.mesh.getSubMesh(i).useSharedVertices:
for v in ( ent.mesh.getSubMesh(i).getVertices(node.position, node.orientation, node.scale)):
vertdata.append(v)
for f in (ent.mesh.getSubMesh(i).indices):
facedata.append(f)
data = ode.TriMeshData()
data.build(vertdata, facedata)
geom = ode.GeomTriMesh(data, space)
del vertdata
del facedata
return geom
def createLogo(self):
logoa = self.sceneManager.getRootSceneNode().createChildSceneNode('logo-a')
elogoa = self.sceneManager.createEntity("m-logo-a","logo-a.mesh")
logoa.attachObject(elogoa)
logov = self.sceneManager.getRootSceneNode().createChildSceneNode('logo-v')
elogov = self.sceneManager.createEntity("m-logo-v","logo-v.mesh")
logoa.attachObject(elogov)
logoPos = (0, 25, -35)
logoa.setPosition(logoPos)
logov.setPosition(logoPos)
logoScale = (5,5,5)
logoa.setScale(logoScale)
logov.setScale(logoScale)
self.logoa = logoa
self.logov = logov
def createMenu(self):
from menu import Menu
self.menu = Menu(self.sceneManager)
def updateMenu(self, frameTime):
x = CEGUI.MouseCursor.getSingleton().getPosition().d_x
y = CEGUI.MouseCursor.getSingleton().getPosition().d_y
self.menu.update(x, y, frameTime)
def updateLogo(self, frameTime):
rotateSpeed = 0.2
self.logoa.yaw(rotateSpeed*frameTime)
self.logov.yaw(rotateSpeed*frameTime)
def _createScene(self):
Engine._createWorld(self)
self.sceneManager.setAmbientLight((1.0, 1.0, 1.0, 1.0))
## self.sceneManager.setShadowTechnique(ogre.SHADOWTYPE_NONE)
## self.sceneManager.setShadowColour((0.5, 0.5, 0.5))
## self.sceneManager.setShadowTextureSize(1024)
## self.sceneManager.setShadowTextureCount(2)
light = self.sceneManager.createLight("sunlight")
light.setType(ogre.Light.LT_DIRECTIONAL) # or .type
light.setDirection((0.4,-0.4,-1))
light.setDiffuseColour(0.85,0.85,0.85)
#light.setCastShadows(True)
entity = self.sceneManager.createEntity('bgE', 'Scene.mesh')
#entity.setCastShadows(False)
node = self.sceneManager.getRootSceneNode().createChildSceneNode('bgN')
node.attachObject(entity)
node.setVisible(True)
## node.setDirection(0,0,-1)
## node.setScale(0.05,0.05,0.05)
## tempWorld = OgreOde.World(self.sceneManager)
## self.createStaticTriangleMesh(entity, self.space)
## geom = self.createStaticTriangleMesh(entity, self.space)
## print type(geom)
self.createLogo()
## self.createMenu()
# Setup Audio
self.soundManager = OgreAL.SoundManager()
self.soundManager.getListener().setDirection((0,25,75))
self.sfx = SFX(self.soundManager)
## setup GUI system
self.GUIRenderer = CEGUI.OgreCEGUIRenderer(self.renderWindow,
ogre.RENDER_QUEUE_OVERLAY, False, 3000, self.sceneManager)
self.GUIsystem = CEGUI.System(self.GUIRenderer)
logger = CEGUI.Logger.getSingleton()
logger.setLoggingLevel( CEGUI.Informative )
winMgr = CEGUI.WindowManager.getSingleton()
## load scheme and set up defaults
CEGUI.SchemeManager.getSingleton().loadScheme("TaharezLook.scheme")
self.GUIsystem.setDefaultMouseCursor("TaharezLook", "MouseArrow")
CEGUI.MouseCursor.getSingleton().setVisible(False)
CEGUI.ImagesetManager.getSingleton().createImageset("menu.imageset")
winMgr.loadWindowLayout("menu.layout")
winMgr.loadWindowLayout("hud.layout")
sheet = winMgr.getWindow("Background")
#self.GUIsystem.setGUISheet(winMgr.getWindow("Menu"))
self.GUIsystem.setGUISheet(sheet)
font = CEGUI.FontManager.getSingleton().createFont("tuffy.font")
textwnd = winMgr.getWindow("TextWindow")
self._scoreWindow = winMgr.getWindow("ScoreWindow")
self._debugWindow = winMgr.getWindow("DebugWindow")
self._vitalsWindow = winMgr.getWindow("VitalsWindow")
# Materials
myMaterial = ogre.MaterialManager.getSingleton().create("bullets","a")
myMaterial.setLightingEnabled(False)
myMaterial.setDepthWriteEnabled(False)
myMaterial.setSceneBlending(ogre.SBT_TRANSPARENT_ALPHA)
## myMaterial.getTechnique(0).getPass(0).setVertexColourTracking(ogre.TVC_DIFFUSE)
myMaterial = ogre.MaterialManager.getSingleton().create("cursor","b")
myMaterial.setLightingEnabled(False)
myMaterial.setDepthWriteEnabled(False)
myMaterial.setSceneBlending(ogre.SBT_TRANSPARENT_ALPHA)
myMaterial.getTechnique(0).getPass(0).setVertexColourTracking(ogre.TVC_AMBIENT | ogre.TVC_DIFFUSE)
myMaterial = ogre.MaterialManager.getSingleton().create("grenade","c")
def displayDebug(self):
self._debugWindow.setText( ("FrameTime: %0.4f\n" +
"1StepTime: %0.4f\n" +
"NumSteps: %4i\n" +
"StepTime: %0.4f\n" +
"NetworkTime: %0.4f\n" +
"ChatTime: %0.4f\n" +
"SNDPacketLen:%5i\n" +
"RCVPacketLen:%5i\n") % \
(self.debugFrameTime,
(self.debugStepTime / self.debugNumSteps) if self.debugNumSteps else 0,
self.debugNumSteps,
self.debugStepTime,
self.debugNetworkTime,
self.debugChatTime,
self.network.debugSendPacketLength,
self.network.debugReceivePacketLength,
)
)
def displayVitals(self):
if self.player:
self._vitalsWindow.setText(self.player.vitals())
def displayScores(self):
text = ""
players = [object for object in self.objects if object.type == objects.PERSON]
for player in players:
text += " %s, %2i, %4i\n" % \
(player._name, player.score, player.ping)
self._scoreWindow.setSize(CEGUI.UVector2(CEGUI.UDim(0.15,0), CEGUI.UDim(0.01 + 0.05*len(players),0)))
self._scoreWindow.setText(text)
def _createFrameListener(self):
## note we pass ourselves as the demo to the framelistener
self.frameListener = FrameListener(self, self.renderWindow, self.camera)
self.root.addFrameListener(self.frameListener)
def updateChat(self, frameTime):
self.timeUntilNextChatUpdate -= frameTime
if self.timeUntilNextChatUpdate <= 0.0:
self.chat.update()
self.timeUntilNextChatUpdate = self.timeBetweenChatUpdates
def updateGUI(self, frameTime):
self.updateLogo(frameTime)
## self.updateMenu(frameTime)
self.displayDebug()
self.displayScores()
self.displayVitals()
def frameEnded(self, frameTime, keyboard, mouse, joystick):
t = timer()
self.updateGUI(frameTime)
Engine.frameEnded(self, frameTime)
t.start()
self.updateChat(frameTime)
t.stop()
t.debugChatTime = t.time()
keyboard = keyboard
self.mouse = mouse
self.joystick = joystick
self.timeUntilNextNetworkUpdate -= frameTime
t.start()
if self.timeUntilNextNetworkUpdate <= 0.0:
self.timeUntilNextNetworkUpdate = self.timeBetweenNetworkUpdates
u = timer()
u.start()
self.network.update(frameTime)
u.stop()
for message in self.network._messages:
if message[1] > self.lastServerUpdate:
self.timeUntilNextEngineUpdate = message[Engine.NET_TIME_UNTIL_UPDATE]
#print message[Engine.NET_TIME_UNTIL_UPDATE]
self.lastServerUpdate = message[Engine.NET_TIME]
for object in self.objects:
object.existsOnServer = False
for serverObject in message[Engine.NET_OBJECTS]:
hasObject = False
for object in self.objects:
if serverObject[Engine.NET_OBJECTS_NAME] == object._name:
hasObject = True
object.existsOnServer = True
object.setAttributes(serverObject[Engine.NET_OBJECTS_ATTRIBUTES])
object.setEvents(serverObject[Engine.NET_OBJECTS_EVENTS])
if not hasObject:
newObject = None
if serverObject[2] == True:
newObject = Player(self, serverObject[Engine.NET_OBJECTS_NAME], self.camera)
self.chat.setNickName(serverObject[Engine.NET_OBJECTS_NAME])
newObject.enable()
self.player = newObject
else:
if serverObject[Engine.NET_OBJECTS_TYPE] == objects.PERSON:
newObject = Person(self, serverObject[Engine.NET_OBJECTS_NAME])
elif serverObject[Engine.NET_OBJECTS_TYPE] == objects.BULLET:
newObject = BulletObject(self, serverObject[Engine.NET_OBJECTS_NAME])
elif serverObject[Engine.NET_OBJECTS_TYPE] == objects.GRENADE:
newObject = GrenadeObject(self, serverObject[Engine.NET_OBJECTS_NAME])
elif serverObject[Engine.NET_OBJECTS_TYPE] == objects.SHRAPNEL:
newObject = ShrapnelObject(self, serverObject[Engine.NET_OBJECTS_NAME])
elif serverObject[Engine.NET_OBJECTS_TYPE] == objects.LASER:
newObject = LaserObject(self, serverObject[Engine.NET_OBJECTS_NAME])
else:
print "Unknown object to create", serverObject[Engine.NET_OBJECTS_TYPE], objects.PERSON
if newObject:
newObject.existsOnServer = True
newObject.setAttributes(serverObject[Engine.NET_OBJECTS_ATTRIBUTES])
newObject.setEvents(serverObject[Engine.NET_OBJECTS_EVENTS])
self.objects += [newObject]
for object in self.objects:
if not object.existsOnServer and object.type == objects.PERSON:
self.objects.remove(object)
object.close()
del object
self.network.clearMessages()
if self.player != None:
self.network.send(self.player.input(keyboard, self.mouse, self.joystick))
t.stop()
self.debugNetworkTime = t.time()
return True # Keep running
if __name__ == "__main__":
try:
import psyco
psyco.full()
print "Psyco Enabled"
except ImportError:
print "No Psyco Support"
world = Client()
import cProfile
cProfile.run('world.go()', 'client-profile.txt')
os._exit(0)