Exemplo n.º 1
0
 def tentative_move(self, world, old_pos, index):
     self.pos[index] += self.vel[index]
     if index == 0:
         self.bounding_box.x = Convert.world_to_pixel(self.pos[index])
     else:
         self.bounding_box.y = Convert.world_to_pixel(self.pos[index])
     block_left = int(Convert.world_to_chunk(self.pos[0])[0])
     chunk_left = Convert.world_to_chunk(self.pos[0])[1]
     block_right = math.ceil(Convert.world_to_chunk(self.pos[0] + self.width)[0])
     chunk_right = Convert.world_to_chunk(self.pos[0] + self.width)[1]
     block_top = int(self.pos[1])
     block_bottom = math.ceil(self.pos[1] + self.height)
     col1 = col2 = col3 = col4 = False
     if chunk_left == chunk_right:
         chunk = world.loaded_chunks.get(chunk_left)
         col1 = self.check_collision(chunk, block_left, block_right, block_top, block_bottom, old_pos, index)
     else:
         #need to check from block_left in chunk_left to block_right in chunk_right and all the blocks in any chunks between them
         chunk = world.loaded_chunks.get(chunk_left)
         col2 = self.check_collision(chunk, block_left, Chunk.WIDTH, block_top, block_bottom, old_pos, index)
         for c in range(chunk_left + 1, chunk_right):
             chunk = world.loaded_chunks.get(c)
             col3 = self.check_collision(chunk, 0, Chunk.WIDTH, block_top, block_bottom, old_pos, index)
         chunk = world.loaded_chunks.get(chunk_right)
         col4 = self.check_collision(chunk, 0, block_right, block_top, block_bottom, old_pos, index)
     if col1 or col2 or col3 or col4:
         self.vel[index] = 0 #reset acceleration?
     if index == 0:
         self.bounding_box.x = Convert.world_to_pixel(self.pos[index])
     else:
         self.bounding_box.y = Convert.world_to_pixel(self.pos[index])
Exemplo n.º 2
0
 def break_block(self, player, mouse_pos, viewport):
     angle = self.find_angle(player, mouse_pos, viewport)
     block_pos = Convert.pixels_to_world(self.find_pos(angle, player.pixel_pos(True), Convert.viewport_to_pixels(mouse_pos, viewport), player.get_break_distance())) #it aint right
     chunk = self.loaded_chunks.get(Convert.world_to_chunk(block_pos[0])[1])
     block = self.get_block(block_pos)
     if block["breakable"]:
         chunk.blocks[block_pos[1]][Convert.world_to_chunk(block_pos[0])[0]] = get_block("water")
         chunk.entities.append(BlockDrop.BlockDrop(block_pos, block["name"]))
Exemplo n.º 3
0
 def update(self):
     for x in range(self.loaded_chunks.first, self.loaded_chunks.end):
         chunk = self.loaded_chunks.get(x)
         for entity in chunk.entities:
             entity.update(self)
             if Convert.world_to_chunk(entity.pos[0])[1] != chunk.x:
                 print("Moving", entity, entity.pos)
                 chunk.entities.remove(entity)
                 self.loaded_chunks.get(Convert.world_to_chunk(entity.pos[0])[1]).entities.append(entity)
Exemplo n.º 4
0
 def update(self, world):
     old_chunk = Convert.world_to_chunk(self.pos[0])[1]
     hspeed = min(abs(self.vel[0] + self.acceleration * self.dir[0]), self.max_speed) * self.dir[0]
     vspeed = min(abs(self.vel[1] + self.acceleration * self.dir[1]), self.max_speed) * self.dir[1]
     self.vel = [hspeed, vspeed]
     super(Player, self).update(world)
     new_chunk = Convert.world_to_chunk(self.pos[0])[1]
     if new_chunk != old_chunk:
         world.load_chunks(new_chunk)
     
     entities = self.get_nearby_entities(world)
     for entity in entities:
         if(self.bounding_box.colliderect(entity.bounding_box)):
             if isinstance(entity, ItemDrop):
                 if self.inventory.insert(entity.get_itemstack()) == None:
                     world.loaded_chunks.get(entity.get_chunk()).entities.remove(entity)
