Files
mice/units/points.py
enne2 b4224ed3a1 v1.1: Performance optimizations and bug fixes
Major improvements:
- NumPy-based collision system supporting 200+ units (~3ms/frame)
- Spatial hashing with vectorized distance calculations
- 4-pass game loop ensuring correct collision timing
- Blood overlay system with pre-generated stain pool
- Cached render positions and viewport bounds
- Spawn protection preventing rats spawning on weapons

Bug fixes:
- Fixed bombs not killing rats (collision system timing)
- Fixed gas not affecting rats (collision system timing)
- Fixed rats spawning on weapons (added has_weapon_at check)
- Fixed AttributeError with Gas collisions (added isinstance check)
- Fixed blood stain transparency (RGBA + SDL_BLENDMODE_BLEND)
- Reduced point lifetime from 200 to 90 frames (~1.5s)
- Blood layer now clears on game restart

Technical changes:
- Added engine/collision_system.py with CollisionLayer enum
- Updated all units to use collision layers
- Pre-allocate NumPy arrays with capacity management
- Hybrid collision approach (<10 simple, ≥10 vectorized)
- Python 3.13 compatibility
2025-10-24 20:04:26 +02:00

49 lines
1.6 KiB
Python

from .unit import Unit
import random
import uuid
# Costanti - Points disappear after ~1.5 seconds (90 frames at 60 FPS)
AGE_THRESHOLD = 90
from .unit import Unit
from engine.collision_system import CollisionLayer
class Point(Unit):
"""
Represents a collectible point in the game.
Appears when a rat dies and can be collected by the player.
"""
def __init__(self, game, position=(0,0), id=None, value=10):
super().__init__(game, position, id, collision_layer=CollisionLayer.POINT)
self.value = value
self.speed = 1 # Points don't move but need speed for draw timing
def move(self):
self.age += self.speed
if self.age == AGE_THRESHOLD:
self.die()
def collisions(self):
pass
def die(self, unit=None, score=None):
"""Handle point cleanup. Points just disappear when they expire."""
target_unit = unit if unit else self
# Use base class cleanup
super().die()
def draw(self):
image = self.game.assets[f"BMP_BONUS_{self.value}"]
image_size = self.game.render_engine.get_image_size(image)
self.rat_image = image
partial_x, partial_y = 0, 0
x_pos = self.position_before[0] * self.game.cell_size + (self.game.cell_size - image_size[0]) // 2 + partial_x
y_pos = self.position_before[1] * self.game.cell_size + (self.game.cell_size - image_size[1]) // 2 + partial_y
self.game.render_engine.draw_image(x_pos, y_pos, image, anchor="nw", tag="unit")