def serialize(self): owner = self.owner table = packer.Table('CubeSetup') table['id'] = self.net_id pos = owner.worldPosition table['pos_x'] = pos[0] table['pos_y'] = pos[1] table['pos_z'] = pos[2] rot = owner.worldOrientation.to_quaternion() table['rot_x'] = rot[0] table['rot_y'] = rot[1] table['rot_z'] = rot[2] table['rot_w'] = rot[3] lv = owner.getLinearVelocity(False) table['lv_x'] = lv[0] table['lv_y'] = lv[1] table['lv_z'] = lv[2] av = owner.getAngularVelocity(False) table['av_x'] = av[0] table['av_y'] = av[1] table['av_z'] = av[2] return packer.to_bytes(table)
def serialize(self): data = list(self.messages) table = packer.Table('ChatSetup') table.set('id', self.net_id) table.set('messages', data) return packer.to_bytes(table)
def send_click(self, sender): table = packer.Table('MessageToServer') table.set('id', self.net_id) table.set('message', self.entry.text) print(self.entry.text) buff = packer.to_bytes(table) self.entry.text = "" bge.logic.netplay.send_to_server(buff)
def update_player_input(self): held = bge.logic.KX_INPUT_ACTIVE events = bge.logic.keyboard.events if events[bge.events.WKEY] == held: self.keystate.set(1, (0,)) else: self.keystate.set(0, (0,)) if events[bge.events.SKEY] == held: self.keystate.set(1, (1,)) else: self.keystate.set(0, (1,)) if events[bge.events.AKEY] == held: self.keystate.set(1, (2,)) else: self.keystate.set(0, (2,)) if events[bge.events.DKEY] == held: self.keystate.set(1, (3,)) else: self.keystate.set(0, (3,)) # Toggle mouse focus if events[bge.events.ACCENTGRAVEKEY] == held: if not self.ACCENTPRESSED: self.ACCENTPRESSED = True self.freeMouse = not self.freeMouse if self.freeMouse: bge.render.showMouse(True) else: bge.render.showMouse(False) self.mouseInit = 6 else: self.ACCENTPRESSED = False self.mouseLook() # Send key state and rotation to server table = packer.Table('ClientState') table.set('id', self.net_id) table.set('input', self.keystate.uint) rot = self.head.worldOrientation.to_euler() table.set('rot_x', rot[0]) table.set('rot_z', rot[2]) buff = packer.to_bytes(table) # No point sending reliable if we're brute-forcing... bge.logic.netplay.send_to_server(buff, reliable=False)
def MessageToServer(self, table): fullmsg = "{}: {}".format(table.source, table.get('message')) self.messages.appendleft(fullmsg) if len(self.messages) > 12: self.messages.pop() logging.info("Chat: " + fullmsg) table = packer.Table('MessageToClient') table.set('id', self.net_id) table.set('fullmessage', fullmsg) buff = packer.to_bytes(table) net = bge.logic.netplay net.send_to_clients(buff)
def on_disconnect(self, peer_id): for comp in self.components: if comp is not None and peer_id in comp.permissions: comp.permissions.remove(peer_id) if len(comp.permissions) == 0: # Destroy the component self.components[comp.net_id] = None comp.owner.endObject() table = packer.Table('Destroy') table.set('id', comp.net_id) buff = packer.to_bytes(table) for client in self.clients: if client is not None and client.peer.incomingPeerID != peer_id: client.send_reliable(buff)
def serialize(self): table = packer.Table('PlayerSetup') pos = self.owner.worldPosition rot = self.owner.worldOrientation.to_quaternion() table.set('id', self.net_id) table.set('pos_x', pos[0]) table.set('pos_y', pos[1]) table.set('pos_z', pos[2]) table.set('rot_x', rot[0]) table.set('rot_y', rot[1]) table.set('rot_z', rot[2]) table.set('rot_w', rot[3]) table.set('input', self.keystate.uint) return packer.to_bytes(table)
def update_server(self): self.move() self.pos_timer -= 1 pos = False if not self.pos_timer: pos = True self.pos_timer = self.pos_timer_reset # Send key state, rotation, and pos to clients table = packer.Table('ClientStatePos') table.set('id', self.net_id) table.set('input', self.keystate.uint) rot = self.head.worldOrientation.to_euler() table.set('rot_x', rot[0]) table.set('rot_z', rot[2]) pos = self.owner.worldPosition table.set('pos_x', pos[0]) table.set('pos_y', pos[1]) table.set('pos_z', pos[2]) else: # Send key state and rotation to clients table = packer.Table('ClientState') table.set('id', self.net_id) table.set('input', self.keystate.uint) rot = self.head.worldOrientation.to_euler() table.set('rot_x', rot[0]) table.set('rot_z', rot[2]) # Send buff = packer.to_bytes(table) bge.logic.netplay.send_to_clients(buff)