123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144 |
- // 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 EventName from "../EventName/EventName";
- import { Global } from "../Global";
- import MyComponent from "../Template/MyComponent";
- import Parabola from "./Parabola";
- const { ccclass, property } = cc._decorator;
- @ccclass
- export default class Character extends MyComponent {
- //最高纪录
- @property(cc.RigidBody)
- CharacterRigidBody: cc.RigidBody = null;
- @property(cc.Graphics)
- graphic_line: cc.Graphics = null;
- @property(Parabola)
- 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;
- onLoad() {
- super.onLoad()
- cc.director.getPhysicsManager().enabled = true;
- }
- onDestroy(): void {
- super.onDestroy()
- }
- start() {
- this.regEvent(EventName.YaoGanMove, this.onTouchMove, this)
- this.regEvent(EventName.YaoGanEnd, this.onTouchEnd, this)
- 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
- }
- onTouchMove(dir: cc.Vec3, power: number) {
- // this.CharacterRigidBody.linearVelocity = cc.v2(dir.x * 0.3 * this.JumpConstX, dir.y * 0.3 * this.JumpConstY)
- // return
- if (!this.JumpOk()) {
- return
- }
- 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 < 5/*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);
- }
- // this.graphic_line.fill()
- // this.graphic_line.stroke()
- }
- onTouchEnd(dir: cc.Vec3, power: number) {
- // console.log('onTouchEnd-dir', dir);
- // console.log('onTouchEnd-power', power);
- if (!this.JumpOk()) {
- return
- }
- this.Parabola.clear()
- this.schedule(this.sendHight.bind(this), 0.1, 9999999, 0.1)
- //跳跃
- this.CharacterRigidBody.linearVelocity = cc.v2(dir.x * power * this.JumpConstX, dir.y * power * this.JumpConstY)
- }
- 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
- }
- }
|