Exemplo n.º 5
0
def render():
    if gamemode == MENU:
        menu.render(screen)
    elif gamemode == PLAYING:
        screen.fill(SKY)
        world.render(screen, viewport)
        if DEBUG:
            #render debug text
            h = 10
            fpsimg = font.render("fps: " + str(clock.get_fps()), 0, WHITE)
            screen.blit(fpsimg, (10, h))
            h += fpsimg.get_height()
            posimg = font.render("pos: [{0:.2f}".format(player.pos[0]) + ", {0:.2f}]".format(player.pos[1]), 0, WHITE)
            screen.blit(posimg, (10, h))
            h += posimg.get_height()
            chunk = world.loaded_chunks.get(Convert.world_to_chunk(player.pos[0])[1])
            chunkimg = font.render("chunk: " + str(chunk.x), 0, WHITE)
            screen.blit(chunkimg, (10, h))
            h += chunkimg.get_height()
            biomeimg = font.render("biome: " + str(chunk.biome["name"]), 0, WHITE)
            screen.blit(biomeimg, (10, h))
            h += biomeimg.get_height()
        player.render(screen, Convert.world_to_viewport(player.pos, viewport))
        if gui is None:
            target_pos = world.find_pos(world.find_angle(player, pygame.mouse.get_pos(), viewport), 
                                        Convert.pixels_to_viewport(player.pixel_pos(True), viewport), 
                                        pygame.mouse.get_pos(),
                                        player.get_break_distance())
            screen.blit(img_target, [x - y for x, y in zip(target_pos, [dim / 2 for dim in img_target.get_size()])]) #zoo-wee mama!
        else:
            gui.render(screen)
    pygame.display.flip()
Exemplo n.º 6
0
 def generate_structure(self, structure, x):
     #TODO: add background blocks defined separately
     if structure["type"] == "column":
         height = random.randint(structure["minheight"], structure["maxheight"])
         for y in range(self.heights[x] - height, self.heights[x]):
             self.set_block_at(x, y, World.get_block(structure["block"]), False)
     elif structure["type"] == "json":
         structure_file = open(structure["location"])
         structure_json = json.load(structure_file)
         curr_y = self.heights[x] - len(structure_json["shape"])
         for line in structure_json["shape"]:
             curr_world_x = Convert.chunk_to_world(x, self)
             for char in line:
                 #find the right chunk
                 chunk = self #world.chunks[Convert.world_to_chunk(x)[1]]- can't really do this...
                 curr_chunk_x = Convert.world_to_chunk(curr_world_x)[0]
                 if curr_chunk_x < WIDTH:
                     if char == " ":
                         block_name = "water"
                     else:
                         block_name = structure_json["blocks"][char]
                     block = World.get_block(block_name)
                     #TODO: add background
                     chunk.set_block_at(curr_chunk_x, curr_y, block, False)
                     if block["entity"] != "":
                         #generate the block entity
                         EntityClass = getattr(importlib.import_module("ent." + block["entity"]), block["entity"])
                         instance = EntityClass([curr_world_x, curr_y], self)
                         self.entities.append(instance)
                 curr_world_x += 1
             curr_y += 1
         structure_file.close()
     elif structure["type"] == "singleblock":
         self.set_block_at(x, self.heights[x] - 1, World.get_block(structure["block"]), False)
Exemplo n.º 7
0
 def generate_structure(self, structure, x):
     if structure["type"] == "column":
         height = random.randint(structure["minheight"], structure["maxheight"])
         for y in range(self.heights[x] - height, self.heights[x]):
             self.blocks[y][x] = World.get_block(structure["block"])
     elif structure["type"] == "json":
         structure_file = open(structure["location"])
         structure_json = json.load(structure_file)
         curr_y = self.heights[x] - len(structure_json["shape"])
         for line in structure_json["shape"]:
             curr_world_x = Convert.chunk_to_world(x, self)
             for char in line:
                 #find the right chunk
                 chunk = self #world.chunks[Convert.world_to_chunk(x)[1]]- can't really do this...
                 curr_chunk_x = Convert.world_to_chunk(curr_world_x)[0]
                 if curr_chunk_x < WIDTH:
                     if char == " ":
                         block = "water"
                     else:
                         block = structure_json["blocks"][char]
                     chunk.blocks[curr_y][curr_chunk_x] = World.get_block(block)
                 curr_world_x += 1
             curr_y += 1
         structure_file.close()
     elif structure["type"] == "other":
         #why did I write this?
         pass
