Projectdd/weapon_system.py

100 lines
3.1 KiB
Python

import pygame
from settings import *
class Weapon:
"""Base weapon class"""
def __init__(self, item_id, name, damage, cooldown, knockback, color):
self.item_id = item_id
self.name = name
self.damage = damage
self.cooldown = cooldown
self.knockback = knockback
self.color = color
self.last_attack_time = 0
def can_attack(self, current_time):
"""Check if enough time has passed for another attack"""
return (current_time - self.last_attack_time) >= self.cooldown
def do_attack(self, current_time):
"""Perform attack and update cooldown"""
if self.can_attack(current_time):
self.last_attack_time = current_time
return True
return False
def get_cooldown_percent(self, current_time):
"""Get cooldown progress (0.0 to 1.0)"""
time_since_attack = current_time - self.last_attack_time
if time_since_attack >= self.cooldown:
return 1.0
return time_since_attack / self.cooldown
class WeaponManager:
"""Manage player weapons and current selection"""
def __init__(self):
self.weapons = {}
self.current_weapon_id = None
self.current_time = 0
self.setup_default_weapons()
def setup_default_weapons(self):
"""Create default weapons"""
for weapon_id, stats in WEAPON_STATS.items():
weapon = Weapon(
weapon_id,
stats["name"],
stats["damage"],
stats["cooldown"],
stats["knockback"],
stats["color"]
)
self.weapons[weapon_id] = weapon
# Set first weapon as current
if self.weapons:
self.current_weapon_id = list(self.weapons.keys())[0]
def get_current_weapon(self):
"""Get the currently equipped weapon"""
if self.current_weapon_id in self.weapons:
return self.weapons[self.current_weapon_id]
return None
def switch_weapon(self, weapon_id):
"""Switch to a different weapon"""
if weapon_id in self.weapons:
self.current_weapon_id = weapon_id
return True
return False
def attack(self):
"""Attempt attack with current weapon"""
weapon = self.get_current_weapon()
if weapon and weapon.do_attack(self.current_time):
return weapon.damage, weapon.knockback
return 0, 0
def update(self, dt):
"""Update weapon manager"""
self.current_time += dt
def add_weapon(self, weapon_id):
"""Add weapon to collection (for inventory)"""
if weapon_id in WEAPON_STATS and weapon_id not in self.weapons:
stats = WEAPON_STATS[weapon_id]
weapon = Weapon(
weapon_id,
stats["name"],
stats["damage"],
stats["cooldown"],
stats["knockback"],
stats["color"]
)
self.weapons[weapon_id] = weapon
return True
return False