Files
wicked-quest/src/weapon.py

72 lines
2.5 KiB
Python

# -*- coding: utf-8 -*-
class Weapon:
def __init__(self, name, damage, range, attackSound, hitSound, cooldown=500, attackDuration=200):
self.name = name
self.damage = damage
self.range = range # Range in tiles
self.attackSound = attackSound
self.hitSound = hitSound
self.cooldown = cooldown # Milliseconds between attacks
self.attackDuration = attackDuration # Milliseconds the attack is active
self.lastAttackTime = 0
self.hitEnemies = set()
@classmethod
def create_nunchucks(cls):
"""Create the nunchucks weapon"""
return cls(
name="nunchucks",
damage=6,
range=4,
attackSound="player_nunchuck_attack",
hitSound="player_nunchuck_hit",
cooldown=250,
attackDuration=100
)
@classmethod
def create_witch_broom(cls):
"""Create the witch's broom weapon"""
return cls(
name="witch_broom",
damage=3,
range=3,
attackSound="player_broom_attack",
hitSound="player_broom_hit",
cooldown=500,
attackDuration=200
)
def can_attack(self, currentTime):
"""Check if enough time has passed since last attack"""
return currentTime - self.lastAttackTime >= self.cooldown
def get_attack_range(self, playerPos, facingRight):
"""Calculate the area that this attack would hit"""
if facingRight:
return (playerPos, playerPos + self.range)
else:
return (playerPos - self.range, playerPos)
def start_attack(self, currentTime):
"""Begin an attack and return True if allowed"""
if self.can_attack(currentTime):
self.lastAttackTime = currentTime
self.hitEnemies.clear() # Clear hit enemies for new attack
return True
return False
def is_attack_active(self, currentTime):
"""Check if the attack is still in its active frames"""
timeSinceAttack = currentTime - self.lastAttackTime
return 0 <= timeSinceAttack <= self.attackDuration
def register_hit(self, enemy, currentTime):
"""Register a hit on an enemy for this attack. Returns True if it's a new hit."""
if self.is_attack_active(currentTime):
if enemy not in self.hitEnemies:
self.hitEnemies.add(enemy)
return True
return False