Skip to content

Commit

Permalink
reduce redundant code
Browse files Browse the repository at this point in the history
  • Loading branch information
jerrychan7 committed Jul 17, 2022
1 parent 8663320 commit 1872528
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 35 deletions.
22 changes: 13 additions & 9 deletions src/Entity/PlayerLocalController.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ class PlayerLocalController extends EntityController {
pm.addEventListener("pause=>play", this["pause=>play"]);
pm.addEventListener("play=>pause", this["play=>pause"]);
};
// 转换成键盘消息 复用键盘的处理逻辑
setMoveBtns(moveBtns = null) {
if (this.moveBtns) {
for (let btn of "up,left,down,right,jump,upleft,upright,flyup,flydown,fly,sneak".split(",")) {
Expand Down Expand Up @@ -133,6 +134,17 @@ class PlayerLocalController extends EntityController {
if (type in this) this[type](event);
this.dispatchEvent(type, event);
};
getHitting(bType = Block.renderType.NORMAL) {
let entity = this.entity,
world = entity.world,
start = entity.getEyePosition(),
end = entity.getDirection(20);
vec3.add(start, end, end);
return world.rayTraceBlock(start, end, (x, y, z) => {
let b = world.getBlock(x, y, z);
return b && b.name !== "air" && b.renderType === bType;
});
};
_setEntityPitchAndYaw(movementX, movementY) {
let i = this.mousemoveSensitivity * (Math.PI / 180);
// movementX left- right+ movementY up- down+
Expand All @@ -159,15 +171,7 @@ class PlayerLocalController extends EntityController {
if (e.button === 0) this.mouseRightBtnDown = true;
if (e.button === 2) this.mouseLeftBtnDown = true;
const destroyOrPlaceBlock = () => {
let entity = this.entity,
world = entity.world,
start = entity.getEyePosition(),
end = entity.getDirection(20);
vec3.add(start, end, end);
let hit = world.rayTraceBlock(start, end, (x, y, z) => {
let b = world.getBlock(x, y, z);
return b && b.name !== "air";
});
const world = this.entity.world, hit = this.getHitting();
if (hit === null || hit.axis === "") return;
let pos = hit.blockPos;
if (this.mouseLeftBtnDown) {
Expand Down
23 changes: 14 additions & 9 deletions src/Renderer/HighlightSelectedBlock.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
import { Block } from "../World/Block.js";
import { vec3, mat4 } from "../utils/gmath.js";
import * as glsl from "./glsl.js";

class HighlightSelectedBlock {
constructor(world, renderer = world.renderer) {
this.world = world;
this.renderer = renderer;
this.mvp = mat4.identity();
const {ctx} = renderer;
this.setRenderer(renderer);
};
#calcMesh() {
const {renderer} = this, {ctx} = this.renderer;
this.meshs = new Map();
for (let renderType of Object.values(Block.renderType)) {
let isFluid = renderType === Block.renderType.FLUID;
Expand Down Expand Up @@ -42,16 +45,18 @@ class HighlightSelectedBlock {
});
}
};
setRenderer(renderer = null) {
if (!renderer) return;
// to free buffer
if (this.renderer) this.dispose();
this.renderer = renderer;
renderer.createProgram("selector", glsl.selector.vert, glsl.selector.frag);
this.#calcMesh();
};
draw() {
const {world} = this, {mainPlayer} = world;
if (mainPlayer.camera === null) return;
let start = mainPlayer.getEyePosition(),
end = mainPlayer.getDirection(20);
vec3.add(start, end, end);
let hit = world.rayTraceBlock(start, end, (x, y, z) => {
let b = world.getBlock(x, y, z);
return b && b.name !== "air";
});
const hit = mainPlayer.controller.getHitting?.() ?? null;
if (hit === null) return;

const {renderer} = this, {ctx} = renderer;
Expand Down
9 changes: 8 additions & 1 deletion src/Renderer/WorldChunkModule.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// 计算顶点,材质坐标,下标
// 负责计算顶点,材质坐标,以及区块的绘制工作
import { Block } from "../World/Block.js";
import {
Chunk,
Expand All @@ -7,6 +7,7 @@ import {
CHUNK_Z_SIZE as Z_SIZE,
} from "../World/Chunk.js";
import { manhattanDis } from "../utils/gmath.js";
import * as glsl from "./glsl.js";

const rxyz2int = Chunk.getLinearBlockIndex;

Expand Down Expand Up @@ -58,6 +59,12 @@ class ChunksModule {
setRenderer(renderer = null) {
if (!renderer) return;
this.renderer = renderer;
if (renderer.isWebGL2)
renderer.createProgram("showBlock", glsl.showBlock_webgl2.vert, glsl.showBlock_webgl2.frag)
.use().bindTex("blockTex", renderer.createTextureArray(Block.defaultBlockTextureImg));
else
renderer.createProgram("showBlock", glsl.showBlock.vert, glsl.showBlock.frag)
.use().bindTex("blockTex", renderer.createTexture(Block.defaultBlockTextureImg));
this.updateMeshs();
};
buildChunkModule(chunkKey) {
Expand Down
9 changes: 0 additions & 9 deletions src/Renderer/WorldRenderer.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import { Render } from "./Render.js";
import { Camera } from "./Camera.js";
import * as glsl from "./glsl.js";
import { Block } from "../World/Block.js";
import { ChunksModule } from "./WorldChunkModule.js";
import { HighlightSelectedBlock } from "./HighlightSelectedBlock.js";

Expand All @@ -23,13 +21,6 @@ class WorldRenderer extends Render {
ctx.frontFace(ctx.CCW);
this.mainCamera = new Camera(this.aspectRatio, { fovy: 60, pitch: -90 * Math.PI / 180, position: [0, 20, 0] });
this.addCamera(this.mainCamera);
if (this.isWebGL2)
this.createProgram("showBlock", glsl.showBlock_webgl2.vert, glsl.showBlock_webgl2.frag)
.use().bindTex("blockTex", this.createTextureArray(Block.defaultBlockTextureImg));
else
this.createProgram("showBlock", glsl.showBlock.vert, glsl.showBlock.frag)
.use().bindTex("blockTex", this.createTexture(Block.defaultBlockTextureImg));
this.createProgram("selector", glsl.selector.vert, glsl.selector.frag);
if (world !== null) this.setWorld(world);
};
setWorld(world) {
Expand Down
8 changes: 1 addition & 7 deletions src/World/World.js
Original file line number Diff line number Diff line change
Expand Up @@ -234,13 +234,7 @@ class World extends EventDispatcher {

const {mainPlayer} = this;

let start = mainPlayer.getEyePosition(),
end = mainPlayer.getDirection(20);
vec3.add(start, end, end);
let hit = this.rayTraceBlock(start, end, (x, y, z) => {
let b = this.getBlock(x, y, z);
return b && b.name !== "air";
});
const hit = mainPlayer.controller.getHitting?.() ?? null;
let block = hit? this.getBlock(...hit.blockPos): null;
let longID = hit? this.getTile(...hit.blockPos): null;
let chunk = this.getChunkByBlockXYZ(...[...mainPlayer.position].map(n => n < 0? n - 1: n));
Expand Down

0 comments on commit 1872528

Please sign in to comment.