three.js 製作一個三維的推箱子游戲

今天郭先生髮現大家更喜歡看我發的three.js小作品,今天我就發一個3d版本推箱子的遊戲,其實webGL有很多框架,three.js並不合適做遊戲引擎,但是可以嘗試一些小遊戲。在線案例請點擊博客原文

要製作一個推箱子游戲,正常要有以下4個步驟

  1. 定義一些數組,要有開始箱子數組、結束箱子數組、地面數組還有牆面數組,有這四個數組就可以組成一個關卡。
  2. 根據數組初始化地面牆面箱子和目標地點標誌物。
  3. 使用FirstPersonControls控制器,控制相機移動,根據地面箱子和牆面算出可移動區域。
  4. 根據相機正對箱子時,用鼠標點擊箱子,控制箱子移動,並做成功性校驗。

下面我們上代碼分析代碼

1. 定義數組

這四個數組分別是牆的數組、地面的數組、箱子初始位置數組和目標數組。

wallArr = [[0, 0], [1, 0], [2, 0], [3, 0], [3, 1], [4, 1], [4, 2], [4, 3], [5, 3], [5, 4], [5, 5], [5, 6], [4, 6], [3, 6], [2, 6], [1, 6], [0, 6], [0, 5], [0, 4], [0, 3], [0, 2], [0, 1]]
scopeArr = [[1, 1], [2, 1], [1, 2], [2, 2], [3, 2], [1, 3], [2, 3], [1, 4], [4, 4], [1, 5], [2, 5], [3, 5], [4, 5]];
boxArr = [[3, 3], [2, 4], [3, 4]];
targetArr = [[2, 2], [1, 3], [2, 3]]; 

2. 根據箱子初始位置數組初始化箱子

initBox() {
    var textureBox = new THREE.TextureLoader().load("/static/images/base/crate.png");
    if (boxGroup) {
        scene.remove(boxGroup)
    }
    boxGroup = new THREE.Group();
    boxGroup.name = 'box_group'
    boxArr.forEach(d => {
        var boxGeom = new THREE.BoxGeometry(40, 40, 40);
        var boxMate = [];
        boxGeom.faces.forEach(d => boxMate.push(new THREE.MeshBasicMaterial({ map: textureBox })))
        var boxMesh = new THREE.Mesh(boxGeom, boxMate);
        boxMesh.position.set(d[0] * 40 - 20, 20, d[1] * 40 - 20);
        boxMesh.name = 'box';
        boxGroup.add(boxMesh);
    })
    scene.add(boxGroup);
    //判斷是否贏得比賽
    this.isWinner(boxArr, targetArr)
}

3. 根據地面數組初始化地面

initGround() {
    var textureGround = new THREE.TextureLoader().load("/static/images/wall/plaster.jpg", () => {this.loaded_num --});
    var textureGroundNormal = new THREE.TextureLoader().load("/static/images/wall/plaster-normal.jpg", () => {this.loaded_num --});
    var textureGroundSpecular = new THREE.TextureLoader().load("/static/images/wall/plaster-diffuse.jpg", () => {this.loaded_num --});
    textureGround.wrapS = textureGround.wrapT = THREE.RepeatWrapping;
    textureGround.repeat.set(50, 50);
    textureGroundNormal.wrapS = textureGroundNormal.wrapT = THREE.RepeatWrapping;
    textureGroundNormal.repeat.set(50, 50);
    var materialGround = new THREE.MeshPhongMaterial({
        map: textureGround
    })
    materialGround.normalMap = textureGroundNormal;
    materialGround.specularMap = textureGroundSpecular;
    var ground = new THREE.Mesh(new THREE.PlaneGeometry(1000, 1000, 1, 1), materialGround);
    ground.rotation.x = - Math.PI / 2;
    scene.add(ground);
}

4. 根據牆數組初始化地面

initWall() {
    var normal = new THREE.TextureLoader().load("/static/images/wall/stone.jpg", () => {this.loaded_num --});
    var bump = new THREE.TextureLoader().load("/static/images/wall/stone-bump.jpg", () => {this.loaded_num --});
    wallArr.forEach(d => {
        var wallBox = new THREE.BoxGeometry(40, 40, 40);
        var material = new THREE.MeshPhongMaterial({
            map: normal,
            bumpMap: bump,
            bumpScale: 1
        })
        var wall = new THREE.Mesh(wallBox, material);
        wall.position.x = d[0] * 40 - 20;
        wall.position.y = 20;
        wall.position.z = d[1] * 40 - 20;
        scene.add(wall);
    })
}

5. 根據目標數組初始化目標物

initTarget() {
    let objLoader = new OBJLoader();
    objLoader.setPath("/static/images/texture/hongqi/");
    objLoader.load('hongqi.obj', (object) => {
        this.loaded_num --;
        let hongqi = object.children[0];
        targetArr.forEach(d => {
            hongqi.position.set(d[0] * 40 - 20, -50, d[1] * 40 - 20)
            hongqi.scale.set(0.12, 0.12, 0.12)
            hongqi.material = new THREE.MeshNormalMaterial({ side: THREE.DoubleSide });
            scene.add(hongqi.clone())
        })
    })
}

6. 監聽箱子的點擊事件

每次點擊的時候執行computeMove方法,判斷如果是否可移動。

initEventListener() {
    raycaster = new THREE.Raycaster();
    document.addEventListener('mousemove', function (event) {
        event.preventDefault();
        mouse.x = (event.clientX / window.innerWidth) * 2 - 1;
        mouse.y = - (event.clientY / window.innerHeight) * 2 + 1;
    }, false)
    document.addEventListener('click', () => {
        if (scene.children && scene.getObjectByName('box')) {
            raycaster.setFromCamera(mouse, camera);
            let intersects = raycaster.intersectObjects(scene.getObjectByName('box_group').children);
            if (intersects[0] && intersects[0].object.name == 'box') {
                this.computeMove(intersects[0].object, camera.position);
            }
        }
    })
}

7. 監聽遊戲成功

如果成功了,那麼簡單的彈出提示。

isWinner(arr1, arr2) {
    let boo = true; //true爲贏
    arr1.forEach(d => {
        let res = arr2.some(dd => {
            return d[0] == dd[0] && d[1] == dd[1]
        })
        if(!res) {
            boo = false;
        }
    })
    if(boo) {
        setTimeout(() => {
            alert('恭喜你贏了!')
        },100)
    }
}

由於當時做這個小案例時還是菜鳥,所以很少用一些three.js的輔助方法,見笑了。

 

轉載請註明地址:郭先生的博客

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章