def right_click(self): pos = pg.mouse.get_pos() global_pos = game_vars.global_mouse_pos() if not self.inventory.right_click(pos): block_x, block_y = game_vars.get_topleft( *[p // BLOCK_W for p in global_pos]) tile = game_vars.tiles[game_vars.get_block_at(block_x, block_y)] # First attempt to activate the tile # Make sure we didn't click the same block more than once if tile.clickable and self.placement_range.collidepoint(*global_pos) and \ (not tile.has_ui or self.active_ui is None or self.active_ui.block_pos != [block_x, block_y]): if tile.activate((block_x, block_y)): return # Then try to drop the cursor item if self.inventory.selected_item.is_item: # Check if we dropped an item drop = self.inventory.drop_item() if drop is not None: # This determines if we clicked to the left or right of the player left = global_pos[0] < self.rect.centerx game_vars.drop_item(drop, left) # If there is no cursor item, use the current hotbar item else: item = self.inventory.get_current_item() if item.is_item: item_obj = game_vars.items[item.item_id] if item_obj.right_click and (self.first_swing or item_obj.auto_use): self.first_swing = False # Use item item_obj.on_right_click() self.item_used = item_obj self.use_time = item_obj.use_time
def on_break(self, pos): data = game_vars.get_block_data(pos) if data: inv = Inventory((1, 1)) inv.load(data) item = inv.get_item(0, 0) if item.is_item: game_vars.drop_item(DroppedItem(item), c.random_sign(), [p * BLOCK_W for p in pos]) game_vars.write_block_data(pos, None) return True
def on_break(self, pos): data = game_vars.get_block_data(pos) # There is an item in the pedestal if data: item = ItemInfo(int.from_bytes(data[:2], byteorder), 1, data=data[2:]) game_vars.drop_item(DroppedItem(item), c.random_sign(), [ p * BLOCK_W + d * BLOCK_W // 2 for p, d in zip(pos, self.dim) ]) game_vars.write_block_data(pos, None) return True
def on_break(self, pos): data = game_vars.get_block_data(pos) if data is not None: item = int.from_bytes(data[:2], byteorder) if item in game_vars.items.keys(): # Drop contents from random import choice amnt = int.from_bytes(data[2:4], byteorder) if amnt > 0: game_vars.drop_item(DroppedItem(ItemInfo(item, amnt)), choice([True, False]), [pos[0] * BLOCK_W, pos[1] * BLOCK_W]) game_vars.write_block_data(pos, None) return True
def hit(self, damage, centerx): self.stats.hp -= damage game_vars.add_damage_text(damage, self.rect.center) self.immunity = .5 if not self.no_knockback: self.v = [math.copysign(5, self.rect.centerx - centerx), -5] if self.stats.hp < 0: # Get random drops and drop them items = self.get_drops() for item in items: game_vars.drop_item(DroppedItem(item), randint(0, 1) == 0, pos_=self.rect.center) return True return False
def craft(self, recipe): # Get result new_item = ItemInfo(*recipe[0]) # First try to have the player hold the item if not self.selected_item.is_item: self.selected_item.set_info(new_item) # Then try to add it to whatever the player is holding elif self.selected_item.same_as(new_item): grab = min(new_item.amnt, self.selected_item.max_stack - self.selected_item.amnt) self.selected_item.amnt += grab new_item.amnt -= grab else: # Finally try to put it into the inventory if not self.pick_up_item(new_item): # Drop whatever is left game_vars.drop_item(DroppedItem(new_item), True) # Get the amounts of ingredients that are required parts = [r.copy() for r in recipe[1:]] # Remove ingredients for y in range(self.dim[1]): for x in range(self.dim[0]): item = self.get_item(y, x) if item.is_item: i = 0 while i < len(parts): item_id, amnt = parts[i] if item.item_id == item_id: # Remove the item transfer = min(item.amnt, amnt) item.amnt -= transfer parts[i][1] -= transfer # Delete this item if parts[i][1] <= 0: del parts[i] if len(parts) == 0: return i -= 1 elif item_id > item.item_id: break i += 1
def activate(self, pos): item = game_vars.player_inventory().get_current_item() data = game_vars.get_block_data(pos) # There is an item in the pedestal if data and (not item.is_item or item.item_id != i.MAGIC_WAND): item = ItemInfo(int.from_bytes(data[:2], byteorder), 1, data=data[2:]) game_vars.drop_item(DroppedItem(item), c.random_sign(), [ p * BLOCK_W + d * BLOCK_W // 2 for p, d in zip(pos, self.dim) ]) game_vars.write_block_data(pos, None) game_vars.player.use_time = .3 return True # There is no item in the pedestal, Make sure we clicked with a magic container item elif item.is_item and isinstance(game_vars.items[item.item_id], ItemTypes.MagicContainer): game_vars.write_block_data( pos, item.item_id.to_bytes(2, byteorder) + item.data) item.amnt -= 1 game_vars.player.use_time = .3 return True return False
def process_events(self, events, mouse, keys): if self.click_inventories(mouse): self.save() else: pos = pg.mouse.get_pos() if self.rect.collidepoint(*pos): pos = [pos[0] - self.rect.x, pos[1] - self.rect.y] for e in events: item = self.inv.get_item(0, 0) if e.type == MOUSEBUTTONUP and e.button == BUTTON_LEFT and \ self.text_rect.collidepoint(*pos) and item.is_item: items = {} item = self.inv.inv_items[0][0] # Go through each item and get a random drop while item.is_item: idx = Objects.CRUSH_DROPS[item].random() if idx in items.keys(): items[idx] += 1 else: items[idx] = 1 item.amnt -= 1 # Drop the results block_pos = [ self.block_pos[0] * BLOCK_W, self.block_pos[1] * BLOCK_W ] for idx, amnt in zip(items.keys(), items.values()): max_stack = game_vars.items[idx].max_stack # Make sure we don't drop more than max stack while amnt > 0: transfer = min(max_stack, amnt) game_vars.drop_item( DroppedItem(ItemInfo(idx, transfer)), choice([True, False]), block_pos) amnt -= transfer self.save()