home · contact · privacy
Add field of view.
[plomrogue2] / plomrogue / things.py
1 from plomrogue.errors import GameError
2 from plomrogue.mapping import YX
3
4
5
6 class ThingBase:
7     type_ = '?'
8
9     def __init__(self, game, id_=None, position=(YX(0,0))):
10         self.game = game
11         if id_ is None:
12             self.id_ = self.game.new_thing_id()
13         else:
14             self.id_ = id_
15         self.position = position
16
17
18
19 class Thing(ThingBase):
20
21     def __init__(self, *args, **kwargs):
22         super().__init__(*args, **kwargs)
23
24     def proceed(self):
25         pass
26
27
28
29 class ThingAnimate(Thing):
30
31     def __init__(self, *args, **kwargs):
32         super().__init__(*args, **kwargs)
33         self.next_tasks = []
34         self.set_task('WAIT')
35         self._fov = None
36
37     def set_task(self, task_name, args=()):
38         task_class = self.game.tasks[task_name]
39         self.task = task_class(self, args)
40         self.task.check()  # will throw GameError if necessary
41
42     def set_next_task(self, task_name, args=()):
43         task_class = self.game.tasks[task_name]
44         self.next_tasks += [task_class(self, args)]
45
46     def get_next_task(self):
47         if len(self.next_tasks) > 0:
48             task = self.next_tasks.pop(0)
49             task.check()
50             return task
51         else:
52             return None
53
54     def proceed(self):
55         self._fov = None
56         if self.task is None:
57             self.task = self.get_next_task()
58             return
59
60         try:
61             self.task.check()
62         except GameError as e:
63             self.task = None
64             raise GameError
65             return
66         self.task.todo -= 1
67         if self.task.todo <= 0:
68             self._last_task_result = self.task.do()
69             self.game.changed = True
70             self.task = self.get_next_task()
71
72     @property
73     def fov_stencil(self):
74         if self._fov:
75             return self._fov
76         fov_map_class = self.game.map_geometry.fov_map_class
77         self._fov = fov_map_class(self.game.map, self.position)
78         return self._fov
79
80     def fov_stencil_map(self, map):
81         visible_terrain = ''
82         for i in range(self.fov_stencil.size_i):
83             if self.fov_stencil.terrain[i] == '.':
84                 visible_terrain += map.terrain[i]
85             else:
86                 visible_terrain += ' '
87         return visible_terrain
88
89
90
91 class ThingPlayer(ThingAnimate):
92     type_ = 'player'
93
94     def __init__(self, *args, **kwargs):
95         super().__init__(*args, **kwargs)
96         self.nickname = 'undefined'