Add comprehensive test suite for game mechanics and level handling
- Introduced `test_final_level_flow.py` to validate final level transitions and game end scenarios. - Created `test_game_over_flow.py` to ensure game over conditions trigger correctly based on rat counts. - Implemented `test_keybindings.py` to verify keybinding configurations and their context-specific actions. - Developed `test_level_editor.py` to assess level editor functionalities and layout computations. - Added `test_level_io.py` for testing level data serialization and deserialization. - Established `test_loop_logic_parity.py` to ensure consistent game state across multiple simulation runs. - Created `test_non_regression.py` to simulate game behavior and capture states for future verification. - Implemented `test_verify.py` to compare current game states against a golden master for regression detection.
This commit is contained in:
+36
-39
@@ -1,4 +1,4 @@
|
||||
from .unit import Unit
|
||||
from .unit import Unit, UnitType
|
||||
from .points import Point
|
||||
from engine.collision_system import CollisionLayer
|
||||
|
||||
@@ -13,8 +13,8 @@ BABY_INTERVAL = 50
|
||||
|
||||
|
||||
class Rat(Unit):
|
||||
def __init__(self, game, position=(0,0), id=None):
|
||||
super().__init__(game, position, id, collision_layer=CollisionLayer.RAT)
|
||||
def __init__(self, game, position=(0,0), id=None, unit_type=None):
|
||||
super().__init__(game, position, id, collision_layer=CollisionLayer.RAT, unit_type=unit_type)
|
||||
# Specific attributes for rats
|
||||
self.speed = 0.10 * getattr(self.game, "rat_speed_multiplier", 1.0)
|
||||
self.fight = False
|
||||
@@ -36,7 +36,7 @@ class Rat(Unit):
|
||||
return "UP"
|
||||
else:
|
||||
return "DOWN"
|
||||
|
||||
|
||||
def find_next_position(self):
|
||||
neighbors = []
|
||||
x, y = self.position
|
||||
@@ -84,17 +84,17 @@ class Rat(Unit):
|
||||
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.graphics.rat_image_sizes[sex][self.direction]
|
||||
|
||||
|
||||
# Calculate partial movement offset
|
||||
if self.direction in ["UP", "DOWN"]:
|
||||
partial_x = 0
|
||||
@@ -102,47 +102,46 @@ class Rat(Unit):
|
||||
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.
|
||||
Optimized collision detection using the spatial grid system.
|
||||
"""
|
||||
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.unit_manager.get_unit_by_id(other_id)
|
||||
|
||||
|
||||
# Skip if not another Rat
|
||||
if not isinstance(other_unit, Rat):
|
||||
if not other_unit or other_unit.type not in [UnitType.RAT_MALE, UnitType.RAT_FEMALE]:
|
||||
continue
|
||||
|
||||
if not other_unit or other_unit.age < AGE_THRESHOLD:
|
||||
|
||||
if 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:
|
||||
@@ -150,18 +149,18 @@ class Rat(Unit):
|
||||
self.die(other_unit)
|
||||
elif self.sex != other_unit.sex:
|
||||
# Different sex = reproduction
|
||||
if "fuck" in dir(self):
|
||||
if hasattr(self, 'fuck'):
|
||||
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.scoring.add_point(score)
|
||||
@@ -175,7 +174,7 @@ class Rat(Unit):
|
||||
sex = self.sex if self.age > AGE_THRESHOLD else "BABY"
|
||||
image = self.game.graphics.rat_assets_textures[sex][self.direction]
|
||||
image_size = self.game.graphics.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()
|
||||
@@ -194,11 +193,9 @@ class Rat(Unit):
|
||||
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)
|
||||
@@ -270,29 +267,29 @@ class Rat(Unit):
|
||||
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.graphics.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)
|
||||
super().__init__(game, position, id, unit_type=UnitType.RAT_MALE)
|
||||
self.sex = "MALE"
|
||||
|
||||
|
||||
def fuck(self, unit):
|
||||
if not unit.pregnant:
|
||||
self.game.render_engine.play_sound("SEX.WAV")
|
||||
@@ -300,14 +297,14 @@ class Male(Rat):
|
||||
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)
|
||||
super().__init__(game, position, id, unit_type=UnitType.RAT_FEMALE)
|
||||
self.sex = "FEMALE"
|
||||
self.pregnant = False
|
||||
self.babies = 0
|
||||
|
||||
|
||||
def procreate(self):
|
||||
self.pregnant -= 1
|
||||
if self.pregnant == self.babies * BABY_INTERVAL:
|
||||
|
||||
Reference in New Issue
Block a user