source: nagslang/environment.py@ 281:9b56e954c674

Last change on this file since 281:9b56e954c674 was 281:9b56e954c674, checked in by Jeremy Thurgood <firxen@…>, 8 years ago

Protagonist actions, now required for operating doors.

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