Add TestVelocityObject to simulate moving entities in the world
All checks were successful
Build Simulation and Test / Run All Tests (push) Successful in 2m13s
All checks were successful
Build Simulation and Test / Run All Tests (push) Successful in 2m13s
This commit is contained in:
parent
ac08ecbc27
commit
a64e705731
8
main.py
8
main.py
@ -4,7 +4,7 @@ import sys
|
|||||||
import random
|
import random
|
||||||
|
|
||||||
from world.world import World, Position
|
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
|
from world.simulation_interface import Camera
|
||||||
|
|
||||||
# Initialize Pygame
|
# Initialize Pygame
|
||||||
@ -62,7 +62,7 @@ def draw_grid(screen, camera, showing_grid=True):
|
|||||||
):
|
):
|
||||||
return # Grid is completely off-screen
|
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(
|
grid_rect = pygame.Rect(
|
||||||
max(0, grid_left),
|
max(0, grid_left),
|
||||||
max(0, grid_top),
|
max(0, grid_top),
|
||||||
@ -146,11 +146,13 @@ def main():
|
|||||||
world = World(CELL_SIZE, (CELL_SIZE * GRID_WIDTH, CELL_SIZE * GRID_HEIGHT))
|
world = World(CELL_SIZE, (CELL_SIZE * GRID_WIDTH, CELL_SIZE * GRID_HEIGHT))
|
||||||
|
|
||||||
# sets seed to 67 >_<
|
# sets seed to 67 >_<
|
||||||
random.seed(67)
|
random.seed(0)
|
||||||
|
|
||||||
if FOOD_SPAWNING:
|
if FOOD_SPAWNING:
|
||||||
world.add_object(FoodObject(Position(x=random.randint(-100, 100), y=random.randint(-100, 100))))
|
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
|
running = True
|
||||||
while running:
|
while running:
|
||||||
deltatime = clock.get_time() / 1000.0 # Convert milliseconds to seconds
|
deltatime = clock.get_time() / 1000.0 # Convert milliseconds to seconds
|
||||||
|
|||||||
@ -171,3 +171,70 @@ class FoodObject(BaseEntity):
|
|||||||
:return: String representation.
|
:return: String representation.
|
||||||
"""
|
"""
|
||||||
return f"FoodObject({self.position}, decay={self.decay:.1f}, decay_rate={self.decay_rate * (1 + (self.neighbors / 10))})"
|
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
|
||||||
|
"""
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user