Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 17 additions & 2 deletions src/environment.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 8 additions & 2 deletions src/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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)
Expand Down
7 changes: 3 additions & 4 deletions src/ui.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__)
Expand Down
Loading