home · contact · privacy
Only send PONG to connection_id that sent the PING.
[plomrogue2-experiments] / new2 / 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_=None, position=(YX(0,0))):
10         self.game = game
11         if id_ is None:
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
28
29 class ThingAnimate(Thing):
30
31     def __init__(self, *args, **kwargs):
32         super().__init__(*args, **kwargs)
33         self.next_tasks = []
34         self.set_task('WAIT')
35
36     def set_task(self, task_name, args=()):
37         task_class = self.game.tasks[task_name]
38         self.task = task_class(self, args)
39         self.task.check()  # will throw GameError if necessary
40
41     def set_next_task(self, task_name, args=()):
42         task_class = self.game.tasks[task_name]
43         self.next_tasks += [task_class(self, args)]
44
45     def get_next_task(self):
46         if len(self.next_tasks) > 0:
47             task = self.next_tasks.pop(0)
48             task.check()
49             return task
50         else:
51             return None
52
53     def proceed(self):
54         if self.task is None:
55             self.task = self.get_next_task()
56             return
57
58         try:
59             self.task.check()
60         except GameError as e:
61             self.task = None
62             raise GameError
63             return
64         self.task.todo -= 1
65         if self.task.todo <= 0:
66             self._last_task_result = self.task.do()
67             self.game.changed = True
68             self.task = self.get_next_task()
69
70
71
72 class ThingPlayer(ThingAnimate):
73     type_ = 'player'
74
75     def __init__(self, *args, **kwargs):
76         super().__init__(*args, **kwargs)
77         self.nickname = 'undefined' 
78