這一次主要是紅石和創造模式的新加入由于代碼較長,所以呃這一段代碼就直接勞煩各位呃插進之前的3.0版本里面!!!!!!!!!
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 AdvancedMinecraftSimulator {
// 窗口尺寸
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 = 64;
private static final int CHUNK_SIZE = 16;
private static final int WORLD_HEIGHT = 64;
private int[][][] world = new int[WORLD_SIZE][WORLD_HEIGHT][WORLD_SIZE];
// 游戲模式
private enum GameMode { SURVIVAL, CREATIVE }
private GameMode currentGameMode = GameMode.CREATIVE;
// 方塊類型
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 GLASS = 9;
private static final int REDSTONE_DUST = 10;
private static final int REDSTONE_TORCH = 11;
private static final int REDSTONE_BLOCK = 12;
private static final int REDSTONE_REPEATER = 13;
private static final int REDSTONE_COMPARATOR = 14;
private static final int REDSTONE_LAMP = 15;
private static final int STICKY_PISTON = 16;
private static final int PISTON = 17;
private static final int OBSIDIAN = 18;
private static final int DIAMOND_BLOCK = 19;
private static final int GOLD_BLOCK = 20;
private static final int IRON_BLOCK = 21;
// 紅石電路狀態
private boolean[][][] redstonePower = new boolean[WORLD_SIZE][WORLD_HEIGHT][WORLD_SIZE];
private int[][][] redstoneSignalStrength = new int[WORLD_SIZE][WORLD_HEIGHT][WORLD_SIZE];
// 方塊顏色映射
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(GLASS, new float[]{0.8f, 0.9f, 1.0f, 0.3f});
blockColors.put(REDSTONE_DUST, new float[]{0.8f, 0.1f, 0.1f});
blockColors.put(REDSTONE_TORCH, new float[]{0.9f, 0.2f, 0.2f});
blockColors.put(REDSTONE_BLOCK, new float[]{0.9f, 0.1f, 0.1f});
blockColors.put(REDSTONE_REPEATER, new float[]{0.7f, 0.5f, 0.5f});
blockColors.put(REDSTONE_COMPARATOR, new float[]{0.7f, 0.6f, 0.5f});
blockColors.put(REDSTONE_LAMP, new float[]{0.9f, 0.8f, 0.4f});
blockColors.put(STICKY_PISTON, new float[]{0.6f, 0.6f, 0.6f});
blockColors.put(PISTON, new float[]{0.7f, 0.7f, 0.7f});
blockColors.put(OBSIDIAN, new float[]{0.15f, 0.07f, 0.2f});
blockColors.put(DIAMOND_BLOCK, new float[]{0.3f, 0.8f, 0.8f});
blockColors.put(GOLD_BLOCK, new float[]{1.0f, 0.8f, 0.0f});
blockColors.put(IRON_BLOCK, new float[]{0.8f, 0.8f, 0.9f});
}
// 當前選擇的方塊(創造模式)
private int selectedBlock = GRASS;
private List<Integer> creativeInventory = Arrays.asList(
GRASS, DIRT, STONE, WOOD, LEAVES, PLANKS, COBBLESTONE,?
GLASS, REDSTONE_DUST, REDSTONE_TORCH, REDSTONE_BLOCK,
REDSTONE_REPEATER, REDSTONE_LAMP, STICKY_PISTON, PISTON,
DIAMOND_BLOCK, GOLD_BLOCK, IRON_BLOCK
);
private int inventoryIndex = 0;
// 紅石電路更新計時器
private long lastRedstoneUpdate = 0;
private static final long REDSTONE_UPDATE_INTERVAL = 100; // 毫秒
// 活塞狀態
private Map<String, PistonState> pistonStates = new HashMap<>();
private class PistonState {
int x, y, z;
boolean extended;
long lastUpdate;
PistonState(int x, int y, int z) {
this.x = x;
this.y = y;
this.z = z;
this.extended = false;
this.lastUpdate = System.currentTimeMillis();
}
}
? ? public static void main(String[] args) {
new AdvancedMinecraftSimulator().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, "Minecraft Simulator - Creative Mode + Redstone", 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_C && action == GLFW_RELEASE) {
// 切換游戲模式
currentGameMode = (currentGameMode == GameMode.SURVIVAL) ? GameMode.CREATIVE : GameMode.SURVIVAL;
}
if (key == GLFW_KEY_R && action == GLFW_RELEASE) {
// 切換選擇的方塊(創造模式)
inventoryIndex = (inventoryIndex + 1) % creativeInventory.size();
selectedBlock = creativeInventory.get(inventoryIndex);
}
if (key == GLFW_KEY_T && action == GLFW_RELEASE) {
// 激活紅石電路
activateRedstoneAtPlayer();
}
});
// 設置光標位置回調
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);
});
// 設置鼠標點擊回調
glfwSetMouseButtonCallback(window, (window, button, action, mods) -> {
if (button == GLFW_MOUSE_BUTTON_LEFT && action == GLFW_PRESS) {
handleBlockInteraction(true); // 破壞方塊
}
if (button == GLFW_MOUSE_BUTTON_RIGHT && action == GLFW_PRESS) {
handleBlockInteraction(false); // 放置方塊
}
});
// 獲取分辨率
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();
// 初始化紅石電路
initializeRedstoneCircuits();
}
? ? private void generateWorld() {
Random random = new Random(12345);
// 生成基礎地形
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, random) * 10;
height += noise(x * 0.05f, z * 0.05f, random) * 20;
height += noise(x * 0.02f, z * 0.02f, random) * 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) 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;
}
}
}
}
}
// 生成一些樹木
generateTrees(20, random);
// 生成一個紅石演示結構
createRedstoneDemoStructure();
}
? ? private void initializeRedstoneCircuits() {
// 初始化紅石信號強度
for (int x = 0; x < WORLD_SIZE; x++) {
for (int y = 0; y < WORLD_HEIGHT; y++) {
for (int z = 0; z < WORLD_SIZE; z++) {
redstoneSignalStrength[x][y][z] = 0;
redstonePower[x][y][z] = false;
}
}
}
}
? ? private void createRedstoneDemoStructure() {
// 創建一個簡單的紅石電路演示
int centerX = WORLD_SIZE / 2;
int centerZ = WORLD_SIZE / 2;
int groundY = 20;
// 紅石火把
world[centerX][groundY + 1][centerZ] = REDSTONE_TORCH;
redstonePower[centerX][groundY + 1][centerZ] = true;
redstoneSignalStrength[centerX][groundY + 1][centerZ] = 15;
// 紅石線
for (int i = 1; i <= 5; i++) {
world[centerX + i][groundY][centerZ] = REDSTONE_DUST;
}
// 紅石燈
world[centerX + 6][groundY][centerZ] = REDSTONE_LAMP;
// 粘性活塞
world[centerX + 3][groundY + 1][centerZ] = STICKY_PISTON;
String pistonKey = centerX + 3 + "," + (groundY + 1) + "," + centerZ;
pistonStates.put(pistonKey, new PistonState(centerX + 3, groundY + 1, centerZ));
}
? ? private void generateTrees(int count, Random random) {
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;
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 handleBlockInteraction(boolean destroy) {
// 計算玩家視線方向
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));
// 射線檢測尋找目標方塊
float rayX = cameraX;
float rayY = cameraY;
float rayZ = cameraZ;
for (int i = 0; i < 10; i++) {
int blockX = (int) rayX;
int blockY = (int) rayY;
int blockZ = (int) rayZ;
if (blockX >= 0 && blockX < WORLD_SIZE &&?
blockY >= 0 && blockY < WORLD_HEIGHT &&?
blockZ >= 0 && blockZ < WORLD_SIZE) {
if (world[blockX][blockY][blockZ] != AIR) {
if (destroy) {
// 破壞方塊
if (currentGameMode == GameMode.CREATIVE || isBreakable(world[blockX][blockY][blockZ])) {
world[blockX][blockY][blockZ] = AIR;
updateRedstoneAround(blockX, blockY, blockZ);
}
} else {
// 放置方塊
if (currentGameMode == GameMode.CREATIVE) {
// 計算放置位置(相鄰的空格)
int placeX = blockX + (int) Math.signum(lookX);
int placeY = blockY + (int) Math.signum(lookY);
int placeZ = blockZ + (int) Math.signum(lookZ);
if (placeX >= 0 && placeX < WORLD_SIZE &&?
placeY >= 0 && placeY < WORLD_HEIGHT &&?
placeZ >= 0 && placeZ < WORLD_SIZE &&?
world[placeX][placeY][placeZ] == AIR) {
world[placeX][placeY][placeZ] = selectedBlock;
// 如果是紅石組件,初始化狀態
if (isRedstoneComponent(selectedBlock)) {
initializeRedstoneComponent(placeX, placeY, placeZ, selectedBlock);
}
}
}
}
break;
}
}
rayX += lookX * 0.5f;
rayY += lookY * 0.5f;
rayZ += lookZ * 0.5f;
}
}
? ? private boolean isBreakable(int blockType) {
// 在生存模式下,某些方塊不能被破壞
return blockType != OBSIDIAN && blockType != BEDROCK;
}
? ? private boolean isRedstoneComponent(int blockType) {
return blockType == REDSTONE_DUST || blockType == REDSTONE_TORCH ||?
blockType == REDSTONE_BLOCK || blockType == REDSTONE_REPEATER ||?
blockType == REDSTONE_COMPARATOR || blockType == REDSTONE_LAMP ||
blockType == STICKY_PISTON || blockType == PISTON;
}
? ? private void initializeRedstoneComponent(int x, int y, int z, int blockType) {
switch (blockType) {
case REDSTONE_TORCH:
redstonePower[x][y][z] = true;
redstoneSignalStrength[x][y][z] = 15;
break;
case REDSTONE_BLOCK:
redstonePower[x][y][z] = true;
redstoneSignalStrength[x][y][z] = 15;
break;
case REDSTONE_LAMP:
// 燈默認關閉
redstonePower[x][y][z] = false;
break;
case STICKY_PISTON:
case PISTON:
String pistonKey = x + "," + y + "," + z;
pistonStates.put(pistonKey, new PistonState(x, y, z));
break;
}
}
? ? private void activateRedstoneAtPlayer() {
int playerX = (int) cameraX;
int playerY = (int) cameraY;
int playerZ = (int) cameraZ;
// 激活玩家周圍的紅石組件
for (int dx = -3; dx <= 3; dx++) {
for (int dy = -3; dy <= 3; dy++) {
for (int dz = -3; dz <= 3; dz++) {
int x = playerX + dx;
int y = playerY + dy;
int z = playerZ + dz;
if (x >= 0 && x < WORLD_SIZE &&?
y >= 0 && y < WORLD_HEIGHT &&?
z >= 0 && z < WORLD_SIZE) {
if (world[x][y][z] == REDSTONE_TORCH) {
// 切換紅石火把狀態
redstonePower[x][y][z] = !redstonePower[x][y][z];
updateRedstoneCircuit();
} else if (world[x][y][z] == REDSTONE_BLOCK) {
// 激活紅石塊
redstonePower[x][y][z] = true;
updateRedstoneCircuit();
}
}
}
}
}
}
? ? private void updateRedstoneCircuit() {
long currentTime = System.currentTimeMillis();
if (currentTime - lastRedstoneUpdate < REDSTONE_UPDATE_INTERVAL) {
return;
}
lastRedstoneUpdate = currentTime;
// 更新紅石信號傳播
propagateRedstoneSignals();
// 更新紅石組件狀態
updateRedstoneComponents();
// 更新活塞狀態
updatePistons();
}
? ? private void propagateRedstoneSignals() {
// 簡單的紅石信號傳播算法
boolean[][][] newPower = new boolean[WORLD_SIZE][WORLD_HEIGHT][WORLD_SIZE];
int[][][] newStrength = new int[WORLD_SIZE][WORLD_HEIGHT][WORLD_SIZE];
// 復制當前狀態
for (int x = 0; x < WORLD_SIZE; x++) {
for (int y = 0; y < WORLD_HEIGHT; y++) {
for (int z = 0; z < WORLD_SIZE; z++) {
newPower[x][y][z] = redstonePower[x][y][z];
newStrength[x][y][z] = redstoneSignalStrength[x][y][z];
}
}
}
// 傳播信號
for (int x = 0; x < WORLD_SIZE; x++) {
for (int y = 0; y < WORLD_HEIGHT; y++) {
for (int z = 0; z < WORLD_SIZE; z++) {
if (world[x][y][z] == REDSTONE_DUST && redstonePower[x][y][z]) {
// 向六個方向傳播信號
propagateToNeighbor(x, y, z, x+1, y, z, newPower, newStrength);
propagateToNeighbor(x, y, z, x-1, y, z, newPower, newStrength);
propagateToNeighbor(x, y, z, x, y+1, z, newPower, newStrength);
propagateToNeighbor(x, y, z, x, y-1, z, newPower, newStrength);
propagateToNeighbor(x, y, z, x, y, z+1, newPower, newStrength);
propagateToNeighbor(x, y, z, x, y, z-1, newPower, newStrength);
}
}
}
}
// 更新狀態
redstonePower = newPower;
redstoneSignalStrength = newStrength;
}
? ? private void propagateToNeighbor(int sourceX, int sourceY, int sourceZ,?
int targetX, int targetY, int targetZ,
boolean[][][] newPower, int[][][] newStrength) {
if (targetX < 0 || targetX >= WORLD_SIZE ||?
targetY < 0 || targetY >= WORLD_HEIGHT ||?
targetZ < 0 || targetZ >= WORLD_SIZE) {
return;
}
int currentStrength = redstoneSignalStrength[sourceX][sourceY][sourceZ];
if (currentStrength <= 1) return;
int targetBlock = world[targetX][targetY][targetZ];
if (targetBlock == REDSTONE_DUST || targetBlock == REDSTONE_LAMP ||?
targetBlock == STICKY_PISTON || targetBlock == PISTON) {
if (currentStrength - 1 > newStrength[targetX][targetY][targetZ]) {
newStrength[targetX][targetY][targetZ] = currentStrength - 1;
newPower[targetX][targetY][targetZ] = true;
}
}
}
? ? private void updateRedstoneComponents() {
for (int x = 0; x < WORLD_SIZE; x++) {
for (int y = 0; y < WORLD_HEIGHT; y++) {
for (int z = 0; z < WORLD_SIZE; z++) {
int blockType = world[x][y][z];
if (blockType == REDSTONE_LAMP) {
// 檢查周圍是否有紅石信號
boolean powered = false;
for (int dx = -1; dx <= 1; dx++) {
for (int dy = -1; dy <= 1; dy++) {
for (int dz = -1; dz <= 1; dz++) {
if (x + dx >= 0 && x + dx < WORLD_SIZE &&?
y + dy >= 0 && y + dy < WORLD_HEIGHT &&?
z + dz >= 0 && z + dz < WORLD_SIZE) {
if (redstonePower[x + dx][y + dy][z + dz]) {
powered = true;
break;
}
}
}
}
}
redstonePower[x][y][z] = powered;
}
}
}
}
}
? ? private void updatePistons() {
long currentTime = System.currentTimeMillis();
for (PistonState piston : pistonStates.values()) {
// 檢查活塞是否應該激活
boolean shouldExtend = checkPistonShouldExtend(piston.x, piston.y, piston.z);
if (shouldExtend != piston.extended) {
if (currentTime - piston.lastUpdate > 500) { // 活塞冷卻時間
piston.extended = shouldExtend;
piston.lastUpdate = currentTime;
// 這里可以添加活塞推動方塊的邏輯
}
}
}
}
? ? private boolean checkPistonShouldExtend(int x, int y, int z) {
// 檢查活塞周圍是否有紅石信號
for (int dx = -1; dx <= 1; dx++) {
for (int dy = -1; dy <= 1; dy++) {
for (int dz = -1; dz <= 1; dz++) {
if (x + dx >= 0 && x + dx < WORLD_SIZE &&?
y + dy >= 0 && y + dy < WORLD_HEIGHT &&?
z + dz >= 0 && z + dz < WORLD_SIZE) {
if (redstonePower[x + dx][y + dy][z + dz]) {
return true;
}
}
}
}
}
return false;
}
? ? private void updateRedstoneAround(int x, int y, int z) {
// 當方塊被破壞時,更新周圍的紅石電路
for (int dx = -1; dx <= 1; dx++) {
for (int dy = -1; dy <= 1; dy++) {
for (int dz = -1; dz <= 1; dz++) {
if (x + dx >= 0 && x + dx < WORLD_SIZE &&?
y + dy >= 0 && y + dy < WORLD_HEIGHT &&?
z + dz >= 0 && z + dz < WORLD_SIZE) {
redstonePower[x + dx][y + dy][z + dz] = false;
redstoneSignalStrength[x + dx][y + dy][z + dz] = 0;
}
}
}
}
updateRedstoneCircuit();
}
? ? private float noise(float x, float z, Random random) {
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();
updateRedstoneCircuit();
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();
renderUI();
glfwSwapBuffers(window);
glfwPollEvents();
}
}
? ? private void processInput() {
float cameraSpeed = currentGameMode == GameMode.CREATIVE ? 0.3f : 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);
// 如果是激活的紅石組件,渲染發光效果
if (isRedstoneComponent(block) && redstonePower[x][y][z]) {
renderRedstoneGlow(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 || blockType == GLASS) {
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 && blockType != GLASS) {
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 && blockType != GLASS) {
glColor3f(color[0], color[1], color[2]);
}
// 其他面...
// [省略其他面的渲染代碼以節省空間]
glEnd();
glPopMatrix();
}
? ? private void renderRedstoneGlow(int x, int y, int z, int blockType) {
glPushMatrix();
glTranslatef(x + 0.5f, y + 0.5f, z + 0.5f);
float[] glowColor;
switch (blockType) {
case REDSTONE_DUST:
case REDSTONE_TORCH:
case REDSTONE_BLOCK:
glowColor = new float[]{1.0f, 0.2f, 0.2f, 0.3f};
break;
case REDSTONE_LAMP:
glowColor = new float[]{1.0f, 0.8f, 0.2f, 0.5f};
break;
default:
glowColor = new float[]{1.0f, 1.0f, 1.0f, 0.3f};
}
glColor4f(glowColor[0], glowColor[1], glowColor[2], glowColor[3]);
glutSolidSphere(0.6f, 16, 16);
glPopMatrix();
}
? ? private void renderUI() {
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(250, 10);
glVertex2f(250, 100);
glVertex2f(10, 100);
glEnd();
glColor3f(1.0f, 1.0f, 1.0f);
drawString(20, 30, "Minecraft Simulator - Enhanced");
drawString(20, 50, "Game Mode: " + currentGameMode);
drawString(20, 70, "Selected Block: " + getBlockName(selectedBlock));
drawString(20, 90, "Controls: WASD-Move, Space/Shift-Up/Down");
// 繪制創造模式物品欄
if (currentGameMode == GameMode.CREATIVE) {
renderCreativeInventory();
}
glEnable(GL_DEPTH_TEST);
glMatrixMode(GL_PROJECTION);
glPopMatrix();
glMatrixMode(GL_MODELVIEW);
glPopMatrix();
}
? ? private void renderCreativeInventory() {
int startX = WIDTH - 200;
int startY = 20;
int slotSize = 30;
glColor4f(0.1f, 0.1f, 0.1f, 0.7f);
glBegin(GL_QUADS);
glVertex2f(startX, startY);
glVertex2f(startX + 180, startY);
glVertex2f(startX + 180, startY + creativeInventory.size() * slotSize + 20);
glVertex2f(startX, startY + creativeInventory.size() * slotSize + 20);
glEnd();
glColor3f(1.0f, 1.0f, 1.0f);
drawString(startX + 10, startY + 15, "Creative Inventory:");
for (int i = 0; i < creativeInventory.size(); i++) {
int block = creativeInventory.get(i);
int yPos = startY + 30 + i * slotSize;
// 高亮當前選擇的方塊
if (i == inventoryIndex) {
glColor4f(0.3f, 0.3f, 0.8f, 0.5f);
glBegin(GL_QUADS);
glVertex2f(startX + 5, yPos - 5);
glVertex2f(startX + 175, yPos - 5);
glVertex2f(startX + 175, yPos + 20);
glVertex2f(startX + 5, yPos + 20);
glEnd();
}
glColor3f(1.0f, 1.0f, 1.0f);
drawString(startX + 10, yPos + 15, getBlockName(block));
}
}
? ? private String getBlockName(int blockType) {
switch (blockType) {
case GRASS: return "Grass";
case DIRT: return "Dirt";
case STONE: return "Stone";
case WOOD: return "Wood";
case LEAVES: return "Leaves";
case PLANKS: return "Planks";
case COBBLESTONE: return "Cobblestone";
case GLASS: return "Glass";
case REDSTONE_DUST: return "Redstone Dust";
case REDSTONE_TORCH: return "Redstone Torch";
case REDSTONE_BLOCK: return "Redstone Block";
case REDSTONE_REPEATER: return "Repeater";
case REDSTONE_LAMP: return "Redstone Lamp";
case STICKY_PISTON: return "Sticky Piston";
case PISTON: return "Piston";
case DIAMOND_BLOCK: return "Diamond Block";
case GOLD_BLOCK: return "Gold Block";
case IRON_BLOCK: return "Iron Block";
default: return "Unknown";
}
}
? ? private void drawString(int x, int y, String text) {
// 簡化文本渲染
glWindowPos2i(x, y);
for (char c : text.toCharArray()) {
glutBitmapCharacter(GLUT_BITMAP_9_BY_15, c);
}
}
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();
}
}
以上就是代碼,文章過長我就不多贅述了!!
?