当前位置: 首页 > news >正文

使用java制作minecraft3.0版本

这一次的更新主要加入了远古城市矿洞,还有更高级的矿脉。

在这次更新中可能需要VIP,请大家谅解。

由于代码较长,所以我就不直接赘述了,接下来上代码:

 

import org.lwjgl.*;
import org.lwjgl.glfw.*;
import org.lwjgl.opengl.*;
import org.lwjgl.system.*;

import java.nio.*;
import java.util.*;

import static org.lwjgl.glfw.Callbacks.*;
import static org.lwjgl.glfw.GLFW.*;
import static org.lwjgl.opengl.GL11.*;
import static org.lwjgl.opengl.GL15.*;
import static org.lwjgl.opengl.GL20.*;
import static org.lwjgl.opengl.GL30.*;
import static org.lwjgl.system.MemoryStack.*;
import static org.lwjgl.system.MemoryUtil.*;

public class AdvancedMinecraftWorld {
// 窗口尺寸
private static final int WIDTH = 1200;
private static final int HEIGHT = 800;
private long window;

// 摄像机参数
private float cameraX = 50.0f;
private float cameraY = 20.0f;
private float cameraZ = 50.0f;
private float cameraPitch = -30.0f;
private float cameraYaw = 45.0f;

// 世界参数
private static final int WORLD_SIZE = 128;
private static final int CHUNK_SIZE = 16;
private static final int WORLD_HEIGHT = 128;
private int[][][] world = new int[WORLD_SIZE][WORLD_HEIGHT][WORLD_SIZE];

// 方块类型
private static final int AIR = 0;
private static final int GRASS = 1;
private static final int DIRT = 2;
private static final int STONE = 3;
private static final int WOOD = 4;
private static final int LEAVES = 5;
private static final int PLANKS = 6;
private static final int COBBLESTONE = 7;
private static final int WATER = 8;
private static final int DEEPSLATE = 9;
private static final int ORE_COAL = 10;
private static final int ORE_IRON = 11;
private static final int ORE_GOLD = 12;
private static final int ORE_DIAMOND = 13;
private static final int ORE_EMERALD = 14;
private static final int OBSIDIAN = 15;
private static final int GLOWSTONE = 16;
private static final int ENCHANTING_TABLE = 17;
private static final int ANCIENT_DEBRIS = 18;
private static final int SCULK = 19;
private static final int SCULK_CATALYST = 20;
private static final int SCULK_SENSOR = 21;
private static final int SCULK_SHRIEKER = 22;

// 颜色映射
private static final Map<Integer, float[]> blockColors = new HashMap<>();
static {
blockColors.put(GRASS, new float[]{0.2f, 0.8f, 0.3f});           // 绿色
blockColors.put(DIRT, new float[]{0.5f, 0.35f, 0.2f});           // 棕色
blockColors.put(STONE, new float[]{0.5f, 0.5f, 0.5f});           // 灰色
blockColors.put(WOOD, new float[]{0.6f, 0.4f, 0.2f});            // 棕色
blockColors.put(LEAVES, new float[]{0.2f, 0.6f, 0.2f});          // 深绿色
blockColors.put(PLANKS, new float[]{0.8f, 0.6f, 0.4f});          // 浅棕色
blockColors.put(COBBLESTONE, new float[]{0.4f, 0.4f, 0.4f});     // 深灰色
blockColors.put(WATER, new float[]{0.2f, 0.4f, 0.8f, 0.7f});     // 蓝色(半透明)
blockColors.put(DEEPSLATE, new float[]{0.2f, 0.2f, 0.2f});       // 深灰色
blockColors.put(ORE_COAL, new float[]{0.1f, 0.1f, 0.1f});        // 黑色
blockColors.put(ORE_IRON, new float[]{0.8f, 0.8f, 0.9f});        // 浅灰色
blockColors.put(ORE_GOLD, new float[]{1.0f, 0.8f, 0.0f});        // 金色
blockColors.put(ORE_DIAMOND, new float[]{0.3f, 0.8f, 0.8f});     // 青色
blockColors.put(ORE_EMERALD, new float[]{0.2f, 0.8f, 0.3f});     // 绿色
blockColors.put(OBSIDIAN, new float[]{0.15f, 0.07f, 0.2f});      // 深紫色
blockColors.put(GLOWSTONE, new float[]{0.9f, 0.8f, 0.4f});       // 黄色(发光)
blockColors.put(ENCHANTING_TABLE, new float[]{0.2f, 0.1f, 0.4f});// 紫色
blockColors.put(ANCIENT_DEBRIS, new float[]{0.4f, 0.2f, 0.1f});  // 棕色
blockColors.put(SCULK, new float[]{0.1f, 0.2f, 0.2f});           // 深青色
blockColors.put(SCULK_CATALYST, new float[]{0.1f, 0.3f, 0.3f});  // 青色
blockColors.put(SCULK_SENSOR, new float[]{0.2f, 0.4f, 0.4f});    // 浅青色
blockColors.put(SCULK_SHRIEKER, new float[]{0.05f, 0.15f, 0.15f});// 深青色
}

// 发光方块列表
private static final Set<Integer> glowingBlocks = new HashSet<>(Arrays.asList(
GLOWSTONE, ENCHANTING_TABLE, SCULK_CATALYST, SCULK_SENSOR
));

// 结构位置
private List<int[]> villages = new ArrayList<>();
private List<int[]> pillagerOutposts = new ArrayList<>();
private List<int[]> mineShafts = new ArrayList<>();
private List<int[]> ancientCities = new ArrayList<>();

// 附魔效果数据
private Map<String, List<EnchantmentEffect>> enchantmentEffects = new HashMap<>();

// 随机数生成器
private Random random = new Random(12345);

// 附魔效果类
private class EnchantmentEffect {
float x, y, z;
int type;
long startTime;
float size;

EnchantmentEffect(float x, float y, float z, int type) {
this.x = x;
this.y = y;
this.z = z;
this.type = type;
this.startTime = System.currentTimeMillis();
this.size = 0.5f + random.nextFloat() * 1.5f;
}

boolean isExpired() {
return System.currentTimeMillis() - startTime > 5000; // 5秒后消失
}

float getAlpha() {
float life = (System.currentTimeMillis() - startTime) / 5000.0f;
return 1.0f - Math.min(1.0f, life);
}
}

