Skip to content

Commit 213dad4

Browse files
committed
Merge pull request #39 from ThaPwned/master
Added WeaponEntity
2 parents 02022f5 + b37939a commit 213dad4

File tree

1 file changed

+115
-0
lines changed
  • addons/source-python/packages/source-python/weapons

1 file changed

+115
-0
lines changed
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
# ../weapons/entity.py
2+
3+
"""Provides simplified weapon functionality."""
4+
5+
# =============================================================================
6+
# >> IMPORTS
7+
# =============================================================================
8+
# Source.Python Imports
9+
# Entities
10+
from entities.entity import BaseEntity
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__ = ('WeaponEntity',
21+
)
22+
23+
24+
# =============================================================================
25+
# >> CLASSES
26+
# =============================================================================
27+
class WeaponEntity(BaseEntity):
28+
29+
"""Allows easy usage of the weapon's attributes."""
30+
31+
# =========================================================================
32+
# >> AMMO
33+
# =========================================================================
34+
def has_ammo(self):
35+
"""Return whether or not the weapon has ammo."""
36+
return weapon_manager[self.classname].ammoprop is not None
37+
38+
def get_ammo(self):
39+
"""Return the amount of ammo the player has for the given weapon."""
40+
# Do we have a valid ammoprop?
41+
if not self.has_ammo():
42+
raise ValueError(
43+
'Unable to get ammoprop for {0}'.format(self.classname))
44+
45+
# Get the index of the owner
46+
index = index_from_inthandle(self.owner)
47+
48+
# Get the owner's entity instance
49+
player = BaseEntity(index)
50+
51+
# Get the weapon
52+
weapon = weapon_manager[self.classname]
53+
54+
# Return the ammo
55+
return player.get_property_int(
56+
'localdata.' + weapon_manager.ammoprop + '%03d' % weapon.ammoprop)
57+
58+
def set_ammo(self, value):
59+
"""Set the player's ammo property for the given weapon."""
60+
# Do we have a valid ammoprop?
61+
if not self.has_ammo():
62+
raise ValueError(
63+
'Unable to set ammoprop for {0}'.format(self.classname))
64+
65+
# Get the index of the owner
66+
index = index_from_inthandle(self.owner)
67+
68+
# Get the owner's entity instance
69+
player = BaseEntity(index)
70+
71+
# Get the weapon
72+
weapon = weapon_manager[self.classname]
73+
74+
# Set the ammo
75+
player.set_property_int(
76+
'localdata.' + weapon_manager.ammoprop + '%03d' % weapon.ammoprop,
77+
value)
78+
79+
# Set the "ammo" property methods
80+
ammo = property(
81+
get_ammo, set_ammo,
82+
doc="""Property to get/set the weapon's ammo.""")
83+
84+
# =========================================================================
85+
# >> CLIP
86+
# =========================================================================
87+
def get_clip(self):
88+
"""
89+
Return the amount of ammo in the primary clip for the given weapon.
90+
"""
91+
return self.get_property_int('m_iClip1')
92+
93+
def set_clip(self, value):
94+
"""Set the player's primary clip value for the given weapon."""
95+
self.set_property_int('m_iClip1', value)
96+
97+
# Set the "clip" property methods
98+
clip = property(
99+
get_clip, set_clip,
100+
doc="""Property to get/set the weapon's primary clip.""")
101+
102+
def get_clip2(self):
103+
"""
104+
Return the amount of ammo in the secondary clip for the given weapon.
105+
"""
106+
return self.get_property_int('m_iClip2')
107+
108+
def set_clip2(self, value):
109+
"""Set the player's secondary clip value for the given weapon."""
110+
self.set_property_int('m_iClip2', value)
111+
112+
# Set the "clip2" property methods
113+
clip2 = property(
114+
get_clip2, set_clip2,
115+
doc="""Property to get/set the weapon's secondary clip.""")

0 commit comments

Comments
 (0)