138 lines
4.1 KiB
JavaScript
138 lines
4.1 KiB
JavaScript
var mapCfg = require("mapTileConfig")
|
|
|
|
var gird = 1000
|
|
var plusManage = function() {
|
|
this.init = function(mapid, size) {
|
|
this.mapsize = size
|
|
this.needpush = []
|
|
this.lootarr = {}
|
|
this.objmap = {}
|
|
this.objpool = []
|
|
var cfg = mapCfg['map' + mapid]
|
|
if(!cfg){
|
|
return
|
|
}
|
|
for (let i = 0, j = cfg.length; i < j; i++) {
|
|
cfg[i].xidx = Math.floor(cfg[i].x / gird)
|
|
cfg[i].yidx = Math.floor(cfg[i].y / gird)
|
|
cfg[i].obj_uniid = i
|
|
if (!this.lootarr[cfg[i].xidx + "-" + cfg[i].yidx]) {
|
|
this.lootarr[cfg[i].xidx + "-" + cfg[i].yidx] = []
|
|
}
|
|
this.lootarr[cfg[i].xidx + "-" + cfg[i].yidx].push(cfg[i])
|
|
|
|
}
|
|
this.jishu = 0
|
|
}
|
|
this.removeobj = function(oid) {
|
|
var nd = this.objmap[oid]
|
|
if (nd) {
|
|
nd.removeFromParent(false)
|
|
this.objpool.push(nd)
|
|
}
|
|
this.objmap[oid] = null
|
|
}
|
|
this.createobj = function(dat) {
|
|
if (this.objmap[dat.obj_uniid]) {
|
|
return
|
|
}
|
|
var obj
|
|
if (this.objpool.length > 0) {
|
|
obj = this.objpool.pop();
|
|
} else {
|
|
obj = new cc.Node()
|
|
obj.addComponent(cc.Sprite)
|
|
}
|
|
obj.x = dat.x
|
|
obj.y = dat.y
|
|
obj.scaleX = dat.sx
|
|
obj.scaleY = dat.sy
|
|
obj.rotation = dat.rot
|
|
this.objmap[dat.obj_uniid] = obj
|
|
obj.getComponent(cc.Sprite).spriteFrame = null
|
|
obj.nowres = dat.name
|
|
cc.loader.loadRes("mapground/" + dat.name, cc.SpriteFrame, function(err, res) {
|
|
if (!err&&obj.nowres == dat.name) {
|
|
obj.getComponent(cc.Sprite).spriteFrame = res
|
|
}
|
|
});
|
|
|
|
cc.gameMgr.ndupmap.addChild(obj)
|
|
}
|
|
this.update = function() {
|
|
this.jishu++;
|
|
this.jishu %= 10
|
|
if (this.jishu != 0) {
|
|
return
|
|
}
|
|
var player = cc.gameMgr.watchPlayer
|
|
var xidx = Math.floor(player.x / gird)
|
|
var yidx = Math.floor(player.y / gird)
|
|
var xykey = xidx + "-" + yidx
|
|
var outarea = []
|
|
//var out_objids = []
|
|
// var full_objects = []
|
|
if (this.area != xykey) {
|
|
|
|
var sx = xidx - 1
|
|
var mx = xidx + 1
|
|
|
|
var sy = yidx - 1
|
|
var my = yidx + 1
|
|
|
|
var temp = [
|
|
xidx + "-" + yidx,
|
|
xidx + "-" + sy,
|
|
xidx + "-" + my,
|
|
|
|
sx + "-" + yidx,
|
|
sx + "-" + sy,
|
|
sx + "-" + my,
|
|
|
|
mx + "-" + yidx,
|
|
mx + "-" + sy,
|
|
mx + "-" + my,
|
|
|
|
]
|
|
for (var i = 0; i < this.needpush.length; i++) {
|
|
var baoliu = false
|
|
for (var j = 0; j < temp.length; j++) {
|
|
if (this.needpush[i] == temp[j]) {
|
|
baoliu = true
|
|
break
|
|
}
|
|
|
|
}
|
|
if (!baoliu) {
|
|
outarea.push(this.needpush[i])
|
|
}
|
|
|
|
}
|
|
this.needpush = temp
|
|
this.area = xykey
|
|
|
|
for (var i = outarea.length - 1; i >= 0; i--) {
|
|
var oar = this.lootarr[outarea[i]]
|
|
if (oar) {
|
|
for (var j = oar.length - 1; j >= 0; j--) {
|
|
this.removeobj(oar[j].obj_uniid)
|
|
//out_objids.push(oar[j].obj_uniid)
|
|
}
|
|
}
|
|
}
|
|
|
|
for (var i = this.needpush.length - 1; i >= 0; i--) {
|
|
var oar = this.lootarr[this.needpush[i]]
|
|
if (oar) {
|
|
for (var j = oar.length - 1; j >= 0; j--) {
|
|
this.createobj(oar[j])
|
|
//full_objects.push(oar[j])
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
}
|
|
}
|
|
|
|
module.exports = plusManage |