    public static void main(String[] args) {
new AdvancedMinecraftWorld().run();
}

    public void run() {
init();
loop();

// 释放资源
glfwFreeCallbacks(window);
glfwDestroyWindow(window);
glfwTerminate();
Objects.requireNonNull(glfwSetErrorCallback(null)).free();
}

    private void init() {
// 设置错误回调
GLFWErrorCallback.createPrint(System.err).set();

// 初始化 GLFW
if (!glfwInit()) {
throw new IllegalStateException("Unable to initialize GLFW");
}

// 配置 GLFW
glfwDefaultWindowHints();
glfwWindowHint(GLFW_VISIBLE, GLFW_FALSE);
glfwWindowHint(GLFW_RESIZABLE, GLFW_TRUE);
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 2);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);

// 创建窗口
window = glfwCreateWindow(WIDTH, HEIGHT, "Advanced Minecraft World Generator", NULL, NULL);
if (window == NULL) {
throw new RuntimeException("Failed to create the GLFW window");
}

// 设置按键回调
glfwSetKeyCallback(window, (window, key, scancode, action, mods) -> {
if (key == GLFW_KEY_ESCAPE && action == GLFW_RELEASE) {
glfwSetWindowShouldClose(window, true);
}
if (key == GLFW_KEY_E && action == GLFW_RELEASE) {
// 在玩家位置创建附魔效果
createEnchantmentEffect(cameraX, cameraY, cameraZ, random.nextInt(3));
}
});

// 设置光标位置回调
glfwSetCursorPosCallback(window, (window, xpos, ypos) -> {
float sensitivity = 0.1f;
cameraYaw += (float) (xpos - WIDTH / 2.0) * sensitivity;
cameraPitch -= (float) (ypos - HEIGHT / 2.0) * sensitivity;

// 限制俯仰角
if (cameraPitch > 89.0f) cameraPitch = 89.0f;
if (cameraPitch < -89.0f) cameraPitch = -89.0f;

// 重置光标位置
glfwSetCursorPos(window, WIDTH / 2.0, HEIGHT / 2.0);
});

// 获取分辨率
try (MemoryStack stack = stackPush()) {
IntBuffer pWidth = stack.mallocInt(1);
IntBuffer pHeight = stack.mallocInt(1);
glfwGetWindowSize(window, pWidth, pHeight);

// 获取显示器分辨率
GLFWVidMode vidmode = glfwGetVideoMode(glfwGetPrimaryMonitor());
// 居中窗口
glfwSetWindowPos(
window,
(vidmode.width() - pWidth.get(0)) / 2,
(vidmode.height() - pHeight.get(0)) / 2
);
}

// 设置 OpenGL 上下文
glfwMakeContextCurrent(window);
// 启用垂直同步
glfwSwapInterval(1);
// 显示窗口
glfwShowWindow(window);

// 初始化 OpenGL
GL.createCapabilities();
glEnable(GL_DEPTH_TEST);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glClearColor(0.6f, 0.8f, 1.0f, 1.0f); // 天蓝色背景

// 生成世界
generateWorld();

