使用JAVA制作minecraft紅石和創造模式插件

這一次主要是紅石和創造模式的新加入由于代碼較長,所以呃這一段代碼就直接勞煩各位呃插進之前的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();
}
}

以上就是代碼,文章過長我就不多贅述了!!

?

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/920687.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/920687.shtml
英文地址,請注明出處:http://en.pswp.cn/news/920687.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

Git 版本管理核心實踐與問題解決手冊

Git 的核心價值版本控制&#xff1a;完整記錄所有修改歷史&#xff0c;支持隨時回退到任意歷史版本團隊協作&#xff1a;允許多開發者同時工作&#xff0c;有效避免代碼沖突和覆蓋問題高效分支&#xff1a;通過分支隔離功能開發與穩定主線&#xff0c;保持項目穩定性變更追溯&a…

hadoop安欣醫院掛號看診管理系統(代碼+數據庫+LW)

摘 要 隨著信息技術的飛速發展&#xff0c;醫療服務行業正逐步向信息化、智能化轉型。安欣醫院掛號看診管理系統正是基于這一背景開發的一款集掛號、看診管理于一體的綜合性系統。本系統采用Hadoop大數據處理技術&#xff0c;旨在提高醫院掛號看診的效率&#xff0c;優化醫療…

【PHP】數學/數字處理相關函數匯總,持續更新中~

目錄 一、取整 二、向上取整 三、向下取整 四、四舍五入取整 五、四舍五入保留小數點 六、浮點數值 七、絕對值 八、生成隨機數 九、數字格式化&#xff08;以千位分割&#xff09; 十、對除法結果取整 十一、返回除法的余數 十二、是否為數字或數字字符串 十三、…

防火墻技術(二):安全區域

安全區域和接口 默認情況下&#xff0c;報文在不同安全區域之間流動時受到控制&#xff0c;報文在同一個安全區域內流動時不受控制。但華為防火墻也支持對同一個安全區域內流動的報文控制&#xff0c;通過安全策略來實現防火墻通過接口來連接網絡&#xff0c;將接口劃分到安全區…

銀河麒麟V10(Phytium,D2000/8 E8C, aarch64)開發Qt

搞了一臺國產計算機&#xff0c;銀河麒麟V10系統 首先查看系統構架 kylinkylin-pc:/data$ uname -m aarch64 是arm架構的&#xff0c;到 https://www.qt.io/download-qt-installer下載 qt-online-installer-linux-arm64-4.10.0.run

騰訊云 MCP 場景征集計劃 | 你的方案,正在定義開發新范式

開發者的進階正在從“寫代碼”走向“做場景”。MCP&#xff08;模型上下文協議&#xff09;讓你以更低心智負擔撬動云AI能力&#xff0c;把時間花在真正的業務價值上。騰訊云開發者MCP廣場 正式啟動「騰訊云 MCP 場景征集計劃」&#xff0c;尋找最懂 MCP 的你&#xff1a;將真實…

21款m1 max升級到macOS 13——Ventura

macOS系統體驗&#xff1a;之前入手的m1 max出廠版本的macOS系統是macOS Monterey&#xff0c;也就是macOS 12&#xff0c;用了一段時間后&#xff0c;其實也是很流暢的&#xff0c;無奈最近vscode上的某插件一直提醒我的macOS系統版本過低。索性升級了一下macOS系統了。macOS系…

PostgreSQL WAL機制深度解析與優化

PostgreSQL 的預寫日志&#xff08;Write-Ahead Logging, WAL&#xff09; 是其事務持久化和數據完整性的核心機制&#xff0c;通過“先寫日志&#xff0c;再寫數據”的原則保障故障恢復能力。以下是深度解析&#xff1a;一、WAL 的核心目標 崩潰恢復&#xff08;Crash Recover…

三重積分的性質

文章目錄前言幾何意義性質先 1 后 2 投影法先 2 后 110.13前言 規律作息。 幾何意義 三重積分&#xff0c;只要被積分函數是正的&#xff0c;那么&#xff0c;積分的結果就是質量。可能工作還是太累了&#xff0c;以后有時間可以買買彩票&#xff0c;碰碰運氣。。。。 性質…

每日Java并發面試系列(5):基礎篇(線程池的核心原理是什么、線程池大小設置為多少更合適、線程池哪幾種類型?ThreadLocal為什么會導致內存泄漏?)

1. 什么是線程池&#xff1f;它的核心原理是什么&#xff1f;什么是線程池&#xff1f; 線程池是一種基于池化思想管理和使用線程的機制。它內部維護了多個線程&#xff0c;等待著分配由用戶提交的并發執行的任務。這避免了頻繁創建和銷毀線程帶來的開銷&#xff0c;從而提高了…

