class UI(ActiveUI): def __init__(self, pos, data): self.inv = Inventory(Chest.INV_DIM) ActiveUI.__init__(self, None, self.inv.rect.move(0, self.inv.rect.h), pos=pos) self.can_drag = False self.inv.load(data) if not game_vars.player.inventory.open: game_vars.player.inventory.toggle() def get_inventories(self): return [self.inv] def on_inv_pickup(self): game_vars.write_block_data(self.block_pos, self.inv.write()) def process_events(self, events, mouse, keys): if self.click_inventories(mouse): game_vars.write_block_data(self.block_pos, self.inv.write())
class UI(ActiveUI): def __init__(self, pos, data): rect = pg.Rect(0, 0, c.MIN_W // 2, c.MIN_H // 2) rect.center = c.screen_center super().__init__(pg.Surface(rect.size), rect, pos=pos) # Set up inventory for item whitelist = [ item_id for item_id, item in game_vars.items.items() if isinstance(item, ItemTypes.Upgradable) ] self.inv = Inventory((1, 1), whitelist=whitelist, max_stack=1) self.inv.rect.move_ip((self.rect.w - c.INV_W) // 2, c.INV_W // 2) self.inv.load(data) # Set up upgrade section with offsets, rect, and surface self.dragging = False self.off_x = self.off_y = 0 self.max_x = self.max_y = 0 y = self.inv.rect.bottom self.tree_r = pg.Rect(0, y, self.rect.w, self.rect.h - y) self.upgrade_tree = self.tree_s = None # Load the data self.load_tree() def get_inventories(self): return [self.inv] def draw(self): super().draw() # Check for hovering over upgrade tree if self.upgrade_tree: pos = pg.mouse.get_pos() if self.rect.collidepoint(*pos): pos = [pos[0] - self.rect.x, pos[1] - self.rect.y] if self.tree_r.collidepoint(*pos): pos = [ pos[0] - self.tree_r.x + self.off_x, pos[1] - self.tree_r.y + self.off_y ] self.upgrade_tree.check_hover(pos) def save(self): item = self.inv.get_item(0, 0) if not self.upgrade_tree or not item.is_item: item.data = None else: item.data = self.upgrade_tree.write() game_vars.write_block_data(self.block_pos, self.inv.write()) def draw_tree(self): self.ui.fill((0, 0, 0), self.tree_r) if self.tree_s: self.ui.blit(self.tree_s, self.tree_r, area=((self.off_x, self.off_y), self.tree_r.size)) def load_tree(self): item = self.inv.get_item(0, 0) if item.data and item.is_item: # Draw upgrade tree and get surface dimensions self.upgrade_tree = game_vars.items[ item.item_id].upgrade_tree.new_tree() self.upgrade_tree.load(item.data) self.tree_s = self.upgrade_tree.get_surface() s_dim = self.tree_s.get_size() # Readjust rectangle y = self.inv.rect.bottom self.tree_r = pg.Rect(0, y, min(self.rect.w, s_dim[0]), min(self.rect.h - y, s_dim[1])) self.tree_r.centerx = self.rect.w // 2 # Update max scroll and current scroll self.max_x = max(0, s_dim[0] - self.tree_r.w) self.off_x = self.max_x // 2 self.max_y = max(0, s_dim[1] - self.tree_r.h) self.off_y = 0 else: self.tree_s = None # Draw upgrade tree to ui surface self.draw_tree() def on_inv_pickup(self): self.load_tree() self.save() def process_events(self, events, mouse, keys): # Get current item data to check if we changed the item prev_data = self.inv.inv_items[0][0].data # Drag upgrade screen if self.dragging: if not mouse[BUTTON_LEFT - 1]: self.dragging = False else: self.off_x -= game_vars.d_mouse[0] self.off_x = min(max(0, self.off_x), self.max_x) self.off_y -= game_vars.d_mouse[1] self.off_y = min(max(0, self.off_y), self.max_y) self.draw_tree() # Check inventories elif self.click_inventories(mouse): # If we changed the item data, draw upgrade tree if prev_data != self.inv.inv_items[0][0].data: self.load_tree() self.save() # Handle other events else: pos = pg.mouse.get_pos() if self.rect.collidepoint(*pos): pos = [pos[0] - self.rect.x, pos[1] - self.rect.y] # Process events for e in events: # Start dragging only if the player is holding nothing if e.type == MOUSEBUTTONDOWN and e.button == BUTTON_LEFT: if self.tree_r.collidepoint( *pos) and not game_vars.player_inventory( ).get_held_item().is_item: self.dragging = True # Click the upgrade tree elif e.type == MOUSEBUTTONUP and e.button == BUTTON_LEFT: if self.tree_r.collidepoint( *pos) and self.upgrade_tree: pos = [ pos[0] - self.tree_r.x + self.off_x, pos[1] - self.tree_r.y + self.off_y ] if self.upgrade_tree.click(pos): self.tree_s = self.upgrade_tree.get_surface( ) self.draw_tree() self.save()
class UI(ActiveUI): def __init__(self, pos, data, max_stack): self.max_stack = max_stack # Generate text font = c.get_scaled_font(-1, c.INV_IMG_W, "Crush!", "Times New Roman") text = font.render("Crush!", 1, (255, 255, 255)) self.text_rect = text.get_rect(centery=c.INV_W * 3 // 2) w = max(self.text_rect.w, c.INV_W) self.text_rect.centerx = w // 2 # Draw surface s = pg.Surface((w, c.INV_W * 2)) s.blit(text, self.text_rect) # This is where we will take/add items self.inv = Inventory((1, 1), whitelist=Objects.CRUSH_DROPS.keys(), max_stack=9) self.inv.rect = pg.Rect((w - c.INV_W) // 2, 0, c.INV_W, c.INV_W) self.inv.load(data) ActiveUI.__init__(self, s, s.get_rect(), pos=pos) self.rect.center = c.screen_center def get_inventories(self): return [self.inv] def save(self): game_vars.write_block_data(self.block_pos, self.inv.write()) def on_inv_pickup(self): self.save() 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()