65 lines
3.0 KiB
Python
65 lines
3.0 KiB
Python
from agentActionType import AgentActionType
|
|
import time
|
|
from turnCar import turn_left_orientation, turn_right_orientation
|
|
from garbageTruck import GarbageTruck
|
|
from typing import Tuple, Dict
|
|
from gridCellType import GridCellType
|
|
from gameContext import GameContext
|
|
from agentOrientation import AgentOrientation
|
|
import pygame
|
|
from bfs import find_path_to_nearest_can
|
|
from agentState import AgentState
|
|
|
|
|
|
def collect_garbage(game_context: GameContext) -> None:
|
|
while True:
|
|
start_agent_state = AgentState(game_context.dust_car.position, game_context.dust_car.orientation)
|
|
path = find_path_to_nearest_can(start_agent_state, game_context.grid, game_context.city)
|
|
if path == None or len(path) == 0:
|
|
break
|
|
move_dust_car(path, game_context)
|
|
next_position = calculate_next_position(game_context.dust_car)
|
|
game_context.grid[next_position] = GridCellType.VISITED_GARBAGE_CAN
|
|
game_context.city.cans_dict[next_position].is_visited = True
|
|
pass
|
|
|
|
|
|
def move_dust_car(actions: list[AgentActionType], game_context: GameContext) -> None:
|
|
for action in actions:
|
|
street_position = game_context.dust_car.position
|
|
has_to_render_street = False
|
|
if action == AgentActionType.TURN_LEFT:
|
|
game_context.dust_car.orientation = turn_left_orientation(game_context.dust_car.orientation)
|
|
elif action == AgentActionType.TURN_RIGHT:
|
|
game_context.dust_car.orientation = turn_right_orientation(game_context.dust_car.orientation)
|
|
elif action == AgentActionType.MOVE_FORWARD:
|
|
game_context.dust_car.position = calculate_next_position(game_context.dust_car)
|
|
has_to_render_street = True
|
|
game_context.dust_car.render(game_context)
|
|
if has_to_render_street:
|
|
if game_context.grid[street_position] == GridCellType.STREET_HORIZONTAL:
|
|
game_context.render_in_cell(street_position, "imgs/street_horizontal.png")
|
|
elif game_context.grid[street_position] == GridCellType.STREET_VERTICAL:
|
|
game_context.render_in_cell(street_position, "imgs/street_vertical.png")
|
|
elif game_context.grid[street_position] == GridCellType.SPEED_BUMP:
|
|
game_context.render_in_cell(street_position, "imgs/speed_bump.png")
|
|
pygame.display.update()
|
|
time.sleep(0.15)
|
|
|
|
|
|
def calculate_next_position(car: GarbageTruck) -> Tuple[int, int]:
|
|
if car.orientation == AgentOrientation.UP:
|
|
if car.position[1] - 1 < 1:
|
|
return None
|
|
return (car.position[0], car.position[1] - 1)
|
|
if car.orientation == AgentOrientation.DOWN:
|
|
if car.position[1] + 1 > 27:
|
|
return None
|
|
return (car.position[0], car.position[1] + 1)
|
|
if car.orientation == AgentOrientation.LEFT:
|
|
if car.position[0] - 1 < 1:
|
|
return None
|
|
return (car.position[0] - 1, car.position[1])
|
|
if car.position[0] + 1 > 27:
|
|
return None
|
|
return (car.position[0] + 1, car.position[1]) |