Exemplo n.º 8
0
 def update(self, world):
     old_chunk = Convert.world_to_chunk(self.pos[0])[1]
     hspeed = min(abs(self.vel[0] + self.acceleration * self.dir[0]), self.max_speed) * self.dir[0]
     vspeed = min(abs(self.vel[1] + self.acceleration * self.dir[1]), self.max_speed) * self.dir[1]
     self.vel = [hspeed, vspeed]
     super(Player, self).update(world)
     new_chunk = Convert.world_to_chunk(self.pos[0])[1]
     if new_chunk != old_chunk:
         world.load_chunks(new_chunk)
     
     entities = world.loaded_chunks.get(Convert.world_to_chunk(self.pos[0])[1]).entities
     for entity in entities:
         if(self.bounding_box.colliderect(entity.bounding_box)):
             if type(entity) is BlockDrop:
                 if self.pickup(entity.blockname):
                     entities.remove(entity)
                 print(self.inventory)
Exemplo n.º 9
0
 def update(self):
     for x in range(self.loaded_chunks.first, self.loaded_chunks.end):
         chunk = self.loaded_chunks.get(x)
         for entity in chunk.entities:
             entity.update(self)
             if Convert.world_to_chunk(entity.pos[0])[1] != chunk.x \
                     and self.loaded_chunks.contains_index(Convert.world_to_chunk(entity.pos[0])[1]) \
                     and entity in chunk.entities:
                 chunk.entities.remove(entity)
                 self.create_entity(entity)
     for layer in [True, False]:
         blocks_to_remove = []
         for breaking_block in self.breaking_blocks[layer]:
             breaking_block["progress"] -= 1
             if breaking_block["progress"] <= 0:
                 blocks_to_remove.append(breaking_block)
         for b in blocks_to_remove:
             self.breaking_blocks[layer].remove(b)
Exemplo n.º 10
0
 def render_break_preview(self, background, world, block, block_pos, screen, viewport):
     chunk = world.loaded_chunks.get(Convert.world_to_chunk(block_pos[0])[1])
     blockimg = world.get_block_render(World.get_block_id(block["name"]), block_pos, block["connectedTexture"], background, chunk, background).copy()
     mask = pygame.mask.from_surface(blockimg)
     olist = mask.outline()
     polysurface = pygame.Surface((Game.BLOCK_SIZE * Game.SCALE, Game.BLOCK_SIZE * Game.SCALE), pygame.SRCALPHA)
     color = self.get_color(background)
     pygame.draw.polygon(polysurface, color, olist, 0)
     screen.blit(polysurface, Convert.world_to_viewport(block_pos, viewport))
Exemplo n.º 11
0
 def is_clear(self, world, pos):
     for x in range(int(pos[0]), int(pos[0] + self.width)):
         for y in range(int(pos[1]), int(pos[1] + self.height)):
             if not world.is_loaded_chunk(Convert.world_to_chunk(x)[1]):
                 return False
             if y >= World.HEIGHT:
                 return False
             block = World.get_block(world.get_block_at((x, y), False))
             if block["solid"]:
                 return False
     return True
