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)
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
def render_block(self, x, y, screen, viewport, background): #don't render air if background: block = World.get_block_from_id(self.background_blocks[y][x]) else: block = World.get_block_from_id(self.foreground_blocks[y][x]) if block["name"] != "air": Game.get_world().render_block(block["id"], [Convert.chunk_to_world(x, self), y], block["connectedTexture"], screen, viewport, background, self)
def check_collision(self, chunk, left, right, top, bottom, old_pos, index): for block_x in range(left, right): for block_y in range(top, bottom): check_block = World.get_block(chunk.get_block_at(block_x, block_y, self.background)) if check_block["solid"] and self.collides([Convert.chunk_to_world(block_x, chunk), block_y]): #found a collision! self.pos[index] = old_pos[index] return True return False
def check_collision(self, chunk, left, right, top, bottom, old_pos, index): for block_x in range(left, right): for block_y in range(top, bottom): check_block = chunk.blocks[block_y][block_x] #if check_block.is_solid() and self.collides([Convert.chunk_to_world(block_x, chunk), block_y]): if check_block["solid"] and self.collides([Convert.chunk_to_world(block_x, chunk), block_y]): #found a collision! self.pos[index] = old_pos[index] return True return False
def render_block(self, block, pos, screen, viewport): """#fast render water if block["name"] == "water": screen.blit(World.block_images[block["id"]], Convert.world_to_viewport([Convert.chunk_to_world(pos[0], self), pos[1]], viewport))""" #don't render air if block["name"] != "air": #print(block["name"] + " " + str(block["id"])) Game.get_world().render_block(block["id"], [Convert.chunk_to_world(pos[0], self), pos[1]], block["connectedTexture"], screen, viewport) if Game.DEBUG: #draw bounding box pygame.draw.rect(screen, Game.BLACK, pygame.Rect(Convert.chunk_to_viewport(pos, self, viewport), (Game.BLOCK_SIZE * Game.SCALE, Game.BLOCK_SIZE * Game.SCALE)), 1)
def populate(self): #Fill in blocks of this chunk for y in range(len(self.foreground_blocks)): for x in range(len(self.foreground_blocks[y])): #surface_depth = self.heights[x] + 2 + random.randrange(4) if y < World.SEA_LEVEL: self.set_blocks_at(x, y, World.get_block("air")) else: world_x = Convert.chunk_to_world(x, self) noise = Generate.terrain((world_x, y), (self.biome["maxelevation"], self.biome["minelevation"])) self.set_blocks_from_noise(x, y, noise[0], False) self.set_blocks_from_noise(x, y, noise[1], True) """elif y < self.heights[x]: self.set_blocks_at(x, y, World.get_block("water")) elif y < surface_depth: self.set_blocks_at(x, y, World.get_block(self.biome["surface"])) else: self.set_blocks_at(x, y, World.get_block(self.biome["base"]))""" self.decorate()