|
| 1 | +# ../weapons/_entity/__init__.py |
| 2 | + |
| 3 | +"""Provides simplified weapon functionality.""" |
| 4 | + |
| 5 | +# ============================================================================= |
| 6 | +# >> IMPORTS |
| 7 | +# ============================================================================= |
| 8 | +# Source.Python Imports |
| 9 | +# Entities |
| 10 | +from entities.entity import Entity |
| 11 | +from entities.helpers import index_from_inthandle |
| 12 | +# Weapons |
| 13 | +from weapons.manager import weapon_manager |
| 14 | + |
| 15 | + |
| 16 | +# ============================================================================= |
| 17 | +# >> GLOBAL VARIABLES |
| 18 | +# ============================================================================= |
| 19 | +# Add all the global variables to __all__ |
| 20 | +__all__ = ('Weapon', |
| 21 | + ) |
| 22 | + |
| 23 | + |
| 24 | +# ============================================================================= |
| 25 | +# >> CLASSES |
| 26 | +# ============================================================================= |
| 27 | +class Weapon(Entity): |
| 28 | + """Allows easy usage of the weapon's attributes.""" |
| 29 | + |
| 30 | + @property |
| 31 | + def current_owner(self): |
| 32 | + """Return a Entity instance of the weapon's owner.""" |
| 33 | + # Does no player currently own the weapon? |
| 34 | + if self.owner == -1: |
| 35 | + return None |
| 36 | + |
| 37 | + # Return a Entity instance of the owner |
| 38 | + return Entity(index_from_inthandle(self.owner)) |
| 39 | + |
| 40 | + def get_clip(self): |
| 41 | + """Return the amount of ammo in the weapon's clip.""" |
| 42 | + return self.clip if self.clip != -1 else 0 |
| 43 | + |
| 44 | + def set_clip(self, value): |
| 45 | + """Set the amount of ammo in the weapon's clip.""" |
| 46 | + # Does the weapon have ammo? |
| 47 | + if self.clip != -1: |
| 48 | + |
| 49 | + # Set the clip amount |
| 50 | + self.clip = value |
| 51 | + |
| 52 | + def get_ammo(self): |
| 53 | + """Return the amount of ammo the player has for the weapon.""" |
| 54 | + # Do we have a valid ammoprop? |
| 55 | + if self.ammoprop == -1: |
| 56 | + raise ValueError( |
| 57 | + 'Unable to get ammoprop for {0}'.format(self.classname)) |
| 58 | + |
| 59 | + # Get the owner of the weapon |
| 60 | + player = self.current_owner |
| 61 | + |
| 62 | + # Does no one currently own the weapon? |
| 63 | + if player is None: |
| 64 | + return |
| 65 | + |
| 66 | + # Return the ammo |
| 67 | + return player.get_property_ushort( |
| 68 | + weapon_manager.ammoprop + '%03d' % self.ammoprop) |
| 69 | + |
| 70 | + def set_ammo(self, value): |
| 71 | + """Set the player's ammo property for the weapon.""" |
| 72 | + # Do we have a valid ammoprop? |
| 73 | + if self.ammoprop == -1: |
| 74 | + raise ValueError( |
| 75 | + 'Unable to set ammoprop for {0}'.format(self.classname)) |
| 76 | + |
| 77 | + # Get the owner of the weapon |
| 78 | + player = self.current_owner |
| 79 | + |
| 80 | + # Does no one currently own the weapon? |
| 81 | + if player is None: |
| 82 | + return |
| 83 | + |
| 84 | + # Set the ammo |
| 85 | + player.set_property_ushort( |
| 86 | + weapon_manager.ammoprop + '%03d' % self.ammoprop, value) |
| 87 | + |
| 88 | + # Set the "ammo" property methods |
| 89 | + ammo = property( |
| 90 | + get_ammo, set_ammo, |
| 91 | + doc="""Property to get/set the weapon's ammo.""") |
| 92 | + |
| 93 | + def get_secondary_fire_clip(self): |
| 94 | + """Return the amount of ammo in the weapon's secondary fire clip.""" |
| 95 | + return ( |
| 96 | + self.secondary_fire_clip if self.secondary_fire_clip != -1 else 0) |
| 97 | + |
| 98 | + def set_secondary_fire_clip(self, value): |
| 99 | + """Set the amount of ammo in the weapon's secondary fire clip.""" |
| 100 | + # Does the weapon have secondary fire ammo? |
| 101 | + if self.secondary_fire_clip != -1: |
| 102 | + |
| 103 | + # Set the secondary fire clip amount |
| 104 | + self.secondary_fire_clip = value |
| 105 | + |
| 106 | + def get_secondary_fire_ammo(self): |
| 107 | + """Return the secondary fire ammo the player has for the weapon.""" |
| 108 | + # Do we have a valid ammoprop? |
| 109 | + if self.secondary_fire_ammoprop == -1: |
| 110 | + raise ValueError( |
| 111 | + 'Unable to get secondary fire ammoprop for {0}'.format( |
| 112 | + self.classname)) |
| 113 | + |
| 114 | + # Get the owner of the weapon |
| 115 | + player = self.current_owner |
| 116 | + |
| 117 | + # Does no one currently own the weapon? |
| 118 | + if player is None: |
| 119 | + return |
| 120 | + |
| 121 | + # Return the ammo |
| 122 | + return player.get_property_ushort( |
| 123 | + weapon_manager.ammoprop + '%03d' % self.secondary_fire_ammoprop) |
| 124 | + |
| 125 | + def set_secondary_fire_ammo(self, value): |
| 126 | + """Set the player's secondary fire ammo property for the weapon.""" |
| 127 | + # Do we have a valid ammoprop? |
| 128 | + if self.secondary_fire_ammoprop == -1: |
| 129 | + raise ValueError( |
| 130 | + 'Unable to set secondary fire ammoprop for {0}'.format( |
| 131 | + self.classname)) |
| 132 | + |
| 133 | + # Get the owner of the weapon |
| 134 | + player = self.current_owner |
| 135 | + |
| 136 | + # Does no one currently own the weapon? |
| 137 | + if player is None: |
| 138 | + return |
| 139 | + |
| 140 | + # Set the ammo |
| 141 | + player.set_property_ushort( |
| 142 | + weapon_manager.ammoprop + |
| 143 | + '%03d' % self.secondary_fire_ammoprop, value) |
| 144 | + |
| 145 | + # Set the "secondary_fire_ammo" property methods |
| 146 | + secondary_fire_ammo = property( |
| 147 | + get_secondary_fire_ammo, set_secondary_fire_ammo, |
| 148 | + doc="""Property to get/set the weapon's secondary fire ammo.""") |
0 commit comments