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

609 lines
20 KiB
JavaScript

var Utils = require("Utils")
var dtoh = 0.0174528
var boomscale = 0.3
var battlenet = require("battlenetmanage")
var temphouse = {
width: 0,
height: 0,
x: 0,
y: 0,
}
var BulletManage = function() {
this.bulletpool = [];
this.bulletarr = [];
this.bulletNode = null;
this.time = 9999
this.reset = function() {
this.bulletpool.length = 0;
this.bulletarr.length = 0;
this.bulletNode = null;
}
this.clean = function() {
for (var i = 0; i < this.bulletpool.length; i++) {
this.bulletpool[i].bullet = null
this.bulletpool[i].effect = null
}
for (var i = 0; i < this.bulletarr.length; i++) {
this.bulletarr[i].bullet = null
this.bulletarr[i].effect = null
}
this.reset()
}
this.createBullet = function(lv, gun, dir, pos, owner, distance, oid,netdata) {
var bullet
if (this.bulletpool.length > 0) {
bullet = this.bulletpool.pop();
} else {
bullet = new cc.Node();
bullet.effect = new cc.Node()
bullet.effect.anchorX = 1
bullet.effect.addComponent(cc.Sprite)
bullet.addChild(bullet.effect)
bullet.bullet = new cc.Node()
bullet.addChild(bullet.bullet)
var sp = bullet.bullet.addComponent(cc.Sprite)
sp.sizeMode = 2
sp.trim = false
this.bulletNode.addChild(bullet);
bullet.shadow = new cc.Node()
bullet.shadow.zIndex = 32767
cc.gameMgr.ndhouse.addChild(bullet.shadow)
var sp2 = bullet.shadow.addComponent(cc.Sprite)
sp2.sizeMode = 2
sp2.trim = false
bullet.shadow.scale = 0.5
bullet.shadow.color = cc.color(0, 0, 0)
bullet.prefabnode = new cc.Node()
bullet.addChild(bullet.prefabnode)
// bullet.effnode = new cc.Node()
// bullete.addChild(bullet.effnode);
}
//test
// dir = Utils.dirRotate(dir,(Utils.randintSeed(10)-5)*dtoh)
// bullet.effect.getComponent(cc.ParticleSystem).resetSystem();
var esp = bullet.effect.getComponent(cc.Sprite)
var tailres = gun.bullet_tail ? gun.bullet_tail : "tail1"
esp.spriteFrame = cc.atlMgr.bulletatlas.getSpriteFrame(tailres)
esp.trim = false
bullet.effect.width = 20
bullet.opacity = 255
bullet.shadow.opacity = 100
// this.bulletNode.addChild(bullet);
bullet.position = pos
bullet.rotation = -Math.atan2(dir.y, dir.x) * (180 / Math.PI);
bullet.bullet.x = bullet.bullet.y = 0
bullet.shadow.rotation = bullet.rotation
bullet.owner = owner;
bullet.isboom = false;
bullet.ownerid = oid
this.bulletarr.push(bullet);
this.setBulletData(lv, bullet, gun, dir, distance,netdata);
};
this.deletabullets = function(delarr){
for(var j=0;j<delarr.length;j++){
for (var i = this.bulletarr.length - 1; i >= 0; i--) {
if(this.bulletarr[i].bullet_uniid == delarr[j]){
this.bulletarr[i].opacity = 0
this.bulletpool.push(this.bulletarr[i])
this.bulletarr.splice(i, 1);
break
}
}
}
}
this.setBulletData = function(lv, bullet, gun, dir, distance,netdata) {
var bdata = gun
var plusdis = 0
if (bullet.effani) {
cc.gameMgr.effectManager.huishoulooteff(bullet.effani.prefabname, bullet.effani)
bullet.effani = null
}
bullet.prefabnode.destroyAllChildren()
bullet.bullet_uniid = netdata.bullet_uniid
bullet.gundata = gun
var sprite = bullet.bullet.getComponent(cc.Sprite);
var sp2 = bullet.shadow.getComponent(cc.Sprite);
bullet.bdistance = (bdata.range + plusdis) * (bdata.range + plusdis);
bullet.bdistanceboom = bdata.range + plusdis
bullet.needEff = null
bullet.isclose = false
bullet.lifetime2 = bdata.missiles_time / 1000
if (!bullet.lifetime2) {
bullet.lifetime2 = 0
}
bullet.rotstartv = 600
bullet.update2start = false
bullet.deadwaittime = 0
bullet.needdead = false
bullet.bullet.opacity = 255
bullet.shadow.opacity = 255
if (bdata.fixrotation) {
bullet.rotation = 0
}
if (bdata.equip_type == 3) {
bullet.isboom = true;
if (!bdata.bullet_eff) {
cc.loader.loadRes("icons/" + bdata.world_img, cc.SpriteFrame, function(err, res) {
if (!err && sprite.isValid) {
sprite.spriteFrame = res;
sprite.sizeMode = 2
sprite.trim = false
sp2.spriteFrame = res;
sp2.sizeMode = 2
sp2.trim = false
}
});
} else {
sprite.spriteFrame = null
sp2.spriteFrame = null
}
bullet.bdistance = distance * distance;
bullet.effect.opacity = 0
// bullet.owner.throw()
bullet.bullet.scale = 1
bullet.shadow.scale = 0.3
} else if (bdata.equip_subtype == 1) {
sprite.spriteFrame = null;
sp2.spriteFrame = null;
bullet.needEff = bdata.hit_fire
// bullet.needEff = "eff_fightfire"
bullet.isclose = true
bullet.effect.opacity = 0
// bullet.owner.shotSound(bullet.gundata.sound_use, bullet.position);
bullet.owner.shotAni(bullet.gundata.sound_use, bullet.position, dir, bullet.gundata.shootfire,bullet.gundata.shootfire2,bullet.rotation,bullet.gundata);
} else {
var bres = bdata.bullet_icon ? bdata.bullet_icon : "b1"
sprite.spriteFrame = cc.atlMgr.bulletatlas.getSpriteFrame(bres)
sprite.sizeMode = 2
sprite.trim = false
sp2.spriteFrame = cc.atlMgr.bulletatlas.getSpriteFrame(bres)
sp2.sizeMode = 2
sp2.trim = false
if (!bdata.isskill) {
bullet.owner.shotAni(bullet.gundata.sound_use, bullet.position, dir, bullet.gundata.shootfire,bullet.gundata.shootfire2,bullet.rotation,bullet.gundata);
}
bullet.effect.opacity = 255
bullet.bullet.scale = 0.5
bullet.shadow.scale = 0.3
// if(bdata.equip_subtype!=8){//
// if(bdata.hit_fire){
// bullet.needEff = bdata.hit_fire
// }else{
// bullet.needEff = "eff_bulletfire"
// }
// }
bullet.needEff = bdata.hit_fire
if (bdata.bullet_life) {
bullet.deadwaittime = bdata.bullet_life / 1000
}
}
if (bdata.bullet_eff) {
var cb = function(res) {
if (bullet && bullet.isValid) {
bullet.effani = res
bullet.effani.prefabname = bdata.bullet_eff
bullet.prefabnode.addChild(bullet.effani);
} else {
console.log("!11111111111111")
}
}
// bullet.prefabname = bdata.bullet_eff
cc.gameMgr.effectManager.createlooteff(bdata.bullet_eff, cb)
}
bullet.bmoveDir = dir;
bullet.bspeed = bdata.bullet_speed;
bullet.width = bullet.height = bdata.bullet_rad * 2
// bullet.bullet.width = bullet.width
// bullet.bullet.height = bullet.height
bullet.effect.width = 20
bullet.startPos = bullet.position
bullet.ispenetrate = bdata.ispenetrate
if(bullet.isup){
bullet.removeFromParent(false)
this.bulletNode.addChild(bullet)
}
bullet.isup = bdata.isup
if(bullet.isup){
bullet.removeFromParent(false)
cc.gameMgr.ndtree.addChild(bullet)
}
if (this.time > 1) {
this.housearr = []
this.playerarr = []
//this.lootarr = []
for (var i = cc.gameMgr.playerarr.length - 1; i >= 0; i--) {
if (cc.gameMgr.playerarr[i].opacity == 255) {
this.playerarr.push(cc.gameMgr.playerarr[i])
}
}
// for(var i = cc.gameMgr.housearr.length-1;i>=0;i--){
// if(cc.gameMgr.housearr[i].opacity==255){
// this.housearr.push(cc.gameMgr.housearr[i])
// }
// }
// for(var i = cc.gameMgr.lootarr.length-1;i>=0;i--){
// if(cc.gameMgr.lootarr[i].opacity==255){
// this.lootarr.push(cc.gameMgr.lootarr[i])
// }
// }
this.time = 0
}
bullet.peoplesound = bdata.sound_hit
}
this.shot = function(gun, dir, pos, owner, shotTime) {
this.createBullet(gun, dir, pos, owner, shotTime);
};
this.checkHouse = function(player, house) {
var count = 0;
for (var i = house.objChildren.length - 1; i >= 0; i--) {
if (Utils.hitTestRectangle(player, house.objChildren[i])) {
return true
}
}
// for(var i=house.doorChildren.length-1;i>=0;i--){
// if(house.doorChildren[i].active && Utils.hitTestRectangle(player, house.doorChildren[i])){
// return true
// }
// }
return false;
};
this.update = function(dt) {
this.time += dt
//var housearr = this.housearr
var playerarr = this.playerarr
// var lootarr = this.lootarr
var vw = 50
var vh = 50
var delta = 0
var dx = 0
var dy = 0
var bt = null
var tpos = null
var eff = null
var bdisb = 0
var bbili = 0
var hitf = null
var bshow = null
var upv = 10
for (var i = this.bulletarr.length - 1; i >= 0; i--) {
bt = this.bulletarr[i];
if (bt.update2start) {
bt.lifetime2 -= dt
bt.rotstartv -= dt * 600
if (bt.rotstartv > 0) {
bt.rotation += dt * bt.rotstartv
}
if (bt.lifetime2 <= 0) {
battlenet.dohit(bt.gundata, null, 4, bt.position)
bt.opacity = 0
bt.shadow.opacity = 0
this.bulletpool.push(bt)
this.bulletarr.splice(i, 1);
}
continue
}
if (bt.needdead) {
bt.deadwaittime -= dt
if (bt.deadwaittime <= 0) {
bt.opacity = 0
this.bulletpool.push(bt)
this.bulletarr.splice(i, 1);
}
continue
}
delta = dt * bt.bspeed;
dx = bt.bmoveDir.x * delta;
dy = bt.bmoveDir.y * delta;
bt.x = bt.x + dx;
bt.y = bt.y + dy;
tpos = bt.position
bshow = bt.shadow
upv = 10
if (bt.isboom) {
upv = 10
bt.rotation += dt * 600
bshow.rotation = bt.rotation
bdisb = Math.sqrt((Math.pow((bt.x - bt.startPos.x), 2) + Math.pow((bt.y - bt.startPos.y), 2)))
bbili = bdisb / bt.bdistanceboom
if (bbili < 0.5) {
bt.bullet.scale = (1 + bbili * 1.5)
} else {
bt.bullet.scale = (1 + (1 - bbili) * 1.5)
}
//bshow.scale = (2-bt.bullet.scale)*0.3
bt.bullet.scale *= boomscale
}
eff = bt.needEff
if (bt&&!bt.ispenetrate) {
for (var j = playerarr.length - 1; j >= 0; j--) {
if (playerarr[j].pctrl.kct||playerarr[j].pctrl.isdead() || playerarr[j].opacity == 0 || bt.owner.node == playerarr[j] || playerarr[j].active == false || playerarr[j].pctrl.horsemode || playerarr[j].pctrl.chuantou>0) {
continue;
}
if (Utils.hitTestCircle(bt, playerarr[j])) {
//bt.opacity = 0
// this.bulletpool.push(bt)
// this.bulletarr.splice(i, 1);
bt.bullet.opacity = 0
bt.shadow.opacity = 0
bt.needdead = true
if (bt.peoplesound) {
cc.gameMgr.playSound(bt.peoplesound, playerarr[j].position)
}
battlenet.dohit(bt.gundata, playerarr[j].objuuid, 1, bt.position, bt.ownerid)
bt = null
break;
}
}
}
if (bt) {
var lootarr = cc.gameMgr.getlootarr({
x: bt.x,
y: bt.y,
width: bt.width * 5,
height: bt.height * 5
})
for (var j = lootarr.length - 1; j >= 0; j--) {
if (lootarr[j].uuid) {
var oneloot = cc.gameMgr.lootmap[lootarr[j].uuid]
if (oneloot.hp <= 0 || oneloot.opacity == 0 || oneloot.istree == 2 || oneloot.istree == 3 || oneloot.event) {
continue;
}
if (oneloot.isdead || oneloot.bullet_hit == 1) {
continue;
}
hitf = oneloot.tp == 2 ? Utils.hitTestRectangle : Utils.hitTestCircle
if (hitf(bt, oneloot)) {
if (oneloot.opacity == 255) {
if (bt.isclose && oneloot.hit_sound2) {
cc.gameMgr.playSound(oneloot.hit_sound2, oneloot.position)
} else {
cc.gameMgr.playSound(oneloot.hit_sound, oneloot.position)
}
}
battlenet.dohit(bt.gundata, oneloot.objuuid, 2, bt.position, bt.ownerid)
// bt.opacity = 0
// this.bulletpool.push(bt)
// this.bulletarr.splice(i, 1);
bt.bullet.opacity = 0
bt.shadow.opacity = 0
bt.needdead = true
bt = null
break;
}
} else {
var oneloot = lootarr[j]
var wall = {
x: oneloot.xx,
y: oneloot.yy,
width: oneloot.width,
height: oneloot.height,
}
if (Utils.hitTestRectangle(bt, wall)) {
cc.gameMgr.playSound("hit_common", bt.position)
// bt.opacity = 0
// this.bulletpool.push(bt)
// this.bulletarr.splice(i, 1);
bt.bullet.opacity = 0
bt.shadow.opacity = 0
bt.needdead = true
battlenet.dohit(bt.gundata, null, 3, bt.position, bt.ownerid)
bt = null
break;
}
}
}
}
if (bt&&!bt.ispenetrate) {
for (var j = cc.gameMgr.npcarr.length - 1; j >= 0; j--) {
let onenpc = cc.gameMgr.npcarr[j]
if (bt.owner.node == onenpc) {
continue;
}
if (Utils.hitTestCircle(bt, onenpc)) {
// bt.opacity = 0
// this.bulletpool.push(bt)
// this.bulletarr.splice(i, 1);
bt.bullet.opacity = 0
bt.shadow.opacity = 0
bt.needdead = true
cc.gameMgr.playSound("hit_common", bt.position)
bt = null
break;
}
}
}
if (bt) {
for (var j = cc.gameMgr.cararr.length - 1; j >= 0; j--) {
let onenpc = cc.gameMgr.cararr[j]
if (bt.owner.node == onenpc) {
continue;
}
if (Utils.hitTestCircle(bt, onenpc)) {
// bt.opacity = 0
// this.bulletpool.push(bt)
// this.bulletarr.splice(i, 1);
bt.bullet.opacity = 0
bt.shadow.opacity = 0
bt.needdead = true
cc.gameMgr.playSound("hit_common", bt.position)
bt = null
break;
}
}
}
// if(bt){
// for(var j=housearr.length-1;j>=0;j--){
// if(Math.abs(housearr[j].x-bt.x)>vw+housearr[j].width/1||Math.abs(housearr[j].y-bt.y>vh+housearr[j].height/1)){
// continue;
// }
// temphouse.width = housearr[j].width*2
// temphouse.height = housearr[j].height*2
// temphouse.x = housearr[j].x
// temphouse.y = housearr[j].y
// if(Utils.hitTestRectangle(bt,temphouse)){
// if(this.checkHouse(bt,housearr[j])){
// cc.gameMgr.playSound("hit_common",bt.position)
// bt.opacity = 0
// this.bulletpool.push(bt)
// this.bulletarr.splice(i, 1);
// battlenet.dohit(bt.gundata,null,3,bt.position,bt.ownerid)
// bt = null
// break;
// }
// }
// }
// }
if (bt == null && eff) {
cc.gameMgr.effectManager.createBulletHit(tpos, eff)
}
if (bt) {
bshow.x = bt.x + upv
bshow.y = bt.y - upv
bdisb = (Math.pow((bt.x - bt.startPos.x), 2) + Math.pow((bt.y - bt.startPos.y), 2))
if (bdisb > bt.bdistance) {
if (bt.isboom) {
bt.update2start = true
bt.shadow.opacity = 0
continue
// battlenet.dohit(bt.gundata,null,4,bt.position)
}
// bt.opacity = 0
// bshow.opacity = 0
// this.bulletpool.push(bt)
// this.bulletarr.splice(i, 1);
bt.bullet.opacity = 0
bt.shadow.opacity = 0
bt.needdead = true
continue;
}
} else {
bshow.opacity = 0
}
}
}
}
module.exports = BulletManage