Mercurial > pyntnclick
comparison gamelib/state.py @ 76:1a5fdc225939
Add description rendering to scene rendering.
author | Simon Cross <hodgestar+bzr@gmail.com> |
---|---|
date | Mon, 23 Aug 2010 22:11:41 +0200 |
parents | a62db6d10009 |
children | bb7c8072f8c0 |
comparison
equal
deleted
inserted
replaced
75:a62db6d10009 | 76:1a5fdc225939 |
---|---|
1 """Utilities and base classes for dealing with scenes.""" | 1 """Utilities and base classes for dealing with scenes.""" |
2 | 2 |
3 from albow.resource import get_image, get_sound | 3 from albow.resource import get_image, get_sound |
4 from albow.utils import frame_rect | 4 from albow.utils import frame_rect |
5 from albow.controls import Label | |
5 from pygame.locals import BLEND_ADD | 6 from pygame.locals import BLEND_ADD |
6 from pygame.rect import Rect | 7 from pygame.rect import Rect |
7 from pygame.color import Color | 8 from pygame.color import Color |
8 | 9 |
9 import constants | 10 import constants |
38 self.items = {} | 39 self.items = {} |
39 # list of item objects in inventory | 40 # list of item objects in inventory |
40 self.inventory = [] | 41 self.inventory = [] |
41 # currently selected tool (item) | 42 # currently selected tool (item) |
42 self.tool = None | 43 self.tool = None |
43 # Result of the most recent action | |
44 self.msg = None | |
45 self.description = None | |
46 # current scene | 44 # current scene |
47 self.current_scene = None | 45 self.current_scene = None |
48 | 46 |
49 def add_scene(self, scene): | 47 def add_scene(self, scene): |
50 self.scenes[scene.name] = scene | 48 self.scenes[scene.name] = scene |
75 def interact(self, pos): | 73 def interact(self, pos): |
76 self.current_scene.interact(self.tool, pos) | 74 self.current_scene.interact(self.tool, pos) |
77 | 75 |
78 def mouse_move(self, pos): | 76 def mouse_move(self, pos): |
79 self.current_scene.mouse_move(self.tool, pos) | 77 self.current_scene.mouse_move(self.tool, pos) |
80 | |
81 def get_message(self): | |
82 return self.msg | |
83 | |
84 def clear_message(self): | |
85 self.msg = None | |
86 | |
87 # FIXME: sort out how state.interact and description updating should work | |
88 | |
89 def check_for_new_description(self, pos): | |
90 """Check if the current mouse position causes a new description""" | |
91 old_desc = self.description | |
92 self.description = self.current_scene.check_description(pos) | |
93 return old_desc != self.description | |
94 | |
95 def get_description(self): | |
96 # | |
97 # DEPRECATED | |
98 # | |
99 return self.description | |
100 | |
101 def message(self, msg): | |
102 self.msg = msg | |
103 | 78 |
104 | 79 |
105 class StatefulGizmo(object): | 80 class StatefulGizmo(object): |
106 | 81 |
107 # initial data (optional, defaults to none) | 82 # initial data (optional, defaults to none) |
139 self.state = state | 114 self.state = state |
140 # map of thing names -> Thing objects | 115 # map of thing names -> Thing objects |
141 self.things = {} | 116 self.things = {} |
142 self._background = get_image(self.FOLDER, self.BACKGROUND) | 117 self._background = get_image(self.FOLDER, self.BACKGROUND) |
143 self._current_thing = None | 118 self._current_thing = None |
119 self._current_description = None | |
144 | 120 |
145 def add_item(self, item): | 121 def add_item(self, item): |
146 self.state.add_item(item) | 122 self.state.add_item(item) |
147 | 123 |
148 def add_thing(self, thing): | 124 def add_thing(self, thing): |
150 thing.set_scene(self) | 126 thing.set_scene(self) |
151 | 127 |
152 def remove_thing(self, thing): | 128 def remove_thing(self, thing): |
153 del self.things[thing.name] | 129 del self.things[thing.name] |
154 | 130 |
131 def _make_description(self, text): | |
132 if text is None: | |
133 return None | |
134 label = Label(text) | |
135 label.margin = 5 | |
136 label.border_width = 1 | |
137 label.border_color = (0, 0, 0) | |
138 label.bg_color = (127, 127, 127) | |
139 label.fg_color = (0, 0, 0) | |
140 return label | |
141 | |
142 def draw_description(self, surface): | |
143 if self._current_description is not None: | |
144 self._current_description.draw(surface) | |
145 | |
155 def draw_background(self, surface): | 146 def draw_background(self, surface): |
156 surface.blit(self._background, (0, 0), None, BLEND_ADD) | 147 surface.blit(self._background, (0, 0), None, BLEND_ADD) |
157 | 148 |
158 def draw_things(self, surface): | 149 def draw_things(self, surface): |
159 for thing in self.things.itervalues(): | 150 for thing in self.things.itervalues(): |
160 thing.draw(surface) | 151 thing.draw(surface) |
161 | 152 |
162 def draw(self, surface): | 153 def draw(self, surface): |
163 self.draw_background(surface) | 154 self.draw_background(surface) |
164 self.draw_things(surface) | 155 self.draw_things(surface) |
156 self.draw_description(surface) | |
165 | 157 |
166 def interact(self, item, pos): | 158 def interact(self, item, pos): |
167 """Interact with a particular position. | 159 """Interact with a particular position. |
168 | 160 |
169 Item may be an item in the list of items or None for the hand. | 161 Item may be an item in the list of items or None for the hand. |
182 if self._current_thing.rect.collidepoint(pos): | 174 if self._current_thing.rect.collidepoint(pos): |
183 return | 175 return |
184 else: | 176 else: |
185 self._current_thing.leave() | 177 self._current_thing.leave() |
186 self._current_thing = None | 178 self._current_thing = None |
179 self._current_description = None | |
187 for thing in self.things.itervalues(): | 180 for thing in self.things.itervalues(): |
188 if thing.rect.collidepoint(pos): | 181 if thing.rect.collidepoint(pos): |
189 thing.enter(item) | 182 thing.enter(item) |
190 self._current_thing = thing | 183 self._current_thing = thing |
184 self._current_description = self._make_description( | |
185 thing.get_description()) | |
191 break | 186 break |
192 | |
193 def check_description(self, pos): | |
194 # | |
195 # DEPRECATED | |
196 # | |
197 desc = None | |
198 for thing in self.things.itervalues(): | |
199 # Last thing in the list that matches wins | |
200 if Rect(thing.rect).collidepoint(pos): | |
201 desc = thing.get_description() | |
202 return desc | |
203 | 187 |
204 | 188 |
205 class Thing(StatefulGizmo): | 189 class Thing(StatefulGizmo): |
206 """Base class for things in a scene that you can interact with.""" | 190 """Base class for things in a scene that you can interact with.""" |
207 | 191 |