ShenNong/assets/Script/GameManager.ts
2024-07-10 18:19:26 +08:00

415 lines
14 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 "./module/Music/AudioManager";
// 主游戏控制类
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: number; //当前关卡名字信息
choice: number; //当前关卡选择第几个按钮
foodPicArray: any; //存放当前每一大局游戏所有植物图片
foodAudioArray: any; //存放当前每一大局游戏所有植物声音
customData:any; //存放当前配置
needleTween: cc.Tween<cc.Sprite>;
onLoad () {
this.fit();
this.init();
this.startGame();
}
start () {
}
//初始化数据
init(){
this.touchable = false;
this.result = -1;
this.foodAudioArray = [];
this.foodPicArray = [];
this.timeTween = null;
this.needleTween = null;
this.choice = 0;
cc.fx.GameConfig.GM_INFO.stepTimeList = 0;
cc.fx.GameConfig.GM_INFO.successList = [];
cc.fx.GameConfig.GM_INFO.level = 0;
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.choice = 0;
this.Plant.opacity = 0;
this.timeNode.getChildByName("time").getComponent(cc.Sprite).fillRange = 0;
this.timeNode.getChildByName("needle").angle = 0;
if(cc.fx.GameConfig.GM_INFO.level >= cc.fx.GameConfig.LEVEL_INFO[cc.fx.GameConfig.GM_INFO.custom].custom.length){
cc.director.loadScene("OverScene");
return;
}
this.customData = cc.fx.GameConfig.LEVEL_INFO[cc.fx.GameConfig.GM_INFO.custom].custom[cc.fx.GameConfig.GM_INFO.level];
let name = this.customData.item;
//埋点数据设置
cc.fx.GameConfig.CLICK_SET("round",cc.fx.GameConfig.GM_INFO.level+1);
cc.fx.GameConfig.CLICK_SET("levelConfig",(cc.fx.GameConfig.GM_INFO.custom+1));
cc.fx.GameConfig.CLICK_SET("item",name);
cc.fx.GameConfig.CLICK_SET("roundType",this.customData.type);
if(this.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;
cc.tween(this.Plant)
.to(0.2,{opacity:255})
.call(()=>{
this.startTimer();
})
.start();
}
else{
this.Plant.getChildByName("name").getComponent(cc.Label).string = ""
this.Plant.getChildByName("icon").getComponent(cc.Sprite).spriteFrame = this.voiceSpriteFrame;
let audioName = name + "_audio";
name = "audio"+ name;
this.now_Food = name;
cc.fx.AudioManager._instance.playEffect(audioName,this.startTimer.bind(this));
cc.tween(this.Plant)
.to(0.2,{opacity:255})
.call(()=>{
})
.start();
}
//关卡+1
cc.fx.GameConfig.GM_INFO.level += 1;
}
//开始转圈倒计时
startTimer(){
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.Btn_Firewood.getComponent(cc.Button).interactable = true;
// if(this.containsNanana(this.now_Food) == true){
// console.log("可以点火");
// }
this.timeNode.getChildByName("time").getComponent(cc.Sprite).fillRange = 0;
this.needleTween =
cc.tween(this.timeNode.getChildByName("needle"))
.to(5,{angle:-360})
.start();
this.timeTween =
cc.tween(this.timeNode.getChildByName("time").getComponent(cc.Sprite))
.to(5,{fillRange:1})
.call(()=>{
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.Btn_Firewood.getComponent(cc.Button).interactable = false;
this.click_Choice(null,null);
this.timeTween = null;
})
.start();
}
containsNanana(str) {
return /muchai/i.test(str);
}
//新植物按钮
click_Choice(customData,data){
//关掉计时器 处理逻辑之前,先行关掉按钮开关
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.Btn_Firewood.getComponent(cc.Button).interactable = false;
this.timeTween.stop();
this.timeTween = null;
this.needleTween.stop();
this.needleTween = null;
//判断是否正确,并把出现的物品存入数组
this.result = this.judgingResult();
if(data == "Btn_New"){
if(this.result == -1){
this.result = 1;
}
else{
this.result = -1;
}
}
var tempNode = null;
if(data != null) tempNode = this[data]
//设置按钮状态
this.setBtnState(tempNode,data);
//获取反应时间
this.setTimeData();
//根据答题,设置上传数据
this.setData(data);
//进行下一关
this.nextLevel();
}
//点火按钮
click_Ignition(){
cc.fx.GameConfig.CLICK_SET("ignite",true);
this.Btn_Firewood.getComponent(cc.Button).interactable = false;
if(this.containsNanana(this.now_Food) == true){
let igniteCount = cc.fx.GameConfig.GM_INFO.igniteCount + 1;
cc.fx.GameConfig.GM_INFO_SET("igniteCount",igniteCount);
cc.fx.GameConfig.CLICK_SET("igniteCount",igniteCount);
this.Btn_Firewood.getChildByName("fireBg").active = true;
this.Btn_Firewood.getChildByName("fire").active = true;
setTimeout(() => {
if(this.Btn_Firewood){
this.Btn_Firewood.getChildByName("fireBg").active = false;
this.Btn_Firewood.getChildByName("fire").active = false;
}
}, 1050);
console.log("正确点火");
}
else if(this.customData.flamable == 1){
console.log("错误点火");
// console.log("正确点火");
}
}
//判断是否有过的结果 type只为new hear see 三选一
judgingResult(){
let result = -1;
//先检查音频
let name = this.now_Food.substring(5,this.now_Food.length);
if(this.foodAudioArray.length > 0){
for(let i=0; i<this.foodAudioArray.length;i++){
if(name == this.foodAudioArray[i].substring(5,this.now_Food.length)){
result = 2;
this.foodAudioArray.splice(i,1);
cc.fx.GameConfig.CLICK_SET("rightChoice",2);
console.log("听到过");
i = 10000;
break;
}
}
}
//再检查图盘
if(this.foodPicArray.length > 0){
for(let i=0; i<this.foodPicArray.length;i++){
if(name == this.foodPicArray[i].substring(5,this.now_Food.length)){
//如果图片里面有
result = 3;
this.foodPicArray.splice(i,1);
console.log("看到过");
cc.fx.GameConfig.CLICK_SET("rightChoice",3);
i = 10000;
break;
}
}
}
//把新生成的存放进数组里
if(this.now_Food.substring(0,4) != "prop"){
this.foodAudioArray.push(this.now_Food);
}
else {
this.foodPicArray.push(this.now_Food);
}
if(result != 2 && result != 3){
console.log("没出险过");
cc.fx.GameConfig.CLICK_SET("rightChoice",1);
}
return result;
}
//计算反应时间
setTimeData(){
this.overTime = cc.fx.GameTool.getTime();
let time = this.overTime - this.startTime;
//防止溢出
if(time < 0 || time > 5000) time = 5000;
cc.fx.GameConfig.CLICK_SET("stepTime",time);
cc.fx.GameConfig.GM_INFO.stepTimeList += time;
}
//设置按钮状态
setBtnState(node,data){
if(data == "Btn_New") this.choice = 1;
else if(data == "Btn_Hear") this.choice = 2;
else if(data == "Btn_See") this.choice = 3;
if(data == null){
this.choice = 0;
}
if(this.choice != cc.fx.GameConfig.CLICK_DATA.rightChoice){
if(node){
cc.fx.AudioManager._instance.playEffect("err",null);
node.getChildByName("err").active = true;
node.getChildByName("correct").active = false;
}
}
else{
if(node){
cc.fx.AudioManager._instance.playEffect("yes",null);
node.getChildByName("err").active = false;
node.getChildByName("correct").active = true;
}
}
if(node) 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;
// this.Btn_Firewood.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.getChildByName("time").getComponent(cc.Sprite).fillRange = 0;
this.timeNode.getChildByName("needle").angle = 0;
}
//上传每次操作数据
setData(data){
if(data == "Btn_New") this.choice = 1;
else if(data == "Btn_Hear") this.choice = 2;
else if(data == "Btn_See") this.choice = 3;
else if(data == null){
this.choice = 0;
cc.fx.GameConfig.CLICK_SET("stepTime",5000);
}
var jg = false;
if(this.choice == cc.fx.GameConfig.CLICK_DATA.rightChoice){
jg = true;
cc.fx.GameConfig.GM_INFO.successList.push(jg);
}
cc.fx.GameConfig.CLICK_SET("choice",this.choice);
cc.fx.GameConfig.CLICK_SET("success",jg);
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) {
}
}