Exemplo n.º 12
0
 def break_block(self, world, mouse_pos, viewport, background):
     block_pos = self.find_angle_pos(mouse_pos, viewport)
     chunk = world.loaded_chunks.get(Convert.world_to_chunk(block_pos[0])[1])
     #if there's a foreground block covering the background, don't break anything
     if background and world.get_block_at(block_pos, False) != "water":
         return
     block = World.get_block(world.get_block_at(block_pos, background))
     held_item = self.get_held_item()
     if held_item is None:
         harvest_level = 0
         break_speed = 1
     else:
         harvest_level = held_item.get_harvest_level()
         break_speed = held_item.get_break_speed()
     if (not block["breakable"]) or (block["harvestlevel"] > harvest_level):
         return
     block_to_break = None
     breaking_blocks = world.breaking_blocks[background]
     for breaking_block in breaking_blocks:
         if breaking_block["pos"] == block_pos:
             block_to_break = breaking_block
     if block_to_break is None:
         block_to_break = {"pos": block_pos, "name": block["name"], "progress": 0, "breaktime": block["breaktime"]}
         breaking_blocks.append(block_to_break)
     block_to_break["progress"] += 2 * break_speed
     if block_to_break["progress"] >= block_to_break["breaktime"]:
         #remove the block
         breaking_blocks.remove(block_to_break)
         chunk.set_block_at(Convert.world_to_chunk(block_pos[0])[0], block_pos[1], World.get_block("water"), background)
         blockentity = None
         if block["entity"] != "":
             #remove the associated entity
             for entity in chunk.entities:
                 if type(entity).__name__ == block["entity"] and [int(entity.pos[0]), int(entity.pos[1])] == block_pos:
                     chunk.entities.remove(entity)
                     blockentity = entity
                     break
         chunk.entities.append(ItemDrop(block_pos, block["name"], blockentity))
Exemplo n.º 13
0
 def load_state(self, path):
     savefile = open(path, "rb")
     save_data = pickle.load(savefile)
     savefile.close()
     self.player.set_pos(save_data["player_pos"])
     player_chunk = Convert.world_to_chunk(self.player.pos[0])[1]
     self.loaded_chunks = TwoWayList.TwoWayList()
     self.load_chunks(player_chunk)
     self.player.inventory = save_data["player_inventory"]
     for row in self.player.inventory:
         for item in row:
             if item is not None:
                 item.load_image()
     self.player.selected_slot = save_data["hotbar_slot"]
Exemplo n.º 14
0
 def load_state(self, path):
     savefile = open(path, "rb")
     save_data = pickle.load(savefile)
     savefile.close()
     self.player = save_data["player"]
     self.seed = save_data["seed"]
     Generate.setup(self.seed)
     player_chunk = Convert.world_to_chunk(self.player.pos[0])[1]
     self.loaded_chunks = TwoWayList.TwoWayList()
     self.load_chunks(player_chunk)
     self.player.load_image()
     for row in self.player.inventory:
         for item in row:
             if item is not None:
                 item.load_image()
Exemplo n.º 15
0
def render():
    if gamemode == MAINMENU or gamemode == PAUSEMENU:
        menu.render_background(screen)
        menu.render(screen)
        
    elif gamemode in (PLAYING, OPENGUI):
        player = world.player
        shift = pygame.key.get_pressed()[pygame.K_LSHIFT] or pygame.key.get_pressed()[pygame.K_RSHIFT]
        screen.fill(SKY)
        
        #background
        world.render(screen, viewport, True)
        if shift:
            player.draw_block_highlight(world, pygame.mouse.get_pos(), viewport, screen, shift)
        world.render_breaks(screen, viewport, True)
        
        #foreground
        world.render(screen, viewport, False)
        if not shift:
            player.draw_block_highlight(world, pygame.mouse.get_pos(), viewport, screen, shift)
        world.render_breaks(screen, viewport, False)
        player.render(screen, Convert.world_to_viewport(player.pos, viewport))
        
        #add blue overlay- not completely sure this is good
        overlay = pygame.Surface((SCREEN_WIDTH, SCREEN_HEIGHT))
        overlay.set_alpha(64)
        overlay.fill(BLUE)
        screen.blit(overlay, (0, 0))
        
        if gamemode == PLAYING:
            hotbarGui.render(screen)
        else:
            gui.render(screen)
        
        if DEBUG:
            #render debug text
            chunk = world.loaded_chunks.get(Convert.world_to_chunk(player.pos[0])[1])
            debugtext = ["fps: {0:.2f}".format(clock.get_fps()),
                        "pos: [{0:.2f}".format(player.pos[0]) + ", {0:.2f}]".format(player.pos[1]),
                        "chunk: " + str(chunk.x),
                        "biome: " + str(chunk.biome["name"])]
            debugimg = GUI.render_string_array(debugtext, font, 0, WHITE)
            h = SCREEN_HEIGHT - debugimg.get_height()
            screen.blit(debugimg, (2 * SCALE, h))
    pygame.display.flip()
