from .unit import Unit from .points import Point from engine.collision_system import CollisionLayer import random import uuid # Costanti AGE_THRESHOLD = 200 SPEED_REDUCTION = 0.5 PREGNANCY_DURATION = 500 BABY_INTERVAL = 50 class Rat(Unit): def __init__(self, game, position=(0,0), id=None): super().__init__(game, position, id, collision_layer=CollisionLayer.RAT) # Specific attributes for rats self.speed = 0.10 # Rats are slower self.fight = False self.gassed = 0 self.direction = "DOWN" # Default direction # Initialize position using pathfinding self.position = self.find_next_position() def calculate_rat_direction(self): x, y = self.position x_before, y_before = self.position_before if x > x_before: return "RIGHT" elif x < x_before: return "LEFT" elif y > y_before: return "DOWN" elif y < y_before: return "UP" else: return "DOWN" def find_next_position(self): neighbors = [] x, y = self.position for dx, dy in [(-1, 0), (1, 0), (0, -1), (0, 1)]: nx, ny = x + dx, y + dy if not self.game.map.in_bounds(nx, ny): continue if self.game.map.is_traversable(nx, ny) and (nx, ny) != self.position_before: neighbors.append((nx, ny)) if not neighbors: for dx, dy in [(-1, 0), (1, 0), (0, -1), (0, 1)]: nx, ny = x + dx, y + dy if not self.game.map.in_bounds(nx, ny): continue if self.game.map.is_traversable(nx, ny): neighbors.append((nx, ny)) if not neighbors: print(f"[flow] rat fallback: no traversable neighbors from position={self.position}", flush=True) return self.position self.position_before = self.position return random.choice(neighbors) def choked(self): self.game.render_engine.play_sound("CHOKE.WAV") self.die(score=10) def move(self): if self.gassed > 35: self.choked() return self.age += 1 if self.age == AGE_THRESHOLD: self.speed *= SPEED_REDUCTION if getattr(self, "pregnant", False): self.procreate() if self.stop: self.stop -= 1 return if self.partial_move < 1: self.partial_move = round(self.partial_move + self.speed, 2) if self.partial_move >= 1: self.partial_move = 0 self.position = self.find_next_position() self.direction = self.calculate_rat_direction() # Pre-calculate render position for draw() - optimization self._update_render_position() def _update_render_position(self): """Pre-calculate rendering position and bbox during move() to optimize draw()""" sex = self.sex if self.age > AGE_THRESHOLD else "BABY" # Get cached image size instead of calling get_image_size() image_size = self.game.rat_image_sizes[sex][self.direction] # Calculate partial movement offset if self.direction in ["UP", "DOWN"]: partial_x = 0 partial_y = self.partial_move * self.game.cell_size * (1 if self.direction == "DOWN" else -1) else: partial_x = self.partial_move * self.game.cell_size * (1 if self.direction == "RIGHT" else -1) partial_y = 0 # Calculate final render position self.render_x = self.position_before[0] * self.game.cell_size + (self.game.cell_size - image_size[0]) // 2 + partial_x self.render_y = self.position_before[1] * self.game.cell_size + (self.game.cell_size - image_size[1]) // 2 + partial_y # Update bbox for collision system self.bbox = (self.render_x, self.render_y, self.render_x + image_size[0], self.render_y + image_size[1]) def collisions(self): """ Optimized collision detection using the vectorized collision system. Uses spatial hashing and numpy for efficient checks with 200+ units. """ OVERLAP_TOLERANCE = self.game.cell_size // 4 # Only adult rats can collide for reproduction/fighting if self.age < AGE_THRESHOLD: return # Get collisions from the optimized collision system collisions = self.game.collision_system.get_collisions_for_unit( self.id, CollisionLayer.RAT, tolerance=OVERLAP_TOLERANCE ) # Process each collision for _, other_id in collisions: other_unit = self.game.get_unit_by_id(other_id) # Skip if not another Rat if not isinstance(other_unit, Rat): continue if not other_unit or other_unit.age < AGE_THRESHOLD: continue # Check if units are actually moving towards each other if self.position != other_unit.position_before: continue # Both units still exist in game if self.id in self.game.units and other_id in self.game.units: if self.sex == other_unit.sex and self.fight: # Same sex + fight mode = combat self.die(other_unit) elif self.sex != other_unit.sex: # Different sex = reproduction if "fuck" in dir(self): self.fuck(other_unit) def die(self, unit=None, score=10): """Handle rat death and spawn points.""" target_unit = unit if unit else self death_position = target_unit.position_before # Use base class cleanup if target_unit.id in self.game.units: self.game.units.pop(target_unit.id) # Rat-specific behavior: spawn points if score not in [None, 0]: self.game.add_point(score) self.game.spawn_unit(Point, death_position, value=score) # Add blood stain directly to background self.game.add_blood_stain(death_position) def draw(self): """Optimized draw using pre-calculated positions from move()""" sex = self.sex if self.age > AGE_THRESHOLD else "BABY" image = self.game.rat_assets_textures[sex][self.direction] image_size = self.game.rat_image_sizes[sex][self.direction] # Calculate render position if not yet set (first frame) if not hasattr(self, 'render_x'): self._calculate_render_position() # Match the original game: the visibility test uses the rat's center point, # not the sprite's top-left corner. center_x = int(self.render_x + image_size[0] / 2) center_y = int(self.render_y + image_size[1] / 2) cell_x = center_x // self.game.cell_size cell_y = center_y // self.game.cell_size if self.game.map.in_bounds(cell_x, cell_y): if self.game.map.is_tunnel(cell_x, cell_y): if self._draw_partially_hidden_in_tunnel(image, image_size, center_x, center_y, cell_x, cell_y): return return if not self.game.map.is_empty(cell_x, cell_y): return # Use pre-calculated positions self.game.render_engine.draw_image(self.render_x, self.render_y, image, anchor="nw", tag="unit") # bbox already updated in _update_render_position() #self.game.render_engine.draw_rectangle(self.bbox[0], self.bbox[1], self.bbox[2] - self.bbox[0], self.bbox[3] - self.bbox[1], "unit") def _draw_partially_hidden_in_tunnel(self, image, image_size, center_x, center_y, cell_x, cell_y): direction = self._get_tunnel_entrance_direction(cell_x, cell_y) if direction is None: return False local_x = center_x - cell_x * self.game.cell_size local_y = center_y - cell_y * self.game.cell_size half_cell = self.game.cell_size / 2 if direction == "UP": visible_ratio = max(0.0, min(1.0, (half_cell - local_y) / half_cell)) visible_height = int(round(image_size[1] * visible_ratio)) if visible_height <= 0: return False self.game.render_engine.draw_image( self.render_x, self.render_y, image, anchor="nw", tag="unit", source_rect=(0, 0, image_size[0], visible_height), dest_size=(image_size[0], visible_height), ) return True if direction == "DOWN": visible_ratio = max(0.0, min(1.0, (local_y - half_cell) / half_cell)) visible_height = int(round(image_size[1] * visible_ratio)) if visible_height <= 0: return False source_y = image_size[1] - visible_height draw_y = self.render_y + source_y self.game.render_engine.draw_image( self.render_x, draw_y, image, anchor="nw", tag="unit", source_rect=(0, source_y, image_size[0], visible_height), dest_size=(image_size[0], visible_height), ) return True if direction == "LEFT": visible_ratio = max(0.0, min(1.0, (half_cell - local_x) / half_cell)) visible_width = int(round(image_size[0] * visible_ratio)) if visible_width <= 0: return False self.game.render_engine.draw_image( self.render_x, self.render_y, image, anchor="nw", tag="unit", source_rect=(0, 0, visible_width, image_size[1]), dest_size=(visible_width, image_size[1]), ) return True visible_ratio = max(0.0, min(1.0, (local_x - half_cell) / half_cell)) visible_width = int(round(image_size[0] * visible_ratio)) if visible_width <= 0: return False source_x = image_size[0] - visible_width draw_x = self.render_x + source_x self.game.render_engine.draw_image( draw_x, self.render_y, image, anchor="nw", tag="unit", source_rect=(source_x, 0, visible_width, image_size[1]), dest_size=(visible_width, image_size[1]), ) return True def _get_tunnel_entrance_direction(self, cell_x, cell_y): directions = [ ("UP", 0, -1), ("DOWN", 0, 1), ("LEFT", -1, 0), ("RIGHT", 1, 0), ] empty_neighbors = [] for direction, dx, dy in directions: neighbor_x = cell_x + dx neighbor_y = cell_y + dy if self.game.map.in_bounds(neighbor_x, neighbor_y) and self.game.map.is_empty(neighbor_x, neighbor_y): empty_neighbors.append(direction) if len(empty_neighbors) != 1: return None return empty_neighbors[0] def _calculate_render_position(self): """Calculate render position and bbox (used when render_x not yet set)""" sex = self.sex if self.age > AGE_THRESHOLD else "BABY" image_size = self.game.render_engine.get_image_size( self.game.rat_assets_textures[sex][self.direction] ) partial_x, partial_y = 0, 0 if self.direction in ["UP", "DOWN"]: partial_y = self.partial_move * self.game.cell_size * (1 if self.direction == "DOWN" else -1) else: partial_x = self.partial_move * self.game.cell_size * (1 if self.direction == "RIGHT" else -1) self.render_x = self.position_before[0] * self.game.cell_size + (self.game.cell_size - image_size[0]) // 2 + partial_x self.render_y = self.position_before[1] * self.game.cell_size + (self.game.cell_size - image_size[1]) // 2 + partial_y self.bbox = (self.render_x, self.render_y, self.render_x + image_size[0], self.render_y + image_size[1]) class Male(Rat): def __init__(self, game, position=(0,0), id=None): super().__init__(game, position, id) self.sex = "MALE" def fuck(self, unit): if not unit.pregnant: self.game.render_engine.play_sound("SEX.WAV") self.stop = 100 unit.stop = 200 unit.pregnant = PREGNANCY_DURATION unit.babies = random.randint(1, 3) class Female(Rat): def __init__(self, game, position=(0,0), id=None): super().__init__(game, position, id) self.sex = "FEMALE" self.pregnant = False self.babies = 0 def procreate(self): self.pregnant -= 1 if self.pregnant == self.babies * BABY_INTERVAL: self.babies -= 1 self.stop = 20 if self.partial_move > 0.2: self.game.spawn_rat(self.position) else: self.game.spawn_rat(self.position_before) self.game.render_engine.play_sound("BIRTH.WAV")