annotate gamelib/state.py @ 78:6bfebfbce42e

Partial message support
author Neil Muller <neil@dip.sun.ac.za>
date Mon, 23 Aug 2010 23:05:55 +0200
parents bb7c8072f8c0
children d7c0a702a0b4
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
68
158a13a48d48 Show interact rectangles when constants.DEBUG is True.
Simon Cross <hodgestar+bzr@gmail.com>
parents: 65
diff changeset
4 from albow.utils import frame_rect
76
1a5fdc225939 Add description rendering to scene rendering.
Simon Cross <hodgestar+bzr@gmail.com>
parents: 75
diff changeset
5 from albow.controls import Label
23
1e90a3e4618e Add code for blitting Scene.
Simon Cross <hodgestar+bzr@gmail.com>
parents: 22
diff changeset
6 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
7 from pygame.rect import Rect
68
158a13a48d48 Show interact rectangles when constants.DEBUG is True.
Simon Cross <hodgestar+bzr@gmail.com>
parents: 65
diff changeset
8 from pygame.color import Color
158a13a48d48 Show interact rectangles when constants.DEBUG is True.
Simon Cross <hodgestar+bzr@gmail.com>
parents: 65
diff changeset
9
158a13a48d48 Show interact rectangles when constants.DEBUG is True.
Simon Cross <hodgestar+bzr@gmail.com>
parents: 65
diff changeset
10 import constants
11
e9dc681911c4 Add missing newline.
Simon Cross <hodgestar+bzr@gmail.com>
parents: 9
diff changeset
11
78
6bfebfbce42e Partial message support
Neil Muller <neil@dip.sun.ac.za>
parents: 77
diff changeset
12 class Result(object):
6bfebfbce42e Partial message support
Neil Muller <neil@dip.sun.ac.za>
parents: 77
diff changeset
13 """Result of interacting with a thing"""
6bfebfbce42e Partial message support
Neil Muller <neil@dip.sun.ac.za>
parents: 77
diff changeset
14
6bfebfbce42e Partial message support
Neil Muller <neil@dip.sun.ac.za>
parents: 77
diff changeset
15 def __init__(self, message=None):
6bfebfbce42e Partial message support
Neil Muller <neil@dip.sun.ac.za>
parents: 77
diff changeset
16 self.message = message
6bfebfbce42e Partial message support
Neil Muller <neil@dip.sun.ac.za>
parents: 77
diff changeset
17
28
0f25f7b9b37a Add loading of initial state.
Simon Cross <hodgestar+bzr@gmail.com>
parents: 23
diff changeset
18
9
6db58d31dd4c Function for loading initial state.
Simon Cross <hodgestar+bzr@gmail.com>
parents: 7
diff changeset
19 def initial_state():
6db58d31dd4c Function for loading initial state.
Simon Cross <hodgestar+bzr@gmail.com>
parents: 7
diff changeset
20 """Load the initial state."""
6db58d31dd4c Function for loading initial state.
Simon Cross <hodgestar+bzr@gmail.com>
parents: 7
diff changeset
21 state = State()
28
0f25f7b9b37a Add loading of initial state.
Simon Cross <hodgestar+bzr@gmail.com>
parents: 23
diff changeset
22 state.load_scenes("cryo")
56
75bf3d3689e9 Refactor thing interactivity and add "fake" bridge scene.
Jeremy Thurgood <firxen@gmail.com>
parents: 55
diff changeset
23 state.load_scenes("bridge")
47
8f1fccb8cadf Skeletons for scenes in plot outline.
Simon Cross <simon@simonx>
parents: 41
diff changeset
24 #state.load_scenes("mess")
8f1fccb8cadf Skeletons for scenes in plot outline.
Simon Cross <simon@simonx>
parents: 41
diff changeset
25 #state.load_scenes("engine")
8f1fccb8cadf Skeletons for scenes in plot outline.
Simon Cross <simon@simonx>
parents: 41
diff changeset
26 #state.load_scenes("machine")
8f1fccb8cadf Skeletons for scenes in plot outline.
Simon Cross <simon@simonx>
parents: 41
diff changeset
27 #state.load_scenes("map")
28
0f25f7b9b37a Add loading of initial state.
Simon Cross <hodgestar+bzr@gmail.com>
parents: 23
diff changeset
28 state.set_current_scene("cryo")
9
6db58d31dd4c Function for loading initial state.
Simon Cross <hodgestar+bzr@gmail.com>
parents: 7
diff changeset
29 return state
6db58d31dd4c Function for loading initial state.
Simon Cross <hodgestar+bzr@gmail.com>
parents: 7
diff changeset
30
6db58d31dd4c Function for loading initial state.
Simon Cross <hodgestar+bzr@gmail.com>
parents: 7
diff changeset
31
7
d7c9d8273d7f Start of base classes for game state.
Simon Cross <hodgestar+bzr@gmail.com>
parents:
diff changeset
32 class State(object):
d7c9d8273d7f Start of base classes for game state.
Simon Cross <hodgestar+bzr@gmail.com>
parents:
diff changeset
33 """Complete game state.
d7c9d8273d7f Start of base classes for game state.
Simon Cross <hodgestar+bzr@gmail.com>
parents:
diff changeset
34
d7c9d8273d7f Start of base classes for game state.
Simon Cross <hodgestar+bzr@gmail.com>
parents:
diff changeset
35 Game state consists of:
d7c9d8273d7f Start of base classes for game state.
Simon Cross <hodgestar+bzr@gmail.com>
parents:
diff changeset
36
d7c9d8273d7f Start of base classes for game state.
Simon Cross <hodgestar+bzr@gmail.com>
parents:
diff changeset
37 * items
d7c9d8273d7f Start of base classes for game state.
Simon Cross <hodgestar+bzr@gmail.com>
parents:
diff changeset
38 * scenes
d7c9d8273d7f Start of base classes for game state.
Simon Cross <hodgestar+bzr@gmail.com>
parents:
diff changeset
39 """
d7c9d8273d7f Start of base classes for game state.
Simon Cross <hodgestar+bzr@gmail.com>
parents:
diff changeset
40
d7c9d8273d7f Start of base classes for game state.
Simon Cross <hodgestar+bzr@gmail.com>
parents:
diff changeset
41 def __init__(self):
d7c9d8273d7f Start of base classes for game state.
Simon Cross <hodgestar+bzr@gmail.com>
parents:
diff changeset
42 # map of scene name -> Scene object
d7c9d8273d7f Start of base classes for game state.
Simon Cross <hodgestar+bzr@gmail.com>
parents:
diff changeset
43 self.scenes = {}
d7c9d8273d7f Start of base classes for game state.
Simon Cross <hodgestar+bzr@gmail.com>
parents:
diff changeset
44 # map of item name -> Item object
d7c9d8273d7f Start of base classes for game state.
Simon Cross <hodgestar+bzr@gmail.com>
parents:
diff changeset
45 self.items = {}
33
f8e02d02c782 Convert inventory to list of item objects.
Simon Cross <hodgestar+bzr@gmail.com>
parents: 32
diff changeset
46 # list of item objects in inventory
f8e02d02c782 Convert inventory to list of item objects.
Simon Cross <hodgestar+bzr@gmail.com>
parents: 32
diff changeset
47 self.inventory = []
73
932b0956e77a Make tools usable.
Simon Cross <hodgestar+bzr@gmail.com>
parents: 72
diff changeset
48 # currently selected tool (item)
932b0956e77a Make tools usable.
Simon Cross <hodgestar+bzr@gmail.com>
parents: 72
diff changeset
49 self.tool = None
28
0f25f7b9b37a Add loading of initial state.
Simon Cross <hodgestar+bzr@gmail.com>
parents: 23
diff changeset
50 # current scene
0f25f7b9b37a Add loading of initial state.
Simon Cross <hodgestar+bzr@gmail.com>
parents: 23
diff changeset
51 self.current_scene = None
0f25f7b9b37a Add loading of initial state.
Simon Cross <hodgestar+bzr@gmail.com>
parents: 23
diff changeset
52
0f25f7b9b37a Add loading of initial state.
Simon Cross <hodgestar+bzr@gmail.com>
parents: 23
diff changeset
53 def add_scene(self, scene):
0f25f7b9b37a Add loading of initial state.
Simon Cross <hodgestar+bzr@gmail.com>
parents: 23
diff changeset
54 self.scenes[scene.name] = scene
0f25f7b9b37a Add loading of initial state.
Simon Cross <hodgestar+bzr@gmail.com>
parents: 23
diff changeset
55
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
56 def add_item(self, item):
35
ebc76bc0c067 First item!
Simon Cross <hodgestar+bzr@gmail.com>
parents: 34
diff changeset
57 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
58
28
0f25f7b9b37a Add loading of initial state.
Simon Cross <hodgestar+bzr@gmail.com>
parents: 23
diff changeset
59 def load_scenes(self, modname):
0f25f7b9b37a Add loading of initial state.
Simon Cross <hodgestar+bzr@gmail.com>
parents: 23
diff changeset
60 mod = __import__("gamelib.scenes.%s" % (modname,), fromlist=[modname])
0f25f7b9b37a Add loading of initial state.
Simon Cross <hodgestar+bzr@gmail.com>
parents: 23
diff changeset
61 for scene_cls in mod.SCENES:
0f25f7b9b37a Add loading of initial state.
Simon Cross <hodgestar+bzr@gmail.com>
parents: 23
diff changeset
62 self.add_scene(scene_cls(self))
0f25f7b9b37a Add loading of initial state.
Simon Cross <hodgestar+bzr@gmail.com>
parents: 23
diff changeset
63
0f25f7b9b37a Add loading of initial state.
Simon Cross <hodgestar+bzr@gmail.com>
parents: 23
diff changeset
64 def set_current_scene(self, name):
29
6322d92dc8f0 Add state widget for rendering state.
Simon Cross <hodgestar+bzr@gmail.com>
parents: 28
diff changeset
65 self.current_scene = self.scenes[name]
28
0f25f7b9b37a Add loading of initial state.
Simon Cross <hodgestar+bzr@gmail.com>
parents: 23
diff changeset
66
30
daf9eecb670f Add inventory manipulation methods.
Simon Cross <hodgestar+bzr@gmail.com>
parents: 29
diff changeset
67 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
68 self.inventory.append(self.items[name])
30
daf9eecb670f Add inventory manipulation methods.
Simon Cross <hodgestar+bzr@gmail.com>
parents: 29
diff changeset
69
daf9eecb670f Add inventory manipulation methods.
Simon Cross <hodgestar+bzr@gmail.com>
parents: 29
diff changeset
70 def remove_inventory_item(self, name):
34
e5c043aeed65 Inventory and items. And stuff.
Jeremy Thurgood <firxen@gmail.com>
parents: 33
diff changeset
71 self.inventory.remove(self.items[name])
30
daf9eecb670f Add inventory manipulation methods.
Simon Cross <hodgestar+bzr@gmail.com>
parents: 29
diff changeset
72
73
932b0956e77a Make tools usable.
Simon Cross <hodgestar+bzr@gmail.com>
parents: 72
diff changeset
73 def set_tool(self, item):
932b0956e77a Make tools usable.
Simon Cross <hodgestar+bzr@gmail.com>
parents: 72
diff changeset
74 self.tool = item
932b0956e77a Make tools usable.
Simon Cross <hodgestar+bzr@gmail.com>
parents: 72
diff changeset
75
28
0f25f7b9b37a Add loading of initial state.
Simon Cross <hodgestar+bzr@gmail.com>
parents: 23
diff changeset
76 def draw(self, surface):
0f25f7b9b37a Add loading of initial state.
Simon Cross <hodgestar+bzr@gmail.com>
parents: 23
diff changeset
77 self.current_scene.draw(surface)
7
d7c9d8273d7f Start of base classes for game state.
Simon Cross <hodgestar+bzr@gmail.com>
parents:
diff changeset
78
73
932b0956e77a Make tools usable.
Simon Cross <hodgestar+bzr@gmail.com>
parents: 72
diff changeset
79 def interact(self, pos):
78
6bfebfbce42e Partial message support
Neil Muller <neil@dip.sun.ac.za>
parents: 77
diff changeset
80 return self.current_scene.interact(self.tool, pos)
71
99c5506de7ea Start of interact handling.
Simon Cross <hodgestar+bzr@gmail.com>
parents: 68
diff changeset
81
73
932b0956e77a Make tools usable.
Simon Cross <hodgestar+bzr@gmail.com>
parents: 72
diff changeset
82 def mouse_move(self, pos):
932b0956e77a Make tools usable.
Simon Cross <hodgestar+bzr@gmail.com>
parents: 72
diff changeset
83 self.current_scene.mouse_move(self.tool, pos)
71
99c5506de7ea Start of interact handling.
Simon Cross <hodgestar+bzr@gmail.com>
parents: 68
diff changeset
84
7
d7c9d8273d7f Start of base classes for game state.
Simon Cross <hodgestar+bzr@gmail.com>
parents:
diff changeset
85
55
2e2f6ff54780 Part of the cryo door puzzle.
Jeremy Thurgood <firxen@gmail.com>
parents: 47
diff changeset
86 class StatefulGizmo(object):
2e2f6ff54780 Part of the cryo door puzzle.
Jeremy Thurgood <firxen@gmail.com>
parents: 47
diff changeset
87
2e2f6ff54780 Part of the cryo door puzzle.
Jeremy Thurgood <firxen@gmail.com>
parents: 47
diff changeset
88 # initial data (optional, defaults to none)
2e2f6ff54780 Part of the cryo door puzzle.
Jeremy Thurgood <firxen@gmail.com>
parents: 47
diff changeset
89 INITIAL_DATA = None
2e2f6ff54780 Part of the cryo door puzzle.
Jeremy Thurgood <firxen@gmail.com>
parents: 47
diff changeset
90
2e2f6ff54780 Part of the cryo door puzzle.
Jeremy Thurgood <firxen@gmail.com>
parents: 47
diff changeset
91 def __init__(self):
2e2f6ff54780 Part of the cryo door puzzle.
Jeremy Thurgood <firxen@gmail.com>
parents: 47
diff changeset
92 self.data = {}
2e2f6ff54780 Part of the cryo door puzzle.
Jeremy Thurgood <firxen@gmail.com>
parents: 47
diff changeset
93 if self.INITIAL_DATA:
2e2f6ff54780 Part of the cryo door puzzle.
Jeremy Thurgood <firxen@gmail.com>
parents: 47
diff changeset
94 self.data.update(self.INITIAL_DATA)
2e2f6ff54780 Part of the cryo door puzzle.
Jeremy Thurgood <firxen@gmail.com>
parents: 47
diff changeset
95
2e2f6ff54780 Part of the cryo door puzzle.
Jeremy Thurgood <firxen@gmail.com>
parents: 47
diff changeset
96 def set_data(self, key, value):
2e2f6ff54780 Part of the cryo door puzzle.
Jeremy Thurgood <firxen@gmail.com>
parents: 47
diff changeset
97 self.data[key] = value
2e2f6ff54780 Part of the cryo door puzzle.
Jeremy Thurgood <firxen@gmail.com>
parents: 47
diff changeset
98
2e2f6ff54780 Part of the cryo door puzzle.
Jeremy Thurgood <firxen@gmail.com>
parents: 47
diff changeset
99 def get_data(self, key):
2e2f6ff54780 Part of the cryo door puzzle.
Jeremy Thurgood <firxen@gmail.com>
parents: 47
diff changeset
100 return self.data.get(key, None)
2e2f6ff54780 Part of the cryo door puzzle.
Jeremy Thurgood <firxen@gmail.com>
parents: 47
diff changeset
101
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 class Scene(StatefulGizmo):
7
d7c9d8273d7f Start of base classes for game state.
Simon Cross <hodgestar+bzr@gmail.com>
parents:
diff changeset
104 """Base class for scenes."""
d7c9d8273d7f Start of base classes for game state.
Simon Cross <hodgestar+bzr@gmail.com>
parents:
diff changeset
105
28
0f25f7b9b37a Add loading of initial state.
Simon Cross <hodgestar+bzr@gmail.com>
parents: 23
diff changeset
106 # sub-folder to look for resources in
22
8e709824306d Add items and things.
Simon Cross <hodgestar+bzr@gmail.com>
parents: 18
diff changeset
107 FOLDER = None
28
0f25f7b9b37a Add loading of initial state.
Simon Cross <hodgestar+bzr@gmail.com>
parents: 23
diff changeset
108
0f25f7b9b37a Add loading of initial state.
Simon Cross <hodgestar+bzr@gmail.com>
parents: 23
diff changeset
109 # name of background image resource
22
8e709824306d Add items and things.
Simon Cross <hodgestar+bzr@gmail.com>
parents: 18
diff changeset
110 BACKGROUND = None
8e709824306d Add items and things.
Simon Cross <hodgestar+bzr@gmail.com>
parents: 18
diff changeset
111
28
0f25f7b9b37a Add loading of initial state.
Simon Cross <hodgestar+bzr@gmail.com>
parents: 23
diff changeset
112 # name of scene (optional, defaults to folder)
0f25f7b9b37a Add loading of initial state.
Simon Cross <hodgestar+bzr@gmail.com>
parents: 23
diff changeset
113 NAME = None
0f25f7b9b37a Add loading of initial state.
Simon Cross <hodgestar+bzr@gmail.com>
parents: 23
diff changeset
114
0f25f7b9b37a Add loading of initial state.
Simon Cross <hodgestar+bzr@gmail.com>
parents: 23
diff changeset
115 def __init__(self, state):
55
2e2f6ff54780 Part of the cryo door puzzle.
Jeremy Thurgood <firxen@gmail.com>
parents: 47
diff changeset
116 StatefulGizmo.__init__(self)
28
0f25f7b9b37a Add loading of initial state.
Simon Cross <hodgestar+bzr@gmail.com>
parents: 23
diff changeset
117 # scene name
0f25f7b9b37a Add loading of initial state.
Simon Cross <hodgestar+bzr@gmail.com>
parents: 23
diff changeset
118 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
119 # link back to state object
0f25f7b9b37a Add loading of initial state.
Simon Cross <hodgestar+bzr@gmail.com>
parents: 23
diff changeset
120 self.state = state
22
8e709824306d Add items and things.
Simon Cross <hodgestar+bzr@gmail.com>
parents: 18
diff changeset
121 # map of thing names -> Thing objects
8e709824306d Add items and things.
Simon Cross <hodgestar+bzr@gmail.com>
parents: 18
diff changeset
122 self.things = {}
28
0f25f7b9b37a Add loading of initial state.
Simon Cross <hodgestar+bzr@gmail.com>
parents: 23
diff changeset
123 self._background = get_image(self.FOLDER, self.BACKGROUND)
71
99c5506de7ea Start of interact handling.
Simon Cross <hodgestar+bzr@gmail.com>
parents: 68
diff changeset
124 self._current_thing = None
76
1a5fdc225939 Add description rendering to scene rendering.
Simon Cross <hodgestar+bzr@gmail.com>
parents: 75
diff changeset
125 self._current_description = None
7
d7c9d8273d7f Start of base classes for game state.
Simon Cross <hodgestar+bzr@gmail.com>
parents:
diff changeset
126
35
ebc76bc0c067 First item!
Simon Cross <hodgestar+bzr@gmail.com>
parents: 34
diff changeset
127 def add_item(self, item):
ebc76bc0c067 First item!
Simon Cross <hodgestar+bzr@gmail.com>
parents: 34
diff changeset
128 self.state.add_item(item)
ebc76bc0c067 First item!
Simon Cross <hodgestar+bzr@gmail.com>
parents: 34
diff changeset
129
39
088a101f5b94 Add an example Thing to cryo scene.
Simon Cross <hodgestar+bzr@gmail.com>
parents: 35
diff changeset
130 def add_thing(self, thing):
088a101f5b94 Add an example Thing to cryo scene.
Simon Cross <hodgestar+bzr@gmail.com>
parents: 35
diff changeset
131 self.things[thing.name] = thing
41
ad6f56bfa8b7 Cryo door, titanium leg and some interaction prototypes.
Jeremy Thurgood <firxen@gmail.com>
parents: 39
diff changeset
132 thing.set_scene(self)
ad6f56bfa8b7 Cryo door, titanium leg and some interaction prototypes.
Jeremy Thurgood <firxen@gmail.com>
parents: 39
diff changeset
133
ad6f56bfa8b7 Cryo door, titanium leg and some interaction prototypes.
Jeremy Thurgood <firxen@gmail.com>
parents: 39
diff changeset
134 def remove_thing(self, thing):
ad6f56bfa8b7 Cryo door, titanium leg and some interaction prototypes.
Jeremy Thurgood <firxen@gmail.com>
parents: 39
diff changeset
135 del self.things[thing.name]
39
088a101f5b94 Add an example Thing to cryo scene.
Simon Cross <hodgestar+bzr@gmail.com>
parents: 35
diff changeset
136
76
1a5fdc225939 Add description rendering to scene rendering.
Simon Cross <hodgestar+bzr@gmail.com>
parents: 75
diff changeset
137 def _make_description(self, text):
1a5fdc225939 Add description rendering to scene rendering.
Simon Cross <hodgestar+bzr@gmail.com>
parents: 75
diff changeset
138 if text is None:
1a5fdc225939 Add description rendering to scene rendering.
Simon Cross <hodgestar+bzr@gmail.com>
parents: 75
diff changeset
139 return None
1a5fdc225939 Add description rendering to scene rendering.
Simon Cross <hodgestar+bzr@gmail.com>
parents: 75
diff changeset
140 label = Label(text)
77
bb7c8072f8c0 Some hacks to make Label work.
Simon Cross <hodgestar+bzr@gmail.com>
parents: 76
diff changeset
141 # TODO: create derived label class that does this
bb7c8072f8c0 Some hacks to make Label work.
Simon Cross <hodgestar+bzr@gmail.com>
parents: 76
diff changeset
142 # manually recalculate size
bb7c8072f8c0 Some hacks to make Label work.
Simon Cross <hodgestar+bzr@gmail.com>
parents: 76
diff changeset
143 d = 5
bb7c8072f8c0 Some hacks to make Label work.
Simon Cross <hodgestar+bzr@gmail.com>
parents: 76
diff changeset
144 w, h = label.size
bb7c8072f8c0 Some hacks to make Label work.
Simon Cross <hodgestar+bzr@gmail.com>
parents: 76
diff changeset
145 label.margin = d
bb7c8072f8c0 Some hacks to make Label work.
Simon Cross <hodgestar+bzr@gmail.com>
parents: 76
diff changeset
146 label.size = (w+2*d, h+2*d)
76
1a5fdc225939 Add description rendering to scene rendering.
Simon Cross <hodgestar+bzr@gmail.com>
parents: 75
diff changeset
147 label.border_width = 1
1a5fdc225939 Add description rendering to scene rendering.
Simon Cross <hodgestar+bzr@gmail.com>
parents: 75
diff changeset
148 label.border_color = (0, 0, 0)
1a5fdc225939 Add description rendering to scene rendering.
Simon Cross <hodgestar+bzr@gmail.com>
parents: 75
diff changeset
149 label.bg_color = (127, 127, 127)
1a5fdc225939 Add description rendering to scene rendering.
Simon Cross <hodgestar+bzr@gmail.com>
parents: 75
diff changeset
150 label.fg_color = (0, 0, 0)
1a5fdc225939 Add description rendering to scene rendering.
Simon Cross <hodgestar+bzr@gmail.com>
parents: 75
diff changeset
151 return label
1a5fdc225939 Add description rendering to scene rendering.
Simon Cross <hodgestar+bzr@gmail.com>
parents: 75
diff changeset
152
1a5fdc225939 Add description rendering to scene rendering.
Simon Cross <hodgestar+bzr@gmail.com>
parents: 75
diff changeset
153 def draw_description(self, surface):
1a5fdc225939 Add description rendering to scene rendering.
Simon Cross <hodgestar+bzr@gmail.com>
parents: 75
diff changeset
154 if self._current_description is not None:
77
bb7c8072f8c0 Some hacks to make Label work.
Simon Cross <hodgestar+bzr@gmail.com>
parents: 76
diff changeset
155 sub = surface.subsurface(
bb7c8072f8c0 Some hacks to make Label work.
Simon Cross <hodgestar+bzr@gmail.com>
parents: 76
diff changeset
156 Rect(5, 5, *self._current_description.size))
bb7c8072f8c0 Some hacks to make Label work.
Simon Cross <hodgestar+bzr@gmail.com>
parents: 76
diff changeset
157 self._current_description.draw_all(sub)
76
1a5fdc225939 Add description rendering to scene rendering.
Simon Cross <hodgestar+bzr@gmail.com>
parents: 75
diff changeset
158
22
8e709824306d Add items and things.
Simon Cross <hodgestar+bzr@gmail.com>
parents: 18
diff changeset
159 def draw_background(self, surface):
23
1e90a3e4618e Add code for blitting Scene.
Simon Cross <hodgestar+bzr@gmail.com>
parents: 22
diff changeset
160 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
161
23
1e90a3e4618e Add code for blitting Scene.
Simon Cross <hodgestar+bzr@gmail.com>
parents: 22
diff changeset
162 def draw_things(self, surface):
1e90a3e4618e Add code for blitting Scene.
Simon Cross <hodgestar+bzr@gmail.com>
parents: 22
diff changeset
163 for thing in self.things.itervalues():
1e90a3e4618e Add code for blitting Scene.
Simon Cross <hodgestar+bzr@gmail.com>
parents: 22
diff changeset
164 thing.draw(surface)
18
c9b124c2f5c6 Start of scene drawing framework.
Simon Cross <hodgestar+bzr@gmail.com>
parents: 11
diff changeset
165
22
8e709824306d Add items and things.
Simon Cross <hodgestar+bzr@gmail.com>
parents: 18
diff changeset
166 def draw(self, surface):
8e709824306d Add items and things.
Simon Cross <hodgestar+bzr@gmail.com>
parents: 18
diff changeset
167 self.draw_background(surface)
23
1e90a3e4618e Add code for blitting Scene.
Simon Cross <hodgestar+bzr@gmail.com>
parents: 22
diff changeset
168 self.draw_things(surface)
76
1a5fdc225939 Add description rendering to scene rendering.
Simon Cross <hodgestar+bzr@gmail.com>
parents: 75
diff changeset
169 self.draw_description(surface)
18
c9b124c2f5c6 Start of scene drawing framework.
Simon Cross <hodgestar+bzr@gmail.com>
parents: 11
diff changeset
170
71
99c5506de7ea Start of interact handling.
Simon Cross <hodgestar+bzr@gmail.com>
parents: 68
diff changeset
171 def interact(self, item, pos):
99c5506de7ea Start of interact handling.
Simon Cross <hodgestar+bzr@gmail.com>
parents: 68
diff changeset
172 """Interact with a particular position.
99c5506de7ea Start of interact handling.
Simon Cross <hodgestar+bzr@gmail.com>
parents: 68
diff changeset
173
99c5506de7ea Start of interact handling.
Simon Cross <hodgestar+bzr@gmail.com>
parents: 68
diff changeset
174 Item may be an item in the list of items or None for the hand.
78
6bfebfbce42e Partial message support
Neil Muller <neil@dip.sun.ac.za>
parents: 77
diff changeset
175
6bfebfbce42e Partial message support
Neil Muller <neil@dip.sun.ac.za>
parents: 77
diff changeset
176 Returns a Result object to provide feedback to the player.
71
99c5506de7ea Start of interact handling.
Simon Cross <hodgestar+bzr@gmail.com>
parents: 68
diff changeset
177 """
99c5506de7ea Start of interact handling.
Simon Cross <hodgestar+bzr@gmail.com>
parents: 68
diff changeset
178 for thing in self.things.itervalues():
99c5506de7ea Start of interact handling.
Simon Cross <hodgestar+bzr@gmail.com>
parents: 68
diff changeset
179 if thing.rect.collidepoint(pos):
78
6bfebfbce42e Partial message support
Neil Muller <neil@dip.sun.ac.za>
parents: 77
diff changeset
180 result = thing.interact(item)
6bfebfbce42e Partial message support
Neil Muller <neil@dip.sun.ac.za>
parents: 77
diff changeset
181 if result:
6bfebfbce42e Partial message support
Neil Muller <neil@dip.sun.ac.za>
parents: 77
diff changeset
182 if self._current_thing:
6bfebfbce42e Partial message support
Neil Muller <neil@dip.sun.ac.za>
parents: 77
diff changeset
183 # Also update descriptions if needed
6bfebfbce42e Partial message support
Neil Muller <neil@dip.sun.ac.za>
parents: 77
diff changeset
184 self._current_description = self._make_description(
6bfebfbce42e Partial message support
Neil Muller <neil@dip.sun.ac.za>
parents: 77
diff changeset
185 self._current_thing.get_description())
6bfebfbce42e Partial message support
Neil Muller <neil@dip.sun.ac.za>
parents: 77
diff changeset
186 return result
71
99c5506de7ea Start of interact handling.
Simon Cross <hodgestar+bzr@gmail.com>
parents: 68
diff changeset
187
99c5506de7ea Start of interact handling.
Simon Cross <hodgestar+bzr@gmail.com>
parents: 68
diff changeset
188 def mouse_move(self, item, pos):
99c5506de7ea Start of interact handling.
Simon Cross <hodgestar+bzr@gmail.com>
parents: 68
diff changeset
189 """Call to check whether the cursor has entered / exited a thing.
99c5506de7ea Start of interact handling.
Simon Cross <hodgestar+bzr@gmail.com>
parents: 68
diff changeset
190
99c5506de7ea Start of interact handling.
Simon Cross <hodgestar+bzr@gmail.com>
parents: 68
diff changeset
191 Item may be an item in the list of items or None for the hand.
99c5506de7ea Start of interact handling.
Simon Cross <hodgestar+bzr@gmail.com>
parents: 68
diff changeset
192 """
99c5506de7ea Start of interact handling.
Simon Cross <hodgestar+bzr@gmail.com>
parents: 68
diff changeset
193 if self._current_thing is not None:
99c5506de7ea Start of interact handling.
Simon Cross <hodgestar+bzr@gmail.com>
parents: 68
diff changeset
194 if self._current_thing.rect.collidepoint(pos):
99c5506de7ea Start of interact handling.
Simon Cross <hodgestar+bzr@gmail.com>
parents: 68
diff changeset
195 return
99c5506de7ea Start of interact handling.
Simon Cross <hodgestar+bzr@gmail.com>
parents: 68
diff changeset
196 else:
99c5506de7ea Start of interact handling.
Simon Cross <hodgestar+bzr@gmail.com>
parents: 68
diff changeset
197 self._current_thing.leave()
99c5506de7ea Start of interact handling.
Simon Cross <hodgestar+bzr@gmail.com>
parents: 68
diff changeset
198 self._current_thing = None
76
1a5fdc225939 Add description rendering to scene rendering.
Simon Cross <hodgestar+bzr@gmail.com>
parents: 75
diff changeset
199 self._current_description = None
71
99c5506de7ea Start of interact handling.
Simon Cross <hodgestar+bzr@gmail.com>
parents: 68
diff changeset
200 for thing in self.things.itervalues():
99c5506de7ea Start of interact handling.
Simon Cross <hodgestar+bzr@gmail.com>
parents: 68
diff changeset
201 if thing.rect.collidepoint(pos):
99c5506de7ea Start of interact handling.
Simon Cross <hodgestar+bzr@gmail.com>
parents: 68
diff changeset
202 thing.enter(item)
99c5506de7ea Start of interact handling.
Simon Cross <hodgestar+bzr@gmail.com>
parents: 68
diff changeset
203 self._current_thing = thing
76
1a5fdc225939 Add description rendering to scene rendering.
Simon Cross <hodgestar+bzr@gmail.com>
parents: 75
diff changeset
204 self._current_description = self._make_description(
1a5fdc225939 Add description rendering to scene rendering.
Simon Cross <hodgestar+bzr@gmail.com>
parents: 75
diff changeset
205 thing.get_description())
71
99c5506de7ea Start of interact handling.
Simon Cross <hodgestar+bzr@gmail.com>
parents: 68
diff changeset
206 break
99c5506de7ea Start of interact handling.
Simon Cross <hodgestar+bzr@gmail.com>
parents: 68
diff changeset
207
7
d7c9d8273d7f Start of base classes for game state.
Simon Cross <hodgestar+bzr@gmail.com>
parents:
diff changeset
208
55
2e2f6ff54780 Part of the cryo door puzzle.
Jeremy Thurgood <firxen@gmail.com>
parents: 47
diff changeset
209 class Thing(StatefulGizmo):
22
8e709824306d Add items and things.
Simon Cross <hodgestar+bzr@gmail.com>
parents: 18
diff changeset
210 """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
211
41
ad6f56bfa8b7 Cryo door, titanium leg and some interaction prototypes.
Jeremy Thurgood <firxen@gmail.com>
parents: 39
diff changeset
212 # 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
213 FOLDER = None
ad6f56bfa8b7 Cryo door, titanium leg and some interaction prototypes.
Jeremy Thurgood <firxen@gmail.com>
parents: 39
diff changeset
214
ad6f56bfa8b7 Cryo door, titanium leg and some interaction prototypes.
Jeremy Thurgood <firxen@gmail.com>
parents: 39
diff changeset
215 # name of image resource
ad6f56bfa8b7 Cryo door, titanium leg and some interaction prototypes.
Jeremy Thurgood <firxen@gmail.com>
parents: 39
diff changeset
216 IMAGE = None
ad6f56bfa8b7 Cryo door, titanium leg and some interaction prototypes.
Jeremy Thurgood <firxen@gmail.com>
parents: 39
diff changeset
217
68
158a13a48d48 Show interact rectangles when constants.DEBUG is True.
Simon Cross <hodgestar+bzr@gmail.com>
parents: 65
diff changeset
218 # Interact rectangle hi-light color (for debugging)
158a13a48d48 Show interact rectangles when constants.DEBUG is True.
Simon Cross <hodgestar+bzr@gmail.com>
parents: 65
diff changeset
219 # (set to None to turn off)
158a13a48d48 Show interact rectangles when constants.DEBUG is True.
Simon Cross <hodgestar+bzr@gmail.com>
parents: 65
diff changeset
220 if constants.DEBUG:
75
a62db6d10009 Red -> red
Stefano Rivera <stefano@rivera.za.net>
parents: 73
diff changeset
221 _interact_hilight_color = Color('red')
68
158a13a48d48 Show interact rectangles when constants.DEBUG is True.
Simon Cross <hodgestar+bzr@gmail.com>
parents: 65
diff changeset
222 else:
158a13a48d48 Show interact rectangles when constants.DEBUG is True.
Simon Cross <hodgestar+bzr@gmail.com>
parents: 65
diff changeset
223 _interact_hilight_color = None
158a13a48d48 Show interact rectangles when constants.DEBUG is True.
Simon Cross <hodgestar+bzr@gmail.com>
parents: 65
diff changeset
224
39
088a101f5b94 Add an example Thing to cryo scene.
Simon Cross <hodgestar+bzr@gmail.com>
parents: 35
diff changeset
225 def __init__(self, name, rect):
55
2e2f6ff54780 Part of the cryo door puzzle.
Jeremy Thurgood <firxen@gmail.com>
parents: 47
diff changeset
226 StatefulGizmo.__init__(self)
39
088a101f5b94 Add an example Thing to cryo scene.
Simon Cross <hodgestar+bzr@gmail.com>
parents: 35
diff changeset
227 self.name = name
68
158a13a48d48 Show interact rectangles when constants.DEBUG is True.
Simon Cross <hodgestar+bzr@gmail.com>
parents: 65
diff changeset
228 # area within scene to render to
158a13a48d48 Show interact rectangles when constants.DEBUG is True.
Simon Cross <hodgestar+bzr@gmail.com>
parents: 65
diff changeset
229 self.rect = Rect(rect)
39
088a101f5b94 Add an example Thing to cryo scene.
Simon Cross <hodgestar+bzr@gmail.com>
parents: 35
diff changeset
230 # area within scene that triggers calls to interact
68
158a13a48d48 Show interact rectangles when constants.DEBUG is True.
Simon Cross <hodgestar+bzr@gmail.com>
parents: 65
diff changeset
231 self.interact_rect = Rect(rect)
39
088a101f5b94 Add an example Thing to cryo scene.
Simon Cross <hodgestar+bzr@gmail.com>
parents: 35
diff changeset
232 # these are set by set_scene
088a101f5b94 Add an example Thing to cryo scene.
Simon Cross <hodgestar+bzr@gmail.com>
parents: 35
diff changeset
233 self.scene = None
088a101f5b94 Add an example Thing to cryo scene.
Simon Cross <hodgestar+bzr@gmail.com>
parents: 35
diff changeset
234 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
235 # 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
236 # TODO: add images
7
d7c9d8273d7f Start of base classes for game state.
Simon Cross <hodgestar+bzr@gmail.com>
parents:
diff changeset
237
39
088a101f5b94 Add an example Thing to cryo scene.
Simon Cross <hodgestar+bzr@gmail.com>
parents: 35
diff changeset
238 def set_scene(self, scene):
088a101f5b94 Add an example Thing to cryo scene.
Simon Cross <hodgestar+bzr@gmail.com>
parents: 35
diff changeset
239 assert self.scene is None
088a101f5b94 Add an example Thing to cryo scene.
Simon Cross <hodgestar+bzr@gmail.com>
parents: 35
diff changeset
240 self.scene = scene
088a101f5b94 Add an example Thing to cryo scene.
Simon Cross <hodgestar+bzr@gmail.com>
parents: 35
diff changeset
241 self.state = scene.state
088a101f5b94 Add an example Thing to cryo scene.
Simon Cross <hodgestar+bzr@gmail.com>
parents: 35
diff changeset
242
63
3087be3463e0 Some framework support for better message handling
Neil Muller <neil@dip.sun.ac.za>
parents: 56
diff changeset
243 def get_description(self):
3087be3463e0 Some framework support for better message handling
Neil Muller <neil@dip.sun.ac.za>
parents: 56
diff changeset
244 return None
3087be3463e0 Some framework support for better message handling
Neil Muller <neil@dip.sun.ac.za>
parents: 56
diff changeset
245
56
75bf3d3689e9 Refactor thing interactivity and add "fake" bridge scene.
Jeremy Thurgood <firxen@gmail.com>
parents: 55
diff changeset
246 def is_interactive(self):
75bf3d3689e9 Refactor thing interactivity and add "fake" bridge scene.
Jeremy Thurgood <firxen@gmail.com>
parents: 55
diff changeset
247 return True
75bf3d3689e9 Refactor thing interactivity and add "fake" bridge scene.
Jeremy Thurgood <firxen@gmail.com>
parents: 55
diff changeset
248
71
99c5506de7ea Start of interact handling.
Simon Cross <hodgestar+bzr@gmail.com>
parents: 68
diff changeset
249 def enter(self, item):
99c5506de7ea Start of interact handling.
Simon Cross <hodgestar+bzr@gmail.com>
parents: 68
diff changeset
250 """Called when the cursor enters the Thing."""
99c5506de7ea Start of interact handling.
Simon Cross <hodgestar+bzr@gmail.com>
parents: 68
diff changeset
251 print "Enter %r -> %r" % (item, self)
99c5506de7ea Start of interact handling.
Simon Cross <hodgestar+bzr@gmail.com>
parents: 68
diff changeset
252
99c5506de7ea Start of interact handling.
Simon Cross <hodgestar+bzr@gmail.com>
parents: 68
diff changeset
253 def leave(self):
99c5506de7ea Start of interact handling.
Simon Cross <hodgestar+bzr@gmail.com>
parents: 68
diff changeset
254 """Called when the cursr leaves the Thing."""
99c5506de7ea Start of interact handling.
Simon Cross <hodgestar+bzr@gmail.com>
parents: 68
diff changeset
255 print "Leaves %r" % self
99c5506de7ea Start of interact handling.
Simon Cross <hodgestar+bzr@gmail.com>
parents: 68
diff changeset
256
22
8e709824306d Add items and things.
Simon Cross <hodgestar+bzr@gmail.com>
parents: 18
diff changeset
257 def interact(self, item):
56
75bf3d3689e9 Refactor thing interactivity and add "fake" bridge scene.
Jeremy Thurgood <firxen@gmail.com>
parents: 55
diff changeset
258 if not self.is_interactive():
75bf3d3689e9 Refactor thing interactivity and add "fake" bridge scene.
Jeremy Thurgood <firxen@gmail.com>
parents: 55
diff changeset
259 return
41
ad6f56bfa8b7 Cryo door, titanium leg and some interaction prototypes.
Jeremy Thurgood <firxen@gmail.com>
parents: 39
diff changeset
260 if item is None:
78
6bfebfbce42e Partial message support
Neil Muller <neil@dip.sun.ac.za>
parents: 77
diff changeset
261 return self.interact_without()
41
ad6f56bfa8b7 Cryo door, titanium leg and some interaction prototypes.
Jeremy Thurgood <firxen@gmail.com>
parents: 39
diff changeset
262 else:
ad6f56bfa8b7 Cryo door, titanium leg and some interaction prototypes.
Jeremy Thurgood <firxen@gmail.com>
parents: 39
diff changeset
263 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
264 if handler is not None:
78
6bfebfbce42e Partial message support
Neil Muller <neil@dip.sun.ac.za>
parents: 77
diff changeset
265 return handler(item)
41
ad6f56bfa8b7 Cryo door, titanium leg and some interaction prototypes.
Jeremy Thurgood <firxen@gmail.com>
parents: 39
diff changeset
266 else:
78
6bfebfbce42e Partial message support
Neil Muller <neil@dip.sun.ac.za>
parents: 77
diff changeset
267 return self.interact_default(item)
41
ad6f56bfa8b7 Cryo door, titanium leg and some interaction prototypes.
Jeremy Thurgood <firxen@gmail.com>
parents: 39
diff changeset
268
ad6f56bfa8b7 Cryo door, titanium leg and some interaction prototypes.
Jeremy Thurgood <firxen@gmail.com>
parents: 39
diff changeset
269 def interact_without(self):
78
6bfebfbce42e Partial message support
Neil Muller <neil@dip.sun.ac.za>
parents: 77
diff changeset
270 return self.interact_default(None)
41
ad6f56bfa8b7 Cryo door, titanium leg and some interaction prototypes.
Jeremy Thurgood <firxen@gmail.com>
parents: 39
diff changeset
271
ad6f56bfa8b7 Cryo door, titanium leg and some interaction prototypes.
Jeremy Thurgood <firxen@gmail.com>
parents: 39
diff changeset
272 def interact_default(self, item):
78
6bfebfbce42e Partial message support
Neil Muller <neil@dip.sun.ac.za>
parents: 77
diff changeset
273 return Result("It doesn't work.")
22
8e709824306d Add items and things.
Simon Cross <hodgestar+bzr@gmail.com>
parents: 18
diff changeset
274
23
1e90a3e4618e Add code for blitting Scene.
Simon Cross <hodgestar+bzr@gmail.com>
parents: 22
diff changeset
275 def draw(self, surface):
68
158a13a48d48 Show interact rectangles when constants.DEBUG is True.
Simon Cross <hodgestar+bzr@gmail.com>
parents: 65
diff changeset
276 if self._interact_hilight_color is not None:
158a13a48d48 Show interact rectangles when constants.DEBUG is True.
Simon Cross <hodgestar+bzr@gmail.com>
parents: 65
diff changeset
277 frame_rect(surface, self._interact_hilight_color,
158a13a48d48 Show interact rectangles when constants.DEBUG is True.
Simon Cross <hodgestar+bzr@gmail.com>
parents: 65
diff changeset
278 self.interact_rect.inflate(1, 1), 1)
158a13a48d48 Show interact rectangles when constants.DEBUG is True.
Simon Cross <hodgestar+bzr@gmail.com>
parents: 65
diff changeset
279 # TODO: draw image if there is one
23
1e90a3e4618e Add code for blitting Scene.
Simon Cross <hodgestar+bzr@gmail.com>
parents: 22
diff changeset
280
22
8e709824306d Add items and things.
Simon Cross <hodgestar+bzr@gmail.com>
parents: 18
diff changeset
281
8e709824306d Add items and things.
Simon Cross <hodgestar+bzr@gmail.com>
parents: 18
diff changeset
282 class Item(object):
8e709824306d Add items and things.
Simon Cross <hodgestar+bzr@gmail.com>
parents: 18
diff changeset
283 """Base class for inventory items."""
8e709824306d Add items and things.
Simon Cross <hodgestar+bzr@gmail.com>
parents: 18
diff changeset
284
35
ebc76bc0c067 First item!
Simon Cross <hodgestar+bzr@gmail.com>
parents: 34
diff changeset
285 # image for inventory
ebc76bc0c067 First item!
Simon Cross <hodgestar+bzr@gmail.com>
parents: 34
diff changeset
286 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
287
35
ebc76bc0c067 First item!
Simon Cross <hodgestar+bzr@gmail.com>
parents: 34
diff changeset
288 def __init__(self, name):
ebc76bc0c067 First item!
Simon Cross <hodgestar+bzr@gmail.com>
parents: 34
diff changeset
289 self.name = name
ebc76bc0c067 First item!
Simon Cross <hodgestar+bzr@gmail.com>
parents: 34
diff changeset
290 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
291 # TODO: needs cursor
22
8e709824306d Add items and things.
Simon Cross <hodgestar+bzr@gmail.com>
parents: 18
diff changeset
292
34
e5c043aeed65 Inventory and items. And stuff.
Jeremy Thurgood <firxen@gmail.com>
parents: 33
diff changeset
293 def get_inventory_image(self):
e5c043aeed65 Inventory and items. And stuff.
Jeremy Thurgood <firxen@gmail.com>
parents: 33
diff changeset
294 return self.inventory_image
e5c043aeed65 Inventory and items. And stuff.
Jeremy Thurgood <firxen@gmail.com>
parents: 33
diff changeset
295