// 初始化附魔效果
enchantmentEffects.put("particles", new ArrayList<>());
}

    private void generateWorld() {
// 基础地形生成
generateTerrain();

// 生成矿洞和矿井
generateMineShafts(10);
generateCaves(20);

// 生成树木
generateTrees(100);

// 生成村庄
generateVillages(5);

// 生成掠夺者哨塔
generatePillagerOutposts(3);

// 生成远古城市
generateAncientCities(2);

// 生成矿石
generateOres();
}

    private void generateTerrain() {
// 生成基础地形(Perlin噪声)
float[][] heightMap = new float[WORLD_SIZE][WORLD_SIZE];
for (int x = 0; x < WORLD_SIZE; x++) {
for (int z = 0; z < WORLD_SIZE; z++) {
// 使用多层噪声创建更自然的地形
float height = noise(x * 0.1f, z * 0.1f) * 10;
height += noise(x * 0.05f, z * 0.05f) * 20;
height += noise(x * 0.02f, z * 0.02f) * 5;
height = Math.max(10, height);
heightMap[x][z] = height;
}
}

// 填充方块
for (int x = 0; x < WORLD_SIZE; x++) {
for (int z = 0; z < WORLD_SIZE; z++) {
int height = (int) heightMap[x][z];

// 地表层
world[x][height][z] = GRASS;

// 地下层
for (int y = height - 1; y > height - 4; y--) {
if (y >= 0) world[x][y][z] = DIRT;
}

// 岩石层
for (int y = height - 4; y >= 0; y--) {
if (y >= 0) {
if (y < 20) {
world[x][y][z] = DEEPSLATE; // 深板岩层
} else {
world[x][y][z] = STONE;
}
}
}

// 水面
if (height < 15) {
for (int y = height + 1; y <= 15; y++) {
if (y < WORLD_HEIGHT) {
world[x][y][z] = WATER;
}
}
}
}
}
}

    private void generateOres() {
// 生成煤炭
generateVein(ORE_COAL, 20, 50, 0.1f, 5, 10);

// 生成铁矿石
generateVein(ORE_IRON, 10, 40, 0.08f, 4, 8);

// 生成金矿石
generateVein(ORE_GOLD, 5, 30, 0.05f, 3, 6);

// 生成钻石矿石
generateVein(ORE_DIAMOND, 5, 15, 0.02f, 2, 4);

// 生成绿宝石矿石
generateVein(ORE_EMERALD, 5, 20, 0.01f, 1, 3);

// 生成远古残骸(下界矿石,这里仅作演示)
generateVein(ANCIENT_DEBRIS, 5, 15, 0.005f, 1, 2);
}

    private void generateVein(int oreType, int minY, int maxY, float chance, int minSize, int maxSize) {
for (int x = 0; x < WORLD_SIZE; x++) {
for (int z = 0; z < WORLD_SIZE; z++) {
for (int y = minY; y < maxY; y++) {
if (random.nextFloat() < chance && world[x][y][z] == STONE) {
int size = minSize + random.nextInt(maxSize - minSize + 1);
generateOreVein(x, y, z, oreType, size);
}
}
}
}
}

    private void generateOreVein(int startX, int startY, int startZ, int oreType, int size) {
for (int i = 0; i < size; i++) {
int x = startX + random.nextInt(3) - 1;
int y = startY + random.nextInt(3) - 1;
int z = startZ + random.nextInt(3) - 1;

if (x >= 0 && x < WORLD_SIZE && 
y >= 0 && y < WORLD_HEIGHT && 
z >= 0 && z < WORLD_SIZE && 
(world[x][y][z] == STONE || world[x][y][z] == DEEPSLATE)) {
world[x][y][z] = oreType;
}
}
}

    private void generateCaves(int count) {
for (int i = 0; i < count; i++) {
int startX = random.nextInt(WORLD_SIZE);
int startY = 10 + random.nextInt(WORLD_HEIGHT - 20);
int startZ = random.nextInt(WORLD_SIZE);

int length = 20 + random.nextInt(50);
float yaw = random.nextFloat() * (float) Math.PI * 2;
float pitch = (random.nextFloat() - 0.5f) * (float) Math.PI / 2;

generateCave(startX, startY, startZ, length, 3 + random.nextInt(3), yaw, pitch);
}
}

    private void generateCave(int startX, int startY, int startZ, int length, int radius, float yaw, float pitch) {
int x = startX;
int y = startY;
int z = startZ;

for (int i = 0; i < length; i++) {
// 生成球形洞穴
for (int dx = -radius; dx <= radius; dx++) {
for (int dy = -radius; dy <= radius; dy++) {
for (int dz = -radius; dz <= radius; dz++) {
if (dx*dx + dy*dy + dz*dz <= radius*radius) {
int nx = x + dx;
int ny = y + dy;
int nz = z + dz;

if (nx >= 0 && nx < WORLD_SIZE && 
ny >= 0 && ny < WORLD_HEIGHT && 
nz >= 0 && nz < WORLD_SIZE) {
world[nx][ny][nz] = AIR;
}
}
}
}
}

// 移动洞穴中心
x += (int) (Math.cos(yaw) * Math.cos(pitch));
z += (int) (Math.sin(yaw) * Math.cos(pitch));
y += (int) Math.sin(pitch);

// 随机改变方向
yaw += (random.nextFloat() - 0.5f) * 0.5f;
pitch += (random.nextFloat() - 0.5f) * 0.3f;

// 确保不超出世界边界
if (x < 0 || x >= WORLD_SIZE || y < 5 || y >= WORLD_HEIGHT - 5 || z < 0 || z >= WORLD_SIZE) {
break;
}
}
}

    private void generateMineShafts(int count) {
for (int i = 0; i < count; i++) {
int startX = random.nextInt(WORLD_SIZE);
int startY = 10 + random.nextInt(30);
int startZ = random.nextInt(WORLD_SIZE);

mineShafts.add(new int[]{startX, startY, startZ});
generateMineShaft(startX, startY, startZ);
}
}

    private void generateMineShaft(int startX, int startY, int startZ) {
int length = 30 + random.nextInt(50);
int branches = 3 + random.nextInt(5);

// 生成主隧道
generateMineTunnel(startX, startY, startZ, length, 0, 0);

// 生成分支
for (int i = 0; i < branches; i++) {
int branchPoint = random.nextInt(length);
int branchX = startX;
int branchY = startY;
int branchZ = startZ;

// 计算分支起点
for (int j = 0; j < branchPoint; j++) {
branchX += random.nextInt(3) - 1;
branchY += random.nextInt(3) - 1;
branchZ += random.nextInt(3) - 1;

// 确保不超出边界
branchX = Math.max(0, Math.min(WORLD_SIZE - 1, branchX));
branchY = Math.max(5, Math.min(WORLD_HEIGHT - 5, branchY));
branchZ = Math.max(0, Math.min(WORLD_SIZE - 1, branchZ));
}

// 生成分支隧道
generateMineTunnel(branchX, branchY, branchZ, 10 + random.nextInt(20), 
random.nextInt(3) - 1, random.nextInt(3) - 1);
}

// 添加矿井支撑结构
for (int x = startX - 1; x <= startX + 1; x++) {
for (int z = startZ - 1; z <= startZ + 1; z++) {
for (int y = startY - 5; y < startY; y++) {
if (x >= 0 && x < WORLD_SIZE && y >= 0 && y < WORLD_HEIGHT && z >= 0 && z < WORLD_SIZE) {
if (world[x][y][z] == AIR) {
world[x][y][z] = WOOD;
}
}
}
}
}
}

    private void generateMineTunnel(int startX, int startY, int startZ, int length, int dirX, int dirZ) {
int x = startX;
int y = startY;
int z = startZ;

for (int i = 0; i < length; i++) {
// 创建3x3隧道
for (int dx = -1; dx <= 1; dx++) {
for (int dy = -1; dy <= 1; dy++) {
for (int dz = -1; dz <= 1; dz++) {
int nx = x + dx;
int ny = y + dy;
int nz = z + dz;

if (nx >= 0 && nx < WORLD_SIZE && 
ny >= 0 && ny < WORLD_HEIGHT && 
nz >= 0 && nz < WORLD_SIZE) {
world[nx][ny][nz] = AIR;

// 每隔一段距离添加支撑
if (dx == 0 && i % 5 == 0 && ny < y) {
world[nx][ny][nz] = WOOD;
}
}
}
}
}

// 移动隧道
x += dirX != 0 ? dirX : (random.nextInt(3) - 1);
y += random.nextInt(3) - 1;
z += dirZ != 0 ? dirZ : (random.nextInt(3) - 1);

// 确保不超出边界
x = Math.max(1, Math.min(WORLD_SIZE - 2, x));
y = Math.max(5, Math.min(WORLD_HEIGHT - 5, y));
z = Math.max(1, Math.min(WORLD_SIZE - 2, z));

// 随机放置火把
if (random.nextFloat() < 0.05f) {
world[x][y][z] = GLOWSTONE; // 用荧石代替火把
}
}
}

    private void generateTrees(int count) {
for (int i = 0; i < count; i++) {
int x = random.nextInt(WORLD_SIZE - 10) + 5;
int z = random.nextInt(WORLD_SIZE - 10) + 5;

// 找到地表高度
int y = 0;
for (int h = WORLD_HEIGHT - 1; h >= 0; h--) {
if (world[x][h][z] != AIR) {
y = h + 1;
break;
}
}

// 确保在地表上
if (y < 10 || y > WORLD_HEIGHT - 10) continue;

// 生成树干(4-6格高)
int trunkHeight = 4 + random.nextInt(3);
for (int h = 0; h < trunkHeight; h++) {
if (y + h < WORLD_HEIGHT) {
world[x][y + h][z] = WOOD;
}
}

// 生成树叶(球形树冠)
int topY = y + trunkHeight;
for (int dx = -2; dx <= 2; dx++) {
for (int dz = -2; dz <= 2; dz++) {
for (int dy = -1; dy <= 2; dy++) {
int nx = x + dx;
int nz = z + dz;
int ny = topY + dy;

// 跳过树干位置
if (dx == 0 && dz == 0 && (dy == 0 || dy == -1)) continue;

// 距离树冠中心的距离
float dist = (float) Math.sqrt(dx*dx + dz*dz + dy*dy);

if (nx >= 0 && nx < WORLD_SIZE && 
nz >= 0 && nz < WORLD_SIZE && 
ny >= 0 && ny < WORLD_HEIGHT && 
dist <= 2.5f) {
world[nx][ny][nz] = LEAVES;
}
}
}
}
}
}

    private void generateVillages(int count) {
for (int i = 0; i < count; i++) {
int centerX = random.nextInt(WORLD_SIZE - 30) + 15;
int centerZ = random.nextInt(WORLD_SIZE - 30) + 15;

villages.add(new int[]{centerX, centerZ});

// 找到地表高度
int centerY = 0;
for (int h = WORLD_HEIGHT - 1; h >= 0; h--) {
if (world[centerX][h][centerZ] != AIR) {
centerY = h + 1;
break;
}
}

// 村庄中心广场
createSquare(centerX, centerY, centerZ, 10, PLANKS);

// 生成房屋
for (int j = 0; j < 4 + random.nextInt(3); j++) {
int angle = random.nextInt(360);
int distance = 10 + random.nextInt(10);

int houseX = centerX + (int)(Math.cos(Math.toRadians(angle)) * distance);
int houseZ = centerZ + (int)(Math.sin(Math.toRadians(angle)) * distance);

// 找到地表高度
int houseY = 0;
for (int h = WORLD_HEIGHT - 1; h >= 0; h--) {
if (world[houseX][h][houseZ] != AIR) {
houseY = h + 1;
break;
}
}

generateHouse(houseX, houseY, houseZ);
}

// 生成铁匠铺
int blacksmithAngle = random.nextInt(360);
int blacksmithDistance = 8 + random.nextInt(5);
int blacksmithX = centerX + (int)(Math.cos(Math.toRadians(blacksmithAngle)) * blacksmithDistance);
int blacksmithZ = centerZ + (int)(Math.sin(Math.toRadians(blacksmithAngle)) * blacksmithDistance);

// 找到地表高度
int blacksmithY = 0;
for (int h = WORLD_HEIGHT - 1; h >= 0; h--) {
if (world[blacksmithX][h][blacksmithZ] != AIR) {
blacksmithY = h + 1;
break;
}
}

generateBlacksmith(blacksmithX, blacksmithY, blacksmithZ);
}
}

    private void generateHouse(int x, int y, int z) {
// 地基
for (int dx = -3; dx <= 3; dx++) {
for (int dz = -3; dz <= 3; dz++) {
setBlockIfAir(x + dx, y - 1, z + dz, COBBLESTONE);
}
}

// 墙壁
for (int dy = 0; dy < 4; dy++) {
for (int dx = -3; dx <= 3; dx++) {
setBlockIfAir(x + dx, y + dy, z - 3, PLANKS);
setBlockIfAir(x + dx, y + dy, z + 3, PLANKS);
}
for (int dz = -3; dz <= 3; dz++) {
setBlockIfAir(x - 3, y + dy, z + dz, PLANKS);
setBlockIfAir(x + 3, y + dy, z + dz, PLANKS);
}
}

// 屋顶
for (int dx = -2; dx <= 2; dx++) {
for (int dz = -2; dz <= 2; dz++) {
setBlockIfAir(x + dx, y + 4, z + dz, WOOD);
}
}

// 门
world[x][y][z - 3] = AIR;
world[x][y + 1][z - 3] = AIR;

// 窗户
world[x - 3][y + 1][z] = AIR;
world[x + 3][y + 1][z] = AIR;
world[x][y + 1][z + 3] = AIR;

// 随机添加附魔台
if (random.nextFloat() < 0.2f) {
world[x][y + 1][z] = ENCHANTING_TABLE;
// 创建附魔效果
createEnchantmentEffect(x + 0.5f, y + 1.5f, z + 0.5f, random.nextInt(3));
}
}

    private void generateBlacksmith(int x, int y, int z) {
// 地基
for (int dx = -4; dx <= 4; dx++) {
for (int dz = -4; dz <= 4; dz++) {
setBlockIfAir(x + dx, y - 1, z + dz, COBBLESTONE);
}
}

// 墙壁
for (int dy = 0; dy < 5; dy++) {
for (int dx = -4; dx <= 4; dx++) {
setBlockIfAir(x + dx, y + dy, z - 4, COBBLESTONE);
setBlockIfAir(x + dx, y + dy, z + 4, COBBLESTONE);
}
for (int dz = -4; dz <= 4; dz++) {
setBlockIfAir(x - 4, y + dy, z + dz, COBBLESTONE);
setBlockIfAir(x + 4, y + dy, z + dz, COBBLESTONE);
}
}

// 屋顶
for (int dx = -3; dx <= 3; dx++) {
for (int dz = -3; dz <= 3; dz++) {
setBlockIfAir(x + dx, y + 5, z + dz, WOOD);
}
}

// 门
world[x][y][z - 4] = AIR;
world[x][y + 1][z - 4] = AIR;

// 窗户
world[x - 4][y + 1][z] = AIR;
world[x + 4][y + 1][z] = AIR;
world[x][y + 1][z + 4] = AIR;

// 熔炉
world[x - 2][y + 1][z - 2] = COBBLESTONE;
world[x - 2][y + 2][z - 2] = COBBLESTONE;

// 铁砧
world[x + 2][y + 1][z + 2] = ORE_IRON;
}

    private void generatePillagerOutposts(int count) {
for (int i = 0; i < count; i++) {
int x = random.nextInt(WORLD_SIZE - 20) + 10;
int z = random.nextInt(WORLD_SIZE - 20) + 10;

pillagerOutposts.add(new int[]{x, z});

// 找到地表高度
int y = 0;
for (int h = WORLD_HEIGHT - 1; h >= 0; h--) {
if (world[x][h][z] != AIR) {
y = h + 1;
break;
}
}

// 地基
for (int dx = -4; dx <= 4; dx++) {
for (int dz = -4; dz <= 4; dz++) {
setBlockIfAir(x + dx, y - 1, z + dz, COBBLESTONE);
}
}

// 塔身
int towerHeight = 10 + random.nextInt(5);
for (int dy = 0; dy < towerHeight; dy++) {
// 塔基
for (int dx = -2; dx <= 2; dx++) {
for (int dz = -2; dz <= 2; dz++) {
if (Math.abs(dx) == 2 || Math.abs(dz) == 2) {
setBlockIfAir(x + dx, y + dy, z + dz, COBBLESTONE);
}
}
}

// 塔楼平台(每3层一个)
if (dy % 3 == 0 && dy > 0) {
for (int dx = -3; dx <= 3; dx++) {
for (int dz = -3; dz <= 3; dz++) {
if (Math.abs(dx) == 3 || Math.abs(dz) == 3) {
setBlockIfAir(x + dx, y + dy, z + dz, WOOD);
}
}
}
}
}

// 塔顶
for (int dx = -3; dx <= 3; dx++) {
for (int dz = -3; dz <= 3; dz++) {
if (Math.abs(dx) <= 1 && Math.abs(dz) <= 1) {
setBlockIfAir(x + dx, y + towerHeight, z + dz, WOOD);
}
}
}

// 塔顶旗帜
for (int dy = 1; dy <= 3; dy++) {
setBlockIfAir(x, y + towerHeight + dy, z, WOOD);
}

// 添加掠夺者(用黑色羊毛表示)
setBlockIfAir(x, y + towerHeight, z, ORE_COAL);
}
}

    private void generateAncientCities(int count) {
for (int i = 0; i < count; i++) {
int centerX = random.nextInt(WORLD_SIZE - 40) + 20;
int centerZ = random.nextInt(WORLD_SIZE - 40) + 20;
int centerY = 20 + random.nextInt(20); // 地下城市

ancientCities.add(new int[]{centerX, centerY, centerZ});
generateAncientCity(centerX, centerY, centerZ);
}
}

    private void generateAncientCity(int centerX, int centerY, int centerZ) {
// 清除一个大区域
int cityRadius = 15;
for (int x = centerX - cityRadius; x <= centerX + cityRadius; x++) {
for (int z = centerZ - cityRadius; z <= centerZ + cityRadius; z++) {
for (int y = centerY - 5; y <= centerY + 10; y++) {
if (x >= 0 && x < WORLD_SIZE && y >= 0 && y < WORLD_HEIGHT && z >= 0 && z < WORLD_SIZE) {
world[x][y][z] = AIR;
}
}
}
}

// 创建城市地板
for (int x = centerX - cityRadius; x <= centerX + cityRadius; x++) {
for (int z = centerZ - cityRadius; z <= centerZ + cityRadius; z++) {
if (x >= 0 && x < WORLD_SIZE && z >= 0 && z < WORLD_SIZE) {
world[x][centerY - 1][z] = DEEPSLATE;
}
}
}

// 创建中央宫殿
generateAncientPalace(centerX, centerY, centerZ);

// 创建周围建筑
for (int i = 0; i < 6; i++) {
double angle = 2 * Math.PI * i / 6;
int buildingX = centerX + (int)(Math.cos(angle) * 10);
int buildingZ = centerZ + (int)(Math.sin(angle) * 10);

generateAncientBuilding(buildingX, centerY, buildingZ);
}

// 添加幽匿系列方块
for (int i = 0; i < 20; i++) {
int x = centerX + random.nextInt(cityRadius * 2) - cityRadius;
int z = centerZ + random.nextInt(cityRadius * 2) - cityRadius;

if (x >= 0 && x < WORLD_SIZE && z >= 0 && z < WORLD_SIZE) {
int type = 19 + random.nextInt(4); // SCULK 到 SCULK_SHRIEKER
world[x][centerY][z] = type;

// 如果是幽匿催发体,创建附魔效果
if (type == SCULK_CATALYST) {
createEnchantmentEffect(x + 0.5f, centerY + 0.5f, z + 0.5f, 2);
}
}
}

// 添加远古城市的中心传送门框架(用黑曜石表示)
for (int dx = -1; dx <= 1; dx++) {
for (int dz = -1; dz <= 1; dz++) {
if (Math.abs(dx) == 1 && Math.abs(dz) == 1) {
world[centerX + dx][centerY][centerZ + dz] = OBSIDIAN;
}
}
}
}

    private void generateAncientPalace(int x, int y, int z) {
// 宫殿基座
for (int dx = -5; dx <= 5; dx++) {
for (int dz = -5; dz <= 5; dz++) {
world[x + dx][y - 1][z + dz] = DEEPSLATE;
}
}

// 宫殿柱子
for (int dx = -4; dx <= 4; dx += 4) {
for (int dz = -4; dz <= 4; dz += 4) {
for (int dy = 0; dy < 6; dy++) {
world[x + dx][y + dy][z + dz] = DEEPSLATE;
}
}
}

// 宫殿屋顶
for (int dy = 0; dy < 3; dy++) {
int size = 5 - dy;
for (int dx = -size; dx <= size; dx++) {
for (int dz = -size; dz <= size; dz++) {
if (Math.abs(dx) == size || Math.abs(dz) == size) {
world[x + dx][y + 6 + dy][z + dz] = DEEPSLATE;
}
}
}
}

// 宫殿内部
world[x][y][z] = ENCHANTING_TABLE;
createEnchantmentEffect(x + 0.5f, y + 1.0f, z + 0.5f, 1);
}

    private void generateAncientBuilding(int x, int y, int z) {
// 建筑基座
for (int dx = -2; dx <= 2; dx++) {
for (int dz = -2; dz <= 2; dz++) {
world[x + dx][y - 1][z + dz] = DEEPSLATE;
}
}

// 建筑墙壁
for (int dy = 0; dy < 4; dy++) {
for (int dx = -2; dx <= 2; dx++) {
world[x + dx][y + dy][z - 2] = DEEPSLATE;
world[x + dx][y + dy][z + 2] = DEEPSLATE;
}
for (int dz = -2; dz <= 2; dz++) {
world[x - 2][y + dy][z + dz] = DEEPSLATE;
world[x + 2][y + dy][z + dz] = DEEPSLATE;
}
}

// 建筑屋顶
for (int dx = -1; dx <= 1; dx++) {
for (int dz = -1; dz <= 1; dz++) {
world[x + dx][y + 4][z + dz] = DEEPSLATE;
}
}

// 建筑入口
world[x][y][z - 2] = AIR;
world[x][y + 1][z - 2] = AIR;

// 建筑内部(随机放置宝箱或幽匿方块)
if (random.nextBoolean()) {
world[x][y + 1][z] = CHEST; // 用某种方块表示宝箱
} else {
int sculkType = 19 + random.nextInt(4);
world[x][y + 1][z] = sculkType;

if (sculkType == SCULK_CATALYST) {
createEnchantmentEffect(x + 0.5f, y + 1.5f, z + 0.5f, 2);
}
}
}

    private void createEnchantmentEffect(float x, float y, float z, int type) {
enchantmentEffects.get("particles").add(new EnchantmentEffect(x, y, z, type));
}

    private void renderEnchantmentEffects() {
List<EnchantmentEffect> particles = enchantmentEffects.get("particles");
Iterator<EnchantmentEffect> iterator = particles.iterator();

while (iterator.hasNext()) {
EnchantmentEffect effect = iterator.next();

if (effect.isExpired()) {
iterator.remove();
continue;
}

float alpha = effect.getAlpha();
float size = effect.size;

glPushMatrix();
glTranslatef(effect.x, effect.y, effect.z);

// 根据类型设置颜色
switch (effect.type) {
case 0: // 紫色附魔效果
glColor4f(0.5f, 0.2f, 0.8f, alpha);
break;
case 1: // 蓝色附魔效果
glColor4f(0.2f, 0.4f, 0.8f, alpha);
break;
case 2: // 绿色幽匿效果
glColor4f(0.2f, 0.6f, 0.4f, alpha);
break;
}

// 绘制效果粒子
glBegin(GL_QUADS);
glVertex3f(-size, -size, 0);
glVertex3f(size, -size, 0);
glVertex3f(size, size, 0);
glVertex3f(-size, size, 0);
glEnd();

glPopMatrix();
}
}

    private void setBlockIfAir(int x, int y, int z, int blockType) {
if (x >= 0 && x < WORLD_SIZE && 
y >= 0 && y < WORLD_HEIGHT && 
z >= 0 && z < WORLD_SIZE && 
world[x][y][z] == AIR) {
world[x][y][z] = blockType;
}
}

    private void createSquare(int x, int y, int z, int size, int blockType) {
for (int dx = -size; dx <= size; dx++) {
for (int dz = -size; dz <= size; dz++) {
if (x + dx >= 0 && x + dx < WORLD_SIZE && 
z + dz >= 0 && z + dz < WORLD_SIZE) {
setBlockIfAir(x + dx, y, z + dz, blockType);
}
}
}
}

    private float noise(float x, float z) {
return (float) Math.sin(x * 0.1) * (float) Math.cos(z * 0.1) + random.nextFloat() * 0.2f;
}

    private void loop() {
// 设置光标位置
glfwSetCursorPos(window, WIDTH / 2.0, HEIGHT / 2.0);
glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED);

