Created
October 16, 2017 02:35
-
-
Save nshermione/0286a442a47934b6e11f9e83caec3ce0 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import {GlobalInfo} from './../core/GlobalInfo'; | |
import {ResourceManager} from '../core/ResourceManager'; | |
import {Cell} from './Cell'; | |
import {Random} from '../core/Random'; | |
import {Config} from '../Config'; | |
import {MODEL_TYPE} from '../core/Constant'; | |
import {ShaderComponent} from '../core/ShaderComponent'; | |
const {ccclass, property} = cc._decorator; | |
@ccclass | |
export class Column extends ShaderComponent { | |
numRow = Config.numRow; | |
cellSize = Config.cellSize; | |
spanX = Config.cellSpanX; | |
spanY = Config.cellSpanY; | |
startY = 0; | |
blockSize = Config.spinBlockSize; | |
items = []; | |
spinDirection = -1; | |
isMiddlePos = false; | |
isPrevMiddlePos = false; | |
@property(cc.Prefab) | |
cell: cc.Prefab; | |
@property(cc.Prefab) | |
winEffect: cc.Prefab; | |
@property(cc.Prefab) | |
winEffectSpecial: cc.Prefab; | |
lastY = 0; | |
running = false; | |
blockIndex = 0; | |
effects = []; | |
accel | |
onLoad() { | |
} | |
updateProperty() { | |
switch (GlobalInfo.curMachine.modeltypeid) { | |
case MODEL_TYPE.ONE_THREE: | |
this.startY = 15; | |
this.spanY = 80; | |
break; | |
default: | |
this.startY = -5; | |
this.spanX = Config.cellSpanX; | |
this.spanY = Config.cellSpanY; | |
break; | |
} | |
} | |
setSize(numRow) { | |
this.updateProperty(); | |
let resourceMgr = ResourceManager.getInstance(); | |
this.numRow = numRow; | |
for (let i = 0; i < this.numRow * this.blockSize; i++) { | |
let item = cc.instantiate(this.cell); | |
let cellComp = item.getComponent(Cell); | |
let itemId = Random.fromList(GlobalInfo.curMachine.listitemid); | |
cellComp.setItemId(itemId); | |
item.x = this.cellSize / 2; | |
item.y = -i * (this.cellSize + this.spanY) - this.spanY / 2 - this.cellSize / 2 + this.spanY + this.startY; | |
this.node.addChild(item); | |
this.items.push(item); | |
} | |
if (this.spinDirection === -1) { | |
let endBlockY = Math.floor(this.blockSize - 1) * this.numRow * (this.cellSize + this.spanY) + this.startY; | |
this.node.y += endBlockY; | |
this.blockIndex = this.blockSize - 1; | |
} | |
} | |
setWidth(width) { | |
this.cellSize = width; | |
} | |
/** | |
* | |
* @param spinRounds One round = one block | |
* @param result list of item id in column | |
*/ | |
spin(spinRounds) { | |
this.lastY = this.node.y; | |
this.running = true; | |
let rowTimes: any = spinRounds * this.numRow; | |
let totalDistance = rowTimes * (this.cellSize + this.spanY); | |
if (this.isPrevMiddlePos) { | |
totalDistance += (this.cellSize + this.spanY) / 2; | |
this.isPrevMiddlePos = false; | |
} | |
if (this.isMiddlePos) { | |
totalDistance -= (this.cellSize + this.spanY) / 2; | |
this.isPrevMiddlePos = true; | |
this.isMiddlePos = false; | |
} | |
let moveAction = cc.sequence( | |
cc.moveTo( | |
Config.spinDuration, | |
cc.v2(this.node.x, this.node.y + this.spinDirection * totalDistance) | |
).easing(cc.easeCubicActionInOut()), | |
cc.delayTime(Config.spinCompleteDelay), | |
cc.callFunc(() => this.onSpinComplete(), this) | |
); | |
this.node.runAction(moveAction); | |
} | |
setResult(spinRounds, result) { | |
let resMgr = ResourceManager.getInstance(); | |
this.blockIndex = this.getResultBlockIndex(spinRounds); | |
let resultStartIndex = this.numRow * this.blockIndex; | |
for (let i = 0; i < this.numRow; i++) { | |
let item = this.node.children[resultStartIndex + i]; | |
let cellComp: Cell = item.getComponent(Cell); | |
cellComp.setItemId(result[i]); | |
} | |
if (GlobalInfo.curMachine.modeltypeid == MODEL_TYPE.ONE_THREE) { | |
if (result[1] == 0) { | |
// Empty | |
result[1] = result[2]; | |
result[2] = Random.fromList(GlobalInfo.curMachine.listitemid); | |
this.isMiddlePos = true; | |
} | |
} | |
} | |
playCellAnim(spinRounds, result, colSpinResult?) { | |
cc.log('Play Column Cell Anim'); | |
let duration = 0; | |
let actions = [ | |
cc.callFunc(() => { | |
this.stopAllCellAnim(); | |
}), | |
cc.delayTime(0.1) | |
]; | |
for (let i = 0; i < this.numRow; i++) { | |
if (result[i]) { | |
let effect; | |
if (colSpinResult && [1, 2, 7].indexOf(colSpinResult[i]) > -1) { | |
effect = cc.instantiate(this.winEffectSpecial); | |
} else { | |
effect = cc.instantiate(this.winEffect); | |
} | |
let spine = effect.getComponent(sp.Skeleton); | |
spine.addComponent(Column); | |
actions.push( | |
cc.callFunc(() => { | |
let startIndex = this.numRow * this.blockIndex; | |
let item = this.node.children[startIndex + i]; | |
let itemCell = item.getComponent(Cell); | |
itemCell.addEffect(effect); | |
spine.animation = 'animation'; | |
this.effects.push(effect); | |
}), | |
cc.delayTime(2) | |
); | |
actions.push(cc.delayTime(Config.detailStepDuration)); | |
} | |
} | |
if (actions.length == 0) { | |
actions.push(cc.callFunc(() => { | |
})); | |
} | |
return actions.length > 1 ? cc.spawn(actions) : actions[0]; | |
} | |
stopAllCellAnim() { | |
for (let effect of this.effects) { | |
effect.removeFromParent(); | |
} | |
this.effects = []; | |
this.node.stopAllActions(); | |
} | |
updateRowSprite(blurId) { | |
for (let item of this.node.children) { | |
let cellComp = item.getComponent(Cell); | |
// cellComp.setItemBlurId(blurId); | |
} | |
} | |
updateRow() { | |
let blockDistance = this.blockSize * this.numRow * (this.cellSize + this.spanY); | |
let item; | |
if (this.spinDirection === 1) { | |
item = this.items.shift(); | |
item.y = item.y - this.spinDirection * blockDistance; | |
this.items.push(item); | |
} else if (this.spinDirection === -1) { | |
item = this.items.pop(); | |
item.y = item.y - this.spinDirection * blockDistance; | |
this.items.unshift(item); | |
} | |
} | |
onWinEffectEnd() { | |
// let anim = this.node.getComponent(cc.Animation); | |
// let animState = anim.getAnimationState(anim.defaultClip.name); | |
// if ((<any>this.node).repeatCount === undefined) { | |
// (<any>this.node).repeatCount = 1; | |
// } else { | |
// (<any>this.node).repeatCount++; | |
// } | |
// if ((<any>this.node).repeatCount >= animState.repeatCount) { | |
// this.node.removeFromParent(); | |
// } | |
} | |
update(dt) { | |
let nUpdates = Math.floor(Math.abs(this.node.y - this.lastY) / this.cellSize); | |
if (this.running && nUpdates >= 1) { | |
for (let i = 0; i < nUpdates; i++) { | |
this.updateRow(); | |
this.lastY += this.spinDirection * (this.cellSize + this.spanY); | |
} | |
} | |
if (!this.running) { | |
this.lastY = this.node.y; | |
} | |
} | |
onSpinComplete() { | |
this.running = false; | |
} | |
private getResultBlockIndex(spinRounds) { | |
if (this.spinDirection === -1) { | |
let nTime = Math.ceil(spinRounds / this.blockSize) + 2; | |
return (this.blockIndex + Math.abs(-spinRounds + nTime * this.blockSize)) % this.blockSize; | |
} else if (this.spinDirection === 1) { | |
return (this.blockIndex + spinRounds) % this.blockSize; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment