123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246 |
- var cfg = require("Configure")
- //布丁状态
- var State = cc.Enum({
- None: 0,
- Jump: 1,
- Drop: 2,
- Dead: 3
- })
- cc.Class({
- extends: cc.Component,
- properties: {
- gravity: 0,
- initJumpSpeed: 0,
- jumpAudio: {
- default: null,
- type: cc.AudioClip
- },
- deadAudio: {
- default: null,
- type: cc.AudioClip
- },
- shield: {
- default: null,
- type: cc.Node
- },
- shieldInvincible: false,
- skSprite: {
- default: null,
- type: cc.Node
- },
- //布丁状态
- _state: {
- default: State.None,
- type: State,
- visible: false
- },
- state: {
- get: function () {
- return this._state;
- },
- set: function (value) {
- if (value !== this._state) {
- this._state = value;
- //var animName = State[this._state];
- //this.anim.stop();
- //this.anim.play(animName);
- if (value != State.Dead) {
- this.skSprite.getComponent("sp.Skeleton").setAnimation(0, cfg.animName[this.colorType][value], value == State.None);
- } else {
- cc.audioEngine.playEffect(this.deadAudio);
- this.skSprite.getComponent("sp.Skeleton").setAnimation(0, cfg.animName[this.colorType][value], false);
- }
- }
- },
- type: State,
- },
- colorType: 0,
- groundY: 0,
- startGroundY: 0
- },
- // LIFE-CYCLE CALLBACKS:
- onLoad: function () {
- this.groundY = (-cc.winSize.height / 2 - 25) * (cc.winSize.width / 750);
- this.node.parent.getChildByName("Ground").setPosition(0, this.groundY);
- this.node.parent.getChildByName("StartGround").setPosition(0, -cc.winSize.height / 2);
- this.startGroundY = this.node.parent.getChildByName("StartGround").y;
- },
- initBuding: function () {
- this.colorType = Math.floor(Math.random() * 4);
- this.node.setPosition(cc.p(0, -200));
- this.skSprite.setPosition(cc.p(0, -200));
- this.node.parent.getChildByName("Ground").setPosition(0, this.groundY);
- var skintype = UserInfo.getSkinIndex();
- var self = this;
- cc.loader.loadRes(cfg.buDingTypePath[skintype][self.colorType], cc.SpriteFrame, function (err, spriteFrame) {
- self.node.getComponent("cc.Sprite").spriteFrame = spriteFrame;
- self.node.active = true;
- self.currentSpeed = 0;
- self.state = State.Drop;
- self.skSprite.active = true;
- self.skSprite.getComponent("sp.Skeleton").setSkin("p" + (skintype + 1));
- self.skSprite.getComponent("sp.Skeleton").setAnimation(0, cfg.animName[self.colorType][self.state], true);
- })
- },
- setSkSpriteVisible: function (bVisible) {
- this.skSprite.active = bVisible;
- },
- stopBuding: function () {
- this.state = State.None;
- this.skSprite.getComponent("sp.Skeleton").setAnimation(0, cfg.animName[this.colorType][this.state], true);
- },
- resumeBuding: function () {
- this.state = State.Drop;
- this.skSprite.getComponent("sp.Skeleton").setAnimation(0, cfg.animName[this.colorType][this.state], true);
- },
- setColorType: function (colorType) {
- this.colorType = colorType;
- },
- jump: function () {
- if (this.state != State.Dead) {
- this.state = State.Jump;
- this.currentSpeed = this.initJumpSpeed;
- //-- 播放跳音效
- cc.audioEngine.playEffect(this.jumpAudio);
- }
- //this.spawnDust('DustUp');
- },
- changeColor: function (colorType) {
- this.colorType = colorType;
- var self = this;
- var skintype = UserInfo.getSkinIndex();
- cc.loader.loadRes(cfg.buDingTypePath[skintype][colorType], cc.SpriteFrame, function (err, spriteFrame) {
- self.node.getComponent("cc.Sprite").spriteFrame = spriteFrame;
- self.skSprite.getComponent("sp.Skeleton").setAnimation(0, cfg.animName[colorType][self.state], true);
- })
- },
- //碰撞检测
- onCollisionEnter: function (other, self) {
- console.log('onCollisionEnter');
- if (other.tag < 4) {
- if (this.colorType == other.tag) {
- cc.log("穿过去了");
- } else {
- cc.log("撞上了");
- if (this.shield.active || this.shieldInvincible) {
- console.log('护盾无敌时间')
- this.removeShield();
- return;
- }
- if (this.state != State.Dead) {
- this.state = State.Dead;
- this.scheduleOnce(this.scheduleCallback, 1);
- }
- }
- } else if (other.tag == 5) {
- cc.log("吃到星星");
- other.node.active = false;
- other.node.parent.getChildByName("ScoreSkSpine").active = true;
- other.node.parent.getChildByName("ScoreSkSpine").getComponent("sp.Skeleton").setAnimation(0, "a1", false);
- this.node.parent.getComponent("GameSence").addScore();
- } else if (other.tag == 4) {
- cc.log("吃到变色块");
- other.node.active = false;
- var colorType = other.node.parent.getComponent("Obstacle").getColorType();
- this.changeColor(colorType);
- this.node.parent.getComponent("GameSence").addLayerNum();
- this.node.parent.getComponent("GameSence").addScore();
- } else if (other.tag == 6) {
- cc.log(this.node.parent.getComponent("GameSence"))
- cc.log("掉到屏幕外 %d", this.node.parent.getChildByName("Ground").y);
- if (this.node.parent.getChildByName("Ground").y > this.groundY) {
- if (this.shield.active) {
- this.removeShield();
- return
- }
- if (this.state != State.Dead) {
- this.state = State.Dead;
- this.scheduleOnce(this.scheduleCallback, 1);
- }
- }
- }
- },
- removeShield() {
- this.shield.active = false;
- this.shieldInvincible = true;
- this.scheduleOnce(() => {
- console.log('复原无敌时间')
- this.shieldInvincible = false;
- }, 1)
- },
- scheduleCallback: function () {
- this.node.parent.getComponent("GameSence").finishGame();
- this.state = State.None;
- },
- update: function (dt) {
- switch (this.state) {
- case State.Jump:
- if (this.currentSpeed < 0) {
- this.state = State.Drop;
- }
- break;
- case State.Drop:
- if (this.node.y < this.startGroundY + 140) {
- this.node.y = this.startGroundY + 140;
- this.shield.y = this.node.y + 5;
- this.skSprite.setPosition(cc.p(0, this.node.y));
- this.state = State.None;
- //this.scheduleOnce(this.scheduleCallback,1);
- }
- break;
- case State.Dead:
- case State.None:
- return;
- }
- var flying = this.state === State.Jump || this.skSprite.y > this.startGroundY + 50;
- if (flying) {
- this.currentSpeed -= dt * this.gravity;
- this.node.y += dt * this.currentSpeed;
- this.shield.y = this.node.y + 5;
- this.skSprite.setPosition(cc.p(0, this.node.y));
- //地面位置
- if (this.skSprite.y > 0 && this.state === State.Jump) {
- this.node.parent.getChildByName("Ground").setPosition(0, this.skSprite.y + this.groundY);
- }
- }
- },
- });
|