Exemplo n.º 1
0
    def autosave(self):
        if SESSION.is_active():
            timer = Timer(SETTINGS.get(Setting.AUTOSAVE_INTERVAL), self.autosave)
            timer.daemon = True
            timer.start()

            SESSION.save_game_to_file(autosave=True)
Exemplo n.º 2
0
    def render(self, ui_screen):
        super().render(ui_screen)

        cursor = self.get_manager(Manager.PLAYER).cursors[Team.RED]

        # Generate a screen of the size of the map
        map = self.get_manager(Manager.MAP)
        map_screen = pygame.Surface(
            (map.width * GRID_WIDTH, map.height * GRID_HEIGHT),
            pygame.SRCALPHA, 32)
        SESSION.render(map_screen, ui_screen)

        # Render the current tile at the bottom of the screen
        ui_screen.fill(clear_color[Team.RED],
                       (0, RESOLUTION_HEIGHT - GRID_HEIGHT, RESOLUTION_WIDTH,
                        GRID_HEIGHT))

        # Render tiles
        tile_display = pygame.Surface((GRID_WIDTH * 2, GRID_HEIGHT),
                                      pygame.SRCALPHA, 32).convert()
        tile_display.set_alpha(255 if cursor.placing_tiles else 64)

        tile_display.blit(
            self.get_tile_sprite_for_tiletype(cursor.tile_type[0]), (0, 0))
        tile_display.blit(spr_cursor[Team.RED], (0, 0))
        tile_display.blit(
            self.get_tile_sprite_for_tiletype(cursor.tile_type[1]),
            (GRID_WIDTH, 0))
        tile_display.blit(spr_cursor[Team.BLUE], (GRID_WIDTH, 0))

        ui_screen.blit(tile_display, (0, RESOLUTION_HEIGHT - GRID_HEIGHT))

        # Render pieces
        piece_display = pygame.Surface((GRID_WIDTH, GRID_HEIGHT),
                                       pygame.SRCALPHA, 32).convert()
        piece_display.set_alpha(255 if not cursor.placing_tiles else 64)

        piece_display.fill(clear_color[cursor.team],
                           (0, 0, GRID_WIDTH, GRID_HEIGHT))
        piece_display.blit(
            self.get_piece_sprite_for_piecetype(cursor.piece_type,
                                                cursor.team), (0, 0))
        piece_display.blit(spr_cursor[cursor.team], (0, 0))

        ui_screen.blit(piece_display,
                       (GRID_WIDTH * 3, RESOLUTION_HEIGHT - GRID_HEIGHT))

        # Trim the screen to just the camera area
        camera_x, camera_y = self.get_manager(
            Manager.PLAYER).get_camera_coords()
        return map_screen.subsurface(
            (camera_x, camera_y, min(CAMERA_WIDTH,
                                     map_screen.get_size()[0]),
             min(CAMERA_HEIGHT,
                 map_screen.get_size()[1])))
Exemplo n.º 3
0
 def handle_menu_selection(self, event):
     if event.option == Option.MENU_SAVE_MAP:
         SESSION.save_map_to_file()
     elif event.option == Option.MENU_QUIT_BATTLE:
         publish_game_event(EventType.E_QUIT_BATTLE, {})
     elif event.option == Option.MENU_FILL_WITH_CURRENT_TILE:
         self.fill_with_current_tile()
     elif event.option == Option.MENU_DESTROY_ALL_PIECES:
         self.clear_all_pieces()
     elif event.option == Option.MENU_MIRROR_X:
         self.mirror_map(mirror_x=True, mirror_y=False)
     elif event.option == Option.MENU_MIRROR_Y:
         self.mirror_map(mirror_x=False, mirror_y=True)
Exemplo n.º 4
0
    def connect_to_host(self):
        self.send_message(MessageCode.NEW_CONNECTION, Team.NONE,
                          str(self.nickname))

        # Block until we receive the map information back from the host.
        while not self.map_data:
            try:
                self.network_step(blocking=True)
            except Exception as err:
                print("Failed to connect to host. Aborting.")
                ERROR_LOGGER.exception("Failed to connect to host", err)
                SESSION.reset()
                publish_game_event(EventType.E_QUIT_BATTLE, {})
                break
