ShenNong/assets/Script/GameManager.ts
2024-06-21 18:44:06 +08:00

262 lines
8.3 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.Node)
Btn_New: cc.Node = null;
@property(cc.Node)
Btn_Hear: cc.Node = null;
@property(cc.Node)
Btn_See: cc.Node = null;
@property(cc.Node)
Btn_Firewood: cc.Node = null;
@property(cc.Node)
Plant: cc.Node = null;
@property(cc.Node)
timeNode: cc.Node = null;
@property(cc.SpriteAtlas)
FoodPlist: cc.SpriteAtlas = null;
voiceSpriteFrame: cc.SpriteFrame; //是否可点击
timeTween: cc.Tween;
touchable: boolean; //是否可点击
countTime: number; //每次游戏倒计时
startTime: number; //游戏开始计时
overTime: number; //游戏结束计时
now_Food: string; //当前关卡名字信息
result: boolean; //当前关卡名字信息
foodPicArray: any; //存放当前每一大局游戏所有植物图片
foodAudioArray: any; //存放当前每一大局游戏所有植物声音
onLoad () {}
start () {
this.fit();
this.init();
this.startGame();
}
//初始化数据
init(){
this.touchable = false;
this.result = false;
this.foodAudioArray = [];
this.foodPicArray = [];
this.timeTween = null;
this.voiceSpriteFrame = this.Plant.getChildByName("icon").getComponent(cc.Sprite).spriteFrame;
this.btnReset();
}
//开始游戏
startGame(){
cc.tween(this.node.getChildByName("Begin"))
.to(0.1,{opacity:0})
.to(0.5,{opacity:255})
.delay(1)
.to(0.5,{opacity:0})
.call(() =>{
this.createCustom();
})
.start();
}
//根据是否全面屏,做独立适配方面
fit(){
var jg = cc.fx.GameTool.setFit();
if(!jg){
this.Btn_Hear.y = -330;
this.Btn_See.y = -460;
this.Btn_Firewood.y = -600;
}
}
//创建关卡
createCustom(){
this.Plant.opacity = 0;
this.timeNode.getComponent(cc.Sprite).fillRange = 0;
let customData = cc.fx.GameConfig.LEVEL_INFO[cc.fx.GameConfig.GM_INFO.custom].custom[cc.fx.GameConfig.GM_INFO.level];
let name = customData.item;
if(customData.type == 1){
this.Plant.getChildByName("name").getComponent(cc.Label).string = cc.fx.GameTool.getFoodName(name);
name = "prop_"+name;
this.Plant.getChildByName("icon").getComponent(cc.Sprite).spriteFrame = this.FoodPlist["_spriteFrames"][name];
this.now_Food = name;
}
else{
this.Plant.getChildByName("name").getComponent(cc.Label).string = ""
this.Plant.getChildByName("icon").getComponent(cc.Sprite).spriteFrame = this.voiceSpriteFrame;
this.now_Food = name;
}
//关卡+1
cc.fx.GameConfig.GM_INFO.level += 1;
cc.tween(this.Plant)
.to(0.2,{opacity:255})
.call(()=>{
this.startTime = cc.fx.GameTool.getTime();
this.Btn_Hear.getChildByName("nomal").getComponent(cc.Button).interactable = true;
this.Btn_New.getChildByName("nomal").getComponent(cc.Button).interactable = true;
this.Btn_See.getChildByName("nomal").getComponent(cc.Button).interactable = true;
this.timeTween =
cc.tween(this.timeNode.getComponent(cc.Sprite))
.to(2.5,{fillRange:1})
.call(()=>{
this.timeTween = null;
this.Btn_Hear.getChildByName("nomal").getComponent(cc.Button).interactable = false;
this.Btn_New.getChildByName("nomal").getComponent(cc.Button).interactable = false;
this.Btn_See.getChildByName("nomal").getComponent(cc.Button).interactable = false;
this.nextLevel();
})
.start();
})
.start();
}
//新植物按钮
click_Choice(customData,data){
//关掉计时器
this.timeTween.stop();
this.timeTween = null;
//判断是否正确,并把出现的物品存入数组
this.result = this.judgingResult();
if(data == "Btn_New") this.result = !this.result;
//设置按钮状态
this.setBtnState(this[data]);
//获取反应时间
this.setTimeData();
//进行下一关
this.nextLevel();
}
//判断是否有过的结果 type只为new hear see 三选一
judgingResult(){
let result = false;
let arrayTemp = this.foodPicArray;
if(this.now_Food.substring(0,4) != "prop"){
arrayTemp = [];
arrayTemp = this.foodAudioArray;
}
if(arrayTemp.length > 0){
for(let i=0; i<arrayTemp.length;i++){
if(this.now_Food == arrayTemp[i]){
result = true;
break;
}
}
}
//判断后放进数组
arrayTemp.push(this.now_Food);
return result;
}
//计算反应时间
setTimeData(){
this.overTime = cc.fx.GameTool.getTime();
let time = this.overTime - this.startTime;
cc.fx.GameConfig.CLICK_DATA.stepTimeList.push(time);
}
//设置按钮状态
setBtnState(node){
if(this.result == false){
node.getChildByName("err").active = true;
node.getChildByName("correct").active = false;
}
else{
node.getChildByName("err").active = false;
node.getChildByName("correct").active = true;
}
node.getChildByName("nomal").active = false;
this.Btn_Hear.getChildByName("nomal").getComponent(cc.Button).interactable = false;
this.Btn_New.getChildByName("nomal").getComponent(cc.Button).interactable = false;
this.Btn_See.getChildByName("nomal").getComponent(cc.Button).interactable = false;
}
//下一轮 首先重置按钮状态
nextLevel(){
cc.tween(this.Plant)
.delay(1)
.call(()=>{
this.btnReset();
})
.to(0.5,{opacity:0})
.call(()=>{
this.createCustom();
})
.start();
}
btnReset(){
this.Btn_New.getChildByName("nomal").active = true;
this.Btn_New.getChildByName("err").active = false;
this.Btn_New.getChildByName("correct").active = false;
this.Btn_Hear.getChildByName("nomal").active = true;
this.Btn_Hear.getChildByName("err").active = false;
this.Btn_Hear.getChildByName("correct").active = false;
this.Btn_See.getChildByName("nomal").active = true;
this.Btn_See.getChildByName("err").active = false;
this.Btn_See.getChildByName("correct").active = false;
this.Btn_Hear.getChildByName("nomal").getComponent(cc.Button).interactable = false;
this.Btn_New.getChildByName("nomal").getComponent(cc.Button).interactable = false;
this.Btn_See.getChildByName("nomal").getComponent(cc.Button).interactable = false;
}
//返回首页
backScene(){
cc.director.loadScene("LoadScene");
}
//下一关或者重新开始或者返回上一关根据level决定
reStart(type){
}
//获取时间戳
getTime(){
const timestamp = new Date().getTime();
return timestamp;
}
//获胜
passLevel(){
}
//失败
loseLevel(type){
}
//如果是倒计时 调用此方法
startCountDownTime () {
this.timeNode.getComponent(cc.Sprite).fillRange = 0;
cc.tween(this.timeNode.getComponent(cc.Sprite))
.to(2,{fillRange:1})
.start();
}
//上传每次操作数据
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 () {
// cc.fx.Notifications.on("clickSun", this.clickSun, this);
}
onDisable () {
// cc.fx.Notifications.off("clickSun", this.clickSun);
}
update (dt) {
}
}