source: nagslang/environment.py

Last change on this file was 359:d42752ab3231, checked in by Jeremy Thurgood <firxen@…>, 8 years ago

Refactor doors and add horrible temporoary image for someone to fix later.

File size: 3.6 KB
Line 
1class ProtagonistCondition(object):
2 """A condition on the protagonist that can be checked.
3 """
4
5 def check(self, protagonist):
6 """Check if this condition applies.
7 """
8 raise NotImplementedError()
9
10
11class YesCondition(ProtagonistCondition):
12 """Condition that is always met.
13 """
14 def check(self, protagonist):
15 return True
16
17
18class AllConditions(ProtagonistCondition):
19 """Condition that is met if all provided conditions are met.
20
21 Conditions are evaluated lazily, so the first condition to fail stops
22 processing.
23 """
24 def __init__(self, *conditions):
25 self.conditions = conditions
26
27 def check(self, protagonist):
28 for condition in self.conditions:
29 if not condition.check(protagonist):
30 return False
31 return True
32
33
34class AnyCondition(ProtagonistCondition):
35 """Condition that is met if any provided condition is met.
36
37 Conditions are evaluated lazily, so the first condition to pass stops
38 processing.
39 """
40 def __init__(self, *conditions):
41 self.conditions = conditions
42
43 def check(self, protagonist):
44 for condition in self.conditions:
45 if condition.check(protagonist):
46 return True
47 return False
48
49
50class WolfFormCondition(ProtagonistCondition):
51 """Condition that is met if the protagonist is in wolf form.
52 """
53 def check(self, protagonist):
54 return protagonist.in_wolf_form()
55
56
57class HumanFormCondition(ProtagonistCondition):
58 """Condition that is met if the protagonist is in human form.
59 """
60 def check(self, protagonist):
61 return protagonist.in_human_form()
62
63
64class ItemRequiredCondition(ProtagonistCondition):
65 """Condition that is met if the protagonist has the required item.
66 """
67 def __init__(self, required_item):
68 self.required_item = required_item
69
70 def check(self, protagonist):
71 return protagonist.has_item(self.required_item)
72
73
74class FunctionCondition(ProtagonistCondition):
75 """Condition that is met if the provided function returns `True`.
76 """
77 def __init__(self, func):
78 self.func = func
79
80 def check(self, protagonist):
81 return self.func(protagonist)
82
83
84class PuzzleStateCondition(ProtagonistCondition):
85 """Condition that is met if the provided function returns `True`.
86 """
87 def __init__(self, puzzler):
88 self.puzzler = puzzler
89
90 def check(self, protagonist):
91 return self.puzzler.get_state()
92
93
94class Action(object):
95 """Representation of an action that can be performed.
96
97 If the (optional) condition is met, the provided function will be called
98 with the protagonist as a parameter. It is assumed that the function
99 already knows about the target.
100 """
101 def __init__(self, func, condition=None):
102 self.func = func
103 self.condition = condition
104
105 def check(self, protagonist):
106 if self.condition is None:
107 return True
108 return self.condition.check(protagonist)
109
110 def perform(self, protagonist):
111 if not self.check(protagonist):
112 raise ValueError("Attempt to perform invalid action.")
113 return self.func(protagonist)
114
115
116class Interactible(object):
117 """The property of interactibility on a thing.
118 """
119
120 def __init__(self, *actions):
121 self.actions = actions
122
123 def set_game_object(self, game_object):
124 self.game_object = game_object
125
126 def select_action(self, protagonist):
127 """Select a possible action given the protagonist's state.
128 """
129 for action in self.actions:
130 if action.check(protagonist):
131 return action
132 return None
Note: See TracBrowser for help on using the repository browser.