Advertisement
philRG

Class Unit MM

Nov 1st, 2021
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.73 KB | None | 0 0
  1. class Position:
  2.     def __init__(self, x, y):
  3.         self.x, self.y = x, y
  4.         self.radius = math.sqrt(self.x ** 2 + self.y ** 2)
  5.  
  6.     def dist(self, other):
  7.         return math.sqrt((other.y - self.y) ** 2 + (other.x - self.x) ** 2)
  8.  
  9.     def __repr__(self):
  10.         return f'{self.x} {self.y}'
  11.  
  12. class Unit:
  13.     def __init__(self, type, player, mass, radius, x, y, vx, vy):
  14.         self.type = UnitType(type)
  15.         self.player = player
  16.         self.mass = mass
  17.         self.radius = radius
  18.         self.pos = Position(x, y)
  19.         self.speed = Position(vx, vy)
  20.  
  21.     def move_to(self, unit) -> Tuple[int, int]:
  22.         vx, vy = self.speed.x, self.speed.y
  23.         if abs(vx) + abs(vy) < 10:
  24.             x, y = unit.pos.x, unit.pos.y
  25.         else:
  26.             x, y = unit.pos.x - MINUS_V * vx, unit.pos.y - MINUS_V * vy
  27.         return x, y
  28.  
  29.     def near(self, units) -> bool:
  30.         for u in units:
  31.             if self.dist(u) < u.radius + 10:
  32.                 return True
  33.         return False
  34.  
  35.     def inside(self, units) -> bool:
  36.         for u in units:
  37.             if self.dist(u) < u.radius:
  38.                 return True
  39.         return False
  40.  
  41.     def dist(self, other) -> int:
  42.         return self.pos.dist(other.pos)
  43.  
  44.     def send_oil_to(self, target) -> Position:
  45.         target_location = np.array([target.pos.x, target.pos.y])
  46.         source_location = np.array([self.pos.x, self.pos.y])
  47.         direction = target_location - source_location
  48.         norm = np.linalg.norm(direction)
  49.         vector = direction / norm
  50.         _range = self.dist(target) - SKILL_RANGE + 1
  51.         point = np.array([self.pos.x, self.pos.y]) + np.array([_range * vector[0], _range * vector[1]])
  52.         return Position(*point)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement