home · contact · privacy
b6fe5e15e5e919483962b87f63fdfa4a99a380e5
[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_Stone(Thing):
39     symbol_hint = 'o'
40
41
42 class ThingAnimate(Thing):
43     blocking = True
44
45     def __init__(self, *args, **kwargs):
46         super().__init__(*args, **kwargs)
47         self.next_tasks = []
48         self.set_task('WAIT')
49         self._fov = None
50
51     def set_task(self, task_name, args=()):
52         task_class = self.game.tasks[task_name]
53         self.task = task_class(self, args)
54         self.task.check()  # will throw GameError if necessary
55
56     def set_next_task(self, task_name, args=()):
57         task_class = self.game.tasks[task_name]
58         self.next_tasks += [task_class(self, args)]
59
60     def get_next_task(self):
61         if len(self.next_tasks) > 0:
62             task = self.next_tasks.pop(0)
63             task.check()
64             return task
65         else:
66             return None
67
68     def proceed(self):
69         self._fov = None
70         if self.task is None:
71             self.task = self.get_next_task()
72             return
73
74         try:
75             self.task.check()
76         except GameError as e:
77             self.task = None
78             raise GameError
79             return
80         self.task.todo -= 1
81         if self.task.todo <= 0:
82             self._last_task_result = self.task.do()
83             self.game.changed = True
84             self.task = self.get_next_task()
85
86     @property
87     def fov_stencil(self):
88         if self._fov:
89             return self._fov
90         fov_map_class = self.game.map_geometry.fov_map_class
91         self._fov = fov_map_class(self.game.map, self.position)
92         return self._fov
93
94     def fov_stencil_map(self, map):
95         visible_terrain = ''
96         for i in range(self.fov_stencil.size_i):
97             if self.fov_stencil.terrain[i] == '.':
98                 visible_terrain += map.terrain[i]
99             else:
100                 visible_terrain += ' '
101         return visible_terrain
102
103
104
105 class Thing_Player(ThingAnimate):
106     symbol_hint = '@'
107
108     def __init__(self, *args, **kwargs):
109         super().__init__(*args, **kwargs)
110         self.nickname = 'undefined'
111         self.carrying = None