158 lines
7.0 KiB
Python
158 lines
7.0 KiB
Python
import random
|
|
|
|
import pygame
|
|
|
|
from algorithms.a_star import a_star, State, TURN_RIGHT, TURN_LEFT, FORWARD, UP, DOWN, LEFT, RIGHT
|
|
from common.constants import *
|
|
from common.helpers import castle_neighbors
|
|
from logic.knights_queue import KnightsQueue
|
|
from logic.spawner import Spawner
|
|
from models.castle import Castle
|
|
from models.knight import Knight
|
|
from models.monster import Monster
|
|
from models.tile import Tile
|
|
|
|
|
|
class Level:
|
|
def __init__(self, screen, logs):
|
|
self.screen = screen
|
|
self.logs = logs
|
|
# sprite group setup
|
|
self.sprites = pygame.sprite.LayeredUpdates()
|
|
|
|
self.map = [['g' for _ in range(COLUMNS)] for y in range(ROWS)]
|
|
|
|
self.list_knights_blue = []
|
|
self.list_knights_red = []
|
|
self.list_monsters = []
|
|
self.list_castles = []
|
|
|
|
self.knights_queue = None
|
|
|
|
def create_map(self):
|
|
self.generate_map()
|
|
self.setup_base_tiles()
|
|
self.setup_objects()
|
|
self.knights_queue = KnightsQueue(self.list_knights_blue, self.list_knights_red)
|
|
|
|
def generate_map(self):
|
|
spawner = Spawner(self.map)
|
|
spawner.spawn_where_possible(['w' for _ in range(NBR_OF_WATER)])
|
|
spawner.spawn_where_possible(['t' for _ in range(NBR_OF_TREES)])
|
|
spawner.spawn_where_possible(['s' for _ in range(NBR_OF_SANDS)])
|
|
|
|
spawner.spawn_in_area(['k_b' for _ in range(4)], LEFT_KNIGHTS_SPAWN_FIRST_ROW, LEFT_KNIGHTS_SPAWN_FIRST_COL,
|
|
KNIGHTS_SPAWN_WIDTH, KNIGHTS_SPAWN_HEIGHT)
|
|
spawner.spawn_in_area(['k_r' for _ in range(4)], RIGHT_KNIGHTS_SPAWN_FIRST_ROW, RIGHT_KNIGHTS_SPAWN_FIRST_COL,
|
|
KNIGHTS_SPAWN_WIDTH, KNIGHTS_SPAWN_HEIGHT)
|
|
|
|
spawner.spawn_in_area(['c'], CASTLE_SPAWN_FIRST_ROW, CASTLE_SPAWN_FIRST_COL, CASTLE_SPAWN_WIDTH,
|
|
CASTLE_SPAWN_HEIGHT, 2)
|
|
|
|
spawner.spawn_where_possible(['m' for _ in range(NBR_OF_MONSTERS)])
|
|
|
|
def setup_base_tiles(self):
|
|
textures = []
|
|
for texture_path in TILES:
|
|
converted_texture = pygame.image.load('resources/textures/' + texture_path).convert_alpha()
|
|
converted_texture = pygame.transform.scale(converted_texture, (40, 40))
|
|
textures.append((texture_path, converted_texture))
|
|
|
|
for row_index, row in enumerate(self.map):
|
|
for col_index, col in enumerate(row):
|
|
|
|
# add base tiles, e.g. water, tree, grass
|
|
if col == "w":
|
|
texture_index = 5
|
|
texture_surface = textures[texture_index][1]
|
|
Tile((col_index, row_index), texture_surface, self.sprites, 'w')
|
|
elif col == "t":
|
|
texture_index = 6
|
|
texture_surface = textures[texture_index][1]
|
|
Tile((col_index, row_index), texture_surface, self.sprites, 't')
|
|
elif col == "s":
|
|
texture_index = 4
|
|
texture_surface = textures[texture_index][1]
|
|
Tile((col_index, row_index), texture_surface, self.sprites)
|
|
else:
|
|
texture_index = random.randint(0, 3)
|
|
texture_surface = textures[texture_index][1]
|
|
Tile((col_index, row_index), texture_surface, self.sprites)
|
|
|
|
def setup_objects(self):
|
|
castle_count = 0 # TODO: find some smarter method to print castle
|
|
|
|
for row_index, row in enumerate(self.map):
|
|
print(row)
|
|
for col_index, col in enumerate(row):
|
|
|
|
# add objects, e.g. knights, monsters, castle
|
|
if col == "k_b":
|
|
knight = Knight(self.screen, (col_index, row_index), self.sprites, "blue")
|
|
self.map[row_index][col_index] = knight
|
|
self.list_knights_blue.append(knight)
|
|
elif col == "k_r":
|
|
knight = Knight(self.screen, (col_index, row_index), self.sprites, "red")
|
|
self.map[row_index][col_index] = knight
|
|
self.list_knights_red.append(knight)
|
|
elif col == "m":
|
|
monster = Monster(self.screen, (col_index, row_index), self.sprites)
|
|
self.map[row_index][col_index] = monster
|
|
self.list_monsters.append(monster)
|
|
elif col == "c":
|
|
castle_count += 1
|
|
if castle_count == 4:
|
|
castle = Castle(self.screen, (col_index, row_index), self.sprites)
|
|
self.map[row_index][col_index] = castle
|
|
self.list_castles.append(castle)
|
|
|
|
def handle_turn(self):
|
|
print("next turn")
|
|
current_knight = self.knights_queue.dequeue_knight()
|
|
knight_pos_x = current_knight.position[0]
|
|
knight_pos_y = current_knight.position[1]
|
|
|
|
castle_cords = (self.list_castles[0].position[0], self.list_castles[0].position[1])
|
|
goal_list = castle_neighbors(self.map, castle_cords[0], castle_cords[1]) # list of castle neighbors
|
|
|
|
state = State((knight_pos_y, knight_pos_x), current_knight.direction.name)
|
|
action_list = a_star(state, self.map, goal_list)
|
|
print(action_list)
|
|
|
|
if len(action_list) == 0:
|
|
return
|
|
|
|
next_action = action_list.pop(0)
|
|
if next_action == TURN_LEFT:
|
|
self.logs.enqueue_log(f'AI {current_knight.team}: Obrót w lewo.')
|
|
current_knight.rotate_left()
|
|
elif next_action == TURN_RIGHT:
|
|
self.logs.enqueue_log(f'AI {current_knight.team}: Obrót w prawo.')
|
|
current_knight.rotate_right()
|
|
elif next_action == FORWARD:
|
|
current_knight.step_forward()
|
|
self.map[knight_pos_y][knight_pos_x] = 'g'
|
|
|
|
# update knight on map
|
|
if current_knight.direction.name == UP:
|
|
self.logs.enqueue_log(f'AI {current_knight.team}: Ruch do góry.')
|
|
self.map[knight_pos_y - 1][knight_pos_x] = current_knight.team_alias()
|
|
elif current_knight.direction.name == RIGHT:
|
|
self.logs.enqueue_log(f'AI {current_knight.team}: Ruch w prawo.')
|
|
self.map[knight_pos_y][knight_pos_x + 1] = current_knight.team_alias()
|
|
elif current_knight.direction.name == DOWN:
|
|
self.logs.enqueue_log(f'AI {current_knight.team}: Ruch w dół.')
|
|
self.map[knight_pos_y + 1][knight_pos_x] = current_knight.team_alias()
|
|
elif current_knight.direction.name == LEFT:
|
|
self.logs.enqueue_log(f'AI {current_knight.team}: Ruch w lewo.')
|
|
self.map[knight_pos_y][knight_pos_x - 1] = current_knight.team_alias()
|
|
|
|
def update(self):
|
|
bg_width = (GRID_CELL_PADDING + GRID_CELL_SIZE) * COLUMNS + BORDER_WIDTH
|
|
bg_height = (GRID_CELL_PADDING + GRID_CELL_SIZE) * ROWS + BORDER_WIDTH
|
|
pygame.draw.rect(self.screen, (255, 255, 255), pygame.Rect(5, 5, bg_width, bg_height), 0, BORDER_RADIUS)
|
|
|
|
# update and draw the game
|
|
self.sprites.draw(self.screen)
|
|
self.sprites.update()
|
|
|