京東商品詳情API返回值應用實踐

一、API核心功能京東商品詳情API&#xff08;如jd.item.get或jd.union.open.goods.query&#xff09;是京東開放平臺提供的核心接口&#xff0c;用于通過商品ID&#xff08;skuId&#xff09;或店鋪ID檢索指定商品的詳細信息。該接口支持獲取商品基礎信息、價格、庫存、規格參數…

學習python第14天

匯報一下秋招進度&#xff0c;字節一面完后9天都沒給回復&#xff0c;大概率被掛了&#xff0c;但是官網還在流程中&#xff0c;我又沒有HR聯系方式&#xff0c;所以直接在平臺上反饋了&#xff0c;要么趕緊給我過&#xff0c;要么趕緊給我掛&#xff0c;耽誤時間。阿里國際一面…

監聽nacos配置中心數據的變化

RefreshScope實現nacos配置中心數據的動態刷新。如果需要監聽nacos配置中心數據的變化&#xff0c;并執行對應的業務邏輯&#xff0c;則可以使用NacosConfigListener注解。除了需要導入微服務和nacos配置中心的jar&#xff0c;還需要額外導入如下的jar&#xff1a;<dependen…

docker搭建Apisix和Apisix Dashboard

第一步&#xff1a;github下載源碼 參考&#xff1a;https://apisix.apache.org/zh/docs/apisix/installation-guide/ git clone https://github.com/apache/apisix-docker.git cd apisix-docker/example第二步&#xff1a;添加Apisix Dashboard鏡像 打開./apisix-docker/examp…

ubuntu 安裝conda, ubuntu24安裝miniConda

1. 官網下載腳本&#xff1a; Download Success | Anaconda 我選的mini版本&#xff0c;也可以選左邊的完整版 2. 下載后&#xff0c;上傳至服務器/opt下 3. 執行腳本安裝&#xff1a; sh Miniconda3-latest-Linux-x86_64.sh 4. 需要按照英文提示&#xff0c;輸入回車&#…

現代貪吃蛇游戲的進化:從經典玩法到多人在線體驗

Hi&#xff0c;我是前端人類學&#xff08;之前叫布蘭妮甜&#xff09;&#xff01; 貪吃蛇游戲自1976年誕生以來&#xff0c;已經從簡單的像素游戲發展成為具有豐富功能的現代游戲體驗。本文將通過一個功能增強版的貪吃蛇游戲&#xff0c;探討如何將經典游戲概念與現代Web技術…

加速智能經濟發展:如何助力“人工智能+”戰略在實時視頻領域的落地

2025年8月&#xff0c;國務院發布了《關于深入實施“人工智能”行動的意見》&#xff08;國發〔2025〕11號&#xff09;&#xff0c;明確提出&#xff0c;到2030年&#xff0c;我國將在人工智能技術的推動下全面邁入智能經濟與智能社會的新階段。政策強調&#xff0c;要通過推動…

從 WPF 到 Avalonia 的遷移系列實戰篇1:依賴屬性的異同點與遷移技巧

從 WPF 到 Avalonia 系列實戰篇1&#xff1a;依賴屬性的異同與實踐&#xff08;基于 BlinkingButton 控件&#xff09; 我的GitHub倉庫Avalonia學習項目包含完整的Avalonia實踐案例與代碼對比。 我的gitcode倉庫是Avalonia學習項目。 文中主要示例代碼均可在倉庫中查看&#xf…

基于開源AI大模型AI智能名片S2B2C商城小程序的產地優勢產品銷售策略研究

摘要&#xff1a;本文聚焦于在開源AI大模型AI智能名片S2B2C商城小程序的商業生態中&#xff0c;探討如何利用產地優勢進行產品銷售。通過分析不同產品類別的產地優勢&#xff0c;如阿膠類選東阿、海參類選沿海、紅酒類選海外等&#xff0c;結合開源AI大模型的技術支持、AI智能名…

大數據畢業設計選題:基于大數據的用戶貸款行為數據分析系統Spark SQL核心技術

&#x1f34a;作者&#xff1a;計算機畢設匠心工作室 &#x1f34a;簡介&#xff1a;畢業后就一直專業從事計算機軟件程序開發&#xff0c;至今也有8年工作經驗。擅長Java、Python、微信小程序、安卓、大數據、PHP、.NET|C#、Golang等。 擅長&#xff1a;按照需求定制化開發項目…