You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
330 lines
11 KiB
330 lines
11 KiB
|
|
import { ryw_Event } from "../../../FrameWork/Event/EventEnum";
|
|
import EventMgr from "../../../FrameWork/Event/EventMgr";
|
|
import Common5 from "../../../Platform/th/Common5";
|
|
import YiMaJiang from "./YiMaJiang";
|
|
import YiMaJiangManager, { MoveDir } from "./YiMaJiangManager";
|
|
|
|
const {ccclass, property} = cc._decorator;
|
|
|
|
|
|
@ccclass
|
|
export default class SingleMaJiang extends cc.Component {
|
|
|
|
@property(cc.Node)
|
|
spFrame: cc.Node = null;
|
|
|
|
@property(cc.Node)
|
|
tipIcon: cc.Node = null;
|
|
|
|
// LIFE-CYCLE CALLBACKS:
|
|
|
|
// onLoad () {}
|
|
|
|
posX:number = 0
|
|
posY:number = 0
|
|
cardNum:number = 0
|
|
_xx:number = 0
|
|
_yy:number = 0
|
|
|
|
|
|
m_x:number = 0//触摸开始的点
|
|
m_y:number = 0//触摸开始的点
|
|
m_startMove:boolean = false//是否开始移动
|
|
m_dir:MoveDir = MoveDir.None//移动方向
|
|
|
|
start () {
|
|
|
|
}
|
|
initCard(x,y,cardNum){
|
|
this.node.zIndex = 0
|
|
|
|
this.posX = x
|
|
this.posY = y
|
|
this.cardNum = cardNum
|
|
|
|
this.setSpFrame(x,y,cardNum)
|
|
this.openTouchEvent(this.node)
|
|
}
|
|
//翻牌
|
|
turnAroundCard(x,y,cardNum){
|
|
cc.tween(this.node)
|
|
.to(0.1,{scaleX:0})
|
|
.call(()=>{
|
|
this.initCard(x,y,cardNum)
|
|
})
|
|
.to(0.1,{scaleX:1})
|
|
.call(()=>{
|
|
YiMaJiang.getInstance().setIsXiaoChuAction(false)
|
|
})
|
|
.start()
|
|
}
|
|
showTipIcon(bShow){
|
|
this.tipIcon.active = bShow
|
|
}
|
|
moveToOriginPos(){
|
|
this.node.x = this._xx
|
|
this.node.y = this._yy
|
|
}
|
|
setSpFrame(i,j,num){
|
|
|
|
let config = YiMaJiangManager.getMjConfig()
|
|
let line = config.line
|
|
let col = config.col
|
|
let leftTopX = -YiMaJiangManager.singleMjWidth*(col-1)/2
|
|
let leftTopY = YiMaJiangManager.singleMjHeight*(line-1)/2
|
|
|
|
this._xx = leftTopX + j*YiMaJiangManager.singleMjWidth
|
|
this._yy = leftTopY - i*YiMaJiangManager.singleMjHeight
|
|
|
|
this.node.x = this._xx
|
|
this.node.y = this._yy
|
|
this.node.attr({posX:i,posY:j,cardNum:num})
|
|
|
|
if(num == 0){
|
|
this.spFrame.scale = 1
|
|
this.spFrame.scale = 1
|
|
this.spFrame.y = 0
|
|
}else{
|
|
this.spFrame.scale = 0.8
|
|
this.spFrame.scale = 0.8
|
|
this.spFrame.y = 6
|
|
}
|
|
Common5.getSpriteFrameFromBundle("YiMaJiang",
|
|
"texture/pai/pai_"+num,this.spFrame.getComponent(cc.Sprite)
|
|
);
|
|
}
|
|
getCardX(){
|
|
return this._xx
|
|
}
|
|
getCardY(){
|
|
return this._yy
|
|
}
|
|
getCardPosX(){
|
|
return this.posX
|
|
}
|
|
getCardPosY(){
|
|
return this.posY
|
|
}
|
|
getCardNum(){
|
|
return this.cardNum
|
|
}
|
|
|
|
openTouchEvent(node){
|
|
node.on(cc.Node.EventType.TOUCH_START,this.touchStartNode,this)
|
|
node.on(cc.Node.EventType.TOUCH_CANCEL,this.touchEndNode,this)
|
|
node.on(cc.Node.EventType.TOUCH_MOVE,this.touchMoveNode,this)
|
|
node.on(cc.Node.EventType.TOUCH_END,this.touchEndNode,this)
|
|
}
|
|
closeTouchEvent(node:cc.Node){
|
|
node.off(cc.Node.EventType.TOUCH_START,this.touchStartNode,this)
|
|
node.off(cc.Node.EventType.TOUCH_CANCEL,this.touchEndNode,this)
|
|
node.off(cc.Node.EventType.TOUCH_MOVE,this.touchMoveNode,this)
|
|
node.off(cc.Node.EventType.TOUCH_END,this.touchEndNode,this)
|
|
}
|
|
touchStartNode(event) {
|
|
if(YiMaJiang.getInstance().getIsXiaoChuAction()){
|
|
return
|
|
}
|
|
let target = event.target;
|
|
let posi = event.getLocation()//世界坐标
|
|
posi = target.parent.convertToNodeSpaceAR(posi)
|
|
|
|
this.m_x = posi.x
|
|
this.m_y = posi.y
|
|
this.m_startMove = true
|
|
this.m_dir = MoveDir.None
|
|
YiMaJiangManager.touchStartInitData()
|
|
YiMaJiang.getInstance().setCanMoveMJInfo(null)
|
|
// console.log('posX,posY,cardNum==',target.posX,target.posY,target.cardNum)
|
|
let result = YiMaJiangManager.checkClickIsCanClearUp(target.posX,target.posY)
|
|
// console.log('result==',result)
|
|
if(result){
|
|
YiMaJiang.getInstance().setIsXiaoChuAction(true)
|
|
let mj1 = {posX:target.posX,posY:target.posY}
|
|
let mj2 = result
|
|
EventMgr.emitEvent_custom(ryw_Event.XiaoChuMJ,mj1,mj2 )
|
|
}
|
|
}
|
|
touchMoveNode(event) {
|
|
if(YiMaJiang.getInstance().getIsXiaoChuAction()){
|
|
return
|
|
}
|
|
let target = event.target
|
|
|
|
let posi = event.getLocation()//世界坐标
|
|
posi = target.parent.convertToNodeSpaceAR(posi)
|
|
// target.setPosition(posi)
|
|
let mjPosData = {posX:target.posX,posY:target.posY}
|
|
|
|
let x = posi.x
|
|
let y = posi.y
|
|
if(this.m_startMove && (Math.abs(this.m_x - x) > 10 || Math.abs(this.m_y - y) > 10) ){
|
|
this.m_startMove = false
|
|
if(Math.abs(this.m_x - x) > Math.abs(this.m_y - y)){
|
|
if(this.m_x < x){
|
|
this.m_dir = MoveDir.Right
|
|
}else{
|
|
this.m_dir = MoveDir.Left
|
|
}
|
|
}else{
|
|
if(this.m_y < y){
|
|
this.m_dir = MoveDir.Up
|
|
}else{
|
|
this.m_dir = MoveDir.Down
|
|
}
|
|
}
|
|
YiMaJiang.getInstance().setCanMoveMJInfo(YiMaJiangManager.getCanMoveMJByDir(this.m_dir,mjPosData))
|
|
}
|
|
|
|
|
|
let disData = {disX:x-this.m_x,disY:y-this.m_y}
|
|
|
|
this.moveAllTiled(this.m_dir,mjPosData,disData,YiMaJiang.getInstance().getCanMoveMJInfo())
|
|
}
|
|
moveAllTiled(dir,mjPosData,disData,canMoveMJInfo){
|
|
if(!canMoveMJInfo){
|
|
return
|
|
}
|
|
|
|
let canMoveMjData = canMoveMJInfo.canMoveMjData
|
|
let maxMoveStep = canMoveMJInfo.maxMoveStep
|
|
if(maxMoveStep <= 0){
|
|
return
|
|
}
|
|
|
|
let moveStep = 0
|
|
//移动距离约束
|
|
if(dir == MoveDir.Up){
|
|
if(disData.disY > maxMoveStep*YiMaJiangManager.singleMjHeight){
|
|
disData.disY = maxMoveStep*YiMaJiangManager.singleMjHeight
|
|
}else if(disData.disY < 0){
|
|
disData.disY = 0
|
|
}
|
|
disData.disX = 0
|
|
moveStep = Math.round(disData.disY/YiMaJiangManager.singleMjHeight)
|
|
}else if(dir == MoveDir.Down){
|
|
if(disData.disY < -maxMoveStep*YiMaJiangManager.singleMjHeight){
|
|
disData.disY = -maxMoveStep*YiMaJiangManager.singleMjHeight
|
|
}else if(disData.disY > 0){
|
|
disData.disY = 0
|
|
}
|
|
disData.disX = 0
|
|
moveStep = Math.round(disData.disY/YiMaJiangManager.singleMjHeight)
|
|
}else if(dir == MoveDir.Left){
|
|
if(disData.disX < -maxMoveStep*YiMaJiangManager.singleMjWidth){
|
|
disData.disX = -maxMoveStep*YiMaJiangManager.singleMjWidth
|
|
}else if(disData.disX > 0){
|
|
disData.disX = 0
|
|
}
|
|
disData.disY = 0
|
|
moveStep = Math.round(disData.disX/YiMaJiangManager.singleMjWidth)
|
|
}else if(dir == MoveDir.Right){
|
|
if(disData.disX > maxMoveStep*YiMaJiangManager.singleMjWidth){
|
|
disData.disX = maxMoveStep*YiMaJiangManager.singleMjWidth
|
|
}else if(disData.disX < 0){
|
|
disData.disX = 0
|
|
}
|
|
disData.disY = 0
|
|
moveStep = Math.round(disData.disX/YiMaJiangManager.singleMjWidth)
|
|
}
|
|
moveStep = Math.abs(moveStep)
|
|
YiMaJiangManager.setMoveTempMjData(dir,canMoveMjData,moveStep,true)
|
|
EventMgr.emitEvent_custom(ryw_Event.MoveMJ,dir,moveStep,mjPosData,disData,canMoveMJInfo)
|
|
}
|
|
touchEndNode(event) {
|
|
if(YiMaJiang.getInstance().getIsXiaoChuAction()){
|
|
return
|
|
}
|
|
let canMoveMJInfo = YiMaJiang.getInstance().getCanMoveMJInfo()
|
|
if(!canMoveMJInfo){
|
|
return
|
|
}
|
|
let canMoveMjData = canMoveMJInfo.canMoveMjData
|
|
console.log('canMoveMjData==',canMoveMjData)
|
|
let maxMoveStep = canMoveMJInfo.maxMoveStep
|
|
if(maxMoveStep <= 0){
|
|
return
|
|
}
|
|
|
|
let target = event.target
|
|
let mjPosData = {posX:target.posX,posY:target.posY}
|
|
let posi = event.getLocation()//世界坐标
|
|
posi = target.parent.convertToNodeSpaceAR(posi)
|
|
|
|
let x = posi.x
|
|
let y = posi.y
|
|
let dir = this.m_dir
|
|
let disData = {disX:x-this.m_x,disY:y-this.m_y}
|
|
|
|
|
|
|
|
let moveStep = 0
|
|
//移动距离约束
|
|
if(dir == MoveDir.Up){
|
|
if(disData.disY > maxMoveStep*YiMaJiangManager.singleMjHeight){
|
|
disData.disY = maxMoveStep*YiMaJiangManager.singleMjHeight
|
|
}else if(disData.disY < 0){
|
|
disData.disY = 0
|
|
}
|
|
disData.disX = 0
|
|
|
|
moveStep = Math.round(disData.disY/YiMaJiangManager.singleMjHeight)
|
|
}else if(dir == MoveDir.Down){
|
|
if(disData.disY < -maxMoveStep*YiMaJiangManager.singleMjHeight){
|
|
disData.disY = -maxMoveStep*YiMaJiangManager.singleMjHeight
|
|
}else if(disData.disY > 0){
|
|
disData.disY = 0
|
|
}
|
|
disData.disX = 0
|
|
moveStep = Math.round(disData.disY/YiMaJiangManager.singleMjHeight)
|
|
}else if(dir == MoveDir.Left){
|
|
if(disData.disX < -maxMoveStep*YiMaJiangManager.singleMjWidth){
|
|
disData.disX = -maxMoveStep*YiMaJiangManager.singleMjWidth
|
|
}else if(disData.disX > 0){
|
|
disData.disX = 0
|
|
}
|
|
disData.disY = 0
|
|
moveStep = Math.round(disData.disX/YiMaJiangManager.singleMjWidth)
|
|
}else if(dir == MoveDir.Right){
|
|
if(disData.disX > maxMoveStep*YiMaJiangManager.singleMjWidth){
|
|
disData.disX = maxMoveStep*YiMaJiangManager.singleMjWidth
|
|
}else if(disData.disX < 0){
|
|
disData.disX = 0
|
|
}
|
|
disData.disY = 0
|
|
moveStep = Math.round(disData.disX/YiMaJiangManager.singleMjWidth)
|
|
}
|
|
moveStep = Math.abs(moveStep)
|
|
console.log('移动格子数==',moveStep)
|
|
|
|
let result = YiMaJiangManager.moveEndCheckXiaoChu(dir,canMoveMjData,moveStep,mjPosData)
|
|
console.log('移动结束result==',result)
|
|
//可消除,直接停在当前位置
|
|
if(result && result.mj2){
|
|
// let mj1 = {posX:target.posX,posY:target.posY}
|
|
// let mj2 = result
|
|
// EventMgr.emitEvent_custom(ryw_Event.XiaoChuMJ,mj1,mj2 )
|
|
YiMaJiang.getInstance().moveToFixedPos(result.mj1,result.mj2)
|
|
//不可消除,弹回初始位置
|
|
}else{
|
|
YiMaJiang.getInstance().moveToOriginPos()
|
|
}
|
|
}
|
|
|
|
moveUp(){
|
|
console.log('上==')
|
|
}
|
|
moveDown(){
|
|
console.log('下==')
|
|
}
|
|
moveLeft(){
|
|
console.log('左==')
|
|
}
|
|
moveRight(){
|
|
console.log('右==')
|
|
}
|
|
|
|
|
|
// update (dt) {}
|
|
}
|
|
|