diff --git a/src/environment.py b/src/environment.py index fd68299..cda089c 100644 --- a/src/environment.py +++ b/src/environment.py @@ -272,11 +272,26 @@ def shutdown(self): if self.simulation_thread.is_alive(): self.simulation_thread.join(timeout=1.0) + def reset(self): + with self.data_lock: + self.tick_counter = 0 + self.food_sources = [] + self.food_items = [] + self.agents = [] + self.area_food_sources = { + area: 0 for area in Area + } + self._spawn_initial_food_sources() + self._spawn_initial_agents() + + def pause(self): - self.pause_sim = True + with self.data_lock: + self.pause_sim = True def resume(self): - self.pause_sim = False + with self.data_lock: + self.pause_sim = False def get_agents(self): return self.agents diff --git a/src/run.py b/src/run.py index e483b9d..03e2289 100644 --- a/src/run.py +++ b/src/run.py @@ -8,6 +8,7 @@ from food import FoodSource from ui import UI import charts +from agent import Agent pygame.init() pygame.display.set_caption("Simulation") @@ -98,13 +99,18 @@ def _chart_hint_text() -> str: config.PANEL_WIDTH, config.PANEL_HEIGHT) if sim_rect.collidepoint(mouse_pos): - grid_x = (mouse_pos[0] - config.PANEL_X) // config.CELL_SIZE - grid_y = (mouse_pos[1] - config.PANEL_Y) // config.CELL_SIZE + pixel_x = (mouse_pos[0] - config.PANEL_X) + pixel_y = (mouse_pos[1] - config.PANEL_Y) + grid_x = pixel_x // config.CELL_SIZE + grid_y = pixel_y // config.CELL_SIZE if env_ui.current_brush: if isinstance(env_ui.current_brush, Area): env.change_area_at(grid_x, grid_y, env_ui.current_brush) elif isinstance(env_ui.current_brush, type(FoodSource)): env.add_manual_food_source(grid_x, grid_y, env_ui.current_brush) + elif isinstance(env_ui.current_brush, type(Agent)): + agent = Agent((pixel_x, pixel_y), env) + env.create_agent(agent) elif event.type == pygame.MOUSEWHEEL: if chart_rect.collidepoint(mouse_pos): charts.scroll(-event.y) diff --git a/src/ui.py b/src/ui.py index 453cfad..0192f02 100644 --- a/src/ui.py +++ b/src/ui.py @@ -208,13 +208,12 @@ def process_events(self, event: pygame.Event, env: Environment): elif event.ui_element == self.sim_pause_btn: env.pause() elif event.ui_element == self.sim_reset_btn: - # TODO reset simulation - pass + env.reset() elif event.ui_element == self.spawn_agent_btn: self.change_brush(Agent, 'Agent') elif event.ui_element in self.set_area_btns: - area = self.set_area_btns[event.ui_element] - self.change_brush(area, area.display_name) + area = self.set_area_btns[event.ui_element] + self.change_brush(area, area.display_name) elif event.ui_element in self.spawn_food_source_btns: food_source = self.spawn_food_source_btns[event.ui_element] self.change_brush(food_source, food_source.__name__)