ShenNong_Simple/assets/Script/GameManager.ts
2024-10-30 15:28:42 +08:00

379 lines
12 KiB
TypeScript
Raw Permalink 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_Old: 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 () {
}
//处理地图
setMap(){
}
//初始化数据
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_Old.y = -450;
this.Btn_New.y = -300;
}
}
//创建关卡
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){
setTimeout(() => {
cc.director.loadScene("OverScene");
}, 1000);
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);
cc.tween(this.Plant)
.to(0.2,{opacity:255})
.call(()=>{
this.startTimer();
})
.start();
}
//关卡+1
cc.fx.GameConfig.GM_INFO.level += 1;
}
//开始转圈倒计时
startTimer(){
this.startTime = cc.fx.GameTool.getTime();
this.Btn_Old.getChildByName("nomal").getComponent(cc.Button).interactable = true;
this.Btn_New.getChildByName("nomal").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(8,{angle:-360})
.start();
this.timeTween =
cc.tween(this.timeNode.getChildByName("time").getComponent(cc.Sprite))
.to(8,{fillRange:1})
.call(()=>{
this.Btn_Old.getChildByName("nomal").getComponent(cc.Button).interactable = false;
this.Btn_New.getChildByName("nomal").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_Old.getChildByName("nomal").getComponent(cc.Button).interactable = false;
this.Btn_New.getChildByName("nomal").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(){
}
//判断是否有过的结果 type只为new hear see 三选一
judgingResult(){
let result = -1;
let name = this.now_Food.substring(5,this.now_Food.length);
//再检查图盘
if(this.foodPicArray.length > 0){
for(let i=0; i<this.foodPicArray.length;i++){
console.log(name,this.foodPicArray[i].substring(5,this.foodPicArray[i].length));
if(name == this.foodPicArray[i].substring(5,this.foodPicArray[i].length)){
//如果图片里面有
result = 2;
console.log("出现过",name);
cc.fx.GameConfig.CLICK_SET("rightChoice",2);
i = 10000;
break;
}
}
}
if(result != 2 ){
console.log("没出险过",name);
cc.fx.GameConfig.CLICK_SET("rightChoice",1);
//把新生成的存放进数组里
this.foodPicArray.push(this.now_Food);
}
return result;
}
//计算反应时间
setTimeData(){
this.overTime = cc.fx.GameTool.getTime();
let time = this.overTime - this.startTime;
//防止溢出
if(time < 0 || time > 8000) time = 8000;
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_Old") 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_Old.getChildByName("nomal").getComponent(cc.Button).interactable = false;
this.Btn_New.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_Old.getChildByName("nomal").active = true;
this.Btn_Old.getChildByName("err").active = false;
this.Btn_Old.getChildByName("correct").active = false;
this.Btn_Old.getChildByName("nomal").getComponent(cc.Button).interactable = false;
this.Btn_New.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.getChildByName("time").getComponent(cc.Sprite).fillRange = 0;
this.timeNode.getChildByName("needle").angle = 0;
}
//上传每次操作数据
setData(data){
console.log("进入上传埋点");
if(data == "Btn_New") this.choice = 1;
else if(data == "Btn_Old") 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);
console.log("进入GameTool.setGameData",cc.fx.GameConfig.CLICK_DATA);
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) {
}
}