games/ChaoJiJieYaGuan_LiuBianXing/assets/Script/GameSc/LevelUpGame/ChallengeGameSc.ts

625 lines
22 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/**
* 挑战游戏 节点
*/
import AudioManager from "../../Tools/AudioManager";
import Define from "../../Tools/Define";
import GameResMgr, { soundName, uiPreName } from "../../Tools/GameResMgr";
import PlayData, { GameState } from "../../Tools/PlayData";
import AdsApiMgr from "../../Tools/Sdk/ads/AdsApiMgr";
import Utils from "../../Tools/Utils";
import gameStorage from "../../Tools/gameStorage";
import TipsManager from "../TipsManager";
import ChallengeSuccessSc from "./ChallengeSuccessSc";
import kapaiSc from "./kapaiSc";
import {TrackingManager, TrackingType} from "../../Tracking/TrackingManager";
const {ccclass, property} = cc._decorator;
@ccclass
export default class ChallengeGameSc extends cc.Component {
@property(cc.Prefab)
dikuang:cc.Prefab = null
@property(cc.Prefab)
kapaiPre:cc.Prefab = null
@property(cc.Node)
dizuo:cc.Node = null
@property(cc.Node) //用于移动卡牌 过渡节点 ,移动卡牌的时候 ,卡牌在最高层
moveNode:cc.Node = null
@property(cc.Node)
notouch:cc.Node = null
@property(cc.Layout)
dizuolayout:cc.Layout = null
@property(cc.Label)
guanstr:cc.Label = null
@property(cc.Label)
timestr:cc.Label = null
dizuoState = [] //底座状态 0未解锁1解锁
levelNum = 0
mixlevelNum = 0
maxlevelNum = 0
moveUp = 15
selectNodeArr = [] //选中的节点
selectIndex = -1
mubiaoIndex = -1
allNodeArr = []
posY = [0, -20, -40, -60, -80, -100, -120, -140, -160, -180]
maxKaPaiNum = 10 //一个框最多放置 数量
linshijiesuo = 0 //可用于临时解锁的id
linshiOperatorNum = [] //一个临时卡槽 操作一定的次数后,会关闭
time01 = 0.01
weizhigailv = 1
gameconfigdata = null
allkapaisezhi = []
needHeCheng = 0 //需要达成条件的个数
onLoad () {
PlayData.Instance.gamestate = GameState.start
this.notouch.active = !1
let guan = PlayData.Instance.guanqia
this.gameconfigdata = Define.cl_gamedata[guan]
this.dizuolayout.spacingX = this.gameconfigdata.spacex
this.dizuolayout.spacingY = this.gameconfigdata.spacey
this.guanstr.string = '关卡' + (guan+1)
this.timestr.string = '00:00'
let index = 0
this.schedule(()=>{
if(PlayData.Instance.gamestate != GameState.gaming) return
index++
//console.log('打印计时', index)
this.updateTime(index)
}, 1, cc.macro.REPEAT_FOREVER)
}
start () {
this.initGameConfig()
//this.initGame()
//console.log('打印关卡数据', PlayData.Instance.guanqia)
}
/**
* 初始化一些配置
*/
initGameConfig(){
let shuzu = []
for (let index = 0; index < this.gameconfigdata.suo.length; index++) {
let ele = this.gameconfigdata.suo[index]
this.dizuoState[ele] = 0
}
for (let index = 0; index < this.gameconfigdata.dizuonum; index++) {
let dikuang = cc.instantiate(this.dikuang)
dikuang.scale = this.gameconfigdata.dizuoscale
dikuang.name = 'di' + index
//dikuang.on(cc.Node.EventType.TOUCH_END, this.kapainodeBtnClick, this)
dikuang.parent = this.dizuo
if(this.dizuoState[index] != 0){
this.dizuoState[index] = 1
cc.find('suo', dikuang).active = !1
}else{
cc.find('suo', dikuang).active = !0
}
let mubiao = cc.find('mubiao', dikuang)
mubiao.active = !1
mubiao.scale = this.gameconfigdata.mubiaoscale
cc.find('duigou', dikuang).active = !1
if(this.gameconfigdata.mubiaose[index] != 0){
let aa = Define.sezhi[this.gameconfigdata.mubiaose[index]]
let chaifen = Utils.randChunkSplit(aa, 3,6)
for (let ind = 0; ind < chaifen.length; ind++) {
const element = chaifen[ind];
shuzu.push(element)
}
}
}
shuzu = Utils.shuffle(shuzu)
for (let index = 0; index < shuzu.length; index++) {
const element = shuzu[index];
this.allkapaisezhi = this.allkapaisezhi.concat(element)
}
//console.log('打乱色值顺序数组', this.allkapaisezhi)
this.scheduleOnce(()=>{this.initGame()}, 0.6)
this.moveNode.scale = this.gameconfigdata.dizuoscale
}
/**
* 初始化
*/
initGame(){
this.mixlevelNum = 1 //this.levelNum - 4
this.maxlevelNum = this.gameconfigdata.senum
for (let index = 0; index < this.dizuoState.length; index++) {
const ele = this.dizuoState[index];
let di = cc.find('di'+index, this.dizuo)
let kapainode = cc.find('kapainode', di)
kapainode.removeAllChildren()
let mubiao = cc.find('mubiao', di)
kapainode.on(cc.Node.EventType.TOUCH_END, this.kapainodeBtnClick, this)
//this.allNodeArr[index] = []
if(this.dizuoState[index] == 1 && index != this.gameconfigdata.kong){
mubiao.active = !0
let sezhi = this.gameconfigdata.mubiaose[index]
//mubiao.getComponent(cc.Sprite).spriteFrame = GameResMgr.Instance.mubiaoSp[sezhi]
let carr = Define.mubiaosezhi[sezhi-1]
mubiao.color = cc.color(carr[0], carr[1], carr[2], 255)
this.needHeCheng ++
let cengshu = 10 //Utils.getRangeRandom(1, 2) //层数
let dabiao = !0
//console.log('打印随机的层数', cengshu)
let arr =[]
for (let ind = 0; ind < cengshu; ind++) {
//let leixing = Utils.getRangeRandom(this.mixlevelNum, this.levelNum - 2) //类型
let leixing = this.allkapaisezhi.pop() //Utils.getRangeRandom(this.mixlevelNum, this.maxlevelNum) //类型
this.addNewKaPai(kapainode, leixing, true)
if(dabiao == !0){
if(sezhi != leixing){
dabiao = !1
}
}
arr.push(leixing)
}
if(dabiao == !0){
cc.find('duigou', di).active = !0
this.needHeCheng --
}
if(arr[0] == arr[1] && arr[1] == arr[2] && arr[2] != arr[3] && PlayData.Instance.guanqia > 0){
let childrennode = kapainode.children
for (let tt = 0; tt < 3; tt++) {
let kanodesc = childrennode[tt].getComponent(kapaiSc)
kanodesc.bianshu = kanodesc.kaNum
kanodesc.setKaNum(0)
if(tt == 2){
kanodesc.showWenHao()
}
}
}
}else{
mubiao.active = !1
}
}
//console.log('过关条件', this.needHeCheng)
}
/**
* 视频解锁 卡槽
*/
videoJieSuoDiZuo(num){
this.dizuoState[num] = 1
this.linshiOperatorNum[num] = 0
let di = cc.find('di'+num, this.dizuo)
let suo = cc.find('suo', di)
suo.active = !1
if(num == 11){
this.linshijiesuo = 9
}else{
this.linshijiesuo = num + 1
}
if(this.dizuoState[this.linshijiesuo] == 0){
let nextsuo = cc.find('di'+(this.linshijiesuo) + '/suo', this.dizuo).getComponent(cc.Sprite)
cc.resources.load('kapaikuang/dikuang3', cc.SpriteFrame, (err, assert)=>{
if(err) return console.error(err)
nextsuo.spriteFrame = assert as cc.SpriteFrame
})
}
AudioManager.playEffect(soundName.kaikacao)
}
/**
* 点击一堆牌
*/
kapainodeBtnClick(t){
//开始游戏 计时
PlayData.Instance.gamestate = GameState.gaming
let moveUp = this.moveUp
let parentname:string = t.currentTarget.parent.name
let chaifen = parentname.split('di')
let e = chaifen[1]
//console.log('打印点击的节点', parentname, chaifen,e)
if(this.selectIndex == -1){
if(this.dizuoState[Number(e)] == 0){ //未解锁
TrackingManager.send(TrackingType.TemporaryCabinet);
var self = this
AdsApiMgr.Instance.motivational_Video_Show(()=>{
self.videoJieSuoDiZuo(Number(e))
}, ()=>{})
return
}
if(t.currentTarget.children.length > 0){
AudioManager.playEffect(soundName.xuanka)
let selectTarget = t.currentTarget
let lastnode = selectTarget.children[selectTarget.children.length - 1]
let lastnodesc:kapaiSc = lastnode.getComponent(kapaiSc)
this.selectIndex = Number(e) //选中的
for (let index = selectTarget.children.length; index > 0; index--) {
//console.log('打印index=', index-1)
let zinode = selectTarget.children[index-1]
let sc:kapaiSc = zinode.getComponent(kapaiSc)
if(sc.kaNum != lastnodesc.kaNum){
break
}else{
zinode.y += moveUp
//this.selectNodeArr.push(zinode)
this.selectNodeArr.push(zinode)
zinode.getComponent(cc.Animation).play()
}
}
}
}else{
if(this.dizuoState[Number(e)] == 0){return TipsManager.Instance.createTips('未解锁')}
if(this.selectIndex == Number(e)){ //点击相同位置,复位
AudioManager.playEffect(soundName.xuanka)
this.kapaiReset()
}else{ //移到对应的位置
this.mubiaoIndex = Number(e)
if(t.currentTarget.children.length >= this.maxKaPaiNum){ //点击的位置满了
this.kapaiReset(1)
return
}else if(t.currentTarget.children.length == 0){ //点击的位置 没有卡牌 ,选中的全部移动
let moveindex = 0
for (let index = this.selectNodeArr.length; index > 0; index--) {
const element = this.selectNodeArr[index-1];
element.getComponent(cc.Animation).stop()
element.scale = 1
let deltime = this.selectNodeArr.length*this.time01 - this.time01*moveindex
this.moveKaPai(element, t.currentTarget, deltime)
moveindex ++
}
if(cc.find('di' + this.selectIndex + '/duigou', this.dizuo).active == !0){
cc.find('di' + this.selectIndex + '/duigou', this.dizuo).active = !1
this.needHeCheng++
}
let kapainode = cc.find('di' + this.selectIndex + '/kapainode', this.dizuo)
this.scheduleOnce(()=>{this.checkWenHao(kapainode)}, this.selectNodeArr.length*this.time01+0.2)
this.selectIndex = -1
this.selectNodeArr = []
return
}
let mubiaochildren = t.currentTarget.children
let selectchildren = this.selectNodeArr
let mubiaonode = mubiaochildren[mubiaochildren.length - 1]
let mubiaonodesc = mubiaonode.getComponent(kapaiSc)
let selectnode = selectchildren[0]
let selectnodesc = selectnode.getComponent(kapaiSc)
let parent = selectnode.parent
let cha = parent.children.length - selectchildren.length
if(selectnodesc.kaNum == mubiaonodesc.kaNum){ //选中和要放置的位置最上面的卡牌一样 可以移动
let moveindex = 0
let totalnum = t.currentTarget.children.length
let duoyu = 0
for (let index = this.selectNodeArr.length; index > 0; index--) {
const element = this.selectNodeArr[index-1];
element.getComponent(cc.Animation).stop()
element.scale = 1
if(totalnum + moveindex >= this.maxKaPaiNum){
element.y = this.posY[cha + duoyu]
duoyu++
}else{
let zongtime = 0
if((totalnum + this.selectNodeArr.length) > this.maxKaPaiNum){
let yushu = this.maxKaPaiNum - totalnum
zongtime = yushu * this.time01
}else{
zongtime = this.selectNodeArr.length * this.time01
}
//let deltime = 0.1*moveindex
let deltime = zongtime - this.time01*moveindex
this.moveKaPai(element, t.currentTarget, deltime)
moveindex ++
}
}
if(cc.find('di' + this.selectIndex + '/duigou', this.dizuo).active == !0){
cc.find('di' + this.selectIndex + '/duigou', this.dizuo).active = !1
this.needHeCheng++
}
let kapainode = cc.find('di' + this.selectIndex + '/kapainode', this.dizuo)
this.scheduleOnce(()=>{this.checkWenHao(kapainode)}, this.selectNodeArr.length*this.time01+0.2)
this.selectIndex = -1
this.selectNodeArr = []
}else{
this.kapaiReset(1)
return
}
}
}
}
/**
* 卡牌复位
*/
kapaiReset(leixing?){
let movetime = 0.04
let mx = 5
for (let index = 0; index < this.selectNodeArr.length; index++) {
const zinode = this.selectNodeArr[index];
if(leixing == 1){
AudioManager.playEffect(soundName.dingdong)
zinode.getComponent(cc.Animation).stop()
zinode.scale = 1
cc.tween(zinode)
.to(movetime, {x:-mx})
.to(movetime, {x:0})
.to(movetime, {x:mx})
.to(movetime, {x:0})
.to(movetime, {x:-mx})
.to(movetime, {x:0})
.to(movetime, {x:mx})
.to(movetime, {x:0})
.call(()=>{
zinode.y -= this.moveUp
})
.start()
}else{
zinode.getComponent(cc.Animation).stop()
zinode.scale = 1
zinode.y -= this.moveUp
}
}
this.selectIndex = -1
this.selectNodeArr = []
}
/**
* 移动卡牌
* @param znode
* @param nodeparent
* @param deltime
*/
moveKaPai(znode, nodeparent, deltime){
let chaifen = nodeparent.parent.name.split('di')
let weizhi = chaifen[1]
//console.log('打印节点位置', weizhi)
let newpos0 = znode.parent.convertToWorldSpaceAR(znode.position)
let newpos1 = this.moveNode.convertToNodeSpaceAR(newpos0) //this.moveNode 是个过渡节点 保障移动的节点在最上层
znode.x = newpos1.x
znode.y = newpos1.y
znode.parent = this.moveNode
let newpos22 = cc.v2(0, this.posY[nodeparent.children.length-1])
let newpos2 = nodeparent.convertToWorldSpaceAR(newpos22)
let newpos3 = this.moveNode.convertToNodeSpaceAR(newpos2)
let newpos4 = cc.v3(newpos3.x,newpos3.y,0)
var self = this
cc.tween(znode)
.delay(deltime)
.to(0.12,{position:newpos4})
.call(()=>{
AudioManager.playEffect(soundName.moveka)
znode.parent = nodeparent
znode.position = cc.v3(0, self.posY[nodeparent.children.length-1], 0)
//console.log('打印卡牌的数量', nodeparent.children.length)
if(nodeparent.children.length >= self.maxKaPaiNum){
//console.log('卡槽满10')
let tongse = !0
let lastnode = nodeparent.children[nodeparent.children.length - 1]
let lastnodesc:kapaiSc = lastnode.getComponent(kapaiSc)
for (let index = 0; index <nodeparent.children.length; index++) {
const element = nodeparent.children[index];
let elsc:kapaiSc = element.getComponent(kapaiSc)
if(elsc.kaNum != lastnodesc.kaNum){
tongse = !1
break
}
}
//同色
if(tongse == !0){
let dizuo = nodeparent.parent
if(lastnodesc.kaNum == this.gameconfigdata.mubiaose[weizhi]){
cc.find('duigou', dizuo).active = !0
this.needHeCheng--
}else{
cc.find('duigou', dizuo).active = !1
}
}else{
let dizuo = nodeparent.parent
cc.find('duigou', dizuo).active = !1
}
}
console.log('查看条件', this.needHeCheng)
if(this.needHeCheng == 0){
console.log('达成通关')
this.notouch.active = !0
let openState = gameStorage.Instance.getChallengeCheckOpenState()
openState[PlayData.Instance.guanqia+1] = 1
gameStorage.Instance.setChallengeCheckOpenState(openState)
PlayData.Instance.gamestate = GameState.end
this.schedule(()=>{
this.addYanHua()
}, 0.8, cc.macro.REPEAT_FOREVER)
this.scheduleOnce(()=>{
let newnode:cc.Node = cc.instantiate(GameResMgr.Instance.uiPre[uiPreName.challengeSuccess])
newnode.parent = this.node
let sc = newnode.getComponent(ChallengeSuccessSc)
sc.showPanel(this.timestr.string)
}, 1)
}
})
.start()
}
/**
* 添加新牌
* @param parentnode 父节点
* @param kanum 卡牌num
*/
addNewKaPai(parentnode, kanum, isfapai = false){
//console.log('是否是发牌', isfapai)
let newnode:cc.Node = cc.instantiate(this.kapaiPre)
newnode.parent = parentnode
let kapaisc = newnode.getComponent(kapaiSc)
kapaisc.setKaNum(kanum)
if(isfapai == !0){
newnode.y = -cc.winSize.height/2 - parentnode.parent.y
cc.tween(newnode)
.to(0.2, {y:this.posY[parentnode.children.length-1]})
.call(()=>{
if(parentnode.children.length >= this.maxKaPaiNum){ //添加卡牌满10
let tongse = !0
let lastnode = parentnode.children[parentnode.children.length - 1]
let lastnodesc:kapaiSc = lastnode.getComponent(kapaiSc)
for (let index = 0; index <parentnode.children.length; index++) {
const element = parentnode.children[index];
let elsc:kapaiSc = element.getComponent(kapaiSc)
if(elsc.kaNum != lastnodesc.kaNum){
tongse = !1
break
}
}
//同色
if(tongse == !0){
let dizuo = parentnode.parent
}else{
let dizuo = parentnode.parent
}
}
})
.start()
}else{ //合成产生新牌
newnode.y = this.posY[parentnode.children.length-1]
newnode.opacity = 0
newnode.scale = 0
cc.tween(newnode)
.to(0.1,{opacity:255, scale:1})
.start()
}
}
/**
* 检测是否有问号
*/
checkWenHao(kapainode){
//console.log('检测卡牌node', kapainode.children)
let childrennode = kapainode.children
let selectarr = []
if(childrennode.length > 0){
let lastnode = childrennode[childrennode.length-1]
let lastnodesc = lastnode.getComponent(kapaiSc)
if(lastnodesc.kaNum == 0){
for (let ind = childrennode.length; ind > 0; ind--) {
const element = childrennode[ind-1];
let elsc:kapaiSc = element.getComponent(kapaiSc)
if(elsc.kaNum != lastnodesc.kaNum){
break
}else{
selectarr.push(element)
}
}
}
}
console.log('打印0号', selectarr)
//let leixing = Utils.getRangeRandom(this.mixlevelNum, this.levelNum - 2)
for (let index = 0; index < selectarr.length; index++) {
let sc = selectarr[index].getComponent(kapaiSc)
sc.setKaNum(sc.bianshu)
sc.node.scale = 0
cc.tween(sc.node)
.to(0.1, {scale:1})
.start()
}
}
/**
* 更新时间
* @param index
*/
updateTime(index){
let str = Utils.secondsFormat(index, false)
this.timestr.string = '' + str
}
// update (dt) {}
/**
* 添加烟花
*/
addYanHua(){
let winSize = cc.winSize
let xx = Utils.getRangeRandom(-winSize.width/2+50, winSize.width/2-50)
let yy = Utils.getRangeRandom(-winSize.height/2+50, winSize.height/2-50)
let newyanhua:cc.Node = cc.instantiate(GameResMgr.Instance.uiPre[uiPreName.yanhua])
newyanhua.position = cc.v3(xx, yy, 0)
newyanhua.parent = this.node
// let ani = newyanhua.getComponent(sp.Skeleton)
// let skinid = Utils.getRangeRandom(1, 5)
// ani.setSkin('' + skinid)
cc.tween(newyanhua)
.delay(0.8)
.removeSelf()
.start()
}
}