Refactor unit classes to reduce code duplication and improve maintainability
- Updated the Unit base class to include common attributes and methods for all units. - Refactored Bomb, Point, and Rat classes to inherit from the new Unit class structure. - Implemented a consistent die method across units for better cleanup. - Removed redundant code in unit initialization and added specific attributes where necessary. - Deleted obsolete configuration file and added a comprehensive architecture guide for future reference.
This commit is contained in:
+16
-18
@@ -13,17 +13,12 @@ BABY_INTERVAL = 50
|
||||
|
||||
class Rat(Unit):
|
||||
def __init__(self, game, position=(0,0), id=None):
|
||||
super().__init__(position)
|
||||
self.id = id if id else uuid.uuid4()
|
||||
self.game = game
|
||||
self.position = self.find_next_position()
|
||||
self.bbox = (0, 0, 0, 0)
|
||||
self.stop = 0
|
||||
self.age = 0
|
||||
self.speed = .10
|
||||
self.partial_move = 0
|
||||
self.position_before = position
|
||||
super().__init__(game, position, id)
|
||||
# Specific attributes for rats
|
||||
self.speed = 0.10 # Rats are slower
|
||||
self.fight = False
|
||||
# Initialize position using pathfinding
|
||||
self.position = self.find_next_position()
|
||||
|
||||
def calculate_rat_direction(self):
|
||||
x, y = self.position
|
||||
@@ -94,10 +89,13 @@ class Rat(Unit):
|
||||
self.fuck(unit)
|
||||
|
||||
def die(self, unit=None, score=10):
|
||||
if not unit:
|
||||
unit = self
|
||||
self.game.units.pop(unit.id)
|
||||
self.game.spawn_unit(Point, unit.position_before, value=score)
|
||||
"""Handle rat death and spawn points."""
|
||||
target_unit = unit if unit else self
|
||||
# Use base class cleanup
|
||||
if target_unit.id in self.game.units:
|
||||
self.game.units.pop(target_unit.id)
|
||||
# Rat-specific behavior: spawn points
|
||||
self.game.spawn_unit(Point, target_unit.position_before, value=score)
|
||||
|
||||
def draw(self):
|
||||
start_perf = self.game.engine.get_perf_counter()
|
||||
@@ -121,8 +119,8 @@ class Rat(Unit):
|
||||
#self.game.engine.draw_rectangle(self.bbox[0], self.bbox[1], self.bbox[2] - self.bbox[0], self.bbox[3] - self.bbox[1], "unit")
|
||||
|
||||
class Male(Rat):
|
||||
def __init__(self, map, position=(0,0), id=None):
|
||||
super().__init__(map, position, id)
|
||||
def __init__(self, game, position=(0,0), id=None):
|
||||
super().__init__(game, position, id)
|
||||
self.sex = "MALE"
|
||||
|
||||
def fuck(self, unit):
|
||||
@@ -134,11 +132,11 @@ class Male(Rat):
|
||||
unit.babies = random.randint(1, 3)
|
||||
|
||||
class Female(Rat):
|
||||
def __init__(self, map, position=(0,0), id=None):
|
||||
def __init__(self, game, position=(0,0), id=None):
|
||||
super().__init__(game, position, id)
|
||||
self.sex = "FEMALE"
|
||||
self.pregnant = False
|
||||
self.babies = 0
|
||||
super().__init__(map, position, id)
|
||||
|
||||
def procreate(self):
|
||||
self.pregnant -= 1
|
||||
|
||||
Reference in New Issue
Block a user