source: nagslang/environment.py@ 31:c62ed518e5c8

Last change on this file since 31:c62ed518e5c8 was 28:c03982fe3c70, checked in by Jeremy Thurgood <firxen@…>, 8 years ago

Protagonist and environment.

File size: 2.3 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 def __init__(self, *conditions):
13 self.conditions = conditions
14
15 def check(self, protagonist):
16 for condition in self.conditions:
17 if not condition.check(protagonist):
18 return False
19 return True
20
21
22class AnyCondition(ProtagonistCondition):
23 def __init__(self, *conditions):
24 self.conditions = conditions
25
26 def check(self, protagonist):
27 for condition in self.conditions:
28 if condition.check(protagonist):
29 return True
30 return False
31
32
33class WolfFormCondition(ProtagonistCondition):
34 def check(self, protagonist):
35 return protagonist.in_wolf_form()
36
37
38class HumanFormCondition(ProtagonistCondition):
39 def check(self, protagonist):
40 return protagonist.in_human_form()
41
42
43class ItemRequiredCondition(ProtagonistCondition):
44 def __init__(self, required_item):
45 self.required_item = required_item
46
47 def check(self, protagonist):
48 return protagonist.has_item(self.required_item)
49
50
51class Action(object):
52 """Representation of an action that can be performed.
53
54 If the (optional) condition is met, the provided function will be called
55 with the protagonist and the target as parameters.
56 """
57 def __init__(self, func, condition=None):
58 self.func = func
59 self.condition = condition
60
61 def check(self, protagonist):
62 if self.condition is None:
63 return True
64 return self.condition.check(protagonist)
65
66 def perform(self, protagonist, target):
67 if not self.check(protagonist):
68 raise ValueError("Attempt to perform invalid action.")
69 return self.func(protagonist, target)
70
71
72class Interactible(object):
73 """The property of interactibility on a thing.
74 """
75
76 def __init__(self, *actions):
77 self.actions = actions
78
79 def select_action(self, protagonist):
80 """Select a possible action given the protagonist's state.
81 """
82 for action in self.actions:
83 if action.check(protagonist):
84 return action
85 return None
Note: See TracBrowser for help on using the repository browser.