Fix gas poisoning, explosion hit detection and render order

- Move gas poisoning from Gas.move() to Gas.collisions() so all rats are
  registered in the collision system before the poison query runs.
- Shrink gas and explosion bboxes so rats must be well inside the tile to
  be poisoned/killed.
- Use AABB overlap instead of partial_move threshold for gas poisoning.
- Draw mobile units first, then top-layer effects (gas, mines, bombs,
  explosions) so rats appear under the gas.
- Add draw_on_top hint to Unit base class and top-layer units.
This commit is contained in:
Matteo Benedetto
2026-06-16 19:32:50 +02:00
parent 3e8cd97fda
commit 2f8e3e8b28
7 changed files with 88 additions and 33 deletions
+19 -22
View File
@@ -1,11 +1,14 @@
from .unit import Unit, UnitType
from engine.collision_system import CollisionLayer
from engine.collision_system import CollisionLayer, shrink_bbox
import random
# Costanti
AGE_THRESHOLD = 200
GAS_BBOX_SHRINK = 0.3 # central 40% of the tile is poisonous
class Gas(Unit):
draw_on_top = True
def __init__(self, game, position=(0,0), id=None, parent_id=None):
super().__init__(game, position, id, collision_layer=CollisionLayer.GAS, unit_type=UnitType.GAS)
self.parent_id = parent_id
@@ -25,27 +28,14 @@ class Gas(Unit):
return
self.age += 1
# Use optimized collision system to find rats in gas cloud
victim_ids = self.game.collision_system.get_units_in_cell(
self.position, use_before=False
# Register a smaller bbox so rats must be well inside the tile to be gassed
cell_bbox = (
self.position[0] * self.game.cell_size,
self.position[1] * self.game.cell_size,
(self.position[0] + 1) * self.game.cell_size,
(self.position[1] + 1) * self.game.cell_size,
)
for victim_id in victim_ids:
victim = self.game.unit_manager.get_unit_by_id(victim_id)
if victim and victim.type in [UnitType.RAT_MALE, UnitType.RAT_FEMALE]:
if victim.partial_move > 0.5:
victim.gassed += 1
# Check position_before as well
victim_ids_before = self.game.collision_system.get_units_in_cell(
self.position, use_before=True
)
for victim_id in victim_ids_before:
victim = self.game.unit_manager.get_unit_by_id(victim_id)
if victim and victim.type in [UnitType.RAT_MALE, UnitType.RAT_FEMALE]:
if victim.partial_move < 0.5:
victim.gassed += 1
self.bbox = shrink_bbox(cell_bbox, GAS_BBOX_SHRINK)
if self.age % self.speed:
return
@@ -61,8 +51,15 @@ class Gas(Unit):
if not any(unit.type == UnitType.GAS for unit in self.game.units.values() if unit.position == (new_x, new_y)):
print(f"Spreading gas from {self.position} to ({new_x}, {new_y})")
self.game.unit_manager.spawn_unit(Gas, (new_x, new_y), parent_id=self.parent_id if self.parent_id else self.id)
def collisions(self):
pass
"""Poison rats that overlap the shrunk gas tile in Pass 2."""
for _, victim_id in self.game.collision_system.get_collisions_for_unit(
self.id, CollisionLayer.GAS, tolerance=0
):
victim = self.game.unit_manager.get_unit_by_id(victim_id)
if victim and victim.type in [UnitType.RAT_MALE, UnitType.RAT_FEMALE] and victim.id in self.game.units:
victim.gassed += 1
def die(self, unit=None, score=None):
if not unit: