123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161 |
- import { _decorator, Component, instantiate, Node,Prefab ,Button,AudioClip,AudioSource} from 'cc';
- const { ccclass, property } = _decorator;
- import { Item } from './Item';
- import { GameManager } from './Manager/GameManager';
- @ccclass('Game')
- export class Game extends Component {
- @property({type:Prefab})
- preBlock = null
- private row:number=13;
- private col:number=12;
- private lockArr:Item[];
- private itemDic={};
-
- @property({type:[AudioClip]})// 晋级 选择颜色 结束
- otherAudioArr = []
- audioSource: AudioSource;
- start() {
- this.audioSource = this.node.getComponent(AudioSource)
- this.lockArr=[];
- this.createItems();
- for(let i=1;i<=4;i++){
- this.node.getChildByPath("Btns/Button_"+i).on(Button.EventType.CLICK, this.callback, this);
- }
- }
- reset(): void {
- this.lockArr=[];
- this.itemDic={}
- this.node.getChildByName("Con").removeAllChildren()
- this.createItems();
- }
- callback (button: Button) {
-
- let number=Number(button.name.replace("<Button>","").split("_")[1]);
- console.log("callback==>>"+button.name+","+number)
- this.checkColor(number-1)
- this.audioSource.playOneShot(this.otherAudioArr[1],1)
- }
- createItems(){
- let con=this.node.getChildByName("Con")
- for(let i=0;i<this.row;i++){
- for(let j=0;j<this.col;j++){
- let item = instantiate(this.preBlock)
- item.parent = con
- item.setPosition(i*80+40,-j*80-40,0)
- let itemScript = item.getComponent(Item)
- let num_type_random = Math.floor(Math.random()*4)
- itemScript.init(num_type_random,i,j)
- if(i==0 && j==0){
- this.lockArr.push(itemScript)
- itemScript.isLock=true
- }
- this.itemDic[i+","+j]=itemScript
- }
- }
- this.checkColor(this.lockArr[0].numType);
- console.log("lockArr=============================>"+this.lockArr.length)
- }
- checkColor(numType){
- let count=0;
- let checkItemDic={};
- let operNum=0;
- for(let i=0;i<this.lockArr.length;i++){
- let itemRow=this.lockArr[i].row
- let itemCol=this.lockArr[i].col
- if(checkItemDic[itemRow+","+itemCol])
- continue
- checkItemDic[itemRow+","+itemCol]=true
-
- for(let m=-1;m<=1;m++){
- for(let n=-1;n<=1;n++){
- // console.log("otherItem=============================>"+m+","+n)
- // operNum++;
- // if(operNum>=1000)
- // {
- // console.log("异常退出")
- // return;
- // }
- //十字型检测
- if((!((m==0) && (n==0))) && this.inArea(itemRow+m,itemCol+n) && ((itemRow+m==itemRow) || (itemCol+n==itemCol))){
- let otherItem=this.itemDic[(itemRow+m)+","+(itemCol+n)]
- if(otherItem)
- {
- // console.log("otherItem=>"+otherItem.numType+","+numType+","+itemRow+","+itemCol+","+(itemRow+m)+","+(itemCol+n))
- // console.log("otherItem=>"+otherItem.row)
- // console.log("otherItem=>"+otherItem.col)
- // console.log("otherItem=============================>")
- if((!otherItem.isLock) && (otherItem.numType==numType)){
- console.log("otherItem=============================>颜色相同")
- this.lockArr.push(otherItem)
- // otherItem.changeType(numType)
- otherItem.isLock=true
- count++
- }
- }
- }
- }
- }
- }
- if(count>0){
- for(let i=0;i<this.lockArr.length;i++){
- this.lockArr[i].changeType(numType)
- }
- }
- console.log("更换颜色==>>"+count+","+this.lockArr.length)
- if(this.lockArr.length==12*13){
- //胜利
- GameManager.getInstance().addScore(100)
- this.audioSource.playOneShot(this.otherAudioArr[0],1)
- }
- }
- inArea( x, y){
- let isIn= x >= 0 && x < this.row && y >= 0 && y < this.col;
- // if(!isIn)
- // {
- // console.log("inArea==>>"+x+","+y)
- // }
- return isIn
- }
- update(deltaTime: number) {
-
- }
- }
|