123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217 |
- // Learn cc.Class:
- // - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/class.html
- // - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/class.html
- // Learn Attribute:
- // - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/reference/attributes.html
- // - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/reference/attributes.html
- // Learn life-cycle callbacks:
- // - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/life-cycle-callbacks.html
- // - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/life-cycle-callbacks.html
- var Module = require('Module');
- const RoomState = {
- Invaild: -1,
- Lock: 0,
- UnLock: 1,
- Update: 2,
- Full: 3
- };
- cc.Class({
- extends: cc.Component,
- properties: {
- roomSprite: {
- default: null,
- type: cc.Sprite
- },
- animationImgNode: {
- default: null,
- type: cc.Node
- },
- roomBuySprites: {
- default: [],
- type: cc.SpriteFrame
- },
- buyButton: {
- default: null,
- type: cc.Button
- },
- priceLabel: {
- default: null,
- type: cc.Label
- },
- buyTipPrefab: {
- default: null,
- type: cc.Prefab
- },
- levelLabel: {
- default: null,
- type: cc.Label
- },
- earnMoneyLabel: {
- default: null,
- type: cc.Label
- },
- },
- init: function(config, totalMoney) {
- this.config = config;
- this.node.setPosition(config.x, config.y);
- this.node.width = config.width;
- this.node.height = config.height;
- let price = config.price[config.lv];
- if (totalMoney >= price) {
- this.setState(RoomState.Update);
- } else {
- this.setState(RoomState.Lock);
- }
- this.priceLabel.string = price.toString();
- this.levelLabel.string = "LV." + config.lv.toString();
- // 从本地资源中读取SpriteFrame
- cc.loader.loadRes("room/" + config.img, cc.SpriteFrame, (error, spriteFrame)=>{
- if (error) {
- console.log(error);
- } else {
- this.roomSprite.spriteFrame = spriteFrame;
- this.animationImgNode.getComponent(cc.Sprite).spriteFrame = spriteFrame;
- }
- });
- },
- // LIFE-CYCLE CALLBACKS:
- onLoad () {
- this.config = null;
- this.setState(RoomState.Invaild);
- this.setSchedule();
- },
- setSchedule: function() {
- this.schedule(function () {
- if (this.isEarnMoney()) {
- let price = this.config.speed[this.config.lv];
- this.earnMoneyLabel.string = "$" + price.toString();
- let moveAction = cc.moveBy(0.5, cc.p(0, 200));
- // let sqawn = cc.spawn([moveAction, cc.fadeOut(0.5)]);
- let callBack = cc.callFunc(function () {
- this.earnMoneyLabel.node.setPosition(cc.p(0, 0));
- // this.earnMoneyLabel.node.opacity = 255;
- Global.GameEvent.fire("earn_money", price);
- }, this);
- let sequence = cc.sequence(moveAction, callBack);
- this.earnMoneyLabel.node.runAction(sequence);
- }
- }, 1.0);
- },
- clickBuyButton: function() {
- if (Module.game.getComponent('game').getBuyTipNode() !== null) {
- return;
- }
- if (this.config !== undefined && this.state === RoomState.Update) {
- if (this.config.lv > 0) {
- let buyTipPrefab = cc.instantiate(this.buyTipPrefab);
- buyTipPrefab.getComponent('BuyTip').init(this, this.config, this.roomSprite.spriteFrame);
- buyTipPrefab.setScale(0, 0);
- buyTipPrefab.parent = Module.game;
- let action = cc.scaleTo(0.2, 1.0, 1.0).easing(cc.easeOut(0.95));
- buyTipPrefab.runAction(action);
- Module.game.getComponent('game').setBuyTipNode(buyTipPrefab);
- } else {
- this.updateAnimation();
- Global.GameEvent.fire("room_update", this);
- }
- }
- },
- isEarnMoney: function() {
- if (this.config.lv === 0) {
- return false;
- }
- return true;
- },
- getRoomConfig: function() {
- return this.config;
- },
- unlockUpdate: function() {
- this.setState(RoomState.Update);
- },
- lockUpdate: function() {
- this.setState(RoomState.Lock);
- },
- full: function() {
- this.setState(RoomState.Full);
- },
- setState: function(state) {
- if (this.state === state) {
- return;
- }
- switch (state) {
- case RoomState.Lock:
- this.buyButton.getComponent(cc.Button).normalSprite = this.roomBuySprites[0];
- break;
- case RoomState.Update:
- this.buyButton.getComponent(cc.Button).normalSprite = this.roomBuySprites[1];
- break;
- case RoomState.Full:
- this.buyButton.node.active = false;
- default:
- break;
- }
- this.state = state;
- },
- updateAnimation: function () {
- let scaleAction = cc.scaleTo(0.35, 1.5, 1.5);
- let spawn = cc.spawn([scaleAction, cc.fadeOut(0.35)]);
- let callBack = cc.callFunc(function () {
- this.animationImgNode.opacity = 255;
- this.animationImgNode.setScale(1.0, 1.0);
- }, this);
- let sequence = cc.sequence(spawn, callBack);
- this.animationImgNode.runAction(sequence);
- },
- // update (dt) {},
- });
|