source: nagslang/puzzle.py @ 201:3495a2025bc6

Last change on this file since 201:3495a2025bc6 was 201:3495a2025bc6, checked in by Stefano Rivera <stefano@…>, 7 years ago

Break puzzlers out of game_object.py

File size: 1.9 KB
Line 
1from nagslang.constants import COLLISION_TYPE_PLAYER
2
3
4class PuzzleGlue(object):
5    """Glue that holds bits of a puzzle together.
6    """
7    def __init__(self):
8        self._components = {}
9
10    def add_component(self, name, puzzler):
11        if not isinstance(puzzler, Puzzler):
12            puzzler = puzzler.puzzler
13        self._components[name] = puzzler
14        puzzler.set_glue(self)
15
16    def get_state_of(self, name):
17        return self._components[name].get_state()
18
19
20class Puzzler(object):
21    """Behaviour specific to a puzzle component.
22    """
23    def set_glue(self, glue):
24        self.glue = glue
25
26    def set_game_object(self, game_object):
27        self.game_object = game_object
28
29    def get_state(self):
30        raise NotImplementedError()
31
32
33class YesPuzzler(Puzzler):
34    """Yes sir, I'm always on.
35    """
36    def get_state(self):
37        return True
38
39
40class NoPuzzler(Puzzler):
41    """No sir, I'm always off.
42    """
43    def get_state(self):
44        return False
45
46
47class CollidePuzzler(Puzzler):
48    def __init__(self, *collision_types):
49        if not collision_types:
50            collision_types = (COLLISION_TYPE_PLAYER,)
51        self._collision_types = collision_types
52
53    def get_state(self):
54        space = self.game_object.get_space()
55        for shape in space.shape_query(self.game_object.get_shape()):
56            if shape.collision_type in self._collision_types:
57                return True
58        return False
59
60
61class StateProxyPuzzler(Puzzler):
62    def __init__(self, state_source):
63        self._state_source = state_source
64
65    def get_state(self):
66        return self.glue.get_state_of(self._state_source)
67
68
69class StateLogicalAndPuzzler(Puzzler):
70    def __init__(self, *state_sources):
71        self._state_sources = state_sources
72
73    def get_state(self):
74        for state_source in self._state_sources:
75            if not self.glue.get_state_of(state_source):
76                return False
77        return True
Note: See TracBrowser for help on using the repository browser.