Exemplo n.º 16
0
 def render_block_preview(self, background, held_item, world, block_pos, screen, viewport):
     held_block = World.get_block(held_item.name)
     chunk = world.loaded_chunks.get(Convert.world_to_chunk(block_pos[0])[1])
     blockimg = world.get_block_render(World.get_block_id(held_block["name"]), block_pos, held_block["connectedTexture"], background, chunk, background).copy()
     mask = pygame.mask.from_surface(blockimg)
     olist = mask.outline()
     polysurface = pygame.Surface((Game.BLOCK_SIZE * Game.SCALE, Game.BLOCK_SIZE * Game.SCALE), pygame.SRCALPHA)
     screen.blit(polysurface, Convert.world_to_viewport(block_pos, viewport))
     collides = False
     entities = world.get_nearby_entities(self.get_chunk())
     entities.append(self)
     for entity in entities:
         if entity.collides(block_pos) and entity.background == background:
             collides = True
     color = self.get_color(background)
     if collides and World.get_block(held_block["name"])["solid"]:
         color = (color[0], 0, 0, color[3])
     pygame.draw.polygon(polysurface, color, olist, 0)
     blockimg.blit(polysurface, (0, 0), special_flags=pygame.BLEND_RGBA_MULT)
     screen.blit(blockimg, Convert.world_to_viewport(block_pos, viewport))
Exemplo n.º 17
0
 def interact(self, player, item):
     if self.locked or self.animating:
         return False
     
     world = Game.get_world()
     sides = [(0, 1), (1, 0), (0, -1), (-1, 0)]
     for side in sides:
         goal_pos = [self.pos[0] + side[0], self.pos[1] + side[1]]
         block = world.get_block_at(goal_pos, False)
         if block == "puzzleBackground":
             entity_collision = False
             entities = world.loaded_chunks.get(Convert.world_to_chunk(goal_pos[0])[1]).entities
             for entity in entities:
                 if entity.pos == goal_pos:
                     entity_collision = True
             if not entity_collision:
                 self.old_pos = self.pos
                 self.animating = True
                 self.set_pos(goal_pos)
                 Game.play_sound("sfx/puzzle/slide.ogg")
                 return True
     return False
Exemplo n.º 18
0
 def get_block(self, world_pos):
     chunk = self.loaded_chunks.get(Convert.world_to_chunk(world_pos[0])[1])
     x_in_chunk = Convert.world_to_chunk(world_pos[0])[0]
     return chunk.blocks[world_pos[1]][x_in_chunk]
Exemplo n.º 19
0
 def set_block_at(self, world_pos, block, background):
     chunk = self.loaded_chunks.get(Convert.world_to_chunk(world_pos[0])[1])
     x_in_chunk = Convert.world_to_chunk(world_pos[0])[0]
     chunk.set_block_at(x_in_chunk, world_pos[1], block, background)
Exemplo n.º 20
0
 def create_entity(self, entity):
     self.loaded_chunks.get(Convert.world_to_chunk(entity.pos[0])[1]).entities.append(entity)
Exemplo n.º 21
0
 def close(self, world):
     if self.moving_item is not None:
         chunk = world.loaded_chunks.get(Convert.world_to_chunk(self.player.pos[0])[1])
         chunk.entities.append(ItemDrop(self.player.pos, self.moving_item.name, self.moving_item.imageurl, self.moving_item.data, self.moving_item.count))
         self.moving_item = None
Exemplo n.º 22
0
 def get_chunk(self):
     return Convert.world_to_chunk(self.pos[0])[1] #should be accurate
Exemplo n.º 23
0
 def get_nearby_entities(self, world):
     entities = list(world.loaded_chunks.get(Convert.world_to_chunk(self.pos[0])[1]).entities)
     entities += world.loaded_chunks.get(Convert.world_to_chunk(self.pos[0])[1] - 1).entities
     entities += world.loaded_chunks.get(Convert.world_to_chunk(self.pos[0])[1] + 1).entities
     return entities