pubgv3/assets/scripts/game/npcctrl.js
2022-05-26 11:46:36 +08:00

860 lines
25 KiB
JavaScript

// Learn cc.Class:
// - [Chinese] https://docs.cocos.com/creator/manual/zh/scripting/class.html
// - [English] http://docs.cocos2d-x.org/creator/manual/en/scripting/class.html
// Learn Attribute:
// - [Chinese] https://docs.cocos.com/creator/manual/zh/scripting/reference/attributes.html
// - [English] http://docs.cocos2d-x.org/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
// - [Chinese] https://docs.cocos.com/creator/manual/zh/scripting/life-cycle-callbacks.html
// - [English] https://www.cocos2d-x.org/docs/creator/manual/en/scripting/life-cycle-callbacks.html
var TempV2 = cc.v2(0, 0)
var walkAniArr = ["", "idle", "run", "walk"]
var AnidirArr = [4, 12, 3, 11, 2, 10, 1, 9, 0, 9, 1, 10, 2, 11, 3, 12, 4, 13, 5, 14, 6, 15, 7, 16, 8, 16, 7, 15, 6, 14, 5, 13]
var handarr = [6, 7, 8, 14, 15, 16] //
var playerconfig = require("gameConfig").playerConfig
var gameConfig = require("gameConfig")
var tileConfig = require("tileConfig")
cc.Class({
extends: cc.Component,
properties: {
parmove: {
default: null,
type: cc.ParticleSystem,
},
anidixing: {
default: null,
type: cc.Animation,
},
nd_effnode: {
default: null,
type: cc.Node,
},
pr_hp: {
default: null,
type: cc.ProgressBar,
},
},
// LIFE-CYCLE CALLBACKS:
// onLoad () {},
dobirth(v) {
this.othertime = v / 1000
if (this.skeleton) {
this.birthani = null
this.skeleton.setAnimation(0, "hide", false);
} else {
this.birthani = "hide"
}
},
initdata(iscar) {
this.pr_hp.node.opacity = 0
this.maxhp = this.hp = 1
this.iswating = false
this.othertime = 0
this.targetGo = cc.v2(0, 0)
this.netShotdir = cc.v2(0, 0)
this.dirGo = cc.v2(0, 0)
this.leganicd = 0
this.legstate = 0
this.logicrotaion = 0
this.chengkeshu = 0
this.hidehptime = 0
// this.node.width = 50
// this.node.height = 50
this.aniDir = -1
this.aniIdx = 0
this.birth = false
this.isdead = false
this.soundclip = null
this.buffidarr = []
this.buffmap = {}
this.mp = this.maxmp = 1
this.iscar = iscar
this.ismaycar = false
this.bulletcount = 0
this.nd_effnode.zIndex = 9999
this.node.zplus = 0
},
canzuo() {
if (this.ismember && this.chengkeshu < this.totalck) {
return true
} else if (this.iswating) {
return true
}
return false
},
refreshNetData(netdata) {
this.targetGo.x = netdata.pos.x
this.targetGo.y = netdata.pos.y
this.netShotdir.x = netdata.dir.x
this.netShotdir.y = netdata.dir.y
if (netdata.passengers) {
this.member = netdata.passengers
this.chengkeshu = this.member.length
}
if (netdata.seat_num) {
this.maxck = netdata.seat_num
}
if (this.member != undefined && this.member.length > 0) {
var uuid = this.member[0].obj_uniid
var obj = cc.gameMgr.objMap[uuid]
if (obj) {
if (obj.pctrl.isMember || obj.pctrl.node.isSelf) {
this.ismember = true
} else {
this.ismember = false
}
if (obj.pctrl.node.isSelf && netdata.car_id) {
obj.pctrl.setcar(netdata.car_id)
}
}
}
if (netdata.born_frameno && netdata.born_frameno == cc.gameMgr.nowframe) {
// this.dobirth()
}
if (netdata.buff_list) {
this.checkbuff(netdata.buff_list)
}
if (netdata.heroid && netdata.heroid != this.carid) {
this.refreshchar(netdata.heroid)
var ncfgdata = playerconfig[netdata.heroid]
if (ncfgdata.id == 30502) {
this.isduoren = true
}
if (this.isduoren) {
this.totalck = 4
} else {
this.totalck = 1
}
this.node.width = this.node.height = ncfgdata.hit_radius * 2
this.node.zplus = this.node.height / 2 - 50
this.resname = ncfgdata.mod
if (ncfgdata.scale) {
this.nd_effnode.scale = ncfgdata.eff_sale / 1000
} else {
this.nd_effnode.scale = 1
}
var self = this
if (ncfgdata.move_sound) {
cc.loader.loadRes("sounds/" + ncfgdata.move_sound, cc.AudioClip, null, function(err, clip) {
if (!err && self.isValid) {
self.soundclip = clip
}
});
}
this.carid = netdata.heroid
if (this.model) {
cc.gameMgr.effectManager.huishouprefab(this.resname, this.model)
this.model = null
}
self.spineloaded = false
var cb = function(nd) {
if (self.isValid) {
// self.resname = "pbcar"
self.model = nd
self.skeleton = nd.getChildByName("car").getComponent(sp.Skeleton)
self.spineloaded = true
self.node.addChild(nd)
if (self.birthani) {
self.skeleton.setAnimation(0, self.birthani, false);
self.birthani = null
} else if (self.initspine) {
self.skeleton.setAnimation(0, self.initspine, true);
self.initspine = null
}
}
}
cc.gameMgr.effectManager.createprefab(this.resname, "prefabs/gameprefab/", cb)
}
if (netdata.oil != undefined) {
this.mp = netdata.oil
}
if (netdata.max_oil != undefined) {
this.maxmp = netdata.max_oil
}
if (netdata.bullet_num != undefined) {
this.bulletcount = netdata.bullet_num
}
if (this.iscar && netdata.passengers) {
this.ismaycar = false
for (var i = 0; i < netdata.passengers.length; i++) {
if (netdata.passengers[i].obj_uniid == cc.gameMgr.player.objuuid) {
this.ismaycar = true
break
}
}
if (this.ismaycar) {
for (var i = 0; i < 4; i++) {
cc.gameMgr.uic.btn_driver[i].active = false
}
if (this.maxck != this.chengkeshu) {
for (var i = 0; i < this.maxck; i++) {
cc.gameMgr.uic.btn_driver[i].active = true
}
for (var i = 0; i < netdata.passengers.length; i++) {
cc.gameMgr.uic.btn_driver[netdata.passengers[i].car_seat].active = false
}
}
}
}
if (netdata.max_health) {
this.maxhp = netdata.max_health
if (this.hp > this.maxhp) {
this.hp = this.maxhp
}
}
var needhprefresh = false
var behurt = false
if (netdata.health != undefined) {
if (this.hp != netdata.health) {
needhprefresh = true
}
if (this.hp > netdata.health) {
this.hurt()
behurt = true
} else if (this.hp < netdata.health) {
}
this.hp = netdata.health
}
if (needhprefresh || this.fixrefeshhp) {
this.refreshHpbar(behurt)
}
},
updateNet(dt) {
if (this.ismaycar) {
cc.gameMgr.uic.pr_you.progress = this.mp / this.maxmp
cc.gameMgr.uic.jijiabullet.string = this.bulletcount
}
this.onframe = false
var move_dirx = this.targetGo.x - this.node.x
var move_diry = this.targetGo.y - this.node.y
if (Math.abs(move_dirx) > 210 || Math.abs(move_diry) > 210) {
// if (cc.battledebug) {
// console.log("view_debug "+(new Date()).toLocaleTimeString()+" flyout uuid:"+this.node.objuuid+" last_sync_frameno:" + this.node.last_sync_frameno +
// ' old_pos:' + this.node.x + ',' + this.node.y + ' new_pos:' + this.targetGo.x + ',' + this.targetGo.y)
// }
this.node.x = this.targetGo.x
this.node.y = this.targetGo.y
} else {
this.node.position.lerp(this.targetGo, 0.16, TempV2)
this.node.x = TempV2.x
this.node.y = TempV2.y
}
this.leganicd -= dt
if (this.leganicd < 0) {
if (Math.abs(this.node.x - this.targetGo.x) < 0.5 && Math.abs(this.node.y - this.targetGo.y) < 0.5) {
if (this.legstate != 1) {
this.legstate = 1 //idle
if (this.audioID) {
cc.audioEngine.stopEffect(this.audioID)
this.audioID = null
}
}
} else {
if (this.legstate != 2) {
this.legstate = 2 //walk
this.leganicd = 0.5
if (this.soundclip && !this.audioID) {
this.audioID = cc.audioEngine.playEffect(this.soundclip, true);
}
}
}
}
var delta = 15
this.dirGo.lerp(this.netShotdir, dt * delta, TempV2)
this.dirGo.x = TempV2.x
this.dirGo.y = TempV2.y
this.logicrotaion = -Math.atan2(this.dirGo.y, this.dirGo.x) * (180 / Math.PI) // + 45;
if (this.logicrotaion > 180) {
this.logicrotaion = this.logicrotaion - 360
}
this.checkAni(dt)
this.updatetile()
if (this.lb_debug) {
this.lb_debug.string = this.node.objuuid
}
},
sethandrotation() {
var rot = this.logicrotaion
var isfront = true
var needflip = false
// var hrot = 0
if (rot >= 0 && rot <= 90) {
//hrot = rot/2
} else if (rot > 90 && rot < 180) {
// hrot = (180-rot)/2
needflip = true
} else if (rot <= 0 && rot >= -90) {
// hrot = rot/2
isfront = false
} else {
//hrot = (180-rot)/2
isfront = false
needflip = true
}
if (this.model) {
if (needflip) {
this.model.scaleX = -1
} else {
this.model.scaleX = 1
}
}
if (isfront) {
} else {
}
return isfront
},
dodestroy() {
if (this.audioID) {
cc.audioEngine.stopEffect(this.audioID)
this.audioID = null
}
if (this.model) {
cc.gameMgr.effectManager.huishouprefab(this.resname, this.model)
this.model = null
}
this.node.destroy()
},
checkAngle(isfront) {
var rot = -1
rot = Math.floor(((this.logicrotaion + 180 + 5.625) % 360) / 11.25)
if (this.aniDir != rot) {
this.aniDir = rot
return true
}
return false
},
anishootplay2(str) {
if (this.spineloaded) {
this.skeleton.clearTrack(1)
this.skeleton.setAnimation(1, str, true);
// console.log(str)
}
},
anishootstop() {
if (this.spineloaded) {
this.skeleton.clearTrack(1)
}
},
anibodyplay(name) {
// console.log("jj:"+name)
if (this.spineloaded) {
this.skeleton.clearTrack(0)
this.skeleton.clearTrack(2)
this.skeleton.setAnimation(0, name, true);
} else {
this.initspine = name
}
},
update(dt) {
if (this.anishoting) {
this.shootingtime -= dt
if (this.shootingtime <= 0) {
this.anishoting = false
this.shootingtime = 0
}
}
if (this.hidehptime > 0) {
this.hidehptime -= dt
} else {
this.pr_hp.node.opacity -= dt * 800
}
},
checkAni(dt) {
if (this.isdead) {
return
}
if (this.iswating && this.spineloaded) {
if (this.nowAni != "nobody") {
this.anishootstop()
this.anibodyplay("nobody")
this.nowAni = "nobody"
}
return
}
if (this.othertime > 0) {
this.othertime -= dt
if (this.model) {
this.model.scaleX = 1
}
return
}
var isfront = this.sethandrotation()
var plusname = isfront ? "" : "_back" //
var nowstr = ""
if (this.anishoting && !this.isduoren) {
var needcheck = this.checkAngle(isfront)
var aniIdx = AnidirArr[this.aniDir]
if (aniIdx == 4) {
plusname = "_back"
}
nowstr = nowstr + "1" + this.legstate + plusname
if (this.nowAni != nowstr) {
this.anibodyplay("shoot_" + walkAniArr[this.legstate] + plusname)
this.nowAni = nowstr
needcheck = true
}
if (!this.anishotb) {
needcheck = true
}
if (needcheck) {
this.anishootplay2("shoot_" + aniIdx)
this.anishotb = true
}
} else {
if (this.anishotb) {
this.anishootstop()
}
this.anishotb = false
if (this.legstate == 1) {
nowstr = "gun_idle"
} else {
nowstr = "gun_run"
}
nowstr = nowstr + plusname
if (this.nowAni != nowstr) {
this.anibodyplay(nowstr)
this.nowAni = nowstr
}
}
// if (this.legstate == 1) {
// nowstr = "gundam_horse_idle"
// } else {
// nowstr = "gundam_horse_run"
// }
// nowstr = nowstr + plusname
// if (this.nowAni != nowstr) {
// this.animation.play(nowstr)
// this.nowAni = nowstr
// }
},
shotAni(sound_use, pos, dir, shotaniname, shot2) {
if (this.onframe) {
return
}
this.shootingtime = 0.5
this.onframe = true
if (sound_use) {
cc.gameMgr.playSound(sound_use, pos)
}
var idx = AnidirArr[Math.floor(((this.logicrotaion + 180 + 5.625) % 360) / 11.25)]
var isleft = false
for (var i = handarr.length - 1; i >= 0; i--) {
if (handarr[i] == idx) {
isleft = true
break
}
}
if (this.spineloaded) {
if (isleft) {
this.skeleton.setAnimation(2, "recoil1", false);
} else {
this.skeleton.setAnimation(2, "recoil2", false);
}
}
},
throw () {
},
shotSound(sound_use, pos) {
cc.gameMgr.playSound(sound_use, pos)
},
createlooteff(name, cb) {
if (cc.gameMgr && cc.gameMgr.effectManager) {
cc.gameMgr.effectManager.createlooteff(name, cb)
}
},
huishoulooteff(name, nd) {
if (cc.gameMgr && cc.gameMgr.effectManager) {
cc.gameMgr.effectManager.huishoulooteff(name, nd)
}
},
changeBuff(res) {
//console.log(res)
var chg = res.chg
if (chg == 0) {
this.addBuff(res.buff)
} else if (chg == 1) {
this.removeBuff(res.buff.buff_id)
}
},
addBuff(buff) {
var bid = buff.buff_id
var bdata = gameConfig.buffConfig[bid]
var efftp = bdata.buff_effect
if (efftp == 53) {
this.iswating = false
this.dobirth(Number(bdata.buff_param1))
} else if (efftp == 54) {
this.iswating = true
}
if (bdata.buff_ef) {
this.addbuffeff(bdata.buff_ef, bdata.buff_effect)
}
},
removeBuff(bid) {
var bdata = gameConfig.buffConfig[bid]
var efftp = bdata.buff_effect
// if (efftp == 54) {
// this.iswating = false
// }
if (bdata.buff_ef) {
this.removebuffeff(bdata.buff_ef)
}
},
removebuffeff(pbname) {
if (this.buffmap[pbname] && this.buffmap[pbname] != "loading") {
this.huishoulooteff(pbname, this.buffmap[pbname])
}
this.buffmap[pbname] = null
},
addbuffeff(pbname, efftype) {
var addnd = this.node
if (pbname && !this.buffmap[pbname]) {
this.buffmap[pbname] = "loading"
this.createlooteff(pbname, (res) => {
addnd.addChild(res)
if (this.buffmap[pbname]) {
this.buffmap[pbname] = res
} else {
this.huishoulooteff(pbname, res)
}
})
}
},
getBuffByEffectId(effectId) {
for (var i = 0, l = this.buffidarr.length; i < l; i++) {
var buffConf = gameConfig.buffConfig[this.buffidarr[i].buff_id]
if (buffConf.buff_effect == effectId) {
return this.buffidarr[i]
}
}
return null;
},
getBuffById(buffId) {
for (var i = 0, l = this.buffidarr.length; i < l; i++) {
if (buffidarr[i].buff_id == buffId) {
return buffidarr[i]
}
}
return null;
},
checkbuff(list) {
this.buffidarr = list
this.iswating = false
for (var i = 0, l = list.length; i < l; i++) {
var buff = list[i]
var bid = buff.buff_id
var bdata = gameConfig.buffConfig[bid]
var efftp = bdata.buff_effect
if (efftp == 53 && this.iswating) {
this.iswating = false
this.dobirth(Number(bdata.buff_param1))
} else if (efftp == 54) {
this.iswating = true
}
// var efftp = gameConfig.buffConfig[list[i].buff_id].buff_effect
if (gameConfig.buffConfig[list[i].buff_id].buff_ef) {
this.addbuffeff(gameConfig.buffConfig[list[i].buff_id].buff_ef)
}
}
},
chged_property(v) {
if (v.property_type == 27) {
this.mp = v.property_subtype
this.maxmp = v.value
} else if (v.property_type == 10) {
if (v.property_subtype == 1) {
this.bulletcount = v.value
}
} else if (v.property_type == 1) {
var behurt = false
if (this.hp > v.value) {
this.hurt()
behurt = true
}
this.hp = v.value
this.refreshHpbar(behurt)
} else if (v.property_type == 2) {
this.maxhp = v.value
if (this.hp > this.maxhp) {
this.hp = this.maxhp
}
this.refreshHpbar()
}
},
hurt() {
},
refreshHpbar(behurt) {
var hbili = this.hp / this.maxhp
if (behurt) {
this.hidehptime = 0.5
this.pr_hp.node.opacity = 255
}
this.pr_hp.progress = hbili
},
ondeadthisframe() {
this.isdead = true
this.anishootstop()
this.anibodyplay("death")
},
doshotevent(data) {
if (data.hole > 0) {
var isfront = this.sethandrotation()
if (this.isduoren && this.model && this.model.scaleX == -1) {
if (data.hole == 3) {
data.hole = 1
} else if (data.hole == 1) {
data.hole = 3
}
}
var plusname = isfront ? "" : "_back"
this.skeleton.setAnimation(2, "atk" + data.hole + plusname, false);
// console.log("atk" + data.hole + plusname)
}
},
updatetile() {
if (cc.gameMgr.maptile && this.parmove && this.anidixing) {
var gid = 0
var cds = cc.gameMgr.maptile._layers
var px = Math.floor(this.node.x / 32)
var py = cc.gameMgr.mapheight - Math.floor(this.node.y / 32) - 1
px = Math.min(Math.max(0, px), cc.gameMgr.mapheight - 1)
py = Math.min(Math.max(0, py), cc.gameMgr.mapheight - 1)
for (var i = cds.length - 1; i >= 0; i--) {
var temp = cds[i].getTileGIDAt(px, py)
if (temp > 0 && cc.gameMgr.tiledata[temp - 1]) {
gid = Number(cc.gameMgr.tiledata[temp - 1])
break
}
}
if (gid != 0) {
if (this.nowmoveid != gid) {
var self = this
this.parmove.spriteFrame = null
cc.loader.loadRes("particle/" + tileConfig.tileres[gid], cc.SpriteFrame, function(err, res) {
if (!err && self.isValid) {
self.parmove.spriteFrame = res
}
});
this.parmove.resetSystem()
this.nowmoveid = gid
if (tileConfig.tileres2[gid]) {
this.anidixing.node.active = true
this.anidixing.play(tileConfig.tileres2[gid])
} else {
this.anidixing.node.active = false
}
}
} else {
if (this.nowmoveid != 0) {
this.parmove.spriteFrame = null
this.anidixing.node.active = false
this.parmove.resetSystem()
}
this.nowmoveid = 0
}
if (this.inhouse) {
this.parmove.node.opacity = 0
this.anidixing.node.opacity = 0
} else {
if (this.legstate == 1) {
this.parmove.node.opacity = 0
} else {
this.parmove.node.opacity = 255
}
this.anidixing.node.opacity = 255
}
}
},
refreshchar(charid) {
//cc.battledebug = false
if (cc.battledebug) {
if (!this.chapz) {
var debugnode = new cc.Node()
this.node.addChild(debugnode)
var sprite = debugnode.addComponent('cc.Sprite')
sprite.sizeMode = 0;
cc.loader.loadRes("btn1", cc.SpriteFrame, function(err, res) {
if (!err) {
sprite.spriteFrame = res;
}
});
var debugnode2 = new cc.Node()
this.node.addChild(debugnode2)
var sprite2 = debugnode2.addComponent('cc.Sprite')
sprite2.sizeMode = 0;
cc.loader.loadRes("btn1", cc.SpriteFrame, function(err, res) {
if (!err) {
sprite2.spriteFrame = res;
}
});
this.db1 = debugnode
this.db2 = debugnode2
this.chapz = true
debugnode.opacity = 160
debugnode2.opacity = 160
debugnode.zIndex = 999
debugnode2.zIndex = 1000
debugnode2.color = cc.Color.RED
var Labelnode = new cc.Node()
this.node.addChild(Labelnode)
this.lb_debug = Labelnode.addComponent('cc.Label')
Labelnode.zIndex = 1001
}
var pcfg = playerconfig[charid]
this.db1.width = this.db1.height = pcfg.hit_radius * 2 / this.node.scaleY
this.db2.y = pcfg.move_offset_y
this.db2.width = this.db2.height = pcfg.radius * 2 / this.node.scaleY
}
}
// update (dt) {},
});