Source code for game.entities.entity_manager

"""Entity management system."""

import random
import time
from .pig import Pig
from .villager import Villager
from ..utils import constants

[docs] class EntityManager: """Manages all entities in the game.""" def __init__(self, isometric_map):
[docs] self.isometric_map = isometric_map
[docs] self.pigs = []
[docs] self.villagers = []
[docs] self.last_pig_spawn = 0
[docs] self.pig_spawn_accumulator = 0
def _find_valid_spawn_point(self): """ Finds a random, valid spawn point on a grass or forest tile. Returns a tuple (x, y) of world coordinates, or None if no point is found. """ max_attempts = 100 for _ in range(max_attempts): # Calculate the world coordinate boundaries of the map map_min_x = self.isometric_map.map_offset_x map_min_y = self.isometric_map.map_offset_y map_max_x = map_min_x + self.isometric_map.width map_max_y = map_min_y + self.isometric_map.height # Generate a random world coordinate within these boundaries if map_min_x >= map_max_x or map_min_y >= map_max_y: print("Warning: Invalid map dimensions, cannot spawn entities.") return None x = random.randint(map_min_x, map_max_x - 1) y = random.randint(map_min_y, map_max_y - 1) # Explicitly check if the tile type is suitable for spawning tile_type = self.isometric_map.get_tile_type(x, y) if tile_type in ['grass', 'forest', 'degraded_grass', 'degraded_forest']: return x, y print("Warning: Could not find a valid spawn point after many attempts.") return None
[docs] def spawn_pig(self): """Spawn a new pig at a random valid location.""" spawn_point = self._find_valid_spawn_point() if spawn_point: x, y = spawn_point pig = Pig(x, y) self.pigs.append(pig)
[docs] def spawn_villager(self): """Spawn a new villager at a random valid location.""" spawn_point = self._find_valid_spawn_point() if spawn_point: x, y = spawn_point villager = Villager(x, y) self.villagers.append(villager)
[docs] def should_spawn_pig(self, dt): """Check if a new pig should be spawned.""" self.pig_spawn_accumulator += dt if self.pig_spawn_accumulator >= 1.0 / constants.PIG_BIRTH_RATE: self.pig_spawn_accumulator = 0 return True return False
[docs] def remove_pig(self): """Remove one pig (send to another island).""" if self.pigs: self.pigs.pop(0) return True return False
[docs] def emergency_feast(self): """Remove half the pigs for emergency feast.""" if not self.pigs: return 0 pigs_to_remove = len(self.pigs) // 2 if pigs_to_remove == 0: pigs_to_remove = 1 for _ in range(pigs_to_remove): if self.pigs: self.pigs.pop(0) return pigs_to_remove
[docs] def get_pig_count(self): """Get current number of pigs.""" return len(self.pigs)
[docs] def get_villager_count(self): """Get current number of villagers.""" return len(self.villagers)
[docs] def update(self, dt): """Update all entities.""" # Update pigs for pig in self.pigs: pig.update(dt, self.isometric_map) # Update villagers for villager in self.villagers: villager.update(dt, self.isometric_map)
[docs] def render(self, screen): """Render all entities.""" # Render pigs for pig in self.pigs: pig.render(screen, self.isometric_map) # Render villagers for villager in self.villagers: villager.render(screen, self.isometric_map)