mirror of
https://gitee.com/jisol/jisol-game/
synced 2025-09-27 10:46:17 +00:00
更新无限地图
This commit is contained in:
@@ -1,7 +1,13 @@
|
||||
import { _decorator, Component, Node } from 'cc';
|
||||
import { JNGSyncBase } from '../../../../App';
|
||||
import { app, JNGSyncBase } from '../../../../App';
|
||||
import { SpriteFrame } from 'cc';
|
||||
import { Camera } from 'cc';
|
||||
import { TableGMap } from '../../../../../resources/config/ts/TableGMap';
|
||||
import { Sprite } from 'cc';
|
||||
import { UITransform } from 'cc';
|
||||
import { v3 } from 'cc';
|
||||
import { Vec2 } from 'cc';
|
||||
import { size } from 'cc';
|
||||
import GNode from '../GNode';
|
||||
const { ccclass, property } = _decorator;
|
||||
|
||||
/**
|
||||
@@ -16,25 +22,74 @@ export class GMapLoop extends JNGSyncBase<{}> {
|
||||
repeat:number = 1;
|
||||
//一块的地图宽度
|
||||
mapWidth:number = 100;
|
||||
//一块的地图高度
|
||||
mapHeight:number = 100;
|
||||
|
||||
//是否初始化
|
||||
isInit:boolean = false;
|
||||
//世界坐标Y轴
|
||||
worldY:number = 0;
|
||||
|
||||
//创建的地图列表{开始区块:地图图片}
|
||||
createMaps:Map<number,Node> = new Map();
|
||||
|
||||
|
||||
init(map:SpriteFrame,repeat:number){
|
||||
init(map:SpriteFrame,repeat:number = 1,width:number = 0,height:number = 0){
|
||||
this.map = map;
|
||||
this.isInit = true;
|
||||
this.repeat = repeat;
|
||||
this.mapWidth = map.width;
|
||||
this.mapWidth = width || this.map.width;
|
||||
this.mapHeight = height || this.map.height;
|
||||
}
|
||||
|
||||
//更新地图(世界坐标X)
|
||||
UpdateMap(x:number){
|
||||
UpdateMap(x:number,offsetX:number = 0,offsetY:number = 0){
|
||||
|
||||
//计算x在第几个区块
|
||||
let blockIndex = Math.floor((x + offsetX) / this.mapWidth);
|
||||
|
||||
//生成区块的列表
|
||||
let maps:number[] = [];
|
||||
for (let index = 0; index < 1 + (this.repeat * 2); index++) {
|
||||
maps.push(blockIndex + (index - this.repeat));
|
||||
}
|
||||
|
||||
//生成
|
||||
maps.forEach((blockIndex) => {
|
||||
this.createMap(blockIndex,offsetX,offsetY);
|
||||
})
|
||||
|
||||
//销毁其他地图
|
||||
let keys = Array.from(this.createMaps.keys());
|
||||
keys.forEach(key => {
|
||||
if(maps.indexOf(key) < 0){
|
||||
this.createMaps.get(key).destroy();
|
||||
this.createMaps.delete(key);
|
||||
}
|
||||
})
|
||||
|
||||
}
|
||||
|
||||
//生成区块地图
|
||||
createMap(blockIndex:number,offsetX:number = 0,offsetY:number = 0){
|
||||
//计算区块开始位置
|
||||
let start = (blockIndex * this.mapWidth) - offsetX;
|
||||
|
||||
if(!this.createMaps.get(blockIndex)){
|
||||
//生成地图
|
||||
let mapNode = GNode.create();
|
||||
let uiTransform = mapNode.addComponent(UITransform);
|
||||
uiTransform.anchorX = 0;
|
||||
this.node.addChild(mapNode);
|
||||
mapNode.worldPosition = v3(start,offsetY,0)
|
||||
let mapImage = mapNode.addComponent(Sprite);
|
||||
mapImage.spriteFrame = this.map;
|
||||
mapImage.sizeMode = Sprite.SizeMode.CUSTOM;
|
||||
uiTransform.contentSize = size(this.mapWidth,this.mapHeight);
|
||||
this.createMaps.set(blockIndex,mapNode);
|
||||
}else{
|
||||
this.createMaps.get(blockIndex).worldPosition = v3(start,offsetY,0)
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
Reference in New Issue
Block a user