Implement optimized collision detection system using NumPy
- Introduced a hybrid collision detection approach that utilizes NumPy for vectorized operations, improving performance for games with many entities (200+). - Added a spatial grid for efficient lookups and AABB (Axis-Aligned Bounding Box) collision detection. - Implemented a new `CollisionSystem` class with methods for registering units, checking collisions, and managing spatial data. - Created performance tests to benchmark the new collision system against the old O(n²) method, demonstrating significant speed improvements. - Updated existing code to integrate the new collision detection system and ensure compatibility with game logic.
This commit is contained in:
+12
-6
@@ -6,14 +6,20 @@ import uuid
|
||||
AGE_THRESHOLD = 200
|
||||
|
||||
|
||||
from .unit import Unit
|
||||
from engine.collision_system import CollisionLayer
|
||||
|
||||
|
||||
class Point(Unit):
|
||||
def __init__(self, game, position=(0,0), id=None, value=5):
|
||||
super().__init__(game, position, id)
|
||||
# Specific attributes for points
|
||||
self.speed = 4 # Points age faster
|
||||
self.fight = False
|
||||
"""
|
||||
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.game.add_point(self.value)
|
||||
self.speed = 1 # Points don't move but need speed for draw timing
|
||||
|
||||
def move(self):
|
||||
self.age += self.speed
|
||||
|
||||
Reference in New Issue
Block a user