// 主循环
while (!glfwWindowShouldClose(window)) {
// 处理输入
processInput();

// 清除颜色和深度缓冲
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

// 设置投影矩阵
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
float aspect = (float) WIDTH / HEIGHT;
gluPerspective(60.0f, aspect, 0.1f, 300.0f);

// 设置模型视图矩阵
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

// 计算摄像机方向
float yawRad = (float) Math.toRadians(cameraYaw);
float pitchRad = (float) Math.toRadians(cameraPitch);

float lookX = (float) (Math.cos(yawRad) * Math.cos(pitchRad));
float lookY = (float) Math.sin(pitchRad);
float lookZ = (float) (Math.sin(yawRad) * Math.cos(pitchRad));

// 设置摄像机位置和方向
gluLookAt(
cameraX, cameraY, cameraZ,
cameraX + lookX, cameraY + lookY, cameraZ + lookZ,
0.0f, 1.0f, 0.0f
);

// 渲染世界
renderWorld();

// 渲染附魔效果
renderEnchantmentEffects();

// 显示信息
renderInfo();

// 交换缓冲
glfwSwapBuffers(window);
// 轮询事件
glfwPollEvents();
}
}

    private void processInput() {
float cameraSpeed = 0.2f;

if (glfwGetKey(window, GLFW_KEY_W) == GLFW_PRESS) {
cameraX += Math.sin(Math.toRadians(cameraYaw)) * cameraSpeed;
cameraZ -= Math.cos(Math.toRadians(cameraYaw)) * cameraSpeed;
}
if (glfwGetKey(window, GLFW_KEY_S) == GLFW_PRESS) {
cameraX -= Math.sin(Math.toRadians(cameraYaw)) * cameraSpeed;
cameraZ += Math.cos(Math.toRadians(cameraYaw)) * cameraSpeed;
}
if (glfwGetKey(window, GLFW_KEY_A) == GLFW_PRESS) {
cameraX -= Math.cos(Math.toRadians(cameraYaw)) * cameraSpeed;
cameraZ -= Math.sin(Math.toRadians(cameraYaw)) * cameraSpeed;
}
if (glfwGetKey(window, GLFW_KEY_D) == GLFW_PRESS) {
cameraX += Math.cos(Math.toRadians(cameraYaw)) * cameraSpeed;
cameraZ += Math.sin(Math.toRadians(cameraYaw)) * cameraSpeed;
}
if (glfwGetKey(window, GLFW_KEY_SPACE) == GLFW_PRESS) {
cameraY += cameraSpeed;
}
if (glfwGetKey(window, GLFW_KEY_LEFT_SHIFT) == GLFW_PRESS) {
cameraY -= cameraSpeed;
}
}

    private void renderWorld() {
// 只渲染玩家附近的区块
int renderDistance = 12;
int playerChunkX = (int) cameraX / CHUNK_SIZE;
int playerChunkZ = (int) cameraZ / CHUNK_SIZE;

for (int cx = playerChunkX - renderDistance; cx <= playerChunkX + renderDistance; cx++) {
for (int cz = playerChunkZ - renderDistance; cz <= playerChunkZ + renderDistance; cz++) {
if (cx < 0 || cz < 0 || cx >= WORLD_SIZE / CHUNK_SIZE || cz >= WORLD_SIZE / CHUNK_SIZE) {
continue;
}

// 渲染区块
for (int x = cx * CHUNK_SIZE; x < (cx + 1) * CHUNK_SIZE; x++) {
for (int z = cz * CHUNK_SIZE; z < (cz + 1) * CHUNK_SIZE; z++) {
for (int y = 0; y < WORLD_HEIGHT; y++) {
int block = world[x][y][z];
if (block != AIR) {
renderBlock(x, y, z, block);
}
}
}
}
}
}
}

    private void renderBlock(int x, int y, int z, int blockType) {
float[] color = blockColors.get(blockType);
if (color == null) return;

if (blockType == WATER) {
glColor4f(color[0], color[1], color[2], color[3]);
} else {
glColor3f(color[0], color[1], color[2]);
}

// 绘制方块
glPushMatrix();
glTranslatef(x, y, z);

// 绘制立方体的六个面
glBegin(GL_QUADS);

// 顶面(稍微亮一点)
if (blockType != WATER) {
glColor3f(color[0] * 1.2f, color[1] * 1.2f, color[2] * 1.2f);
}
glVertex3f(0, 1, 0);
glVertex3f(1, 1, 0);
glVertex3f(1, 1, 1);
glVertex3f(0, 1, 1);

if (blockType != WATER) {
glColor3f(color[0], color[1], color[2]);
}

// 底面
glVertex3f(0, 0, 1);
glVertex3f(1, 0, 1);
glVertex3f(1, 0, 0);
glVertex3f(0, 0, 0);

// 前面
glVertex3f(0, 0, 1);
glVertex3f(1, 0, 1);
glVertex3f(1, 1, 1);
glVertex3f(0, 1, 1);

// 后面
glVertex3f(1, 0, 0);
glVertex3f(0, 0, 0);
glVertex3f(0, 1, 0);
glVertex3f(1, 1, 0);

// 左面(稍微暗一点)
if (blockType != WATER) {
glColor3f(color[0] * 0.8f, color[1] * 0.8f, color[2] * 0.8f);
}
glVertex3f(0, 0, 0);
glVertex3f(0, 0, 1);
glVertex3f(0, 1, 1);
glVertex3f(0, 1, 0);

if (blockType != WATER) {
glColor3f(color[0], color[1], color[2]);
}

// 右面(稍微暗一点)
if (blockType != WATER) {
glColor3f(color[0] * 0.8f, color[1] * 0.8f, color[2] * 0.8f);
}
glVertex3f(1, 0, 1);
glVertex3f(1, 0, 0);
glVertex3f(1, 1, 0);
glVertex3f(1, 1, 1);

glEnd();
glPopMatrix();

// 如果是发光方块,添加发光效果
if (glowingBlocks.contains(blockType)) {
renderGlowEffect(x, y, z, blockType);
}
}

    private void renderGlowEffect(int x, int y, int z, int blockType) {
glPushMatrix();
glTranslatef(x + 0.5f, y + 0.5f, z + 0.5f);

// 根据方块类型设置发光颜色
float[] color;
switch (blockType) {
case GLOWSTONE:
color = new float[]{0.9f, 0.8f, 0.4f, 0.3f};
break;
case ENCHANTING_TABLE:
color = new float[]{0.5f, 0.2f, 0.8f, 0.4f};
break;
case SCULK_CATALYST:
color = new float[]{0.2f, 0.6f, 0.4f, 0.4f};
break;
case SCULK_SENSOR:
color = new float[]{0.3f, 0.7f, 0.5f, 0.3f};
break;
default:
color = new float[]{1.0f, 1.0f, 1.0f, 0.3f};
}

glColor4f(color[0], color[1], color[2], color[3]);

// 绘制发光球体
glutSolidSphere(0.6f, 16, 16);

glPopMatrix();
}

    private void renderInfo() {
// 切换到正交投影以渲染文本
glMatrixMode(GL_PROJECTION);
glPushMatrix();
glLoadIdentity();
glOrtho(0, WIDTH, HEIGHT, 0, -1, 1);

glMatrixMode(GL_MODELVIEW);
glPushMatrix();
glLoadIdentity();

// 禁用深度测试以便文本显示在最前面
glDisable(GL_DEPTH_TEST);

// 绘制信息面板背景
glColor4f(0.1f, 0.1f, 0.1f, 0.7f);
glBegin(GL_QUADS);
glVertex2f(10, 10);
glVertex2f(350, 10);
glVertex2f(350, 160);
glVertex2f(10, 160);
glEnd();

// 设置文本颜色
glColor3f(1.0f, 1.0f, 1.0f);

// 绘制文本
drawString(20, 30, "Advanced Minecraft World Generator");
drawString(20, 50, "Position: " + String.format("%.1f, %.1f, %.1f", cameraX, cameraY, cameraZ));
drawString(20, 70, "Villages: " + villages.size());
drawString(20, 90, "Pillager Outposts: " + pillagerOutposts.size());
drawString(20, 110, "Mine Shafts: " + mineShafts.size());
drawString(20, 130, "Ancient Cities: " + ancientCities.size());
drawString(20, 150, "Press E to create enchantment effect");

// 恢复设置
glEnable(GL_DEPTH_TEST);
glMatrixMode(GL_PROJECTION);
glPopMatrix();
glMatrixMode(GL_MODELVIEW);
glPopMatrix();
}

    private void drawString(int x, int y, String text) {
// 由于我们没有GLUT的文本渲染功能,这里使用简单的点绘制文本
// 实际应用中应该使用纹理字体或更高级的文本渲染方法
glWindowPos2i(x, y);
for (char c : text.toCharArray()) {
// 简化文本渲染 - 实际应用中应该使用更好的方法
glutBitmapCharacter(GLUT_BITMAP_9_BY_15, c);
}
}

