95 lines
3.1 KiB
Python
95 lines
3.1 KiB
Python
from Empty import Empty
|
|
from Moving_truck import Moving_truck
|
|
from Package import Package
|
|
from Package_types import Package_types
|
|
from Shelf import Shelf
|
|
import pygame
|
|
import random
|
|
from Grid import Grid
|
|
from Truck import Truck
|
|
from Global_variables import Global_variables as G_var
|
|
from pygame.constants import *
|
|
|
|
from Astar import Pathfinding, State
|
|
|
|
|
|
class Environment:
|
|
def __init__(self, window):
|
|
self.window = window
|
|
self.grid = Grid(window)
|
|
self.initialize_eviroment_2d()
|
|
self.add_shelfs_to_enviroment_2d()
|
|
# TEST CREATE PACKAGE
|
|
new_package = Package(self.window, 26, 7)
|
|
self.enviroment_2d[26][7] = new_package
|
|
new_truck = Truck(window, 14, 7)
|
|
self.enviroment_2d[14][7] = new_truck
|
|
self.truck = new_truck
|
|
self.moving_truck = Moving_truck(
|
|
self.window, self.enviroment_2d, self.truck)
|
|
self.astar = Pathfinding(self.enviroment_2d)
|
|
|
|
def draw_all_elements(self):
|
|
for row in self.enviroment_2d:
|
|
for field in row:
|
|
field.draw()
|
|
self.grid.draw_grid()
|
|
# self.astar.draw_path(self.window)
|
|
pygame.display.flip()
|
|
|
|
def update_all_elements(self,event):
|
|
# self.use_astar()
|
|
self.update_truck(event)
|
|
|
|
|
|
def use_astar(self):
|
|
start_state = State(1,self.truck.x,self.truck.y)
|
|
package = self.find_packate()
|
|
end_state = State(1,package.x, package.y)
|
|
self.astar.find_path(start_state,end_state)
|
|
|
|
def find_packate(self): #ta funkcja została zrobiona na szybko, może nie być potrzebna w przyszłości kiedy
|
|
#ktoś wpadnie na lepsze rozwiązanie
|
|
for row in self.enviroment_2d:
|
|
for field in row:
|
|
if isinstance(field,Package):
|
|
return field
|
|
return None
|
|
|
|
def update_truck(self, event):
|
|
if event.type == KEYDOWN:
|
|
if event.key == K_LEFT:
|
|
self.moving_truck.move(-1, 0)
|
|
if event.key == K_RIGHT:
|
|
self.moving_truck.move(1, 0)
|
|
if event.key == K_UP:
|
|
self.moving_truck.move(0, -1)
|
|
if event.key == K_DOWN:
|
|
self.moving_truck.move(0, 1)
|
|
|
|
def gen_shelf_type(self):
|
|
shelve_types = list(Package_types)
|
|
while True:
|
|
yield random.choice(shelve_types)
|
|
|
|
def initialize_eviroment_2d(self):
|
|
self.enviroment_2d = [[
|
|
Empty(self.window, j, i)
|
|
for i in range(G_var().DIMENSION_Y)]
|
|
for j in range(G_var().DIMENSION_X)
|
|
]
|
|
|
|
def add_shelfs_to_enviroment_2d(self):
|
|
shelf_2_offset = 9
|
|
avaiable_types = self.gen_shelf_type()
|
|
for x in range(2, 22, 3):
|
|
type_of_new_shelf_1 = next(avaiable_types)
|
|
type_of_new_shelf_2 = next(avaiable_types)
|
|
for y in range(0, 6):
|
|
self.enviroment_2d[x][y] = Shelf(
|
|
self.window, x, y, type_of_new_shelf_1
|
|
)
|
|
self.enviroment_2d[x][y + shelf_2_offset] = Shelf(
|
|
self.window, x, (y + shelf_2_offset), type_of_new_shelf_2
|
|
)
|