62 lines
1.9 KiB
Python
62 lines
1.9 KiB
Python
import pygame
|
|
import math
|
|
from game.config import COLOR_WHITE, COLOR_ORANGE, COLOR_CYAN
|
|
from game.utils import distance
|
|
|
|
|
|
class Projectile:
|
|
def __init__(self, x, y, target, damage, proj_type="arrow", splash=0, slow_factor=0, slow_duration=0):
|
|
self.x = x
|
|
self.y = y
|
|
self.target = target
|
|
self.damage = damage
|
|
self.proj_type = proj_type
|
|
self.splash = splash
|
|
self.slow_factor = slow_factor
|
|
self.slow_duration = slow_duration
|
|
self.speed = 400
|
|
self.alive = True
|
|
|
|
def update(self, dt, enemies):
|
|
if not self.alive:
|
|
return
|
|
|
|
if not self.target or not self.target.alive:
|
|
self.alive = False
|
|
return
|
|
|
|
tx, ty = self.target.x, self.target.y
|
|
dx = tx - self.x
|
|
dy = ty - self.y
|
|
dist = math.hypot(dx, dy)
|
|
|
|
move = self.speed * dt
|
|
if dist <= move:
|
|
self._hit(enemies)
|
|
else:
|
|
self.x += dx / dist * move
|
|
self.y += dy / dist * move
|
|
|
|
def _hit(self, enemies):
|
|
self.alive = False
|
|
if self.proj_type == "cannon" and self.splash > 0:
|
|
for e in enemies:
|
|
if e.alive and distance(self.target.x, self.target.y, e.x, e.y) <= self.splash:
|
|
e.take_damage(self.damage)
|
|
else:
|
|
self.target.take_damage(self.damage)
|
|
|
|
if self.proj_type == "slow" and self.target.alive:
|
|
self.target.apply_slow(self.slow_factor, self.slow_duration)
|
|
|
|
def draw(self, surface):
|
|
if not self.alive:
|
|
return
|
|
ix, iy = int(self.x), int(self.y)
|
|
if self.proj_type == "arrow":
|
|
pygame.draw.circle(surface, COLOR_WHITE, (ix, iy), 3)
|
|
elif self.proj_type == "cannon":
|
|
pygame.draw.circle(surface, COLOR_ORANGE, (ix, iy), 5)
|
|
elif self.proj_type == "slow":
|
|
pygame.draw.circle(surface, COLOR_CYAN, (ix, iy), 4)
|