From 1fbbb94f2033513dcb6a607d34298f0fb499af54 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rihards=20=C5=A0ev=C4=8Duks?= Date: Wed, 25 Feb 2026 06:29:03 +0000 Subject: [PATCH] Add weapon_system.py --- weapon_system.py | 100 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 100 insertions(+) create mode 100644 weapon_system.py diff --git a/weapon_system.py b/weapon_system.py new file mode 100644 index 0000000..4a4ce9b --- /dev/null +++ b/weapon_system.py @@ -0,0 +1,100 @@ +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 \ No newline at end of file