Exemplo n.º 5
0
    def render(self, ui_screen):
        super().render(ui_screen)

        # Generate a screen of the size of the map
        map = self.get_manager(Manager.MAP)
        map_screen = Surface((map.width * GRID_WIDTH, map.height * GRID_HEIGHT), SRCALPHA, 32)

        SESSION.render(map_screen, ui_screen)

        # Trim the screen to just the camera area
        camera_x, camera_y = self.get_manager(Manager.PLAYER).get_camera_coords()
        return map_screen.subsurface((camera_x, camera_y,
                                      min(CAMERA_WIDTH, map_screen.get_size()[0]),
                                      min(CAMERA_HEIGHT, map_screen.get_size()[1])))
Exemplo n.º 6
0
 def get_manager(self, manager_type):
     return SESSION.get(manager_type)
Exemplo n.º 7
0
    def step(self, event):
        super().step(event)

        SESSION.step(event)
Exemplo n.º 8
0
 def destroy(self):
     super().destroy()
     if SESSION:
         SESSION.reset()
Exemplo n.º 9
0
    def handle_message(self, message, address):
        print("Received message '{}' from '{}'".format(str(message.decode()),
                                                       str(address)))

        command = message.decode()[:9]
        team = Team(message.decode()[9:14])
        body = message.decode()[14:]

        if command == MessageCode.NEW_CONNECTION.value:
            print("Connection request from: " + str(address))
            self.clients.append(address)
            client_team = self.get_next_open_team(body)

            self.send_game_state_to_client(SESSION.save_game_to_string()[0],
                                           client_team.value)

            publish_game_event(EventType.NETWORK_CLIENT_CONNECTED, {
                'team': self.team,
                'nickname': str(body)
            })
        elif command == MessageCode.DROP_CONNECTION.value:
            print("Connection dropped for: " + str(address))
            self.clients.remove(address)
            self.remove_team(team)
            publish_game_event(EventType.NETWORK_CLIENT_DISCONNECTED, {
                'team': self.team,
                'nickname': str(body)
            })
        elif command == MessageCode.END_CONNECTION.value:
            print("Connection ended from: " + str(address))
            self.connection.close()
            self.quit_network_game(notify_host=False)
            publish_game_event_from_network(EventType.E_QUIT_BATTLE, {})
        elif command == MessageCode.SET_ORDERS.value:
            print("Setting orders from net msg: " + str(body))
            orders = literal_eval(body)

            parsed_orders = {}
            for coord, order in orders.items():
                if order:
                    parsed_orders[coord] = deserialize_order(order)
                else:
                    parsed_orders[coord] = None

            self.get_manager(Manager.PIECE).set_orders(team, parsed_orders)
            publish_game_event_from_network(EventType.E_SUBMIT_TURN,
                                            {'team': team})
        elif command == MessageCode.SET_TEAM.value:
            print("Receiving our assigned team from host: " + str(body))
            self.team = Team(body)
        elif command == MessageCode.CANCEL_ORDERS.value:
            print("Canceling orders from net msg:" + str(body))
            publish_game_event_from_network(EventType.E_CANCEL_TURN,
                                            {'team': team})
        elif command == MessageCode.PLAYER_CONCEDED.value:
            print("Player has conceded from net msg: " + str(body))
            publish_game_event_from_network(EventType.E_CONCEDE,
                                            {'team': team})
        elif command == MessageCode.SET_GAME_STATE.value:
            print("Setting game state from net msg: " + str(body))
            self.map_data = body
        elif command == MessageCode.START_BATTLE.value:
            print("Network has started the battle")
            publish_game_event_from_network(EventType.E_START_NETWORK_BATTLE,
                                            {})
        elif command == MessageCode.TEAM_FILLED.value:
            print("Got filled team from network")
            self.fill_team(team, body)
        elif command == MessageCode.TEAM_EMPTIED.value:
            print("Emptied team from network")
            self.remove_team(team, notify=False)
Exemplo n.º 10
0
 def trigger_save_game(self, event):
     SESSION.save_game_to_file()