Guest User

UnitFactory

a guest
Jan 6th, 2025
36
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.44 KB | None | 0 0
  1. unit_configs = {
  2.     "Knight": {"hp": 150, "atk": 40, "range": 1, "speed": 3, "cost": 150},
  3.     "Archer": {"hp": 40, "atk": 30, "range": 5, "speed": 2, "cost": 50},
  4.     "Crossbowman": {"hp": 60, "atk": 40, "range": 4, "speed": 2, "cost": 75},
  5.     "Footman": {"hp": 80, "atk": 20, "range": 1, "speed": 2, "cost": 25},
  6.     "DeathKnight": {"hp": 140, "atk": 20, "range": 1, "speed": 2, "cost": 200, "lifesteal": 10}
  7. }
  8.  
  9.  
  10. class Unit:
  11.     def __init__(self, x, y, game, team, name, hp, atk, range, speed, cost, lifesteal=0):
  12.         self.x = x
  13.         self.y = y
  14.         self.name = name
  15.         self.hp = hp
  16.         self.atk = atk
  17.         self.range = range
  18.         self.speed = speed
  19.         self.cost = cost
  20.         self.dead = False
  21.         self.team = team
  22.         self.game = game
  23.         self.lifesteal = lifesteal
  24.         self.grid = Grid(matrix=self.game.unit_map.unit_map)
  25.         self.finder = AStarFinder()
  26.         self.ready_to_attack = False
  27.  
  28.  
  29. class UnitFactory:
  30.     _unit_configs = {}
  31.  
  32.     @classmethod
  33.     def register_unit(cls, unit_type, config):
  34.         UnitFactory._unit_configs[unit_type] = config
  35.  
  36.     @classmethod
  37.     def create_unit(cls, unit_type, x, y, team, game):
  38.         if unit_type not in UnitFactory._unit_configs:
  39.             raise ValueError(f"Unknown unit type: {unit_type}")
  40.         config = UnitFactory._unit_configs[unit_type]
  41.         return Unit(x=x, y=y, game=game, team=team, name=unit_type, **config)
Advertisement
Add Comment
Please, Sign In to add comment