コード例 #1
0
 def get_block_img(self, pos):
     data = game_vars.get_block_data(pos)
     if data and len(data) > 2:
         item = game_vars.items[int.from_bytes(data[:2], byteorder)]
         if isinstance(item, ItemTypes.MagicContainer):
             final_img = self.image.copy()
             # Draw magic ball
             img_w = BLOCK_W * 3 // 4
             img = c.scale_to_fit(item.image, img_w, img_w)
             img_rect = img.get_rect(center=(BLOCK_W // 2, img_w // 2))
             final_img.blit(img, img_rect)
             element = int.from_bytes(data[2:3], byteorder)
             current_amnt = int.from_bytes(data[3:], byteorder)
             px_h = int(current_amnt * img_w / item.capacity)
             px_arr = pg.surfarray.pixels2d(final_img)
             for y in range(img_rect.bottom - px_h, img_rect.bottom):
                 first = False
                 for x in range(img_rect.left, img_rect.right):
                     if first:
                         if px_arr[x][y] == 0xff000000:
                             break
                         else:
                             px_arr[x][y] = 0xff0000ff
                     else:
                         if px_arr[x][y] == 0xff000000:
                             first = True
             del px_arr
             return final_img
     return self.image
コード例 #2
0
 def activate(self, pos):
     data = game_vars.get_block_data(pos)
     if data is not None:
         game_vars.player.set_active_ui(self.UI(pos, data))
         if not game_vars.player.inventory.open:
             game_vars.player.inventory.toggle()
         return True
     return False
コード例 #3
0
 def get_block_img(self, pos):
     data = game_vars.get_block_data(pos)
     if data and len(data) >= 2:
         item, amnt = ItemTypes.load_id_amnt(data[:4])
         if item in game_vars.items.keys():
             return game_vars.animations[self.anim_idx].get_frame(
                 img=game_vars.items[item].inv_img)
     return game_vars.animations[self.anim_idx].get_frame()
コード例 #4
0
 def on_left_click(self):
     pos = game_vars.get_topleft(*game_vars.global_mouse_pos(blocks=True))
     item_id = game_vars.get_block_at(*pos)
     if item_id == t.PORTAL:
         data = game_vars.get_block_data(pos)
         if data:
             # TODO: ingame message system
             print("Magic Stored:", int.from_bytes(data, byteorder))
コード例 #5
0
 def on_break(self, pos):
     data = game_vars.get_block_data(pos)
     if data is not None:
         # Check if we have any i
         for byte in data:
             if byte != 0:
                 return False
         # If not, remove our data
         c.remove_from_dict(*pos, game_vars.world.block_data)
     return True
コード例 #6
0
 def get_space(self, x, y):
     tile_id = game_vars.get_block_at(x, y)
     if tile_id == t.PEDESTAL:
         data = game_vars.get_block_data((x, y))
         if data and len(data) > 2:
             item = game_vars.items[int.from_bytes(data[:2], byteorder)]
             if isinstance(item, ItemTypes.MagicContainer):
                 current_amnt = int.from_bytes(data[3:item.int_bytes + 3],
                                               byteorder)
                 return item.capacity - current_amnt
     return 0
コード例 #7
0
 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
コード例 #8
0
 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
コード例 #9
0
 def add_magic(self, x, y, amnt):
     data = game_vars.get_block_data((x, y))
     if data and len(data) > 2:
         item = game_vars.items[int.from_bytes(data[:2], byteorder)]
         if isinstance(item, ItemTypes.MagicContainer):
             current_amnt = int.from_bytes(data[3:item.int_bytes + 3],
                                           byteorder)
             transfer = min(item.capacity - current_amnt, amnt)
             amnt -= transfer
             new_data = data[:3] + (current_amnt + transfer).to_bytes(
                 item.int_bytes, byteorder)
             game_vars.write_block_data((x, y), new_data)
コード例 #10
0
 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
コード例 #11
0
 def tick(self, x, y, dt):
     magic = 0
     rect = pg.Rect(x * BLOCK_W, y * BLOCK_W, self.dim[0] * BLOCK_W,
                    self.dim[1] * BLOCK_W)
     items = game_vars.handler.items
     for item in items:
         if item.item.magic_value > 0 and item.rect.colliderect(rect):
             magic += item.item.magic_value * item.info.amnt
             items.remove(item)
     if magic > 0:
         data = game_vars.get_block_data((x, y))
         if data:
             current_magic = int.from_bytes(data[:8], byteorder)
             game_vars.write_block_data(
                 (x, y), (current_magic + magic).to_bytes(8, byteorder))
コード例 #12
0
 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
コード例 #13
0
 def summon(self, pos):
     data = game_vars.get_block_data(pos)
     if data:
         magic = int.from_bytes(data[:8], byteorder)
         # Must have at least 5 magic
         if magic < 5:
             return
         # Every X5 for magic increases max level by 1
         max_level = math.log(magic, 5)
         chance = max(max_level % 1, .5)
         max_level = int(max_level)
         # Find chances for current level and up to two levels below
         if max_level == 1:
             chances = {1: 1}
         elif max_level == 2:
             chances = {2: chance, 1: 1 - chance}
         else:
             chances = {
                 max_level: chance,
                 max_level - 1: (1 - chance) * 2 / 3,
                 max_level - 2: (1 - chance) / 3
             }
         # Choose a random level
         num = uniform(0, 1)
         for lvl, chance in chances.items():
             if num > chance:
                 num -= chance
             else:
                 # Summon mage
                 # element = choice(list(ItemTypes.MagicContainer.ELEMENT_NAMES.keys()))
                 game_vars.spawn_entity(
                     mobs.Mage(ItemTypes.MagicContainer.FIRE, lvl),
                     [p * BLOCK_W for p in pos])
                 break
         magic = int(magic / 2)
         game_vars.write_block_data(pos, magic.to_bytes(8, byteorder))
コード例 #14
0
 def activate(self, pos):
     data = game_vars.get_block_data(pos)
     if data is not None:
         game_vars.player.set_active_ui(self.UI(pos, data))
         return True
     return False
コード例 #15
0
 def get_block_img(self, pos):
     data = game_vars.get_block_data(pos)
     if data and self.anim_idx != -1:
         self.image = game_vars.animations[self.anim_idx].get_frame()
     return self.image