-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathunit.py
More file actions
366 lines (235 loc) · 10.5 KB
/
unit.py
File metadata and controls
366 lines (235 loc) · 10.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
import glad
import animation
import util
from util import Rect, Vector
from object import AbstractObject
from projectile import *
class Corpse(AbstractObject):
def __init__(self, pos, owner, **kwargs):
shape = Rect.createAtOrigin(32, 32)
AbstractObject.__init__(self, pos, shape, **kwargs)
self.owner = self #keep a copy of who the corpse belongs to
self.collisionType = 'OBJ' #should add collision type for corpses, keys, and other stuff you can walk over
self.currentAnimation = 'ANIM_BLOOD_'+str(random.randint(0,3))
self.animationPlayer = animation.AnimationPlayer(glad.resource.resourceDict[self.currentAnimation], 0.2, True)
class BasicUnit(AbstractObject):
def __init__(self, pos, shape = Rect.createAtOrigin(32, 32), hue=180, name='SOLDIER', slime=False, **kwargs):
AbstractObject.__init__(self, pos, shape, **kwargs)
self.collisionType = 'UNIT'
#Default statistics
self.strength = 10
self.dexterity = 10
self.constitution = 10
self.intelligence = 10
self.armor = 10
self.level = 1
self.life = 100
self.mana = 100
self.rangedWeapon = None
self.meleeWeapon = None
self.rangedDamage = 10
self.meleeDamage = 10
self.range = 400 #range for ranged weapon (in pixels)
self.moveSpeed = 200
#By default, have units face 'right'
self.orientation = Vector(1,0)
self.directionString = self.orientationToString() #may not be needed
#self.name = "SOLDIER" #might as well be soldier by default, name used to load animations with current name format
self.name=name
self.slime=slime
if self.slime:
self.currentAnimation = 'ANIM_' + self.name + '_TEAM_' + str(self.team) +'_MOVE'
else:
self.currentAnimation = 'ANIM_' + self.name + '_TEAM_' + str(self.team) +'_MOVE' + self.orientationToString()
self.animationTime = 0.2
self.animationPlayer = animation.AnimationPlayer(glad.resource.resourceDict[self.currentAnimation], self.animationTime , True)
self.alwaysMove = False
#turning
self.turnTime = 0.08
#Attacking
self.attacking = False
#attack animation
self.attackTime = 0.1 #time attack frame is up
self.attackTimer = 0
self.animateAttack = False
self.hue = hue #I thnk this is from my old method of team coloring - can probably be removed
def attack(self):
self.attacking = True
def meleeAttack(self, target):
print 'Melee'
pass
def rangedAttack(self):
#spawn the ranged attack just outside of the rect
#gap = space between center of player and center of projectile
#TODO: abstract/put in the correct place
gap = 16
if self.rangedWeapon.attack(self.getPos(),
gap,
self.orientation,
self.team):
#subtract mana points - just for testing now
self.mana -= 5
return True
#self.animation.rangedAttack(self.directionString)
else:
return False
def rangedHit(self, target):
target.life -= self.rangedDamage
#update stats here
print target, ' Remaning life: ', target.life
def die(self):
"""Kill unit and handle his death"""
self.alive = False
sound = 'die' + str(random.randint(1,2))
glad.resource.resourceDict[sound].play(self.pos) #just testing sound out again
corpse = Corpse(self.pos, self)
glad.world.objectList.append(corpse)
def update(self, time):
#update everything needed for a basic unit, sloppy at the moment
if self.life <= 0:
self.die()
if self.rangedWeapon:
self.rangedWeapon.update(time)
#update turnTime
if self.turning:
self.turnTimer += time
oldAnimation = self.currentAnimation
self.directionString = self.orientationToString()
#update current animation
if self.slime:
self.currentAnimation = 'ANIM_' + self.name + '_TEAM_' + str(self.team) +'_MOVE'
else:
self.currentAnimation = 'ANIM_' + self.name + '_TEAM_' + str(self.team) +'_MOVE' + self.directionString
if self.attacking:
#Check if melee or ranged
#Just assume ranged for now
if self.rangedAttack(): #fires if ready
if not self.alwaysMove:
#dont animate attacking for firelems etc. doesnt look smooth
self.animateAttack = True
if self.animateAttack:
self.currentAnimation = 'ANIM_' + self.name + '_TEAM_' + str(self.team) +'_ATTACK' + self.directionString
self.animationPlayer.timer = self.attackTime
if self.animationPlayer.currentFrameIndex == 1: #Stop after reaching last frame
self.animateAttack=False
self.animationPlayer.timer = self.animationTime
if self.currentAnimation != oldAnimation: #only change if not the same
self.animationPlayer.currentFrameIndex=0 #removes any index errors
self.animationPlayer.animation = glad.resource.resourceDict[self.currentAnimation]
#Animate only if moving or animating attack
if self.alwaysMove or self.animateAttack:
if self.animationPlayer:
self.animationPlayer.update(time)
elif not self.alwaysMove:
if self.vel[0] != 0 or self.vel[1] != 0:
if self.animationPlayer:
self.animationPlayer.update(time)
#Turn off attacking
self.attacking = False
class Soldier(BasicUnit):
def __init__(self, pos, **kwargs):
#For now, just use BasicUnit Defaults
#default soldier size
shape = Rect.createAtOrigin(32, 32)
BasicUnit.__init__(self, pos, shape, name='SOLDIER', **kwargs)
#self.rangedWeapon = BasicRangedAttack('knife',size=(12,12))
self.rangedWeapon = KnifeThrower(self)
class FireElem(BasicUnit):
#based off sample soldier for the start
def __init__(self, pos, **kwargs):
shape = Rect.createAtOrigin(32, 32)
BasicUnit.__init__(self, pos, shape, name='FIRE_ELEM', **kwargs)
#self.rangedWeapon = KnifeThrower()
self.rangedWeapon = BasicRangedAttack(self, 'meteor')
self.alwaysMove = True
self.animationPlayer.timer = 0.1
class Archer(BasicUnit):
def __init__(self, pos, **kwargs):
shape = Rect.createAtOrigin(32, 32)
BasicUnit.__init__(self, pos, shape, name='ARCHER', **kwargs)
self.rangedWeapon = BasicRangedAttack(self, 'arrow', size=(14,14))
class Archmage(BasicUnit):
def __init__(self, pos, **kwargs):
shape = Rect.createAtOrigin(32, 28)
BasicUnit.__init__(self, pos, shape, name='ARCHMAGE', **kwargs)
self.rangedWeapon = BasicRangedAttack(self, 'fireball')
class Barbarian(BasicUnit):
def __init__(self, pos, **kwargs):
shape = Rect.createAtOrigin(32, 28)
BasicUnit.__init__(self, pos, shape, name='BARBARIAN', **kwargs)
self.rangedWeapon = BasicRangedAttack(self, 'hammer', size=(12,12))
class Cleric(BasicUnit):
def __init__(self, pos, **kwargs):
shape = Rect.createAtOrigin(32, 24)
BasicUnit.__init__(self, pos, shape, name='CLERIC', **kwargs)
self.rangedWeapon = KnifeThrower(self)
class Druid(BasicUnit):
def __init__(self, pos, **kwargs):
shape = Rect.createAtOrigin(32, 22)
BasicUnit.__init__(self, pos, shape, name='DRUID', **kwargs)
self.rangedWeapon = BasicRangedAttack(self, 'lightning')
class Elf(BasicUnit):
def __init__(self, pos, **kwargs):
shape = Rect.createAtOrigin(20, 20)
BasicUnit.__init__(self, pos, shape, name='ELF', **kwargs)
self.rangedWeapon = BasicRangedAttack(self, 'rock',size=(12,12))
class Faerie(BasicUnit):
def __init__(self, pos, **kwargs):
shape = Rect.createAtOrigin(16, 16)
BasicUnit.__init__(self, pos, shape, name='FAERIE', **kwargs)
self.rangedWeapon = BasicRangedAttack(self, 'sparkle')
self.alwaysMove = True
class Ghost(BasicUnit):
def __init__(self, pos, **kwargs):
shape = Rect.createAtOrigin(26, 28)
BasicUnit.__init__(self, pos, shape, name='GHOST', **kwargs)
self.rangedWeapon = KnifeThrower(self)
self.alwaysMove = True
class Golem(BasicUnit):
def __init__(self, pos, **kwargs):
shape = Rect.createAtOrigin(96, 72)
BasicUnit.__init__(self, pos, shape, name='GOLEM', **kwargs)
self.rangedWeapon = BasicRangedAttack(self, 'boulder', size=(26,26))
class Mage(BasicUnit):
def __init__(self, pos, **kwargs):
shape = Rect.createAtOrigin(32, 28)
BasicUnit.__init__(self, pos, shape, name='MAGE', **kwargs)
self.rangedWeapon = BasicRangedAttack(self, 'fireball')
class Orc(BasicUnit):
def __init__(self, pos, **kwargs):
shape = Rect.createAtOrigin(32, 32)
BasicUnit.__init__(self, pos, shape, name='ORC', **kwargs)
self.rangedWeapon = KnifeThrower(self)
class OrcCaptain(BasicUnit):
def __init__(self, pos, **kwargs):
shape = Rect.createAtOrigin(32, 32)
BasicUnit.__init__(self, pos, shape, name='ORC_CAPTAIN', **kwargs)
self.rangedWeapon = BasicRangedAttack(self, 'knife', size=(12,12))
class Skeleton(BasicUnit):
def __init__(self, pos, **kwargs):
shape = Rect.createAtOrigin(30, 26)
BasicUnit.__init__(self, pos, shape, name='SKELETON', **kwargs)
self.rangedWeapon = BasicRangedAttack(self, 'bone', size=(14,14))
class SmallSlime(BasicUnit):
def __init__(self, pos, **kwargs):
shape = Rect.createAtOrigin(24, 24)
BasicUnit.__init__(self, pos, shape, name='SMALL_SLIME', slime=True, **kwargs)
self.rangedWeapon = SlimeAttack(self)
self.alwaysMove = True
class MediumSlime(BasicUnit):
def __init__(self, pos, **kwargs):
shape = Rect.createAtOrigin(40, 40)
BasicUnit.__init__(self, pos, shape, name='MEDIUM_SLIME', slime=True, **kwargs)
self.rangedWeapon = SlimeAttack(self)
self.alwaysMove = True
class BigSlime(BasicUnit):
def __init__(self, pos, **kwargs):
shape = Rect.createAtOrigin(64, 64)
BasicUnit.__init__(self, pos, shape, name='BIG_SLIME', slime=True, **kwargs)
self.rangedWeapon = SlimeAttack(self)
self.alwaysMove = True
class Thief(BasicUnit):
def __init__(self, pos, **kwargs):
shape = Rect.createAtOrigin(32, 26)
BasicUnit.__init__(self, pos, shape, name='THIEF', **kwargs)
self.rangedWeapon = BasicRangedAttack(self, 'knife', size=(12,12))