From a64e705731e65451aeeb2e3bf30b5820029a9186 Mon Sep 17 00:00:00 2001 From: Sam <61994039+fourthDimensional@users.noreply.github.com> Date: Sat, 7 Jun 2025 22:58:18 -0500 Subject: [PATCH] Add TestVelocityObject to simulate moving entities in the world --- main.py | 8 +++-- world/render_objects.py | 67 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 72 insertions(+), 3 deletions(-) diff --git a/main.py b/main.py index f439e89..7812e1f 100644 --- a/main.py +++ b/main.py @@ -4,7 +4,7 @@ import sys import random from world.world import World, Position -from world.render_objects import DebugRenderObject, FoodObject +from world.render_objects import DebugRenderObject, FoodObject, TestVelocityObject from world.simulation_interface import Camera # Initialize Pygame @@ -62,7 +62,7 @@ def draw_grid(screen, camera, showing_grid=True): ): return # Grid is completely off-screen - # Fill the grid area with dark gray background + # Fill the grid area awith dark gray background grid_rect = pygame.Rect( max(0, grid_left), max(0, grid_top), @@ -146,11 +146,13 @@ def main(): world = World(CELL_SIZE, (CELL_SIZE * GRID_WIDTH, CELL_SIZE * GRID_HEIGHT)) # sets seed to 67 >_< - random.seed(67) + random.seed(0) if FOOD_SPAWNING: world.add_object(FoodObject(Position(x=random.randint(-100, 100), y=random.randint(-100, 100)))) + world.add_object(TestVelocityObject(Position(x=random.randint(-100, 100), y=random.randint(-100, 100)))) + running = True while running: deltatime = clock.get_time() / 1000.0 # Convert milliseconds to seconds diff --git a/world/render_objects.py b/world/render_objects.py index f75b9e3..1a4e8b0 100644 --- a/world/render_objects.py +++ b/world/render_objects.py @@ -171,3 +171,70 @@ class FoodObject(BaseEntity): :return: String representation. """ return f"FoodObject({self.position}, decay={self.decay:.1f}, decay_rate={self.decay_rate * (1 + (self.neighbors / 10))})" + + +class TestVelocityObject(BaseEntity): + """ + Test object that moves in a randomly set direction. + """ + + def __init__(self, position: Position) -> None: + """ + Initializes the test velocity object. + + :param position: The position of the object. + """ + super().__init__(position) + self.velocity = (random.uniform(-0.1, 0.5), random.uniform(-0.1, 0.5)) + self.max_visual_width: int = 10 + self.interaction_radius: int = 50 + self.flags: dict[str, bool] = { + "death": False, + "can_interact": True, + } + + def tick(self, interactable: Optional[List[BaseEntity]] = None) -> "TestVelocityObject": + """ + Updates the object by moving it according to its velocity. + + :param interactable: List of nearby entities (unused). + :return: Self. + """ + if interactable is None: + interactable = [] + + x, y = self.position.get_position() + x += self.velocity[0] + y += self.velocity[1] + self.position.set_position(x, y) + + return self + + def render(self, camera: Any, screen: Any) -> None: + """ + Renders the test object as a circle. + + :param camera: The camera object for coordinate transformation. + :param screen: The Pygame screen surface. + """ + if camera.is_in_view(*self.position.get_position()): + pygame.draw.circle( + screen, + (0, 255, 0), + camera.world_to_screen(*self.position.get_position()), + int(5 * camera.zoom) + ) + + def __repr__(self) -> str: + """ + Returns a string representation of the test object. + + :return: String representation. + """ + return f"TestVelocityObject({self.position}, velocity={self.velocity})" + + +class DefaultCell(BaseEntity): + """ + Cell object + """