-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbarrel.py
277 lines (249 loc) · 10.5 KB
/
barrel.py
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
"""
Class for barrels
"""
import math
import random
from framework import GameObject, SpriteSheet, Clock
from spriteManager import SpriteManager
from enum import Enum
import enemySpawner
from collisionDetector import CollisionTypes, CollisionDirection
from effectSpawner import EffectSpawner
# Enum to represent the type of code.
class BarrelType(Enum):
NORMAL = 'normal'
FIRE = 'fire'
EXPLOSIVE = 'exp'
GOO = 'goo'
def __str__(self):
return self.value
# Possible Barrel States.
class BarrelState(Enum):
MOVE = 0
FALL = 1
DEAD = 2
# Possible Directions the Barrel is moving.
class BarrelDir(Enum):
RIGHT = 0
LEFT = 1
class Barrel(GameObject):
def __init__(self, barrelType):
GameObject.__init__(self)
self._speed = 80
self._sheet = SpriteSheet('barrel')
# Type of barrel being handled. Given when Created.
self.type = barrelType
# Counter for Barrel actions.
self.tick = 0
self.dirChanged = False
self.hitWall = False
self._lastPlatform = {}
self._ladderIgnore = 0
# List of all sprite states for animations.
self._sprites = {
'normal_roll1': self._sheet.sprite(112, 0, 24, 20),
'normal_roll2': self._sheet.sprite(160, 0, 24, 20),
'normal_roll3': self._sheet.sprite(208, 0, 24, 20),
'normal_fall1': self._sheet.sprite(290, 0, 40, 20),
'normal_fall2': self._sheet.sprite(340, 0, 40, 20),
'normal_fall3': self._sheet.sprite(290, 0, 40, 20).flip(),
'normal_fall4': self._sheet.sprite(340, 0, 40, 20).flip(),
'fire_roll1': self._sheet.sprite(112, 22, 24, 20),
'fire_roll2': self._sheet.sprite(160, 22, 24, 20),
'fire_roll3': self._sheet.sprite(208, 22, 24, 20),
'fire_fall1': self._sheet.sprite(290, 22, 40, 20),
'fire_fall2': self._sheet.sprite(340, 22, 40, 20),
'fire_fall3': self._sheet.sprite(290, 22, 40, 20).flip(),
'fire_fall4': self._sheet.sprite(340, 22, 40, 20).flip(),
'exp_roll1': self._sheet.sprite(112, 44, 24, 20),
'exp_roll2': self._sheet.sprite(160, 44, 24, 20),
'exp_roll3': self._sheet.sprite(208, 44, 24, 20),
'exp_fall1': self._sheet.sprite(290, 44, 40, 20),
'exp_fall2': self._sheet.sprite(340, 44, 40, 20),
'exp_fall3': self._sheet.sprite(290, 44, 40, 20).flip(),
'exp_fall4': self._sheet.sprite(340, 44, 40, 20).flip(),
'goo_roll1': self._sheet.sprite(112, 66, 24, 20),
'goo_roll2': self._sheet.sprite(160, 66, 24, 20),
'goo_roll3': self._sheet.sprite(208, 66, 24, 20),
'goo_fall1': self._sheet.sprite(290, 66, 40, 20),
'goo_fall2': self._sheet.sprite(340, 66, 40, 20),
'goo_fall3': self._sheet.sprite(290, 66, 40, 20).flip(),
'goo_fall4': self._sheet.sprite(340, 66, 40, 20).flip()
}
# This Barrel's Sprite Manager.
self.spriteManager = SpriteManager(self._sprites)
# Initial Spawning Position.
self.x = 127
self.y = 160
# Initalizing the state of the Barrel.
self.state = BarrelState.MOVE
self.dir = BarrelDir.RIGHT
# Hasn't Collided with a Ladder yet.
self.isFalling = False
# Set the starting tick Value.
self.setTick()
self.setSprites()
# Update, Called each game loop update.
def update(self):
""" Method used for updating state of a sprite/object """
# Decrement the tick count.
self.tick -= 1
# Handle any behaviors of the barrel for each type.
self.handleBehavior()
# Animate
self.spriteManager.animate()
self.move()
# Set the sprites for each action.
def setSprites(self):
if self.state == BarrelState.MOVE:
if self.dir == BarrelDir.RIGHT:
self.spriteManager.useSprites([
str(self.type) + '_roll1',
str(self.type) + '_roll2',
str(self.type) + '_roll3'
], self.animationSpeed())
elif self.dir == BarrelDir.LEFT:
self.spriteManager.useSprites([
str(self.type) + '_roll3',
str(self.type) + '_roll2',
str(self.type) + '_roll1'
],self.animationSpeed())
elif self.state == BarrelState.FALL:
if self.dir == BarrelDir.LEFT:
self.spriteManager.useSprites([
str(self.type) + '_fall1',
str(self.type) + '_fall2',
], self.animationSpeed())
elif self.dir == BarrelDir.RIGHT:
self.spriteManager.useSprites([
str(self.type) + '_fall3',
str(self.type) + '_fall4',
], self.animationSpeed())
def animationSpeed(self):
if self.state == BarrelState.MOVE:
return math.ceil((2500/((1/3*self._speed)**2))+2)
else:
return 10
def move(self):
if self.state == BarrelState.MOVE:
# If the barrel is on a ladder.
if self.isFalling:
if self._ladderIgnore > 0 and not self.hitWall:
self.isFalling = False
self._ladderIgnore -= 1
elif random.randint(1, 100) > 50 and not self.hitWall:
self._ladderIgnore = 6
self.isFalling = False
elif self.hitWall:
if self.dir == BarrelDir.LEFT:
self.dir = BarrelDir.RIGHT
elif self.dir == BarrelDir.RIGHT:
self.dir = BarrelDir.LEFT
self.state = BarrelState.FALL
else:
# Change the direction so that when we stop falling,
# we are heading the oposite direction as before.
# it is falling down the ladder.
if self.dir == BarrelDir.LEFT:
self.dir = BarrelDir.RIGHT
elif self.dir == BarrelDir.RIGHT:
self.dir = BarrelDir.LEFT
# Adjust the position to make it so the sprite actually looks like
self.x -= 8
# Change the state to falling because we are on a ladder.
self.hitWall = False
self.state = BarrelState.FALL
# Barrel is moving right, move right.
self.x += (self._speed if self.dir == BarrelDir.RIGHT else -self._speed) * Clock.timeDelta
# If Falling.
elif self.state == BarrelState.FALL:
# If not on a ladder anymore.
if not self.isFalling:
# Change state to move.
self.state = BarrelState.MOVE
# self.setSprites()
self.y += (self._speed * 1.5) * Clock.timeDelta
if self.dirChanged:
self.setSprites()
self.dirChanged = False
self.hitWall = False
# Collision Handling.
def collision(self, collisionType, direction, obj):
""" Checks for collision with another spirte """
# If we are hitting a platform.
if collisionType == CollisionTypes.Platform:
self._lastPlatform = obj
# And we are not on a ladder, stop falling.
if not self.isFalling:
self.bottom = obj.top + 1
# IF we are on the top of a ladder, set ladder flag.
elif collisionType == CollisionTypes.Ladder and direction == CollisionDirection.Bottom:
if abs(self.bottom - 1) <= abs(obj.centerY):
# IF we were not on a ladder before
if not self.isFalling:
# flag the falling and the direction changed flags
self.dirChanged = True
self.isFalling = True
# If we hit a Immovable, Boundary for Platforms.
elif collisionType == CollisionTypes.Immovable:
# Stop moving down.
self.bottom = obj.top + 1
# No Longer on a Ladder. update flags if they are not updated.
if self.isFalling:
self.dirChanged = True
self.isFalling = False
elif collisionType == CollisionTypes.Wall:
self._ladderIgnore = 0
self.hitWall = True
if abs(self.x) < obj.x:
self.right = obj.left - 3
else:
self.left = obj.right + 3
if not self.isFalling:
self.dirChanged = True
self.isFalling = True
# Handles behavior for each barrel type.
def handleBehavior(self):
# If the barrel is of a type that has a timed affect.
if self.type == BarrelType.EXPLOSIVE or self.type == BarrelType.GOO:
# check to see if the time is up.
if self.tick <= 0:
# Do the action for this barrel.
self.action()
return
# Gets the current sprite of this barrel.
def getSprite(self):
""" Returns the current sprite for the game object """
return self.spriteManager.currentSprite()
# Sets the tick value to be what it should be based on the barrel type.
def setTick(self):
# TODO: make randomize ranges.
if self.type == BarrelType.EXPLOSIVE:
self.tick = random.randint(300, 1900)
elif self.type == BarrelType.GOO:
self.tick = random.randint(300, 1900)
else:
self.tick = 0
# Action to do for each type of barrel.
def action(self):
if self.isFalling:
self.tick = 1
return
# TODO: add explosion AOE
if self.type == BarrelType.EXPLOSIVE:
EffectSpawner().spawnExplosion(self)
self.state = BarrelState.DEAD
self.kill()
# TODO: change current platform state.
elif self.type == BarrelType.GOO:
EffectSpawner().spawnGoo(self._lastPlatform)
self.state = BarrelState.DEAD
self.kill()
# Called if item collected. Used to despawn barrels when they reach the end.
def collectedItem(self, collectible, collectionType):
if collectible.name == 'FlamingOilContainer':
self.state = BarrelState.DEAD
self.kill()
if self.type == BarrelType.FIRE:
self.fireType = 0
enemySpawner.EnemySpawner().spawnFire(self.fireType, self.x, self.y)