home · contact · privacy
Shrink sound Dijkstra map, reach; refactor lots of mapping code.
[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_=0, position=(YX(0,0))):
10         self.game = game
11         if id_ == 0:
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     blocking = False
21
22     def __init__(self, *args, **kwargs):
23         super().__init__(*args, **kwargs)
24
25     def proceed(self):
26         pass
27
28     @property
29     def type_(self):
30         return self.__class__.get_type()
31
32     @classmethod
33     def get_type(cls):
34         return cls.__name__[len('Thing_'):]
35
36
37
38 class Thing_Item(Thing):
39     symbol_hint = 'i'
40
41
42
43 class Thing_Furniture(Thing):
44     symbol_hint = 'h'
45
46
47
48 class ThingAnimate(Thing):
49     blocking = True
50
51     def __init__(self, *args, **kwargs):
52         super().__init__(*args, **kwargs)
53         self.next_tasks = []
54         self.set_task('WAIT')
55         self._fov = None
56
57     def set_task(self, task_name, args=()):
58         task_class = self.game.tasks[task_name]
59         self.task = task_class(self, args)
60         self.task.check()  # will throw GameError if necessary
61
62     def set_next_task(self, task_name, args=()):
63         task_class = self.game.tasks[task_name]
64         self.next_tasks += [task_class(self, args)]
65
66     def get_next_task(self):
67         if len(self.next_tasks) > 0:
68             task = self.next_tasks.pop(0)
69             task.check()
70             return task
71         else:
72             return None
73
74     def proceed(self):
75         self._fov = None
76         if self.task is None:
77             self.task = self.get_next_task()
78             return
79
80         try:
81             self.task.check()
82         except GameError as e:
83             self.task = None
84             raise GameError
85             return
86         self.task.todo -= 1
87         if self.task.todo <= 0:
88             self._last_task_result = self.task.do()
89             self.game.changed = True
90             self.task = self.get_next_task()
91
92     @property
93     def fov_stencil(self):
94         if self._fov:
95             return self._fov
96         fov_map_class = self.game.map_geometry.fov_map_class
97         self._fov = fov_map_class(self.game.map, self.position, 12)
98         return self._fov
99
100     def fov_test(self, yx):
101         test_position = self.fov_stencil.target_yx(yx)
102         if self.fov_stencil.inside(test_position):
103             if self.fov_stencil[test_position] == '.':
104                 return True
105         return False
106
107     def fov_stencil_map(self, map):
108         visible_terrain = ''
109         for yx in self.fov_stencil:
110             if self.fov_stencil[yx] == '.':
111                 visible_terrain += map[self.fov_stencil.source_yx(yx)]
112             else:
113                 visible_terrain += ' '
114         return visible_terrain
115
116
117
118 class Thing_Player(ThingAnimate):
119     symbol_hint = '@'
120
121     def __init__(self, *args, **kwargs):
122         super().__init__(*args, **kwargs)
123         self.carrying = None