"use strict"; cc._RF.push(module, '42eafpA7wRDZ6zpdAZ7t9KK', 'BattleScene'); // scripts/scenes/BattleScene.ts "use strict"; // Learn TypeScript: // - https://docs.cocos.com/creator/manual/en/scripting/typescript.html // Learn Attribute: // - https://docs.cocos.com/creator/manual/en/scripting/reference/attributes.html // Learn life-cycle callbacks: // - https://docs.cocos.com/creator/manual/en/scripting/life-cycle-callbacks.html var __extends = (this && this.__extends) || (function () { var extendStatics = function (d, b) { extendStatics = Object.setPrototypeOf || ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; }; return extendStatics(d, b); }; return function (d, b) { extendStatics(d, b); function __() { this.constructor = d; } d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); }; })(); var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) { var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d; if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc); else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r; return c > 3 && r && Object.defineProperty(target, key, r), r; }; Object.defineProperty(exports, "__esModule", { value: true }); var SaveDataManager_1 = require("../component/SaveDataManager"); var Global_1 = require("../Global"); var BattleConst_1 = require("../kernel/battle/BattleConst"); var BattleManager_1 = require("../kernel/battle/BattleManager"); var PhysicsControl_1 = require("../kernel/physics/PhysicsControl"); var IntroTableMgr_1 = require("../kernel/table/IntroTableMgr"); var TableDefine_1 = require("../kernel/table/TableDefine"); var Ball_1 = require("../prefabs/Ball"); var UIManager_1 = require("../ui/UIManager"); var UIBattle_1 = require("../ui/uiView/UIBattle"); var UITitleData_1 = require("../ui/uiView/UITitleData"); var _a = cc._decorator, ccclass = _a.ccclass, property = _a.property; var BattleScene = /** @class */ (function (_super) { __extends(BattleScene, _super); function BattleScene() { var _this = _super !== null && _super.apply(this, arguments) || this; _this.prefabCollection = null; _this._pointerVec = cc.v2(); _this._ballWorldP = cc.v2(); _this._hitPList = []; _this._hitBallList = []; _this._minShootDeg = 1; return _this; //#endregion } Object.defineProperty(BattleScene.prototype, "mapView", { get: function () { return this._mapView; }, enumerable: false, configurable: true }); //#region LIFE-CYCLE CALLBACKS: BattleScene.prototype.onLoad = function () { this._gameRoot = this.node.getChildByName('GameRoot'); BattleManager_1.BattleManager.ins.eventNode.on(BattleConst_1.BattleEvent.ROUND_END, this.onRoundEnd, this); BattleManager_1.BattleManager.ins.eventNode.on(BattleConst_1.BattleEvent.SKILL_START, this.onSkillStart, this); BattleManager_1.BattleManager.ins.eventNode.on(BattleConst_1.BattleEvent.SKILL_END, this.onSkillEnd, this); BattleManager_1.BattleManager.ins.eventNode.on(BattleConst_1.BattleEvent.BORN_POS_CHANGE, this.onBornPosChange, this); this._minShootDeg = Global_1.tMgr.getConfig(TableDefine_1.TableName.GAME_PLAY, 10).Value; }; BattleScene.prototype.onEnable = function () { this._gameRoot.on(cc.Node.EventType.TOUCH_START, this.onTouchStart, this); UIManager_1.UIManager.ins.widgetLayer.active = false; this.schedule(this.onLazyUpdate, 1 / 20); //预加载弹窗 cc.resources.preload([Global_1.WindowName.BATTLE_RESULT, Global_1.WindowName.USER_SKILL_INFO, Global_1.WindowName.BATTLE_RESULT_PRE_ANI], cc.Prefab); BattleConst_1.getGlobalNode().on(Global_1.GlobalEvent.SHAKE, this.onShake, this); }; BattleScene.prototype.onDisable = function () { this._gameRoot.off(cc.Node.EventType.TOUCH_START, this.onTouchStart, this); this.unschedule(this.onLazyUpdate); }; BattleScene.prototype.onDestroy = function () { BattleManager_1.BattleManager.ins.eventNode.targetOff(this); BattleConst_1.getGlobalNode().targetOff(this); // BattleManager.ins.eventNode.on(BattleEvent.SKILL_START, this.onSkillStart, this); }; BattleScene.prototype.start = function () { this._gameRoot.getComponent(cc.Widget).updateAlignment(); //第二次进场景时 widiget没有更新,手动更新下 // this._diagonal = Math.sqrt(this._gameRoot.width * this._gameRoot.width + this._gameRoot.height * this._gameRoot.height); //对角线长 if (UITitleData_1.UITitleData.id == 0) { BattleManager_1.BattleManager.ins.startBattle(0, this.onMapLoaded, this, this.prefabCollection); } else { BattleManager_1.BattleManager.ins.startBattle(UITitleData_1.UITitleData.id, this.onMapLoaded, this, this.prefabCollection); } var introConfig = IntroTableMgr_1.IntroTableMgr.ins.getTableByLevel(UITitleData_1.UITitleData.id); if (introConfig) { //需要弹提示 UIManager_1.UIManager.ins.openWindow(Global_1.WindowName.INTRO, introConfig.Level); } else { //不需要弹提示,且未使用黄金瞄准期间,概率弹出黄金瞄准 if (Date.now() > SaveDataManager_1.SaveDataManager.ins.runtimeData.goldenAimEndTime) { var rate = Global_1.tMgr.getConfig(TableDefine_1.TableName.GAME_PLAY, 30).Value; if (slib.MathUtil.getRandom(0, 100) < rate) { UIManager_1.UIManager.ins.openWindow(Global_1.WindowName.GOLDEN_AIM); } } } this._gameRoot.getChildByName('tips1').active = false; this._gameRoot.getChildByName('tips2').active = false; this._gameRoot.getChildByName('guideAim').active = false; if (UITitleData_1.UITitleData.id == 1 || UITitleData_1.UITitleData.id == 2) { //提示 this._gameRoot.getChildByName('tips1').active = true; this._gameRoot.getChildByName('tips2').active = true; this._gameRoot.getChildByName('guideAim').active = true; this._gameRoot.getChildByName('tips1').getComponent(cc.Label).string = Global_1.tMgr.getConfig(TableDefine_1.TableName.GAME_PLAY, 28).Comment; this._gameRoot.getChildByName('tips2').getComponent(cc.Label).string = Global_1.tMgr.getConfig(TableDefine_1.TableName.GAME_PLAY, 29).Comment; } }; BattleScene.prototype.onLazyUpdate = function () { if (BattleManager_1.BattleManager.ins.curBattle) { if (BattleManager_1.BattleManager.ins.curBattle.roundRunning) { this._ballNumNode.getComponentInChildren(cc.Label).string = BattleManager_1.BattleManager.ins.curBattle.marblesCollectCount.toString(); } else { var rate = (BattleManager_1.BattleManager.ins.curBattle.doubleShoot ? 2 : 1); this._ballNumNode.getComponentInChildren(cc.Label).string = (BattleManager_1.BattleManager.ins.curBattle.marblesNum * rate).toString(); } } }; // update (dt) {} //#endregion //#region public method //#endregion //#region private method BattleScene.prototype.onMapLoaded = function () { this._mapView = BattleManager_1.BattleManager.ins.curBattle.mapView; var scale = this._gameRoot.width / this._mapView.width; this._mapView.setScale(scale, scale); // 按宽度缩放 this._mapView.setPosition(0, -this._gameRoot.height / 2 + this._mapView.height * scale / 2); //将 mapView和gameRoot底端对齐 BattleManager_1.BattleManager.ins.curBattle.scale = scale; this._gameRoot.addChild(this._mapView); this._spliterNode = this._gameRoot.getChildByName('spliter'); this._spliterNode.removeFromParent(); this._mapView.addChild(this._spliterNode); this._pointerNode = this._gameRoot.getChildByName('imgPointer'); this._pointerNode.removeFromParent(); this._mapView.addChild(this._pointerNode); this._ballNumNode = this._pointerNode.getChildByName('numNode'); if (UITitleData_1.UITitleData.id == 1 || UITitleData_1.UITitleData.id == 2) { //提示 var tips = this._gameRoot.getChildByName('tips1'); var gridPos = (UITitleData_1.UITitleData.id == 1 ? cc.v2(4, 5) : cc.v2(5, 7)); var pos = BattleManager_1.BattleManager.ins.curBattle.mapData.getPixelPosByGridPos(gridPos); this.mapView.convertToWorldSpaceAR(pos, pos); this._gameRoot.convertToNodeSpaceAR(pos, pos); tips.setPosition(pos); gridPos = (UITitleData_1.UITitleData.id == 1 ? cc.v2(7, 4) : cc.v2(10, 6)); pos = BattleManager_1.BattleManager.ins.curBattle.mapData.getPixelPosByGridPos(gridPos); this.mapView.convertToWorldSpaceAR(pos, pos); this._gameRoot.convertToNodeSpaceAR(pos, pos); this._gameRoot.getChildByName('guideAim').active = true; this._gameRoot.getChildByName('guideAim').setPosition(pos); } this.onRoundEnd(); }; BattleScene.prototype.testRayLineNew = function (vec) { this._hitPList.length = 0; var v = cc.v2(); vec.normalize(v); // let speed = (tMgr.getConfig(TableName.GAME_PLAY, 3) as GamePlayTable).Value v.multiplyScalar(200); //速度固定为200,避免高速带来的误差,避免速度太慢带来的性能开销 PhysicsControl_1.PhysicsControl.ins.setPhysicsEnable(true); this._mapView.testBall.setPosition(BattleManager_1.BattleManager.ins.curBattle.marblesBornPos); this._mapView.testBall.getComponent(cc.RigidBody).linearVelocity = v; // let len = (tMgr.getConfig(TableName.GAME_PLAY, 9) as GamePlayTable).Value // let time = len / speed; // for (let i = 0; i < 10; ++i) { // PhysicsControl.ins.update(time); // } // PhysicsControl.ins.setPhysicsEnable(false); var goldenAim = (SaveDataManager_1.SaveDataManager.ins.runtimeData.goldenAimTry || Date.now() <= SaveDataManager_1.SaveDataManager.ins.runtimeData.goldenAimEndTime); var hitCount = (goldenAim ? 3 : 1); while (this._hitPList.length < hitCount) { PhysicsControl_1.PhysicsControl.ins.update(1 / 60); } PhysicsControl_1.PhysicsControl.ins.update(2 / 60); PhysicsControl_1.PhysicsControl.ins.setPhysicsEnable(false); this._hitPList = this._hitPList.slice(0, hitCount); var i = 0; for (i; i < this._hitPList.length; ++i) { if (i == 0) { this.drawLine(this._ballWorldP, this._hitPList[i]); } else { this.drawLine(this._hitPList[i - 1], this._hitPList[i]); } var ball = void 0; if (i < this._hitBallList.length) { ball = this._hitBallList[i]; } else { ball = cc.instantiate(BattleManager_1.BattleManager.ins.curBattle.prefabCollection.getChildByName('ball')); ball.getComponent(Ball_1.default).scaleEnable = false; ball.getComponent(cc.Collider).enabled = false; ball.getChildByName('img').getComponent(cc.Sprite).sizeMode = cc.Sprite.SizeMode.CUSTOM; var colliderSize = ball.getComponent(cc.PhysicsCircleCollider).radius * 2; ball.getChildByName('img').setContentSize(colliderSize, colliderSize); ball.getChildByName('img').scale = 1; // ball.scale = 0.5; this._hitBallList.push(ball); this._gameRoot.addChild(ball); } ball.active = true; var pos = this._gameRoot.convertToNodeSpaceAR(this._hitPList[i]); ball.setPosition(pos); } for (i; i < this._hitBallList.length; ++i) { this._hitBallList[i].active = false; } // let t = this._mapView.convertToWorldSpaceAR(this._mapView.testBall.position); // let tt = cc.v2(t.x, t.y); // if (this._hitPList.length == 0) { // this.drawLine(this._ballWorldP, tt); // } // else { // this.drawLine(this._hitPList[this._hitPList.length - 1], tt); // } // this._lastCollisionV.normalizeSelf(); // this._lastCollisionV.multiplyScalar(100); var lastPoint = this._hitPList[this._hitPList.length - 1]; var t = this._mapView.convertToWorldSpaceAR(this._mapView.testBall.position); var tt = cc.v2(t.x - lastPoint.x, t.y - lastPoint.y); //从终点指向小球的向量 tt.normalizeSelf(); tt.multiplyScalar(Global_1.tMgr.getConfig(TableDefine_1.TableName.GAME_PLAY, 9).Value); tt.x = lastPoint.x + tt.x; tt.y = lastPoint.y + tt.y; this.drawLine(this._hitPList[this._hitPList.length - 1], tt); }; // private _lastCollisionV: cc.Vec2 = new cc.Vec2(); BattleScene.prototype.onTestBallCollision = function (contact, selfCollider, otherCollider) { // let manifold = contact.getWorldManifold(); // if (manifold.points.length > 0) { // this._hitPList.push(cc.v2(manifold.points[0].x, manifold.points[0].y)); // } this._hitPList.push(selfCollider.node.convertToWorldSpaceAR(cc.Vec2.ZERO)); }; BattleScene.prototype.drawLine = function (worldStart, worldEnd) { var goldenAim = (SaveDataManager_1.SaveDataManager.ins.runtimeData.goldenAimTry || Date.now() <= SaveDataManager_1.SaveDataManager.ins.runtimeData.goldenAimEndTime); var g = this._gameRoot.getComponent(cc.Graphics); g.fillColor.fromHEX(goldenAim ? '#FFFF00' : '#FFFFFF'); var gStart = this._gameRoot.convertToNodeSpaceAR(worldStart); var gEnd = this._gameRoot.convertToNodeSpaceAR(worldEnd); var vec = cc.v2(); cc.Vec2.subtract(vec, gEnd, gStart); var len = vec.mag(); var step = 0; var lerpVec = cc.v2(); while (true) { var ratio = step / len; var end = false; if (ratio > 1) { ratio = 1; end = true; } gEnd.lerp(gStart, ratio, lerpVec); // g.moveTo(); g.circle(lerpVec.x, lerpVec.y, 3); g.fill(); step += 20; if (end) { break; } } }; BattleScene.prototype.changePointer = function (double) { // let ballNode = this._pointerNode.getChildByName('ball'); // ballNode.active = !double; this._spliterNode.active = double; if (double) { BattleManager_1.BattleManager.ins.shootTouchEnable = false; this._spliterNode.getComponent(cc.Animation).once('finished', function () { BattleManager_1.BattleManager.ins.shootTouchEnable = true; }, this); this._spliterNode.getComponent(cc.Animation).play('ani_gun001'); this._spliterNode.setPosition(BattleManager_1.BattleManager.ins.curBattle.marblesBornPos); } }; //#endregion //#region event BattleScene.prototype.onTouchStart = function (e) { if (!BattleManager_1.BattleManager.ins.shootTouchEnable) { return; } this.node.getChildByName('UIBattle').getComponent(UIBattle_1.default).setCancelNodeVisible(true); this._gameRoot.on(cc.Node.EventType.TOUCH_MOVE, this.onTouchMove, this); this._gameRoot.on(cc.Node.EventType.TOUCH_END, this.onTouchEnd, this); this._gameRoot.on(cc.Node.EventType.TOUCH_CANCEL, this.onTouchCancel, this); this._mapView.testBall.active = true; this.onTouchMove(e); var chargingNode = this._pointerNode.getChildByName('chargingNode'); chargingNode.active = true; chargingNode.getComponent(cc.Animation).play('fx_xuli_001'); }; BattleScene.prototype.onTouchMove = function (e) { var touchP = e.getLocation(); // cc.Vec2.subtract(this._pointerVec, touchP, this._ballWorldP); var deg = slib.MathUtil.get360DegByVec(touchP.x - this._ballWorldP.x, touchP.y - this._ballWorldP.y); if (deg < this._minShootDeg || deg > 180 - this._minShootDeg) { this.node.getChildByName('UIBattle').getComponent(UIBattle_1.default).setCancelNodeHit(true); return; } this.node.getChildByName('UIBattle').getComponent(UIBattle_1.default).setCancelNodeHit(false); cc.Vec2.subtract(this._pointerVec, touchP, this._ballWorldP); this._gameRoot.getComponent(cc.Graphics).clear(); this.testRayLineNew(this._pointerVec); //旋转指针 if (BattleManager_1.BattleManager.ins.curBattle.doubleShoot) { var deg_1 = slib.MathUtil.get360DegByVec(this._pointerVec.x, this._pointerVec.y); var nodeA = this._spliterNode.getChildByName('ani_guna'); var nodeB = this._spliterNode.getChildByName('ani_gunb'); nodeA.angle = deg_1 - 90; //指向正上方 nodeB.angle = (90 - deg_1); var otherPointer = cc.v2(-this._pointerVec.x, this._pointerVec.y); this.testRayLineNew(otherPointer); } }; BattleScene.prototype.onTouchCancel = function () { var pointer = this._mapView.getChildByName('imgPointer'); pointer.angle = 0; // let g = this._gameRoot.getComponent(cc.Graphics); // g.clear(); this._gameRoot.off(cc.Node.EventType.TOUCH_MOVE, this.onTouchMove, this); this._gameRoot.off(cc.Node.EventType.TOUCH_END, this.onTouchEnd, this); this._gameRoot.off(cc.Node.EventType.TOUCH_CANCEL, this.onTouchCancel, this); this.node.getChildByName('UIBattle').getComponent(UIBattle_1.default).setCancelNodeVisible(false); var chargingNode = this._pointerNode.getChildByName('chargingNode'); chargingNode.getComponent(cc.Animation).stop('fx_xuli_001'); chargingNode.active = false; this._mapView.testBall.active = false; this._gameRoot.getComponent(cc.Graphics).clear(); for (var i = 0; i < this._hitBallList.length; ++i) { this._hitBallList[i].active = false; } }; BattleScene.prototype.onTouchEnd = function () { this.onTouchCancel(); BattleManager_1.BattleManager.ins.curBattle.shoot(this._pointerVec); this._gameRoot.getChildByName('tips1').active = false; this._gameRoot.getChildByName('guideAim').active = false; }; BattleScene.prototype.onSkillStart = function (skillId) { if (skillId == BattleConst_1.UserSkill.DOUBLE_SHOOT) { this.changePointer(true); } else if (skillId == BattleConst_1.UserSkill.SHIELD) { this.setShieldEnable(true); } }; BattleScene.prototype.onSkillEnd = function (skillId) { if (skillId == BattleConst_1.UserSkill.SHIELD) { this.setShieldEnable(false); } }; BattleScene.prototype.onBornPosChange = function () { this._pointerNode.getChildByName('ball').active = true; this._pointerNode.setPosition(BattleManager_1.BattleManager.ins.curBattle.marblesBornPosNextRound); }; BattleScene.prototype.onRoundEnd = function () { this._ballWorldP = this._mapView.convertToWorldSpaceAR(BattleManager_1.BattleManager.ins.curBattle.marblesBornPos); this._pointerNode.setPosition(BattleManager_1.BattleManager.ins.curBattle.marblesBornPos); this.changePointer(false); }; BattleScene.prototype.setShieldEnable = function (value) { var effectNode = cc.find('GameRoot/borderCollector/effect', this.node); effectNode.active = value; if (value) { var duration = effectNode.getComponent(cc.Animation).play('fx_gl_chzd_001').duration; this.schedule(function () { effectNode.getComponent(cc.Animation).play('fx_gl_chzd_002'); }, duration, 1); } else { // this.unscheduleAllCallbacks(); } }; BattleScene.prototype.onShake = function (value) { var ani = this.node.getChildByName('Main Camera').getComponent(cc.Animation); if (value) { ani.stop('ani_shake'); ani.play('ani_shake'); } else { this.node.getChildByName('Main Camera').setPosition(0, 0, 0); // let state = ani.getAnimationState('ani_shake'); // state.time = 0; // state.stop(); ani.stop('ani_shake'); } // ani.play('ani_shake'); }; __decorate([ property(cc.Prefab) ], BattleScene.prototype, "prefabCollection", void 0); BattleScene = __decorate([ ccclass ], BattleScene); return BattleScene; }(cc.Component)); exports.default = BattleScene; cc._RF.pop();