home · contact · privacy
Add basic non-player things system.
[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
21     def __init__(self, *args, **kwargs):
22         super().__init__(*args, **kwargs)
23
24     def proceed(self):
25         pass
26
27     @property
28     def type_(self):
29         return self.__class__.get_type()
30
31     @classmethod
32     def get_type(cls):
33         return cls.__name__[len('Thing_'):]
34
35
36
37 class Thing_Stone(Thing):
38     symbol_hint = 'o'
39
40
41 class ThingAnimate(Thing):
42
43     def __init__(self, *args, **kwargs):
44         super().__init__(*args, **kwargs)
45         self.next_tasks = []
46         self.set_task('WAIT')
47         self._fov = None
48
49     def set_task(self, task_name, args=()):
50         task_class = self.game.tasks[task_name]
51         self.task = task_class(self, args)
52         self.task.check()  # will throw GameError if necessary
53
54     def set_next_task(self, task_name, args=()):
55         task_class = self.game.tasks[task_name]
56         self.next_tasks += [task_class(self, args)]
57
58     def get_next_task(self):
59         if len(self.next_tasks) > 0:
60             task = self.next_tasks.pop(0)
61             task.check()
62             return task
63         else:
64             return None
65
66     def proceed(self):
67         self._fov = None
68         if self.task is None:
69             self.task = self.get_next_task()
70             return
71
72         try:
73             self.task.check()
74         except GameError as e:
75             self.task = None
76             raise GameError
77             return
78         self.task.todo -= 1
79         if self.task.todo <= 0:
80             self._last_task_result = self.task.do()
81             self.game.changed = True
82             self.task = self.get_next_task()
83
84     @property
85     def fov_stencil(self):
86         if self._fov:
87             return self._fov
88         fov_map_class = self.game.map_geometry.fov_map_class
89         self._fov = fov_map_class(self.game.map, self.position)
90         return self._fov
91
92     def fov_stencil_map(self, map):
93         visible_terrain = ''
94         for i in range(self.fov_stencil.size_i):
95             if self.fov_stencil.terrain[i] == '.':
96                 visible_terrain += map.terrain[i]
97             else:
98                 visible_terrain += ' '
99         return visible_terrain
100
101
102
103 class Thing_Player(ThingAnimate):
104     symbol_hint = '@'
105
106     def __init__(self, *args, **kwargs):
107         super().__init__(*args, **kwargs)
108         self.nickname = 'undefined'