def proceed_packet(self, dt: float): units, timestamp = self.game_client.get_message() if not units: return ping = time() - timestamp print(ping) t_units = {} #self.units.clear() for p in units: id = int(p['id']) new_dir = Point(float(p['dx']), float(p['dy'])) new_pos = Point(float(p['x']), float( p['y'])) + new_dir * ping * 0.5 unit = self.units.get(id) if unit: if new_dir != unit.dir: unit.dir = new_dir if new_pos.distance(unit.pos) > MOVE_DIST: unit.pos = new_pos else: arg = { 'id': id, 'cls': p['cls'], 'pos': new_pos, 'dir': new_pos } if p['cls'] in CHARACTERS: arg['name'] = p['name'] unit = Character(**arg) else: unit = Unit(**arg) if p['cls'] in CHARACTERS: unit.hp = p['hp'] t_units[id] = unit self.units = t_units
def init_game_view(self, loaded_map, init_response): self.players = {} # Player replicas. for player in init_response['players']: if player[1] == init_response['id']: # Local player self.char = Character(nick=player[0], playerid=player[1], team=player[2]) self.players[player[1]] = self.char else: self.players[player[1]] = Replica(nick=player[0], playerid=player[1], team=player[2], pos=(-42, -42)) self.mv = MapView(loaded_map, self.char, self.players) self.add_widget(self.mv) # self.add_widget(self.char) # after the map to keep it always visible! self.char.bind(offset=self.mv.update_pos) self.char.set_game_pos(init_response['pos']) # Also updates the map