forked from Akalay27/pythonRoguelike
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathanimationController.py
More file actions
60 lines (41 loc) · 1.21 KB
/
Copy pathanimationController.py
File metadata and controls
60 lines (41 loc) · 1.21 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
import time
import pygame
class AnimationController(object):
"""controls the animations"""
def __init__(self):
self.states = []
self.currentState = None
def addAnimationState(self,name,frames,speed,loop=False):
state = AnimationController.AnimationState(name,frames,speed,loop)
self.states.append(state)
def setState(self,name):
for state in self.states:
if state.name == name:
self.currentState = state
state.begin()
else:
state.active = False
def getFrame(self):
if self.currentState != None:
return self.currentState.getFrame()
else:
return self.states[0].getFrame()
class AnimationState(object):
'''stores a single set of sprites and properties including speed and if looping'''
def __init__(self,name,frames,speed,loop):
self.frames = frames
self.speed = speed
self.loop = loop
self.active = False
self.name = name
self.stillFrame = len(self.frames) == 1
def begin(self):
if self.active == False:
self.active = True
self.startTime = time.time()
def getFrame(self):
if not self.stillFrame:
frame = int(((time.time()-self.startTime)/self.speed)%len(self.frames))
else:
frame = 0
return self.frames[frame]