-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathlevelmanager.py
145 lines (117 loc) · 4.23 KB
/
levelmanager.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
# ------------------------------------------------------------------
# levelmanager.py
#
# Manage reading list of levels and switching between them
# ------------------------------------------------------------------
import importlib
import pickle
import os
import gamedata.levels
import gamedata.levels.levelselect
levels_list_filename = os.path.join('gamedata', 'levels', 'list')
class LevelManager:
def __init__(self):
self.levels = {}
self.current_level = -1
self.requested_level = -1
# add level select menu
self.levels[0] = gamedata.levels.levelselect.Main()
# add the levels
self.parse_level_list(levels_list_filename)
# add each level from file at 'path'
def parse_level_list(self, path):
f = open(path, 'r')
for line in f:
if line[0] == '#':
continue #skip comments
words = line.split()
ind = int(words.pop(0))
nextind = 0
if words[0].isdigit(): #might skip nextind, 0 by default
nextind = int(words.pop(0))
modulename = words.pop(0)
deps = list(map(int, words))
self.add_level(ind, nextind, modulename, deps)
# add level in module 'modulename'
def add_level(self, ind, nextind, modulename, deps):
mod = importlib.import_module('gamedata.levels.' + modulename, gamedata.levels)
level = mod.Main()
level.ind = ind
level.next_level = nextind
level.deps = deps
self.levels[ind] = level
# start the game
def start(self):
if self.current_level < 0:
self.goto_level(0)
# stop the game
def stop(self):
if self.current_level >= 0:
self.levels[self.current_level].stop()
self.current_level = -1
# jump to a given level if it's unlocked, else go to 0
def goto_level(self, ind):
if not self.unlocked(self.levels[ind]):
if self.current_level == 0:
return #target locked and we're already at 0
else:
ind = 0
if self.current_level >= 0:
self.levels[self.current_level].stop()
self.current_level = ind
self.levels[self.current_level].start()
def request_goto_level(self, ind):
self.requested_level = ind
# go to the next level
def next_level(self):
if self.current_level < 0:
self.goto_level(0)
else:
nxt = self.levels[self.current_level].next_level
self.goto_level(nxt)
def request_next_level(self):
if self.current_level < 0:
self.request_goto_level(0)
else:
nxt = self.levels[self.current_level].next_level
self.request_goto_level(nxt)
# notify current level of step event
def step(self, elapsed):
if self.current_level >= 0:
self.levels[self.current_level].step(elapsed)
# notify current level of a pygame event
def event(self, event):
if self.current_level >= 0:
self.levels[self.current_level].event(event)
# handle level switch requests
def handle_requests(self):
if self.requested_level >= 0:
self.goto_level(self.requested_level)
self.requested_level = -1
# whether a level is playable
def unlocked(self, level):
if level.ind == 0:
return True # level select menu is always unlocked
for depind in level.deps:
dep = self.levels[depind]
if not dep.data.completed:
return False
return True
# save level data for all levels to file
def save_level_data(self, f):
all_data = {}
for ind, level in self.levels.iteritems():
# TODO this doesn't work the first time it's run
all_data[ind] = level.data
pickle.dump(all_data, f)
# load level data from file
def load_level_data(self, f):
all_data = pickle.load(f)
for ind, data in all_data.iteritems():
self.levels[ind].data = data
# get current level object
def get_current_level(self):
if self.current_level >= 0:
return self.levels[self.current_level]
else:
return None