// Learn TypeScript: // - https://docs.cocos.com/creator/2.4/manual/en/scripting/typescript.html // Learn Attribute: // - https://docs.cocos.com/creator/2.4/manual/en/scripting/reference/attributes.html // Learn life-cycle callbacks: // - https://docs.cocos.com/creator/2.4/manual/en/scripting/life-cycle-callbacks.html import CanmeraScrpts from "../Canmera/CanmeraScrpts"; import EventName from "../EventName/EventName"; import { Global } from "../Global"; import MyComponent from "../Template/MyComponent"; import RewardMnr from "../Template/RewardMnr"; import Parabola from "./Parabola"; import YaoGan from "./YaoGan"; export enum SpineAnimationState { Null = 'Null', Idle = 'idle', Xuli = 'ready', Fly = 'jump', Round = 'dowm' } const { ccclass, property } = cc._decorator; @ccclass export default class Character extends MyComponent { @property(cc.RigidBody) CharacterRigidBody: cc.RigidBody = null; @property(sp.Skeleton) Spine: sp.Skeleton = null; Parabola: Parabola = null; //临时物理值============================================ UpPower: number = 100; Xscale: number = 4; Yscale: number = 4; //跳跃的常量 JumpConstX: number = 0; JumpConstY: number = 0; // LIFE-CYCLE CALLBACKS: //游戏的重量G值 G: number = 4; //人物临时值 Jump_before_Data: cc.Vec2 = null; Jump_late_Data: cc.Vec2 = null; FirstBornRoundFlag: boolean = true; onLoad() { super.onLoad() cc.director.getPhysicsManager().enabled = true; cc.director.getCollisionManager().enabled = true; } onDestroy(): void { super.onDestroy() } start() { this.regEvent(EventName.YaoGanMove, this.onTouchMove, this) this.regEvent(EventName.YaoGanEnd, this.onTouchEnd, this) this.regEvent(EventName.YaoGanCancel, this.YaoGanCancel, this) this.regEvent(EventName.ChangeRoleState, this.ChangeRoleState, this) this.regEvent(EventName.UseSupjump, this.UseSupjump, this) this.regEvent(EventName.UseFly, this.UseFly, this) // cc.director.getPhysicsManager().debugDrawFlags = cc.PhysicsManager.DrawBits.e_aabbBit | // cc.PhysicsManager.DrawBits.e_jointBit | // cc.PhysicsManager.DrawBits.e_shapeBit // ; //开启碰撞监听 this.CharacterRigidBody.enabledContactListener = true; this.UpPower = Global.UpPower this.Xscale = Global.Xscale this.Yscale = Global.Yscale cc.director.getPhysicsManager().gravity = cc.v2(0, Global.G) this.G = cc.director.getPhysicsManager().gravity.y this.JumpConstX = this.UpPower * this.Xscale this.JumpConstY = this.UpPower * this.Yscale let ParabolaCom = cc.Canvas.instance.node.getChildByName("Parabola").getComponent(Parabola) this.Parabola = ParabolaCom let YaoGanComp = cc.Camera.main.node.getChildByName("YaoGan").getComponent(YaoGan) YaoGanComp.CharacterComp = this } onBeginContact(contact, selfCollider: cc.PhysicsBoxCollider, otherCollider) { if (selfCollider.tag == 1) { cc.systemEvent.emit(EventName.ChangeRoleState, SpineAnimationState.Round); if (this.FirstBornRoundFlag) { this.FirstBornRoundFlag = false cc.Camera.main.node.getComponent(CanmeraScrpts).BronAni() } } } YaoGanCancel() { this.Parabola.clear() } protected update(dt: number): void { if (this.Fly && this.lastFlydir && this.lastFlypower) { this.CharacterRigidBody.linearVelocity = cc.v2(this.lastFlydir.x * 0.3 * this.JumpConstX, this.lastFlydir.y * 0.5 * this.JumpConstY) return } } lastFlydir: cc.Vec3 lastFlypower: number onTouchMove(dir: cc.Vec3, power: number) { if (this.Fly) { // console.log('----------------'); // console.log(dir.x); // console.log(this.JumpConstX); // console.log(this.JumpConstY); this.lastFlydir = dir this.lastFlypower = power this.Parabola.clear() return } if (!this.JumpOk()) { return } //抛物线点是从人物的锚点开始的 let TempCount = 0 if (RewardMnr.getInstance().ParabolaCount > 0) { TempCount = 15 } else { TempCount = 5 } this.Parabola.clear() let Temp = cc.v2(dir.x * power * this.JumpConstX, dir.y * power * this.JumpConstY) let START_POS = cc.v2(this.node.position.x, this.node.position.y) const dt = 0.2; for (let count = 1; count < TempCount/*5*/; count++) { const time = dt * count; // s = v_x * t const dx = Temp.x * time; // h = v_y * t + 0.5 * a * t * t const dy = Temp.y * time + 0.5 * this.G * this.CharacterRigidBody.gravityScale * time * time; // 当前时间点坐标 const targetX = START_POS.x + dx; const targetY = START_POS.y + dy; // 坐标超过地板就不画了 // if (targetY < START_POS.y) break; this.Parabola.circle(targetX, targetY); } } onTouchEnd(dir: cc.Vec3, power: number) { // console.log('onTouchEnd-dir', dir); // console.log('onTouchEnd-power', power);\ if (!this.JumpOk()) { return } this.Parabola.clear() RewardMnr.getInstance().useOnceParabola() this.schedule(this.sendHight.bind(this), 0.1, 9999999, 0.1) if (dir.x >= 0) { this.node.scaleX = Math.abs(this.node.scaleX) } else { this.node.scaleX = -Math.abs(this.node.scaleX) } //跳跃 this.CharacterRigidBody.linearVelocity = cc.v2(dir.x * power * this.JumpConstX, dir.y * power * this.JumpConstY) } UseSupjump() { //跳跃 this.CharacterRigidBody.linearVelocity = cc.v2(0, 2 * this.JumpConstY) let CircleColliders = this.getComponents(cc.PhysicsCircleCollider) let CircleColliderComp: cc.PhysicsCircleCollider = null CircleColliders.forEach(e => { if (e.tag == 0) { CircleColliderComp = e CircleColliderComp.enabled = false } }) this.scheduleOnce(() => { CircleColliderComp.enabled = true }, 1) } //是否可以飞 Fly: boolean = false UseFly() { this.Fly = true this.scheduleOnce(() => { this.Fly = false this.lastFlydir = null this.lastFlypower = null }, 5) } sendHight() { this.unschedule(this.sendHight) cc.systemEvent.emit(EventName.Update_CurrentHight, this.getHight()) } getHight(): number { let y = this.node.getPosition().y y = y + (1334 / 2) + (this.node.getContentSize().height / 2) return parseInt(y.toString()) } JumpOk() { if (this.CharacterRigidBody.awake == false) { return true } return false } currentState: SpineAnimationState = SpineAnimationState.Idle; changeState: SpineAnimationState; ChangeRoleState(changeState: SpineAnimationState) { this.changeState = changeState switch (this.currentState) { case SpineAnimationState.Idle: switch (this.changeState) { case SpineAnimationState.Idle: break; case SpineAnimationState.Xuli: this.currentState = SpineAnimationState.Xuli; this.changeState = SpineAnimationState.Null; this.Spine.setAnimation(0, SpineAnimationState.Xuli, false); break; case SpineAnimationState.Fly: //cc.error('不可以 从站立直接转向空中'); break; case SpineAnimationState.Round: //cc.error('不可以 从站立直接转向落地'); break; default: break; } break; case SpineAnimationState.Xuli: switch (this.changeState) { case SpineAnimationState.Idle: this.currentState = SpineAnimationState.Idle; this.changeState = SpineAnimationState.Null; this.Spine.setAnimation(0, SpineAnimationState.Idle, true); break; case SpineAnimationState.Xuli: //cc.error('蓄力->蓄力'); break; case SpineAnimationState.Fly: this.currentState = SpineAnimationState.Fly; this.changeState = SpineAnimationState.Null; this.Spine.setAnimation(0, SpineAnimationState.Fly, false); break; case SpineAnimationState.Round: //cc.error('不可以 从站立直接转向落地'); break; default: break; } break; case SpineAnimationState.Fly: switch (this.changeState) { case SpineAnimationState.Idle: //cc.error('飞翔->站立'); break; case SpineAnimationState.Xuli: //cc.error('飞翔->蓄力'); break; case SpineAnimationState.Fly: //cc.error('飞翔->飞翔'); break; case SpineAnimationState.Round: this.currentState = SpineAnimationState.Round; this.changeState = SpineAnimationState.Null; let RoundState = this.Spine.setAnimation(0, SpineAnimationState.Round, false); this.scheduleOnce(() => { cc.systemEvent.emit(EventName.ChangeRoleState, SpineAnimationState.Idle); }, RoundState.animationEnd) break; default: break; } break; case SpineAnimationState.Round: switch (this.changeState) { case SpineAnimationState.Idle: this.currentState = SpineAnimationState.Idle; this.changeState = SpineAnimationState.Null; this.Spine.setAnimation(0, SpineAnimationState.Idle, true); break; case SpineAnimationState.Xuli: //cc.error('落地->蓄力'); break; case SpineAnimationState.Fly: //cc.error('落地->飞翔'); break; case SpineAnimationState.Round: //cc.error('落地->落地'); break; default: break; } break; default: break; } } }