// 简化版本的GLUT函数
private void glutSolidSphere(float radius, int slices, int stacks) {
// 实际实现需要更复杂的球体绘制代码
// 这里使用一个立方体作为替代
glBegin(GL_QUADS);
glVertex3f(-radius, -radius, -radius);
glVertex3f(radius, -radius, -radius);
glVertex3f(radius, radius, -radius);
glVertex3f(-radius, radius, -radius);

glVertex3f(-radius, -radius, radius);
glVertex3f(radius, -radius, radius);
glVertex3f(radius, radius, radius);
glVertex3f(-radius, radius, radius);

glVertex3f(-radius, -radius, -radius);
glVertex3f(-radius, radius, -radius);
glVertex3f(-radius, radius, radius);
glVertex3f(-radius, -radius, radius);

glVertex3f(radius, -radius, -radius);
glVertex3f(radius, radius, -radius);
glVertex3f(radius, radius, radius);
glVertex3f(radius, -radius, radius);

glVertex3f(-radius, -radius, -radius);
glVertex3f(radius, -radius, -radius);
glVertex3f(radius, -radius, radius);
glVertex3f(-radius, -radius, radius);

glVertex3f(-radius, radius, -radius);
glVertex3f(radius, radius, -radius);
glVertex3f(radius, radius, radius);
glVertex3f(-radius, radius, radius);
glEnd();
}

