class GunControl(object): def __init__(self, cont): self.log = Logging("GunControl", "Debug") self.cont = cont self.own = cont.owner self.scene = bge.logic.getCurrentScene() self.bullets = self.own["Bullets"] if self.own["Bullets"] else 0 self.magazines = self.own["Magazines"] if self.own["Magazines"] else 0 self.reload = self.cont.sensors["Reload"] self.shoot = self.cont.sensors["Shoot"] self.aim = self.cont.sensors["Ray"] self.triggerPull = self.cont.sensors["Trigger"] self.gun = self.own.parent self._updateHUD() self.log.msg("Init Completed.") def update(self): if self.aim.positive: # Add the laser targeting pointer to the scene bge.render.drawLine(self.own.worldPosition, self.aim.hitPosition, [255, 0, 0]) if self.reload.positive and self.magazines > 0: self._reload() self.gun.state = 1 # flip the gun back to shoot mode if self.bullets > 0: if self.shoot.positive or self.triggerPull.positive: self._shoot() else: self.gun.state = 2 # gun is empty self._updateHUD() def _shoot(self): self.bullets += -1 if self.aim.positive: bulletForce = self.scene.addObject("BulletForce", self.own, 3) bulletForce.worldPosition = Vector(self.aim.hitPosition) + (Vector(self.aim.hitNormal) * 0.01) bulletHole = self.scene.addObject("BulletHole", self.own, 200) # position the bullet based on the ray, give a margin factor due to rendering collision bulletHole.worldPosition = Vector(self.aim.hitPosition) + (Vector(self.aim.hitNormal) * 0.01) bulletHole.alignAxisToVect(self.aim.hitNormal, 2) self._activate(self.cont.actuators["Fire"]) self._activate(self.cont.actuators["MuzzleFlash"]) def _reload(self): # self._activate(self.cont.actuators["Reload"]) self.magazines += -1 self.bullets = self.own["Bullets"] if self.own["Bullets"] else 0 def _updateHUD(self): bge.logic.sendMessage("Ammo", str(self.bullets)) bge.logic.sendMessage("Clips", str(self.magazines)) if self.magazines == 0 and self.bullets == 0: bge.logic.sendMessage("GameOver", "GameOver") def _activate(self, actuator): self.cont.activate(actuator)
class GunUdpHandler(object): def __init__(self): self.protocolFormat = '<ffffff' self.log = Logging("GunUDPHandler","Debug") self.log.msg("Init Completed.") def parse(self,rawData): try: data = struct.unpack(self.protocolFormat,rawData) #self.log.msg("data: "+str(data[0])+" "+str(data[1])) return str(data[0])+','+str(data[1])+','+str(data[2])+','+str(data[3])+','+str(data[4])+','+str(data[5]) except Exception as e: self.log.msg("error: "+str(e))
class GameOver(object): ''' this class is the main entry point and control of the game scene ''' def __init__(self,cont): self.cont = cont self.own = cont.owner self.scene = bge.logic.getCurrentScene() self.log = Logging("GameOver","Debug") self.showScoreboard = self.cont.actuators['GoScoreboard'] self.showScoreboard.scene="Scoreboard" self.kbd = self.cont.sensors['Keyboard'] self.log.msg('init completed') def update(self): if self.kbd.positive: self.cont.activate(self.showScoreboard)
class Networking(object): def __init__(self): self.host = "0.0.0.0" self.port = 10001 self.protocol = ProtocolHandler() self.socketClient = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) self.socketClient.bind((self.host, self.port)) self.socketClient.setsockopt(socket.SOL_SOCKET, socket.SO_RCVBUF, 256) self.socketClient.setblocking(0) self.log = Logging("Networking", "Debug") self.log.msg("Init Completed.") def update(self): try: rawData, SRIP = self.socketClient.recvfrom(256) data = self.protocol.parse(rawData) self.log.msg(data) bge.logic.sendMessage("SpawnEnemy", data, "Source", "Source") except: pass
class Splash(object): ''' this class is the Splash ''' def __init__(self,cont): self.cont = cont self.own = cont.owner self.log = Logging("Splash","Debug") self.scene = bge.logic.getCurrentScene() self.title = self.scene.objects["Title"] self.title.text = "SAND-Matrix" self.log.msg("Init complete.") def update(self): self._handleMessage() def _handleMessage(self): pass
class Menu(object): ''' this class is the main entry point and control of the game scene ''' def __init__(self,cont): self.cont = cont self.own = cont.owner self.scene = bge.logic.getCurrentScene() self.log = Logging("Menu","Debug") self.startGame = self.cont.actuators['StartGame'] self.startGame.scene="Main" self.mouse = self.cont.sensors['Mouse'] self.trigger = self.cont.sensors['Trigger'] self.log.msg('init completed') def update(self): if self.mouse.positive or self.trigger.positive: self.log.msg('here') self.cont.activate(self.startGame)
class GunUdpListener(object): def __init__(self): self.host = "0.0.0.0" self.port = 10002 self.protocol = GunUdpHandler() self.socketClient = socket.socket(socket.AF_INET,socket.SOCK_DGRAM) self.socketClient.bind((self.host, self.port)) self.socketClient.setsockopt(socket.SOL_SOCKET, socket.SO_RCVBUF, 256) self.socketClient.setblocking(0) self.log = Logging("GunUdpListener","Debug") self.log.msg("Init Completed.") def update(self): try: rawData, SRIP = self.socketClient.recvfrom(256) #self.log.msg(rawData) data = self.protocol.parse(rawData) #self.log.msg(data) bge.logic.sendMessage("GunPos", data) except Exception as e: #self.log.msg(e) pass
class ProtocolHandler(object): """ Struct-based Binary Protocol Definition (UDP) ============================================= < = little endian L = Source IP (integer) L = Destination IP (integer) """ def __init__(self): self.protocolFormat = "<LL" self.log = Logging("ProtocolHandler", "Debug") self.log.msg("Init Completed.") def parse(self, rawData): data = struct.unpack(self.protocolFormat, rawData) return str(data[0]) + "," + str(data[1]) def _dec2dot(numbericIP): if type(numbericIP) == types.StringType and not numbericIP.isdigit(): return None numIP = long(numbericIP) return "%d.%d.%d.%d" % ((numIP >> 24) & 0xFF, (numIP >> 16) & 0xFF, (numIP >> 8) & 0xFF, numIP & 0xFF)
class GunHID(object): gun = {'key1':1, 'key2':2, 'key3':4, 'key4':8, 'key5':16, 'key7':32, 'key8':64, 'key8':128, 'key9':256, 'key10':512, 'hatCenter':1, 'hatLeft':2, 'hatRight':4, 'hatForward':8, 'hatBack':16 } def __init__(self,mousemovecore): self.core = mousemovecore self.cont = self.core.cont self.log = Logging("GunHID","Debug") self.log.msg("Init Completed.") def getWindowSize(self): return (render.getWindowWidth(), render.getWindowHeight()) def getCenter(self): size = self.getWindowSize() screenCenter = (size[0]//2, size[1]//2) return (screenCenter[0] * (1.0/size[0]), screenCenter[1] * (1.0/size[1])) def layout(self,controls): if(self.recieveGunMsg()): controls.forward = 2 if (self.hat&self.gun['hatForward']==self.gun['hatForward']) else 0 controls.back = 2 if (self.hat&self.gun['hatBack']==self.gun['hatBack']) else 0 controls.left = 2 if (self.hat&self.gun['hatLeft']==self.gun['hatLeft']) else 0 controls.right = 2 if (self.hat&self.gun['hatRight']==self.gun['hatRight']) else 0 controls.up = 0 controls.down = 0 controls.crouch = 0 controls.run = 0 else: controls.forward = 0 controls.back = 0 controls.left = 0 controls.right = 0 controls.up = 0 controls.down = 0 controls.crouch = 0 controls.run = 0 def recieveGunMsg(self): gunpos = self.cont.sensors["GunPos"] if(gunpos.positive): gunData = [ i for i in gunpos.bodies[gunpos.subjects.index("GunPos")].split(',')] self.pitch = self.convert(float(gunData[0])*-1) self.roll = self.convert(float(gunData[1])*-1) self.trigger = int(float(gunData[2])) self.wheel = int(float(gunData[3])) self.hat = int(float(gunData[4])) self.keys = int(float(gunData[5])) if(self.trigger): #self.log.msg("trigger") logic.sendMessage("Trigger", '1') #self.log.msg("hat: "+str(self.hat)) return True def convert(self,x): return (x+5)/10 def pos(self): if(self.recieveGunMsg()): return [self.roll,self.pitch] else: return logic.mouse.position