London/assets/Script/GameManager.ts
2024-08-28 11:14:05 +08:00

331 lines
11 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.

// 主游戏控制类
const {ccclass, property} = cc._decorator;
@ccclass
export default class GameManager extends cc.Component {
@property(cc.Prefab)
goal_Prefab: cc.Prefab = null;
@property(cc.Prefab)
middle_Prefab: cc.Prefab = null;
countTime: number; //游戏总daojishi
steps: number; //地图最大步数限制
tube1: cc.Node; //管子1
tube2: cc.Node; //管子2
tube3: cc.Node; //管子3
ball1: cc.Node; //球1
ball2: cc.Node; //球2
ball3: cc.Node; //球3
goal: cc.Node; //管子1
middle: cc.Node; //管子2
tube_Array: any;//管子数组
goal_Array: any;//管子数组
middle_Array: any;//管子数组
onLoad () {}
start () {
this.fit();
this.init();
setTimeout(() => {
cc.fx.Notifications.on("moveTube", this.moveTube, this);
}, 1000);
}
//初始化数据
init(){
this.tube1 = this.node.getChildByName("GameArea").getChildByName("tube1");
this.tube2 = this.node.getChildByName("GameArea").getChildByName("tube2");
this.tube3 = this.node.getChildByName("GameArea").getChildByName("tube3");
this.ball1 = this.node.getChildByName("GameArea").getChildByName("ball1");
this.ball2 = this.node.getChildByName("GameArea").getChildByName("ball2");
this.ball3 = this.node.getChildByName("GameArea").getChildByName("ball3");
this.goal = this.node.getChildByName("Goal");
this.middle = this.node.getChildByName("Middle");
// this.node.getChildByName("GameArea").getChildByName("tube1").zIndex = 1;
// this.node.getChildByName("GameArea").getChildByName("tube2").zIndex = 1;
// this.node.getChildByName("GameArea").getChildByName("tube3").zIndex = 1;
this.initMap()
this.initBall();
}
//初始化 管子和球的关卡数据
initMap(){
this.tube_Array = this.changeMap(cc.fx.GameConfig.LEVEL_INFO[0].start,"start");
this.middle_Array = this.changeMap(cc.fx.GameConfig.LEVEL_INFO[0].middle,"middle");
this.goal_Array = this.changeMap(cc.fx.GameConfig.LEVEL_INFO[0].goal,"goal");
this.steps = cc.fx.GameConfig.LEVEL_INFO[0].steps;
}
/**
* 转变地图参数到程序可用
* @param tagArr tagArr为目标数组可能为二维数组
* @param name 关卡球和管子的类型有start,middle,goal,其中middle是二维数组最多为3个
*/
changeMap(tagArr, name){
//中间态可能有多组目标 故单独处理
if(name == "middle"){
for(let k=0; k<tagArr.length;k++){
let tempArr = tagArr[k];
for(let i=0; i<tempArr.length;i++){
if(tempArr[i].length > 0){
for(let j=0; j<tagArr[i].length; j++){
if(this.conversion_Char(tempArr[i][j]))
tempArr[i][j] = this.conversion_Char(tempArr[i][j]);
}
}
}
}
}
else{
for(let i=0; i<tagArr.length;i++){
if(tagArr[i].length > 0){
for(let j=0; j<tagArr[i].length; j++){
if(this.conversion_Char(tagArr[i][j]))
tagArr[i][j] = this.conversion_Char(tagArr[i][j]);
}
}
}
}
return tagArr;
}
//转换符号,"rgb"配置改为123
conversion_Char(str: string){
if(str == "r") return 1;
else if(str == "g") return 2;
else if(str == "b") return 3;
}
//初始化球球 管子状态所有关都一样-
initBall(){
//初始状态球的初始化
for(let i=0; i<this.tube_Array.length; i++){
if(this.tube_Array[i].length > 0 ){
for(let j =0; j<this.tube_Array[i].length; j++){
let name = "ball" + this.tube_Array[i][j];
this[name].active = true;
this[name].setPosition((i-1)*210,-100+j*100);
this[name].getComponent("Ball").init(j==(this.tube_Array[i].length-1)?true:false,this.tube_Array[i][j]);
}
}
}
//最终状态球的初始化
for(let i=0; i<this.goal_Array.length; i++){
if(this.goal_Array[i].length > 0 ){
for(let j =0; j<this.goal_Array[i].length; j++){
let name = this.goal_Array[i][j] + 4;
this.goal.children[name].active = true;
this.goal.children[name].setPosition((i-1)*210,-100+j*100);
this.goal.children[name].getComponent("Ball").init(false,this.goal_Array[i][j]);
}
}
}
//中间状态球的初始化
for(let i=0; i<this.middle_Array.length;i++){
if(this.middle_Array.length > 0){
if(this.middle_Array.length == 1){
this.createMiddle(0);
}
else if(this.middle_Array.length == 3){
this.createMiddle(1);
this.createMiddle(2);
this.createMiddle(3);
// this.middle.getChildByName("part").active = false;
}
}
}
}
createMiddle(num){
let nodeName = "part";
if(num != 0) nodeName = nodeName + num;
let mid = this.middle.getChildByName(nodeName);
mid.active = true;
let mid_Arr = this.middle_Array[num-1];
for(let i=0; i<mid_Arr.length; i++){
if(mid_Arr[i].length > 0 ){
for(let j =0; j<mid_Arr[i].length; j++){
let name = mid_Arr[i][j] + 4;
mid.children[name].active = true;
mid.children[name].setPosition((i-1)*210,-100+j*100);
mid.children[name].getComponent("Ball").init(false,mid_Arr[i][j]);
}
}
}
}
/**
* 当管子内球发生移动时改变管子数组并且重置所有球是否可移动状态
* @param data data格式为对象
* id:number 移动球的自己的id
* tube:number 移动到目标个管子0,1,2
start_Pos:cc.v2(), 移动之前的位置
end_Pos:cc.v2() 移动之后的终点位置
*/
moveTube(data){
this.tube_Array[data.tube].push(data.id);
var tube_Arr = this.tube_Array[1];
if(data.start_Pos.x == -210){
tube_Arr = this.tube_Array[0];
}
else if(data.start_Pos.x == 210){
tube_Arr = this.tube_Array[2];
}
if(tube_Arr.length > 0) tube_Arr.pop();
if(data.start_Pos.y != 50 && tube_Arr.length > 0){
let name = "ball" + tube_Arr[tube_Arr.length-1];
this[name].getComponent("Ball").set_Touch(true);
}
if(data.end_Pos.y != 50){
tube_Arr = this.tube_Array[1];
if(data.end_Pos.x == -210){
tube_Arr = this.tube_Array[0];
}
else if(data.end_Pos.x == 210){
tube_Arr = this.tube_Array[2];
}
if(tube_Arr.length > 1){
let name = "ball" + tube_Arr[tube_Arr.length-2];
console.log(name);
this[name].getComponent("Ball").set_Touch(false);
}
}
this.gameWin();
}
gameWin(){
var result = false;
//如果没有中间状态,或者中间状态都已达到,则达到最终目标则算胜利
if(this.middle_Array.length == 0){
result = this.isArrEqual(this.goal_Array);
if(result){
alert("成功");
}
}
else{
for(let i=0; i<this.middle_Array.length; i++){
if(this.isArrEqual(this.middle_Array[i])){
alert("达到中间态");
this.middle_Array.splice(i,1);
}
}
}
}
//判断数组是否相同如果join情况下都不相同就不用往下比较
isArrEqual(tagArr){
var result = false;
if(this.tube_Array.join() == tagArr.join()){
result = true;
for(let i=0; i<this.tube_Array.length; i++){
if(this.tube_Array[i].length != tagArr[i].length){
result = false;
}
}
}
return result;
}
//根据是否全面屏做独立适配方面
fit(){
var jg = this.setFit();
if(!jg){
}
}
//判断全面屏
getSetScreenResolutionFlag () {
let size = cc.winSize;
let width = size.width;
let height = size.height;
if ((height / width) > (16.2 / 9)) return false;
return true;
}
//判断全面屏适配
setFit () {
let flag = this.getSetScreenResolutionFlag();
if (flag) {
} else {
}
return flag;
}
//返回首页
backScene(){
cc.director.loadScene("LoadScene");
}
//下一关或者重新开始或者返回上一关根据level决定
reStart(type){
}
//获取时间戳
getTime(){
const timestamp = new Date().getTime();
return timestamp;
}
//获胜
passLevel(){
}
//失败
loseLevel(type){
}
//开始游戏
startGame(){
}
//如果是倒计时 调用此方法
updateCountDownTime () {
if (this.countTime > 0) {
this.countTime -= 1;
// this.time.string = cc.fx.GameTool.getTimeMargin(this.countTime);
if(this.countTime < 5){
// cc.tween(this.time.node)
// .to(0.25,{scale:1.5,color:cc.color(255,0,0)})
// .to(0.25,{scale:1,color:cc.color(255,255,255)})
// .start()
let over = this.node.getChildByName("Over");
cc.tween(over)
.to(0.2,{opacity:255})
.delay(0.1)
.to(0.2,{opacity:0})
.start();
}
if(this.countTime <= 0){
this.unschedule(this.updateCountDownTime);
var time = 0;
this.gameOver(time);
}
}
}
//上传每次操作数据
setData(){
cc.fx.GameTool.setGameData();
}
//上传排行榜数据
gameOver(time){
cc.fx.GameTool.setRank(time);
this.node.getChildByName("GameOver").active = true;
this.node.getChildByName("GameOver").opacity = 0;
cc.tween(this.node.getChildByName("GameOver"))
.to(0.4,{opacity:255})
.delay(2)
.to(0.4,{opacity:50})
.call(() =>{
cc.director.loadScene("OverScene");
})
.start()
}
onEnable () {
// setTimeout(() => {
// cc.fx.Notifications.on("moveTube", this.moveTube, this);
// }, 100);
}
onDisable () {
cc.fx.Notifications.off("moveTube", this.moveTube);
}
update (dt) {
}
}