annotate gamelib/state.py @ 65:cab924519037

Move some description management from widget to state object
author Neil Muller <neil@dip.sun.ac.za>
date Mon, 23 Aug 2010 19:45:07 +0200
parents 3087be3463e0
children 158a13a48d48
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
7
d7c9d8273d7f Start of base classes for game state.
Simon Cross <hodgestar+bzr@gmail.com>
parents:
diff changeset
1 """Utilities and base classes for dealing with scenes."""
d7c9d8273d7f Start of base classes for game state.
Simon Cross <hodgestar+bzr@gmail.com>
parents:
diff changeset
2
18
c9b124c2f5c6 Start of scene drawing framework.
Simon Cross <hodgestar+bzr@gmail.com>
parents: 11
diff changeset
3 from albow.resource import get_image, get_sound
23
1e90a3e4618e Add code for blitting Scene.
Simon Cross <hodgestar+bzr@gmail.com>
parents: 22
diff changeset
4 from pygame.locals import BLEND_ADD
63
3087be3463e0 Some framework support for better message handling
Neil Muller <neil@dip.sun.ac.za>
parents: 56
diff changeset
5 from pygame.rect import Rect
11
e9dc681911c4 Add missing newline.
Simon Cross <hodgestar+bzr@gmail.com>
parents: 9
diff changeset
6
28
0f25f7b9b37a Add loading of initial state.
Simon Cross <hodgestar+bzr@gmail.com>
parents: 23
diff changeset
7
9
6db58d31dd4c Function for loading initial state.
Simon Cross <hodgestar+bzr@gmail.com>
parents: 7
diff changeset
8 def initial_state():
6db58d31dd4c Function for loading initial state.
Simon Cross <hodgestar+bzr@gmail.com>
parents: 7
diff changeset
9 """Load the initial state."""
6db58d31dd4c Function for loading initial state.
Simon Cross <hodgestar+bzr@gmail.com>
parents: 7
diff changeset
10 state = State()
28
0f25f7b9b37a Add loading of initial state.
Simon Cross <hodgestar+bzr@gmail.com>
parents: 23
diff changeset
11 state.load_scenes("cryo")
56
75bf3d3689e9 Refactor thing interactivity and add "fake" bridge scene.
Jeremy Thurgood <firxen@gmail.com>
parents: 55
diff changeset
12 state.load_scenes("bridge")
47
8f1fccb8cadf Skeletons for scenes in plot outline.
Simon Cross <simon@simonx>
parents: 41
diff changeset
13 #state.load_scenes("mess")
8f1fccb8cadf Skeletons for scenes in plot outline.
Simon Cross <simon@simonx>
parents: 41
diff changeset
14 #state.load_scenes("engine")
8f1fccb8cadf Skeletons for scenes in plot outline.
Simon Cross <simon@simonx>
parents: 41
diff changeset
15 #state.load_scenes("machine")
8f1fccb8cadf Skeletons for scenes in plot outline.
Simon Cross <simon@simonx>
parents: 41
diff changeset
16 #state.load_scenes("map")
28
0f25f7b9b37a Add loading of initial state.
Simon Cross <hodgestar+bzr@gmail.com>
parents: 23
diff changeset
17 state.set_current_scene("cryo")
9
6db58d31dd4c Function for loading initial state.
Simon Cross <hodgestar+bzr@gmail.com>
parents: 7
diff changeset
18 return state
6db58d31dd4c Function for loading initial state.
Simon Cross <hodgestar+bzr@gmail.com>
parents: 7
diff changeset
19
6db58d31dd4c Function for loading initial state.
Simon Cross <hodgestar+bzr@gmail.com>
parents: 7
diff changeset
20
7
d7c9d8273d7f Start of base classes for game state.
Simon Cross <hodgestar+bzr@gmail.com>
parents:
diff changeset
21 class State(object):
d7c9d8273d7f Start of base classes for game state.
Simon Cross <hodgestar+bzr@gmail.com>
parents:
diff changeset
22 """Complete game state.
d7c9d8273d7f Start of base classes for game state.
Simon Cross <hodgestar+bzr@gmail.com>
parents:
diff changeset
23
d7c9d8273d7f Start of base classes for game state.
Simon Cross <hodgestar+bzr@gmail.com>
parents:
diff changeset
24 Game state consists of:
d7c9d8273d7f Start of base classes for game state.
Simon Cross <hodgestar+bzr@gmail.com>
parents:
diff changeset
25
d7c9d8273d7f Start of base classes for game state.
Simon Cross <hodgestar+bzr@gmail.com>
parents:
diff changeset
26 * items
d7c9d8273d7f Start of base classes for game state.
Simon Cross <hodgestar+bzr@gmail.com>
parents:
diff changeset
27 * scenes
d7c9d8273d7f Start of base classes for game state.
Simon Cross <hodgestar+bzr@gmail.com>
parents:
diff changeset
28 """
d7c9d8273d7f Start of base classes for game state.
Simon Cross <hodgestar+bzr@gmail.com>
parents:
diff changeset
29
d7c9d8273d7f Start of base classes for game state.
Simon Cross <hodgestar+bzr@gmail.com>
parents:
diff changeset
30 def __init__(self):
d7c9d8273d7f Start of base classes for game state.
Simon Cross <hodgestar+bzr@gmail.com>
parents:
diff changeset
31 # map of scene name -> Scene object
d7c9d8273d7f Start of base classes for game state.
Simon Cross <hodgestar+bzr@gmail.com>
parents:
diff changeset
32 self.scenes = {}
d7c9d8273d7f Start of base classes for game state.
Simon Cross <hodgestar+bzr@gmail.com>
parents:
diff changeset
33 # map of item name -> Item object
d7c9d8273d7f Start of base classes for game state.
Simon Cross <hodgestar+bzr@gmail.com>
parents:
diff changeset
34 self.items = {}
33
f8e02d02c782 Convert inventory to list of item objects.
Simon Cross <hodgestar+bzr@gmail.com>
parents: 32
diff changeset
35 # list of item objects in inventory
f8e02d02c782 Convert inventory to list of item objects.
Simon Cross <hodgestar+bzr@gmail.com>
parents: 32
diff changeset
36 self.inventory = []
63
3087be3463e0 Some framework support for better message handling
Neil Muller <neil@dip.sun.ac.za>
parents: 56
diff changeset
37 # Result of the most recent action
3087be3463e0 Some framework support for better message handling
Neil Muller <neil@dip.sun.ac.za>
parents: 56
diff changeset
38 self.msg = None
65
cab924519037 Move some description management from widget to state object
Neil Muller <neil@dip.sun.ac.za>
parents: 63
diff changeset
39 self.description = None
28
0f25f7b9b37a Add loading of initial state.
Simon Cross <hodgestar+bzr@gmail.com>
parents: 23
diff changeset
40 # current scene
0f25f7b9b37a Add loading of initial state.
Simon Cross <hodgestar+bzr@gmail.com>
parents: 23
diff changeset
41 self.current_scene = None
0f25f7b9b37a Add loading of initial state.
Simon Cross <hodgestar+bzr@gmail.com>
parents: 23
diff changeset
42
0f25f7b9b37a Add loading of initial state.
Simon Cross <hodgestar+bzr@gmail.com>
parents: 23
diff changeset
43 def add_scene(self, scene):
0f25f7b9b37a Add loading of initial state.
Simon Cross <hodgestar+bzr@gmail.com>
parents: 23
diff changeset
44 self.scenes[scene.name] = scene
0f25f7b9b37a Add loading of initial state.
Simon Cross <hodgestar+bzr@gmail.com>
parents: 23
diff changeset
45
32
18cb1af411ef Add helper for adding items. Give items names. Make things take a rectangle.
Simon Cross <hodgestar+bzr@gmail.com>
parents: 30
diff changeset
46 def add_item(self, item):
35
ebc76bc0c067 First item!
Simon Cross <hodgestar+bzr@gmail.com>
parents: 34
diff changeset
47 self.items[item.name] = item
32
18cb1af411ef Add helper for adding items. Give items names. Make things take a rectangle.
Simon Cross <hodgestar+bzr@gmail.com>
parents: 30
diff changeset
48
28
0f25f7b9b37a Add loading of initial state.
Simon Cross <hodgestar+bzr@gmail.com>
parents: 23
diff changeset
49 def load_scenes(self, modname):
0f25f7b9b37a Add loading of initial state.
Simon Cross <hodgestar+bzr@gmail.com>
parents: 23
diff changeset
50 mod = __import__("gamelib.scenes.%s" % (modname,), fromlist=[modname])
0f25f7b9b37a Add loading of initial state.
Simon Cross <hodgestar+bzr@gmail.com>
parents: 23
diff changeset
51 for scene_cls in mod.SCENES:
0f25f7b9b37a Add loading of initial state.
Simon Cross <hodgestar+bzr@gmail.com>
parents: 23
diff changeset
52 self.add_scene(scene_cls(self))
0f25f7b9b37a Add loading of initial state.
Simon Cross <hodgestar+bzr@gmail.com>
parents: 23
diff changeset
53
0f25f7b9b37a Add loading of initial state.
Simon Cross <hodgestar+bzr@gmail.com>
parents: 23
diff changeset
54 def set_current_scene(self, name):
29
6322d92dc8f0 Add state widget for rendering state.
Simon Cross <hodgestar+bzr@gmail.com>
parents: 28
diff changeset
55 self.current_scene = self.scenes[name]
28
0f25f7b9b37a Add loading of initial state.
Simon Cross <hodgestar+bzr@gmail.com>
parents: 23
diff changeset
56
30
daf9eecb670f Add inventory manipulation methods.
Simon Cross <hodgestar+bzr@gmail.com>
parents: 29
diff changeset
57 def add_inventory_item(self, name):
33
f8e02d02c782 Convert inventory to list of item objects.
Simon Cross <hodgestar+bzr@gmail.com>
parents: 32
diff changeset
58 self.inventory.append(self.items[name])
30
daf9eecb670f Add inventory manipulation methods.
Simon Cross <hodgestar+bzr@gmail.com>
parents: 29
diff changeset
59
daf9eecb670f Add inventory manipulation methods.
Simon Cross <hodgestar+bzr@gmail.com>
parents: 29
diff changeset
60 def remove_inventory_item(self, name):
34
e5c043aeed65 Inventory and items. And stuff.
Jeremy Thurgood <firxen@gmail.com>
parents: 33
diff changeset
61 self.inventory.remove(self.items[name])
30
daf9eecb670f Add inventory manipulation methods.
Simon Cross <hodgestar+bzr@gmail.com>
parents: 29
diff changeset
62
28
0f25f7b9b37a Add loading of initial state.
Simon Cross <hodgestar+bzr@gmail.com>
parents: 23
diff changeset
63 def draw(self, surface):
0f25f7b9b37a Add loading of initial state.
Simon Cross <hodgestar+bzr@gmail.com>
parents: 23
diff changeset
64 self.current_scene.draw(surface)
7
d7c9d8273d7f Start of base classes for game state.
Simon Cross <hodgestar+bzr@gmail.com>
parents:
diff changeset
65
63
3087be3463e0 Some framework support for better message handling
Neil Muller <neil@dip.sun.ac.za>
parents: 56
diff changeset
66 def get_message(self):
3087be3463e0 Some framework support for better message handling
Neil Muller <neil@dip.sun.ac.za>
parents: 56
diff changeset
67 return self.msg
3087be3463e0 Some framework support for better message handling
Neil Muller <neil@dip.sun.ac.za>
parents: 56
diff changeset
68
3087be3463e0 Some framework support for better message handling
Neil Muller <neil@dip.sun.ac.za>
parents: 56
diff changeset
69 def clear_message(self):
3087be3463e0 Some framework support for better message handling
Neil Muller <neil@dip.sun.ac.za>
parents: 56
diff changeset
70 self.msg = None
3087be3463e0 Some framework support for better message handling
Neil Muller <neil@dip.sun.ac.za>
parents: 56
diff changeset
71
65
cab924519037 Move some description management from widget to state object
Neil Muller <neil@dip.sun.ac.za>
parents: 63
diff changeset
72 # FIXME: sort out how state.interact and description updating should work
cab924519037 Move some description management from widget to state object
Neil Muller <neil@dip.sun.ac.za>
parents: 63
diff changeset
73
cab924519037 Move some description management from widget to state object
Neil Muller <neil@dip.sun.ac.za>
parents: 63
diff changeset
74 def check_for_new_description(self, pos):
cab924519037 Move some description management from widget to state object
Neil Muller <neil@dip.sun.ac.za>
parents: 63
diff changeset
75 """Check if the current mouse position causes a new description"""
cab924519037 Move some description management from widget to state object
Neil Muller <neil@dip.sun.ac.za>
parents: 63
diff changeset
76 old_desc = self.description
cab924519037 Move some description management from widget to state object
Neil Muller <neil@dip.sun.ac.za>
parents: 63
diff changeset
77 self.description = self.current_scene.check_description(pos)
cab924519037 Move some description management from widget to state object
Neil Muller <neil@dip.sun.ac.za>
parents: 63
diff changeset
78 return old_desc != self.description
cab924519037 Move some description management from widget to state object
Neil Muller <neil@dip.sun.ac.za>
parents: 63
diff changeset
79
cab924519037 Move some description management from widget to state object
Neil Muller <neil@dip.sun.ac.za>
parents: 63
diff changeset
80 def get_description(self):
cab924519037 Move some description management from widget to state object
Neil Muller <neil@dip.sun.ac.za>
parents: 63
diff changeset
81 return self.description
63
3087be3463e0 Some framework support for better message handling
Neil Muller <neil@dip.sun.ac.za>
parents: 56
diff changeset
82
41
ad6f56bfa8b7 Cryo door, titanium leg and some interaction prototypes.
Jeremy Thurgood <firxen@gmail.com>
parents: 39
diff changeset
83 def message(self, msg):
63
3087be3463e0 Some framework support for better message handling
Neil Muller <neil@dip.sun.ac.za>
parents: 56
diff changeset
84 self.msg = msg
41
ad6f56bfa8b7 Cryo door, titanium leg and some interaction prototypes.
Jeremy Thurgood <firxen@gmail.com>
parents: 39
diff changeset
85
7
d7c9d8273d7f Start of base classes for game state.
Simon Cross <hodgestar+bzr@gmail.com>
parents:
diff changeset
86
55
2e2f6ff54780 Part of the cryo door puzzle.
Jeremy Thurgood <firxen@gmail.com>
parents: 47
diff changeset
87 class StatefulGizmo(object):
2e2f6ff54780 Part of the cryo door puzzle.
Jeremy Thurgood <firxen@gmail.com>
parents: 47
diff changeset
88
2e2f6ff54780 Part of the cryo door puzzle.
Jeremy Thurgood <firxen@gmail.com>
parents: 47
diff changeset
89 # initial data (optional, defaults to none)
2e2f6ff54780 Part of the cryo door puzzle.
Jeremy Thurgood <firxen@gmail.com>
parents: 47
diff changeset
90 INITIAL_DATA = None
2e2f6ff54780 Part of the cryo door puzzle.
Jeremy Thurgood <firxen@gmail.com>
parents: 47
diff changeset
91
2e2f6ff54780 Part of the cryo door puzzle.
Jeremy Thurgood <firxen@gmail.com>
parents: 47
diff changeset
92 def __init__(self):
2e2f6ff54780 Part of the cryo door puzzle.
Jeremy Thurgood <firxen@gmail.com>
parents: 47
diff changeset
93 self.data = {}
2e2f6ff54780 Part of the cryo door puzzle.
Jeremy Thurgood <firxen@gmail.com>
parents: 47
diff changeset
94 if self.INITIAL_DATA:
2e2f6ff54780 Part of the cryo door puzzle.
Jeremy Thurgood <firxen@gmail.com>
parents: 47
diff changeset
95 self.data.update(self.INITIAL_DATA)
2e2f6ff54780 Part of the cryo door puzzle.
Jeremy Thurgood <firxen@gmail.com>
parents: 47
diff changeset
96
2e2f6ff54780 Part of the cryo door puzzle.
Jeremy Thurgood <firxen@gmail.com>
parents: 47
diff changeset
97 def set_data(self, key, value):
2e2f6ff54780 Part of the cryo door puzzle.
Jeremy Thurgood <firxen@gmail.com>
parents: 47
diff changeset
98 self.data[key] = value
2e2f6ff54780 Part of the cryo door puzzle.
Jeremy Thurgood <firxen@gmail.com>
parents: 47
diff changeset
99
2e2f6ff54780 Part of the cryo door puzzle.
Jeremy Thurgood <firxen@gmail.com>
parents: 47
diff changeset
100 def get_data(self, key):
2e2f6ff54780 Part of the cryo door puzzle.
Jeremy Thurgood <firxen@gmail.com>
parents: 47
diff changeset
101 return self.data.get(key, None)
2e2f6ff54780 Part of the cryo door puzzle.
Jeremy Thurgood <firxen@gmail.com>
parents: 47
diff changeset
102
2e2f6ff54780 Part of the cryo door puzzle.
Jeremy Thurgood <firxen@gmail.com>
parents: 47
diff changeset
103
2e2f6ff54780 Part of the cryo door puzzle.
Jeremy Thurgood <firxen@gmail.com>
parents: 47
diff changeset
104 class Scene(StatefulGizmo):
7
d7c9d8273d7f Start of base classes for game state.
Simon Cross <hodgestar+bzr@gmail.com>
parents:
diff changeset
105 """Base class for scenes."""
d7c9d8273d7f Start of base classes for game state.
Simon Cross <hodgestar+bzr@gmail.com>
parents:
diff changeset
106
28
0f25f7b9b37a Add loading of initial state.
Simon Cross <hodgestar+bzr@gmail.com>
parents: 23
diff changeset
107 # sub-folder to look for resources in
22
8e709824306d Add items and things.
Simon Cross <hodgestar+bzr@gmail.com>
parents: 18
diff changeset
108 FOLDER = None
28
0f25f7b9b37a Add loading of initial state.
Simon Cross <hodgestar+bzr@gmail.com>
parents: 23
diff changeset
109
0f25f7b9b37a Add loading of initial state.
Simon Cross <hodgestar+bzr@gmail.com>
parents: 23
diff changeset
110 # name of background image resource
22
8e709824306d Add items and things.
Simon Cross <hodgestar+bzr@gmail.com>
parents: 18
diff changeset
111 BACKGROUND = None
8e709824306d Add items and things.
Simon Cross <hodgestar+bzr@gmail.com>
parents: 18
diff changeset
112
28
0f25f7b9b37a Add loading of initial state.
Simon Cross <hodgestar+bzr@gmail.com>
parents: 23
diff changeset
113 # name of scene (optional, defaults to folder)
0f25f7b9b37a Add loading of initial state.
Simon Cross <hodgestar+bzr@gmail.com>
parents: 23
diff changeset
114 NAME = None
0f25f7b9b37a Add loading of initial state.
Simon Cross <hodgestar+bzr@gmail.com>
parents: 23
diff changeset
115
0f25f7b9b37a Add loading of initial state.
Simon Cross <hodgestar+bzr@gmail.com>
parents: 23
diff changeset
116 def __init__(self, state):
55
2e2f6ff54780 Part of the cryo door puzzle.
Jeremy Thurgood <firxen@gmail.com>
parents: 47
diff changeset
117 StatefulGizmo.__init__(self)
28
0f25f7b9b37a Add loading of initial state.
Simon Cross <hodgestar+bzr@gmail.com>
parents: 23
diff changeset
118 # scene name
0f25f7b9b37a Add loading of initial state.
Simon Cross <hodgestar+bzr@gmail.com>
parents: 23
diff changeset
119 self.name = self.NAME if self.NAME is not None else self.FOLDER
0f25f7b9b37a Add loading of initial state.
Simon Cross <hodgestar+bzr@gmail.com>
parents: 23
diff changeset
120 # link back to state object
0f25f7b9b37a Add loading of initial state.
Simon Cross <hodgestar+bzr@gmail.com>
parents: 23
diff changeset
121 self.state = state
22
8e709824306d Add items and things.
Simon Cross <hodgestar+bzr@gmail.com>
parents: 18
diff changeset
122 # map of thing names -> Thing objects
8e709824306d Add items and things.
Simon Cross <hodgestar+bzr@gmail.com>
parents: 18
diff changeset
123 self.things = {}
28
0f25f7b9b37a Add loading of initial state.
Simon Cross <hodgestar+bzr@gmail.com>
parents: 23
diff changeset
124 self._background = get_image(self.FOLDER, self.BACKGROUND)
7
d7c9d8273d7f Start of base classes for game state.
Simon Cross <hodgestar+bzr@gmail.com>
parents:
diff changeset
125
35
ebc76bc0c067 First item!
Simon Cross <hodgestar+bzr@gmail.com>
parents: 34
diff changeset
126 def add_item(self, item):
ebc76bc0c067 First item!
Simon Cross <hodgestar+bzr@gmail.com>
parents: 34
diff changeset
127 self.state.add_item(item)
ebc76bc0c067 First item!
Simon Cross <hodgestar+bzr@gmail.com>
parents: 34
diff changeset
128
39
088a101f5b94 Add an example Thing to cryo scene.
Simon Cross <hodgestar+bzr@gmail.com>
parents: 35
diff changeset
129 def add_thing(self, thing):
088a101f5b94 Add an example Thing to cryo scene.
Simon Cross <hodgestar+bzr@gmail.com>
parents: 35
diff changeset
130 self.things[thing.name] = thing
41
ad6f56bfa8b7 Cryo door, titanium leg and some interaction prototypes.
Jeremy Thurgood <firxen@gmail.com>
parents: 39
diff changeset
131 thing.set_scene(self)
ad6f56bfa8b7 Cryo door, titanium leg and some interaction prototypes.
Jeremy Thurgood <firxen@gmail.com>
parents: 39
diff changeset
132
ad6f56bfa8b7 Cryo door, titanium leg and some interaction prototypes.
Jeremy Thurgood <firxen@gmail.com>
parents: 39
diff changeset
133 def remove_thing(self, thing):
ad6f56bfa8b7 Cryo door, titanium leg and some interaction prototypes.
Jeremy Thurgood <firxen@gmail.com>
parents: 39
diff changeset
134 del self.things[thing.name]
39
088a101f5b94 Add an example Thing to cryo scene.
Simon Cross <hodgestar+bzr@gmail.com>
parents: 35
diff changeset
135
22
8e709824306d Add items and things.
Simon Cross <hodgestar+bzr@gmail.com>
parents: 18
diff changeset
136 def draw_background(self, surface):
23
1e90a3e4618e Add code for blitting Scene.
Simon Cross <hodgestar+bzr@gmail.com>
parents: 22
diff changeset
137 surface.blit(self._background, (0, 0), None, BLEND_ADD)
18
c9b124c2f5c6 Start of scene drawing framework.
Simon Cross <hodgestar+bzr@gmail.com>
parents: 11
diff changeset
138
23
1e90a3e4618e Add code for blitting Scene.
Simon Cross <hodgestar+bzr@gmail.com>
parents: 22
diff changeset
139 def draw_things(self, surface):
1e90a3e4618e Add code for blitting Scene.
Simon Cross <hodgestar+bzr@gmail.com>
parents: 22
diff changeset
140 for thing in self.things.itervalues():
1e90a3e4618e Add code for blitting Scene.
Simon Cross <hodgestar+bzr@gmail.com>
parents: 22
diff changeset
141 thing.draw(surface)
18
c9b124c2f5c6 Start of scene drawing framework.
Simon Cross <hodgestar+bzr@gmail.com>
parents: 11
diff changeset
142
22
8e709824306d Add items and things.
Simon Cross <hodgestar+bzr@gmail.com>
parents: 18
diff changeset
143 def draw(self, surface):
8e709824306d Add items and things.
Simon Cross <hodgestar+bzr@gmail.com>
parents: 18
diff changeset
144 self.draw_background(surface)
23
1e90a3e4618e Add code for blitting Scene.
Simon Cross <hodgestar+bzr@gmail.com>
parents: 22
diff changeset
145 self.draw_things(surface)
18
c9b124c2f5c6 Start of scene drawing framework.
Simon Cross <hodgestar+bzr@gmail.com>
parents: 11
diff changeset
146
65
cab924519037 Move some description management from widget to state object
Neil Muller <neil@dip.sun.ac.za>
parents: 63
diff changeset
147 def check_description(self, pos):
63
3087be3463e0 Some framework support for better message handling
Neil Muller <neil@dip.sun.ac.za>
parents: 56
diff changeset
148 desc = None
3087be3463e0 Some framework support for better message handling
Neil Muller <neil@dip.sun.ac.za>
parents: 56
diff changeset
149 for thing in self.things.itervalues():
3087be3463e0 Some framework support for better message handling
Neil Muller <neil@dip.sun.ac.za>
parents: 56
diff changeset
150 # Last thing in the list that matches wins
3087be3463e0 Some framework support for better message handling
Neil Muller <neil@dip.sun.ac.za>
parents: 56
diff changeset
151 if Rect(thing.rect).collidepoint(pos):
3087be3463e0 Some framework support for better message handling
Neil Muller <neil@dip.sun.ac.za>
parents: 56
diff changeset
152 desc = thing.get_description()
3087be3463e0 Some framework support for better message handling
Neil Muller <neil@dip.sun.ac.za>
parents: 56
diff changeset
153 return desc
3087be3463e0 Some framework support for better message handling
Neil Muller <neil@dip.sun.ac.za>
parents: 56
diff changeset
154
7
d7c9d8273d7f Start of base classes for game state.
Simon Cross <hodgestar+bzr@gmail.com>
parents:
diff changeset
155
55
2e2f6ff54780 Part of the cryo door puzzle.
Jeremy Thurgood <firxen@gmail.com>
parents: 47
diff changeset
156 class Thing(StatefulGizmo):
22
8e709824306d Add items and things.
Simon Cross <hodgestar+bzr@gmail.com>
parents: 18
diff changeset
157 """Base class for things in a scene that you can interact with."""
7
d7c9d8273d7f Start of base classes for game state.
Simon Cross <hodgestar+bzr@gmail.com>
parents:
diff changeset
158
41
ad6f56bfa8b7 Cryo door, titanium leg and some interaction prototypes.
Jeremy Thurgood <firxen@gmail.com>
parents: 39
diff changeset
159 # sub-folder to look for resources in
ad6f56bfa8b7 Cryo door, titanium leg and some interaction prototypes.
Jeremy Thurgood <firxen@gmail.com>
parents: 39
diff changeset
160 FOLDER = None
ad6f56bfa8b7 Cryo door, titanium leg and some interaction prototypes.
Jeremy Thurgood <firxen@gmail.com>
parents: 39
diff changeset
161
ad6f56bfa8b7 Cryo door, titanium leg and some interaction prototypes.
Jeremy Thurgood <firxen@gmail.com>
parents: 39
diff changeset
162 # name of image resource
ad6f56bfa8b7 Cryo door, titanium leg and some interaction prototypes.
Jeremy Thurgood <firxen@gmail.com>
parents: 39
diff changeset
163 IMAGE = None
ad6f56bfa8b7 Cryo door, titanium leg and some interaction prototypes.
Jeremy Thurgood <firxen@gmail.com>
parents: 39
diff changeset
164
39
088a101f5b94 Add an example Thing to cryo scene.
Simon Cross <hodgestar+bzr@gmail.com>
parents: 35
diff changeset
165 def __init__(self, name, rect):
55
2e2f6ff54780 Part of the cryo door puzzle.
Jeremy Thurgood <firxen@gmail.com>
parents: 47
diff changeset
166 StatefulGizmo.__init__(self)
39
088a101f5b94 Add an example Thing to cryo scene.
Simon Cross <hodgestar+bzr@gmail.com>
parents: 35
diff changeset
167 self.name = name
088a101f5b94 Add an example Thing to cryo scene.
Simon Cross <hodgestar+bzr@gmail.com>
parents: 35
diff changeset
168 # area within scene that triggers calls to interact
32
18cb1af411ef Add helper for adding items. Give items names. Make things take a rectangle.
Simon Cross <hodgestar+bzr@gmail.com>
parents: 30
diff changeset
169 self.rect = rect
39
088a101f5b94 Add an example Thing to cryo scene.
Simon Cross <hodgestar+bzr@gmail.com>
parents: 35
diff changeset
170 # these are set by set_scene
088a101f5b94 Add an example Thing to cryo scene.
Simon Cross <hodgestar+bzr@gmail.com>
parents: 35
diff changeset
171 self.scene = None
088a101f5b94 Add an example Thing to cryo scene.
Simon Cross <hodgestar+bzr@gmail.com>
parents: 35
diff changeset
172 self.state = None
32
18cb1af411ef Add helper for adding items. Give items names. Make things take a rectangle.
Simon Cross <hodgestar+bzr@gmail.com>
parents: 30
diff changeset
173 # TODO: add masks
18cb1af411ef Add helper for adding items. Give items names. Make things take a rectangle.
Simon Cross <hodgestar+bzr@gmail.com>
parents: 30
diff changeset
174 # TODO: add images
7
d7c9d8273d7f Start of base classes for game state.
Simon Cross <hodgestar+bzr@gmail.com>
parents:
diff changeset
175
39
088a101f5b94 Add an example Thing to cryo scene.
Simon Cross <hodgestar+bzr@gmail.com>
parents: 35
diff changeset
176 def set_scene(self, scene):
088a101f5b94 Add an example Thing to cryo scene.
Simon Cross <hodgestar+bzr@gmail.com>
parents: 35
diff changeset
177 assert self.scene is None
088a101f5b94 Add an example Thing to cryo scene.
Simon Cross <hodgestar+bzr@gmail.com>
parents: 35
diff changeset
178 self.scene = scene
088a101f5b94 Add an example Thing to cryo scene.
Simon Cross <hodgestar+bzr@gmail.com>
parents: 35
diff changeset
179 self.state = scene.state
088a101f5b94 Add an example Thing to cryo scene.
Simon Cross <hodgestar+bzr@gmail.com>
parents: 35
diff changeset
180
41
ad6f56bfa8b7 Cryo door, titanium leg and some interaction prototypes.
Jeremy Thurgood <firxen@gmail.com>
parents: 39
diff changeset
181 def message(self, msg):
ad6f56bfa8b7 Cryo door, titanium leg and some interaction prototypes.
Jeremy Thurgood <firxen@gmail.com>
parents: 39
diff changeset
182 self.state.message(msg)
ad6f56bfa8b7 Cryo door, titanium leg and some interaction prototypes.
Jeremy Thurgood <firxen@gmail.com>
parents: 39
diff changeset
183
63
3087be3463e0 Some framework support for better message handling
Neil Muller <neil@dip.sun.ac.za>
parents: 56
diff changeset
184 def get_description(self):
3087be3463e0 Some framework support for better message handling
Neil Muller <neil@dip.sun.ac.za>
parents: 56
diff changeset
185 return None
3087be3463e0 Some framework support for better message handling
Neil Muller <neil@dip.sun.ac.za>
parents: 56
diff changeset
186
56
75bf3d3689e9 Refactor thing interactivity and add "fake" bridge scene.
Jeremy Thurgood <firxen@gmail.com>
parents: 55
diff changeset
187 def is_interactive(self):
75bf3d3689e9 Refactor thing interactivity and add "fake" bridge scene.
Jeremy Thurgood <firxen@gmail.com>
parents: 55
diff changeset
188 return True
75bf3d3689e9 Refactor thing interactivity and add "fake" bridge scene.
Jeremy Thurgood <firxen@gmail.com>
parents: 55
diff changeset
189
22
8e709824306d Add items and things.
Simon Cross <hodgestar+bzr@gmail.com>
parents: 18
diff changeset
190 def interact(self, item):
56
75bf3d3689e9 Refactor thing interactivity and add "fake" bridge scene.
Jeremy Thurgood <firxen@gmail.com>
parents: 55
diff changeset
191 if not self.is_interactive():
75bf3d3689e9 Refactor thing interactivity and add "fake" bridge scene.
Jeremy Thurgood <firxen@gmail.com>
parents: 55
diff changeset
192 return
41
ad6f56bfa8b7 Cryo door, titanium leg and some interaction prototypes.
Jeremy Thurgood <firxen@gmail.com>
parents: 39
diff changeset
193 if item is None:
ad6f56bfa8b7 Cryo door, titanium leg and some interaction prototypes.
Jeremy Thurgood <firxen@gmail.com>
parents: 39
diff changeset
194 self.interact_without()
ad6f56bfa8b7 Cryo door, titanium leg and some interaction prototypes.
Jeremy Thurgood <firxen@gmail.com>
parents: 39
diff changeset
195 else:
ad6f56bfa8b7 Cryo door, titanium leg and some interaction prototypes.
Jeremy Thurgood <firxen@gmail.com>
parents: 39
diff changeset
196 handler = getattr(self, 'interact_with_' + item.name, None)
ad6f56bfa8b7 Cryo door, titanium leg and some interaction prototypes.
Jeremy Thurgood <firxen@gmail.com>
parents: 39
diff changeset
197 if handler is not None:
ad6f56bfa8b7 Cryo door, titanium leg and some interaction prototypes.
Jeremy Thurgood <firxen@gmail.com>
parents: 39
diff changeset
198 handler(item)
ad6f56bfa8b7 Cryo door, titanium leg and some interaction prototypes.
Jeremy Thurgood <firxen@gmail.com>
parents: 39
diff changeset
199 else:
ad6f56bfa8b7 Cryo door, titanium leg and some interaction prototypes.
Jeremy Thurgood <firxen@gmail.com>
parents: 39
diff changeset
200 self.interact_default(item)
ad6f56bfa8b7 Cryo door, titanium leg and some interaction prototypes.
Jeremy Thurgood <firxen@gmail.com>
parents: 39
diff changeset
201
ad6f56bfa8b7 Cryo door, titanium leg and some interaction prototypes.
Jeremy Thurgood <firxen@gmail.com>
parents: 39
diff changeset
202 def interact_without(self):
ad6f56bfa8b7 Cryo door, titanium leg and some interaction prototypes.
Jeremy Thurgood <firxen@gmail.com>
parents: 39
diff changeset
203 self.interact_default(None)
ad6f56bfa8b7 Cryo door, titanium leg and some interaction prototypes.
Jeremy Thurgood <firxen@gmail.com>
parents: 39
diff changeset
204
ad6f56bfa8b7 Cryo door, titanium leg and some interaction prototypes.
Jeremy Thurgood <firxen@gmail.com>
parents: 39
diff changeset
205 def interact_default(self, item):
ad6f56bfa8b7 Cryo door, titanium leg and some interaction prototypes.
Jeremy Thurgood <firxen@gmail.com>
parents: 39
diff changeset
206 self.message("It doesn't work.")
22
8e709824306d Add items and things.
Simon Cross <hodgestar+bzr@gmail.com>
parents: 18
diff changeset
207
23
1e90a3e4618e Add code for blitting Scene.
Simon Cross <hodgestar+bzr@gmail.com>
parents: 22
diff changeset
208 def draw(self, surface):
1e90a3e4618e Add code for blitting Scene.
Simon Cross <hodgestar+bzr@gmail.com>
parents: 22
diff changeset
209 pass
1e90a3e4618e Add code for blitting Scene.
Simon Cross <hodgestar+bzr@gmail.com>
parents: 22
diff changeset
210
22
8e709824306d Add items and things.
Simon Cross <hodgestar+bzr@gmail.com>
parents: 18
diff changeset
211
8e709824306d Add items and things.
Simon Cross <hodgestar+bzr@gmail.com>
parents: 18
diff changeset
212 class Item(object):
8e709824306d Add items and things.
Simon Cross <hodgestar+bzr@gmail.com>
parents: 18
diff changeset
213 """Base class for inventory items."""
8e709824306d Add items and things.
Simon Cross <hodgestar+bzr@gmail.com>
parents: 18
diff changeset
214
35
ebc76bc0c067 First item!
Simon Cross <hodgestar+bzr@gmail.com>
parents: 34
diff changeset
215 # image for inventory
ebc76bc0c067 First item!
Simon Cross <hodgestar+bzr@gmail.com>
parents: 34
diff changeset
216 INVENTORY_IMAGE = None
32
18cb1af411ef Add helper for adding items. Give items names. Make things take a rectangle.
Simon Cross <hodgestar+bzr@gmail.com>
parents: 30
diff changeset
217
35
ebc76bc0c067 First item!
Simon Cross <hodgestar+bzr@gmail.com>
parents: 34
diff changeset
218 def __init__(self, name):
ebc76bc0c067 First item!
Simon Cross <hodgestar+bzr@gmail.com>
parents: 34
diff changeset
219 self.name = name
ebc76bc0c067 First item!
Simon Cross <hodgestar+bzr@gmail.com>
parents: 34
diff changeset
220 self.inventory_image = get_image('items', self.INVENTORY_IMAGE)
32
18cb1af411ef Add helper for adding items. Give items names. Make things take a rectangle.
Simon Cross <hodgestar+bzr@gmail.com>
parents: 30
diff changeset
221 # TODO: needs cursor
22
8e709824306d Add items and things.
Simon Cross <hodgestar+bzr@gmail.com>
parents: 18
diff changeset
222
34
e5c043aeed65 Inventory and items. And stuff.
Jeremy Thurgood <firxen@gmail.com>
parents: 33
diff changeset
223 def get_inventory_image(self):
e5c043aeed65 Inventory and items. And stuff.
Jeremy Thurgood <firxen@gmail.com>
parents: 33
diff changeset
224 return self.inventory_image
e5c043aeed65 Inventory and items. And stuff.
Jeremy Thurgood <firxen@gmail.com>
parents: 33
diff changeset
225