private void glutBitmapCharacter(int font, char c) {
// 简化版本的字符渲染
// 实际应用中应该使用纹理字体
glPointSize(2);
glBegin(GL_POINTS);
for (int i = 0; i < 10; i++) {
glVertex2i(i, 0);
}
glEnd();
}
}

关于制作简易版minecraft还我还有c++版,还有python版。

大家感兴趣的可以去查看一下。

由于后续的更新代码会越来越长,所以更新的时间会越来越长。

 

http://www.dtcms.com/a/343149.html

相关文章:

  • 什么是默克尔树
  • Android系统框架知识系列(十三):Sensor Manager Service - Android的感官世界
  • Trae配置rules与MCP
  • 企业微信+AI在金融行业落地:从部署到场景的实践路径
  • CLruCache::BucketFromIdentifier函数分析
  • CroCT
  • 在互联网大厂的Java面试:谢飞机的搞笑历险记
  • Uniapp非脚手架项目打包为5+ App后,在Android端按返回键会意外退出应用。
  • 基于昇腾玩转电影级视频生成模型Wan 2.2
  • ES_索引的操作
  • 基础网络模型
  • 【矩池云】实现Pycharm远程连接,上传数据并解压缩
  • 为什么程序部署到线上,就无法读取环境变量了
  • B2B工业品制造业TOB大客户营销培训老师培训师唐兴通谈AI数字化销售AI销冠底层逻辑数字化转型创新增长业绩
  • MyBatis-Plus MetaObjectHandler的几个坑(主要是id字段)
  • 《AI智脉速递》2025 年 8 月15 日 - 21 日
  • JetBrains 内的 GitHub Copilot Agent Mode + MCP:从配置到实战
  • vmware安装centos7
  • 深入理解Java虚拟机:JVM高级特性与最佳实践(第3版)第二章知识点问答(21题)
  • A股大盘数据-20250821 分析
  • 领域驱动中IUnitOfWork是干什么的
  • 【StarRocks】-- SQL CTE 语法
  • 机器学习中的集成算法与 k 均值聚类算法概述
  • 机器学习5
  • 解决办法:Chrome插件不能用,这些扩展程序不再受支持,因此已停用
  • 动态寻北仪如何在矿用掘进机中进行应用?
  • 用Vue2和Echarts画图的基本流程
  • AI升级社区便民服务:AI办事小程序高效办证+应急系统秒响应,告别跑腿愁住得更安心
  • K8s快速上手-微服务篇
  • AI资深 Java 研发专家系统解析Java 中常见的 Queue实现类