diff --git a/config/constants.py b/config/constants.py index 7a74fed..f0ebad2 100644 --- a/config/constants.py +++ b/config/constants.py @@ -13,6 +13,7 @@ WHITE = (255, 255, 255) RED = (255, 0, 0) BLUE = (0, 0, 255) GREEN = (0, 255, 0) +ORANGE = (255, 165, 0) LIGHT_BLUE = (52, 134, 235) SELECTION_BLUE = (0, 128, 255) SELECTION_GRAY = (128, 128, 128, 80) diff --git a/core/renderer.py b/core/renderer.py index bb5e46a..30f030c 100644 --- a/core/renderer.py +++ b/core/renderer.py @@ -5,7 +5,7 @@ import pygame import math from config.constants import * from world.base.brain import CellBrain -from world.objects import DefaultCell +from world.objects import DefaultCell, FoodObject class Renderer: @@ -251,3 +251,68 @@ class Renderer: size = camera.get_relative_size(width) rect = pygame.Rect(screen_x - size // 2, screen_y - size // 2, size, size) pygame.draw.rect(self.render_area, SELECTION_BLUE, rect, 1) + + def render_food_connections(self, world, camera, selected_objects): + """Render orange bounding boxes around closest food and lines from selected cells.""" + if not selected_objects: + return + + # Find closest food to each selected cell + for selected_cell in selected_objects: + # Check if selected cell can have food interactions (DefaultCell) + if not isinstance(selected_cell, DefaultCell): + continue + + # Find all food objects in world + all_objects = world.get_objects() + food_objects = [obj for obj in all_objects if isinstance(obj, FoodObject)] + + if not food_objects: + continue + + # Get selected cell position + cell_x, cell_y = selected_cell.position.get_position() + + # Find closest food object + closest_food = None + closest_distance = float('inf') + + for food in food_objects: + food_x, food_y = food.position.get_position() + distance = ((food_x - cell_x) ** 2 + (food_y - cell_y) ** 2) ** 0.5 + + if distance < closest_distance: + closest_distance = distance + closest_food = food + + # Draw bounding box around closest food and connecting line + if closest_food and closest_food != selected_cell: + food_x, food_y = closest_food.position.get_position() + food_width = closest_food.max_visual_width + food_size = int(food_width * camera.zoom) + + # Calculate bounding box position (centered on food) + box_x, box_y = camera.world_to_screen(food_x, food_y) + box_rect = pygame.Rect( + box_x - food_size, + box_y - food_size, + food_size * 2, + food_size * 2 + ) + + # Draw orange bounding box (2 pixel width) + pygame.draw.rect(self.render_area, ORANGE, box_rect, 2) + + # Draw line from selected cell to closest food + screen_x, screen_y = camera.world_to_screen(cell_x, cell_y) + + # Calculate line thickness based on zoom (minimum 1 pixel) + line_thickness = max(1, int(2 * camera.zoom)) + + pygame.draw.line( + self.render_area, + ORANGE, + (screen_x, screen_y), + (box_x, box_y), + line_thickness + ) diff --git a/core/simulation_engine.py b/core/simulation_engine.py index 6bafabd..cf3a6f8 100644 --- a/core/simulation_engine.py +++ b/core/simulation_engine.py @@ -259,6 +259,12 @@ class SimulationEngine: self.input_handler.selected_objects, self.simulation_core.camera ) + if self.input_handler.show_interaction_radius: + self.renderer.render_food_connections( + self.simulation_core.world, + self.simulation_core.camera, + self.input_handler.selected_objects + ) self.screen.blit(self.sim_view, (self.sim_view_rect.left, self.sim_view_rect.top)) def _update_and_render_ui(self, deltatime):