forked from BinkyToo/CCDQuest
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPlayer.py
54 lines (50 loc) · 1.69 KB
/
Player.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
import threading
import collectables
import config
import worlds
from MGO.GEPlayer import GEPlayer
class Player(object):
def __init__(self):
self.geplayer = None
self.statelock = threading.Lock()
self.state = 'normal'
self.mapdef = None
self.score = {
collectables.COIN: 0,
collectables.CHOCOLATE: 10000,
collectables.DYNAMITE: 15
}
self.setworld(config.get('map', 'initialmap', str), blocking=True)
def setworld(self, name, position=None, blocking=False):
with self.statelock:
if self.state != 'normal':
return False
self.state = 'loading'
self.mapdef = worlds.mapdefs[name]
if self.geplayer is not None:
self.geplayer.world.removegeplayer(self.geplayer)
def lworld():
try:
geplayer = GEPlayer(self, worlds.getworld(name), position)
except Exception:
with self.statelock:
self.state = 'crashed'
raise; return
with self.statelock:
self.geplayer = geplayer
self.state = 'normal'
t = threading.Thread(target=lworld)
t.start()
if blocking:
t.join()
def stepworld(self, step):
nextname = worlds.stepname(self.geplayer.world.mapdef['dir'], step) #FIXME
if nextname:
self.setworld(nextname)
def action(self, arg):
if self.state != 'normal':
return False
self.geplayer.action(arg)
self.score = self.geplayer.score
if self.score[collectables.CHOCOLATE] <= 0:
self.state = 'lost'