Zaczątek generowania mapy (JESZCZE NIE DZIAŁA!)

This commit is contained in:
andrzej 2020-04-04 22:03:15 +02:00
parent 89b63b585c
commit fc7328470d
3 changed files with 126 additions and 25 deletions

View File

@ -1,5 +1,6 @@
class Forklifter:
def __init__(self, start_x, start_y):
class Agent:
def __init__(self, start_x, start_y, radius=5):
self.x = start_x
self.y = start_y
self.radius = radius

61
main.py
View File

@ -1,22 +1,59 @@
import pygame
import warehouse
import agent
import random
from attributes import PackSize
WINDOW_SIZE = (1000, 800)
COLORS = [(255, 255, 255), (0, 0, 0)]
WINDOW_SIZE = (600, 600)
COLORS = {
'white': (255, 255, 255),
'black': (0, 0, 0),
'gray': (128, 128, 128),
'darkgray': (60,60,60),
'yellow': (225,225,0)
}
COLOR_OF_FIELD = {
'Floor': 'gray',
'Rack': 'white',
'Pack': 'yellow'
}
TILE_WIDTH = 30
TILE_HEIGHT = 30
CIRCLE_CENTER_X, CIRCLE_CENTER_Y = int(TILE_WIDTH/2), int(TILE_HEIGHT/2)
class MainGameFrame:
def __init__(self):
self.display = pygame.display.set_mode(WINDOW_SIZE)
self.warehouse_map = warehouse.Warehouse(20, 20)
starting_x, starting_y = random.randrange(40), random.randrange(40)
self.agent = agent.Agent(starting_x, starting_y, 20)
def run(self):
self.draw_floor()
self.draw_agent()
while True:
pygame.display.update()
def main():
pygame.init()
display = pygame.display.set_mode(WINDOW_SIZE)
warehouse_map = warehouse.Warehouse(50, 50)
def draw_floor(self):
for x in range(self.warehouse_map.width):
for y in range(self.warehouse_map.height):
self.draw_field(x,y)
def draw_field(self, x, y):
current_tile = self.warehouse_map.tiles[x][y]
color = COLOR_OF_FIELD.get(current_tile.category.name, 'white')
color = COLORS[color]
pygame.draw.rect(self.display, COLORS['black'],
(x * TILE_WIDTH, y * TILE_HEIGHT, TILE_WIDTH, TILE_HEIGHT))
pygame.draw.rect(self.display, color,
((x * TILE_WIDTH) + 1, (y * TILE_HEIGHT) + 1, TILE_WIDTH - 1, TILE_HEIGHT - 1))
def draw_agent(self):
agent_position_x, agent_position_y = self.agent.x, self.agent.y
agent_screen_position = ((agent_position_x*TILE_WIDTH) + CIRCLE_CENTER_X, (agent_position_y*TILE_HEIGHT) + CIRCLE_CENTER_Y)
# import pdb
# pdb.set_trace()
print(warehouse_map)
for t in warehouse_map.tiles[0][:10]:
print(t)
while True:
pygame.display.flip()
pygame.draw.circle(self.display, COLORS['black'], agent_screen_position, self.agent.radius, int(self.agent.radius/2))
if __name__ == '__main__':
main()
maingame = MainGameFrame()
maingame.run()

View File

@ -1,6 +1,7 @@
from attributes import PackSize
import random
import queue
import itertools
class CategoryData:
def __init__(self, name, passable=False, can_store=True, location='general', pack_size=PackSize.ALL):
self.name = name
@ -28,31 +29,93 @@ CATEGORY = {
class Warehouse:
def __init__(self, width, length):
self.width = width
self.length = length
self.height = length
self.tiles = self.generate_map()
self.generate_racks()
# import pdb
# pdb.set_trace()
def __str__(self):
return "Magazyn {}x{}".format(self.width, self.length)
return "Magazyn {}x{}".format(self.width, self.height)
def generate_map(self):
warehouse_tiles = []
categories = list(CATEGORY.keys())
for x in range(self.width):
row = [Tile('floor', x, y) for y in range(self.length)]
row = [Tile('floor', x, y) for y in range(self.height)]
warehouse_tiles.append(row)
# warehouse_tiles = self.create_racks(20, warehouse_tiles)
return warehouse_tiles
# def create_racks(self, number_of_racks=0, warehouse_tiles=[]):
def generate_racks(self):
q = queue.Queue()
# import pdb
# pdb.set_trace()
node_x, node_y = random.randrange(1, self.width-1), random.randrange(1, self.height-1)
node = self.tiles[node_x][node_y]
next_node = None
self.tiles[node_x][node_y] = Tile('rack', node_x, node_y)
q.put(node)
import pdb
while not q.empty():
if next_node is not None:
q.put(next_node)
not_rack_nodes = self.get_not_rack_nodes(node_x, node_y)
if len(not_rack_nodes) == 0:
next_node = q.get()
while len(self.get_not_rack_nodes(next_node.x_position, next_node.y_position)) == 0:
if q.empty():
return
next_node = q.get()
else:
current_node = next_node if next_node is not None else node
next_node = random.choice(not_rack_nodes)
self.break_wall(next_node, current_node)
node_x = next_node.x_position
node_y = next_node.y_position
self.tiles[node_x][node_y] = Tile('rack', node_x, node_y)
def get_not_rack_nodes(self, node_x, node_y):
adjacent_tiles = self.get_adjacent_tiles(node_x, node_y)
possible_nodes = [line for line in adjacent_tiles if line.category.name != "Rack"]
return possible_nodes
def get_adjacent_tiles(self, x, y):
x_start = x - 2 if x - 2 >= 0 else 0
x_stop = x + 2 if x + 2 < self.width else self.width-1
y_start = y - 2 if y - 2 >= 0 else 0
y_stop = y + 2 if y + 2 < self.height else self.height - 1
adjacent_tiles = [self.tiles[x_start][y], self.tiles[x_stop][y], self.tiles[x][y_start], self.tiles[x][y_stop]]
return adjacent_tiles
def break_wall(self, next_node, current_node):
wall = self.get_wall(next_node, current_node)
self.tiles[wall.x_position][wall.y_position] = Tile("rack", wall.x_position, wall.y_position)
def get_wall(self, next_node, current_node):
x_relative = 0
y_relative = 0
if next_node.x_position > current_node.x_position:
x_relative = 1
elif next_node.x_position < current_node.x_position:
x_relative = -1
if next_node.y_position > current_node.y_position:
y_relative = 1
elif next_node.y_position < current_node.y_position:
y_relative = -1
wall_x = current_node.x_position + x_relative
wall_y = current_node.y_position + y_relative
wall = self.tiles[wall_x][wall_y]
return wall
class Tile:
def __init__(self, category, x_position, y_position):
self.category = CATEGORY.get(category, CATEGORY['floor'])
self.x_position = x_position
self.y_position = y_position
def __str__(self):
return "Tile type: {} on position ({},{})".format(self.category, self.x_position, self.y_position)