def action(self, actions, objs, pos): obj = self.level.top_obj(objs) # go through actions and do the first we can actions = list(actions) on_fail = [] while True: if actions: action = actions.pop(0) else: # nothing to do if on_fail: self.level.say(on_fail[0]) return if action == 'inspect': if obj is None: on_fail.append('There\'s nothing here.') else: break if action == 'move': break if action == 'grab': if obj is None: on_fail.append('There\'s nothing to pick up.') elif self.item is not None: on_fail.append('I\'m already holding something.') elif not obj.holdable: msg = 'I can\'t pick that {} up.' on_fail.append(msg.format(obj_module.name(obj))) else: break if action == 'drop': if self.item is None: on_fail.append('I have nothing to drop.') elif obj is not None and obj.solid: on_fail.append('I can\'t put this there: that {} is in ' 'the way.'.format(obj_module.name(obj))) else: break if action == 'use': if self.item is None: on_fail.append('I have nothing to use.') elif obj is None and not hasattr(self.item, 'use_on_ground'): msg = 'I can\'t use this {} on the ground.' on_fail.append(msg.format(obj_module.name(self.item))) else: break if action == 'move': self.move(pos) elif action in ('inspect', 'grab', 'drop', 'use'): self._action_with_pos(action, obj, pos)
def action(self, actions, objs, pos): obj = self.level.top_obj(objs) # go through actions and do the first we can actions = list(actions) on_fail = [] while True: if actions: action = actions.pop(0) else: # nothing to do if on_fail: self.level.say(on_fail[0]) return if action == "inspect": if obj is None: on_fail.append("There's nothing here.") else: break if action == "move": break if action == "grab": if obj is None: on_fail.append("There's nothing to pick up.") elif self.item is not None: on_fail.append("I'm already holding something.") elif not obj.holdable: msg = "I can't pick that {} up." on_fail.append(msg.format(obj_module.name(obj))) else: break if action == "drop": if self.item is None: on_fail.append("I have nothing to drop.") elif obj is not None and obj.solid: on_fail.append("I can't put this there: that {} is in " "the way.".format(obj_module.name(obj))) else: break if action == "use": if self.item is None: on_fail.append("I have nothing to use.") elif obj is None and not hasattr(self.item, "use_on_ground"): msg = "I can't use this {} on the ground." on_fail.append(msg.format(obj_module.name(self.item))) else: break if action == "move": self.move(pos) elif action in ("inspect", "grab", "drop", "use"): self._action_with_pos(action, obj, pos)
def _do_action(self, action, obj, pos): if action == 'inspect': obj.interact(self) elif action == 'grab': self.grab(obj) elif action == 'drop': self.drop() elif action == 'use': if obj is None: name = 'ground' else: name = obj_module.ident(obj) method = 'use_on_' + name.replace(' ', '_') if not hasattr(self.item, method): msg = 'I don\'t know how to use this {} on that {}.' self.level.say(msg.format(obj_module.name(self.item), name)) else: getattr(self.item, method)(self, obj, pos)
def _do_action(self, action, obj, pos): if action == "inspect": obj.interact(self) elif action == "grab": self.grab(obj) elif action == "drop": self.drop() elif action == "use": if obj is None: name = "ground" else: name = obj_module.ident(obj) method = "use_on_" + name.replace(" ", "_") if not hasattr(self.item, method): msg = "I don't know how to use this {} on that {}." self.level.say(msg.format(obj_module.name(self.item), name)) else: getattr(self.item, method)(self, obj, pos)
def _move_mouse (self, evt): if self._grab_move(evt): orig_x, orig_y = evt.pos x = orig_x / TILE_SIZE[0] y = orig_y / TILE_SIZE[1] obj = self.top_obj(self.objs[x][y]) if obj is None: self._rm_ui('label') return label = obj_module.name(obj) sfc = self.game.render_text( 'label', label, conf.FONT_COLOUR, bg = conf.UI_BG, pad = conf.LABEL_PADDING, cache = ('label', label) )[0] o = conf.LABEL_OFFSET ws, hs = sfc.get_size() x, y = orig_x + o[0], orig_y - hs + o[1] w, h = conf.RES x = min(max(x, 0), w - ws) y = min(max(y, 0), h - hs) self._add_ui('label', sfc, (x, y))
def drop(self, obj=None, pos=None): if obj is None: obj = self.item else: # set this as our item in case we can't drop it anywhere assert self.item is None self.item = obj if obj is None: return if pos is None: # find a place to put the object pos = self.pos objs = self.level.objs sx, sy = conf.LEVEL_SIZE success = False df_axis = self.dirn % 2 df_sgn = 1 if self.dirn > 1 else -1 ds_axis = (df_axis + 1) % 2 for d_sideways, d_facing in [(0, 1), (-1, 0), (1, 0), (-1, 1), (1, 1), (-1, -1), (1, -1), (0, -1)]: p = list(pos) p[df_axis] += d_facing * df_sgn p[ds_axis] += d_sideways x, y = p if x >= 0 and y >= 0 and x < sx and y < sy \ and not any(o.solid for o in objs[x][y]): success = True break if success: pos = p else: msg = 'I can\'t find anywhere to drop this {}.' self.level.say(msg.format(obj_module.name(obj))) return # drop object self.item = None obj.drop(pos) self.level.update_held()
def _move_mouse(self, evt): if self._grab_move(evt): orig_x, orig_y = evt.pos x = orig_x / TILE_SIZE[0] y = orig_y / TILE_SIZE[1] obj = self.top_obj(self.objs[x][y]) if obj is None: self._rm_ui('label') return label = obj_module.name(obj) sfc = self.game.render_text('label', label, conf.FONT_COLOUR, bg=conf.UI_BG, pad=conf.LABEL_PADDING, cache=('label', label))[0] o = conf.LABEL_OFFSET ws, hs = sfc.get_size() x, y = orig_x + o[0], orig_y - hs + o[1] w, h = conf.RES x = min(max(x, 0), w - ws) y = min(max(y, 0), h - hs) self._add_ui('label', sfc, (x, y))
def drop(self, obj=None, pos=None): if obj is None: obj = self.item else: # set this as our item in case we can't drop it anywhere assert self.item is None self.item = obj if obj is None: return if pos is None: # find a place to put the object pos = self.pos objs = self.level.objs sx, sy = conf.LEVEL_SIZE success = False df_axis = self.dirn % 2 df_sgn = 1 if self.dirn > 1 else -1 ds_axis = (df_axis + 1) % 2 for d_sideways, d_facing in [(0, 1), (-1, 0), (1, 0), (-1, 1), (1, 1), (-1, -1), (1, -1), (0, -1)]: p = list(pos) p[df_axis] += d_facing * df_sgn p[ds_axis] += d_sideways x, y = p if x >= 0 and y >= 0 and x < sx and y < sy and not any(o.solid for o in objs[x][y]): success = True break if success: pos = p else: msg = "I can't find anywhere to drop this {}." self.level.say(msg.format(obj_module.name(obj))) return # drop object self.item = None obj.drop(pos) self.level.update_held()