1 | class 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 |
|
---|
11 | class 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 |
|
---|
27 | class 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 |
|
---|
43 | class 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 |
|
---|
50 | class 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 |
|
---|
57 | class 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 |
|
---|
67 | class 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 |
|
---|
77 | class Action(object):
|
---|
78 | """Representation of an action that can be performed.
|
---|
79 |
|
---|
80 | If the (optional) condition is met, the provided function will be called
|
---|
81 | with the protagonist and the target as parameters.
|
---|
82 | """
|
---|
83 | def __init__(self, func, condition=None):
|
---|
84 | self.func = func
|
---|
85 | self.condition = condition
|
---|
86 |
|
---|
87 | def check(self, protagonist):
|
---|
88 | if self.condition is None:
|
---|
89 | return True
|
---|
90 | return self.condition.check(protagonist)
|
---|
91 |
|
---|
92 | def perform(self, protagonist, target):
|
---|
93 | if not self.check(protagonist):
|
---|
94 | raise ValueError("Attempt to perform invalid action.")
|
---|
95 | return self.func(protagonist, target)
|
---|
96 |
|
---|
97 |
|
---|
98 | class Interactible(object):
|
---|
99 | """The property of interactibility on a thing.
|
---|
100 | """
|
---|
101 |
|
---|
102 | def __init__(self, *actions):
|
---|
103 | self.actions = actions
|
---|
104 |
|
---|
105 | def select_action(self, protagonist):
|
---|
106 | """Select a possible action given the protagonist's state.
|
---|
107 | """
|
---|
108 | for action in self.actions:
|
---|
109 | if action.check(protagonist):
|
---|
110 | return action
|
---|
111 | return None
|
---|