Laya商业级3d实战_010无缝地图_障碍物和鱼

laya商业级3d游戏开发

本节目标:实现无缝随机创建障碍物和鱼
Unity
克隆Example_RandomBuild场景命名为Game

创建三个对象,挂上buildspwan 脚本
Laya商业级3d实战_010无缝地图_障碍物和鱼_第1张图片

进行以下配置
Fishspwan
Laya商业级3d实战_010无缝地图_障碍物和鱼_第2张图片

obstaclespwan
Laya商业级3d实战_010无缝地图_障碍物和鱼_第3张图片

导出game场景

奖励物(鱼)创建器
//继承建筑物创建类
FishSpwan.ts
export class FishSpwan extends BuildSpawn {

xpos = 0;
//重载生成方法,连续创建N条鱼
Create2End() {
    var p_endZ = this.endZ();
    while (this.startCreateZ < p_endZ) {
        let arryNum = [-1.25, 0, 1.25];
        this.xpos = this.seed.getRandomIntArry(arryNum);
        for (let index = 0; index < 10; index++) {
            let spawnItem = this.DoSpawnItem(this.startCreateZ);
            this.startCreateZ += spawnItem.spwanItemData.length;;
        }
    }
}
//重载创建方法
//跑道有三条,障碍物被创建时随机放到左中右其中一个位置
protected onSpawn(newGo: Laya.Sprite3D, spwanItemData: SpwanItemData, z) {
    newGo.transform.position = new Laya.Vector3(this.xpos, 0.41, z);
    //对象(鱼)被碰撞到了会隐藏
    //对象池重新使用激对象(鱼)
    newGo.active = true;
}

}

障碍物创建器
新建ObstacleSpawn.ts

import BuildSpawn, { SpwanItemData, SpawnItem } from “./BuildSpawn”;
import Dictionary from “…/JFrameWork/Core/Dictionary”;

//障碍物模型
export class Obstacle {
ani: Laya.Animator;
//碰撞系统章节会用到
//collider: AABBShape;
}

//继承建筑物创建类
export class ObstacleSpawn extends BuildSpawn {
//提供外部访问,让外部可以对障碍物进行控制
//后面碰撞器章节会根据撞到的对象ID播放倒下动画
itemMap: Dictionary = new Dictionary();
public gap = 10;
//重载区间创建方法,多了一个间隔属性
Create2End() {
var p_endZ = this.endZ();
while (this.startCreateZ < p_endZ) {

        let spwanitem = this.DoSpawnItem(this.startCreateZ);
        this.startCreateZ += spwanitem.spwanItemData.length + this.gap;

    }

}

//重载创建方法,原因
//障碍物被创建时随机放到左中右其中一个位置
//障碍物推倒后播放倒下动画,所以创建时播放默认动画把它树立起来
protected onSpawn(newGo: Laya.Sprite3D, spwanItemData: SpwanItemData, z) {

    let arryNum = [-1.25, 0, 1.25];
    let x = this.seed.getRandomIntArry(arryNum);
    newGo.transform.position = new Laya.Vector3(x, 0, z);

    let obstacle = this.itemMap.getNumberKey(newGo.id)
    obstacle.ani.play('idle', 0, 0);

}

//重载CreateSpwanItem原因
//添加障碍物实体类型表
protected CreateSpwanItem(spwanItemData: SpwanItemData): SpawnItem {

    let spwanitem = super.CreateSpwanItem(spwanItemData)
    let newgo = spwanitem.gob;

    let obstacle = new Obstacle();
    obstacle.ani = newgo.getChildAt(0).getComponent(Laya.Animator) as Laya.Animator;
    this.itemMap.add(newgo.id, obstacle)

    return spwanitem;
}

}

Mian.ts

 example_fish_build_obstacle() {
    let node = new Laya.Node();
    Laya.stage.addChild(node);
    node.addComponent(UnityEnagine)

    SceneManager.LoadSceneByName('Game', this, (p_Scene3D) => {

        Laya.stage.addChild(p_Scene3D);

        let buildSpawn = new BuildSpawn()
        let bulidjsonStr = '{"findRoot":"Resources/BuildItem","spwanItemDatas":[{"goName":"IndustrialWarehouse01","length":18.0},{"goName":"IndustrialWarehouse03","length":27.0}],"startCreateZ":0.0,"CreateLength":150.0,"recoverOffset":-10.0}';
        buildSpawn.spwanConfigObj = JSON.parse(bulidjsonStr);
        buildSpawn.scene = p_Scene3D;
        node.addComponentIntance(buildSpawn);

        let fishSpwan = new FishSpwan()
        let fishjsonStr = '{"findRoot":"Resources/items","spwanItemDatas":[{"goName":"Fish","length":1.0}],"startCreateZ":30.0,"CreateLength":150.0,"recoverOffset":-5.0}';
        fishSpwan.spwanConfigObj = JSON.parse(fishjsonStr);
        fishSpwan.scene = p_Scene3D;
        node.addComponentIntance(fishSpwan);

        let obstacleSpwan = new ObstacleSpawn()
        let obstaclejsonStr = '{"findRoot":"Resources/items","spwanItemDatas":[{"goName":"ObstacleRoadworksBarrier","length":1.0}],"startCreateZ":30.0,"CreateLength":150.0,"recoverOffset":-5.0}';
        obstacleSpwan.spwanConfigObj = JSON.parse(obstaclejsonStr);
        obstacleSpwan.scene = p_Scene3D;
        node.addComponentIntance(obstacleSpwan);

    });
}

F8 f5

Laya商业级3d实战_010无缝地图_障碍物和鱼_第4张图片
Laya商业级3d实战_010无缝地图_障碍物和鱼_第5张图片

你可能感兴趣的:(h5,laya)