home · contact · privacy
Refactor.
[plomrogue2-experiments] / server_ / game.py
1 class GameError(Exception):
2     pass
3
4
5 def move_pos(direction, pos_yx):
6     if direction == 'UP':
7         pos_yx[0] -= 1
8     elif direction == 'DOWN':
9         pos_yx[0] += 1
10     elif direction == 'RIGHT':
11         pos_yx[1] += 1
12     elif direction == 'LEFT':
13         pos_yx[1] -= 1
14
15
16 class Task:
17
18     def __init__(self, thing, name, args=(), kwargs={}):
19         self.name = name
20         self.thing = thing
21         self.args = args
22         self.kwargs = kwargs
23         self.todo = 1
24
25     def check(self):
26         if self.name == 'move':
27             if len(self.args) > 0:
28                 direction = self.args[0]
29             else:
30                 direction = self.kwargs['direction']
31             test_pos = self.thing.position[:]
32             move_pos(direction, test_pos)
33             if test_pos[0] < 0 or test_pos[1] < 0 or \
34                test_pos[0] >= self.thing.world.map_size[0] or \
35                test_pos[1] >= self.thing.world.map_size[1]:
36                 raise GameError('would move outside map bounds')
37             pos_i = test_pos[0] * self.thing.world.map_size[1] + test_pos[1]
38             map_tile = self.thing.world.map_[pos_i]
39             if map_tile != '.':
40                 raise GameError('would move into illegal terrain')
41
42
43 class Thing:
44
45     def __init__(self, world, type_, position):
46         self.world = world
47         self.type_ = type_
48         self.position = position
49         self.task = Task(self, 'wait')
50
51     def task_wait(self):
52         pass
53
54     def task_move(self, direction):
55         move_pos(direction, self.position)
56
57     def decide_task(self):
58         if self.position[1] > 1:
59             self.set_task('move', 'LEFT')
60         elif self.position[1] < 3:
61             self.set_task('move', 'RIGHT')
62         else:
63             self.set_task('wait')
64
65     def set_task(self, task, *args, **kwargs):
66         self.task = Task(self, task, args, kwargs)
67         self.task.check()
68
69     def proceed(self, is_AI=True):
70         """Further the thing in its tasks.
71
72         Decrements .task.todo; if it thus falls to <= 0, enacts method whose
73         name is 'task_' + self.task.name and sets .task = None. If is_AI, calls
74         .decide_task to decide a self.task.
75         """
76         self.task.todo -= 1
77         if self.task.todo <= 0:
78             task = getattr(self, 'task_' + self.task.name)
79             task(*self.task.args, **self.task.kwargs)
80             self.task = None
81         if is_AI and self.task is None:
82             self.decide_task()
83
84
85 class World:
86
87     def __init__(self):
88         self.turn = 0
89         self.map_size = (5, 5)
90         self.map_ = 'xxxxx' +\
91                     'x...x' +\
92                     'x.X.x' +\
93                     'x...x' +\
94                     'xxxxx'
95         self.things = [
96             Thing(self, 'human', [3, 3]),
97             Thing(self, 'monster', [1, 1])
98         ]
99         self.player_i = 0
100         self.player = self.things[self.player_i]