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