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