Commit 396d96dd authored by 范雪寒's avatar 范雪寒

feat: global问题

parent 181499e2
const tools = require("../script/tools"); const tools = require("../script/tools");
import { initGlobal_NGT_02 } from "../../script/common/_preDefine";
var picNode = cc.Class({ var picNode = cc.Class({
extends: cc.Component, extends: cc.Component,
...@@ -12,6 +14,7 @@ var picNode = cc.Class({ ...@@ -12,6 +14,7 @@ var picNode = cc.Class({
}, },
ctor: function () { ctor: function () {
initGlobal_NGT_02();
picNode.inst = this; picNode.inst = this;
g.picNode = picNode; g.picNode = picNode;
}, },
......
import { initGlobal_NGT_02 } from "../script/common/_preDefine";
/** /**
* 游戏主逻辑 * 游戏主逻辑
*/ */
...@@ -8,8 +10,8 @@ var game = cc.Class({ ...@@ -8,8 +10,8 @@ var game = cc.Class({
btnRestart: { btnRestart: {
default: null, default: null,
type: cc.Node, type: cc.Node,
displayName: "重开按钮" displayName: "重开按钮",
} },
}, },
ctor: function () { ctor: function () {
...@@ -23,9 +25,9 @@ var game = cc.Class({ ...@@ -23,9 +25,9 @@ var game = cc.Class({
//初始化游戏 //初始化游戏
this.initGame(); this.initGame();
if (window.addEventListener) { if (window.addEventListener) {
window.addEventListener('resize', this.scaleEventCallBack, false) window.addEventListener("resize", this.scaleEventCallBack, false);
} else if (window.attachEvent) { } else if (window.attachEvent) {
window.attachEvent('resize', this.scaleEventCallBack, false) window.attachEvent("resize", this.scaleEventCallBack, false);
} }
cc.debug.setDisplayStats(false); cc.debug.setDisplayStats(false);
}, },
...@@ -52,10 +54,10 @@ var game = cc.Class({ ...@@ -52,10 +54,10 @@ var game = cc.Class({
checkScale: function (num) { checkScale: function (num) {
var scale = 1; var scale = 1;
if (num > 2 && num <= 4) { if (num > 2 && num <= 4) {
scale = 0.74 scale = 0.74;
} }
if (num > 4) { if (num > 4) {
scale = 0.65 scale = 0.65;
} }
return scale; return scale;
}, },
...@@ -66,8 +68,9 @@ var game = cc.Class({ ...@@ -66,8 +68,9 @@ var game = cc.Class({
this.idx = this.checkNodeParent(); this.idx = this.checkNodeParent();
g.data_mgr.nowNum = itemArr.length; g.data_mgr.nowNum = itemArr.length;
itemArr.sort(function () { return Math.random() > 0.5 ? -1 : 1; }) itemArr.sort(function () {
return Math.random() > 0.5 ? -1 : 1;
});
for (var i = 0; i < itemArr.length; i++) { for (var i = 0; i < itemArr.length; i++) {
let newItem_0 = cc.instantiate(this.Item_0[itemArr.length > 14 ? 0 : 1]); let newItem_0 = cc.instantiate(this.Item_0[itemArr.length > 14 ? 0 : 1]);
...@@ -78,11 +81,12 @@ var game = cc.Class({ ...@@ -78,11 +81,12 @@ var game = cc.Class({
newItem_0.active = true; newItem_0.active = true;
//设置类别 //设置类别
let sheepInfo = g.data_mgr.getSheep(i); let sheepInfo = g.data_mgr.getSheep(i);
newItem_0.getChildByName("item").type = this.checkType(sheepInfo.sheepfoldId); newItem_0.getChildByName("item").type = this.checkType(
sheepInfo.sheepfoldId
);
newItem_0.parent = this.contentArr_2[this.idx]; newItem_0.parent = this.contentArr_2[this.idx];
}; }
}, },
//检测放到哪个节点底下 //检测放到哪个节点底下
...@@ -136,7 +140,8 @@ var game = cc.Class({ ...@@ -136,7 +140,8 @@ var game = cc.Class({
//初始化单个题目 //初始化单个题目
InitQuestion() { InitQuestion() {
var question = g.data_mgr.data.starArr[g.data_mgr.startId].queArr[g.data_mgr.ansId] var question =
g.data_mgr.data.starArr[g.data_mgr.startId].queArr[g.data_mgr.ansId];
//初始化灯 //初始化灯
g.Light.inst.addLightNum(question.hotZoneItemArr.length); g.Light.inst.addLightNum(question.hotZoneItemArr.length);
//初始化标题 //初始化标题
...@@ -145,7 +150,7 @@ var game = cc.Class({ ...@@ -145,7 +150,7 @@ var game = cc.Class({
g.data_mgr.gameState = 2; g.data_mgr.gameState = 2;
setTimeout(() => { setTimeout(() => {
g.titleType.inst.onBtnPlayEffect(); g.titleType.inst.onBtnPlayEffect();
}, 500) }, 500);
} }
//设置中间图片 //设置中间图片
g.picNode.inst.onShow(question); g.picNode.inst.onShow(question);
...@@ -161,7 +166,10 @@ var game = cc.Class({ ...@@ -161,7 +166,10 @@ var game = cc.Class({
let num = g.Light.inst.showLight(); let num = g.Light.inst.showLight();
if (num == -1) { if (num == -1) {
g.data_mgr.ansId += 1; g.data_mgr.ansId += 1;
if (g.data_mgr.ansId >= g.data_mgr.data.starArr[g.data_mgr.startId].queArr.length) { if (
g.data_mgr.ansId >=
g.data_mgr.data.starArr[g.data_mgr.startId].queArr.length
) {
//大星星音效 //大星星音效
g.speaker.inst.playEffect(g.enum.E_Audio.Star); g.speaker.inst.playEffect(g.enum.E_Audio.Star);
g.scoreStart.inst.showStar(); g.scoreStart.inst.showStar();
...@@ -173,19 +181,20 @@ var game = cc.Class({ ...@@ -173,19 +181,20 @@ var game = cc.Class({
g.speaker.inst.playEffect(g.enum.E_Audio.Flowers); g.speaker.inst.playEffect(g.enum.E_Audio.Flowers);
g.effect.inst.showEffect2(); g.effect.inst.showEffect2();
this.btnRestart.active = true; this.btnRestart.active = true;
const middleLayer = cc.find('middleLayer'); const middleLayer = cc.find("middleLayer");
if (middleLayer) { if (middleLayer) {
const middleLayerComponent = middleLayer.getComponent('middleLayer'); const middleLayerComponent =
if (middleLayerComponent.role == 'student') { middleLayer.getComponent("middleLayer");
middleLayerComponent.onHomeworkFinish(() => { }); if (middleLayerComponent.role == "student") {
middleLayerComponent.onHomeworkFinish(() => {});
} }
} else { } else {
console.log('onHomeworkFinish'); console.log("onHomeworkFinish");
} }
} else { } else {
this.InitQuestion(); this.InitQuestion();
} }
}, 1500) }, 1500);
} }
}, },
...@@ -223,45 +232,35 @@ var game = cc.Class({ ...@@ -223,45 +232,35 @@ var game = cc.Class({
this.setAudioInfo(1); this.setAudioInfo(1);
}, },
checkAnswer(caller, option_i) { checkAnswer(caller, option_i) {
if (caller == undefined) { if (caller == undefined) {
return; return;
} }
if (option_i != -1) { if (option_i != -1) {
let nodOption = this.contType[this.OptionType].children[option_i - 1] let nodOption = this.contType[this.OptionType].children[option_i - 1];
if (nodOption) { if (nodOption) {
let AABB = nodOption.getComponent("DragGameOptionObject").setAABB(); let AABB = nodOption.getComponent("DragGameOptionObject").setAABB();
let callerAABB = this.getCallerAABB(caller); let callerAABB = this.getCallerAABB(caller);
var isIntersect = AABB.containsRect(callerAABB);//判断是否被包含 var isIntersect = AABB.containsRect(callerAABB); //判断是否被包含
console.log(isIntersect) console.log(isIntersect);
if (isIntersect) { if (isIntersect) {
return nodOption; return nodOption;
} }
return false return false;
} }
} }
}, },
getCallerAABB(caller) { getCallerAABB(caller) {
let svLeftBottomPoint = caller.parent.convertToWorldSpaceAR( let svLeftBottomPoint = caller.parent.convertToWorldSpaceAR(
cc.v2( cc.v2(caller.x, caller.y)
caller.x,
caller.y
)
); );
var posNode_1 = cc.rect( var posNode_1 = cc.rect(svLeftBottomPoint.x, svLeftBottomPoint.y, 0, 0);
svLeftBottomPoint.x, return posNode_1;
svLeftBottomPoint.y,
0,
0
);
return posNode_1
}, },
onDestroy() { onDestroy() {
window.g = null; window.g = null;
} },
}); });
import { initDataMgr_NGT_02 } from "../manager/data_mgr";
import { initEventMgr_NGT_02 } from "../manager/event_mgr";
import { initStorageMgr_NGT_02 } from "../manager/local_storage";
import { initResMgr_NGT_02 } from "../manager/res_mgr";
import { initSndMgr_NGT_02 } from "../manager/snd_mgr";
import { initUtils_NGT_02 } from "./utils";
import { initEnumMgr_NGT_02 } from "../game/speaker";
// 全局环境预声明 // 全局环境预声明
function initGlobal_NGT_02() { export function initGlobal_NGT_02() {
console.log('window.g init'); console.log('window.g init');
if (!window.g) {
window.g = {}; window.g = {};
initDataMgr_NGT_02(); initDataMgr_NGT_02();
initEventMgr_NGT_02(); initEventMgr_NGT_02();
...@@ -8,4 +16,6 @@ function initGlobal_NGT_02() { ...@@ -8,4 +16,6 @@ function initGlobal_NGT_02() {
initResMgr_NGT_02(); initResMgr_NGT_02();
initSndMgr_NGT_02(); initSndMgr_NGT_02();
initUtils_NGT_02(); initUtils_NGT_02();
initEnumMgr_NGT_02();
}
} }
\ No newline at end of file
function initUtils_NGT_02() { export function initUtils_NGT_02() {
g.utils = { g.utils = {
// 范围随机 // 范围随机
randFromTo: function (_min, _max) { randFromTo: function (_min, _max) {
...@@ -42,16 +42,17 @@ function initUtils_NGT_02() { ...@@ -42,16 +42,17 @@ function initUtils_NGT_02() {
} }
}, },
}; };
}
export function getPosByAngle(angle, len) { export function getPosByAngle(angle, len) {
const radian = (angle * Math.PI) / 180; const radian = (angle * Math.PI) / 180;
const x = Math.sin(radian) * len; const x = Math.sin(radian) * len;
const y = Math.cos(radian) * len; const y = Math.cos(radian) * len;
return { x, y }; return { x, y };
} }
export function getAngleByPos(px, py, mx, my) { export function getAngleByPos(px, py, mx, my) {
const x = Math.abs(px - mx); const x = Math.abs(px - mx);
const y = Math.abs(py - my); const y = Math.abs(py - my);
...@@ -87,27 +88,25 @@ function initUtils_NGT_02() { ...@@ -87,27 +88,25 @@ function initUtils_NGT_02() {
// console.log('angle: ', angle); // console.log('angle: ', angle);
return angle; return angle;
} }
export function exchangeNodePos(baseNode, targetNode) { export function exchangeNodePos(baseNode, targetNode) {
return baseNode.convertToNodeSpaceAR( return baseNode.convertToNodeSpaceAR(
targetNode._parent.convertToWorldSpaceAR( targetNode._parent.convertToWorldSpaceAR(cc.v2(targetNode.x, targetNode.y))
cc.v2(targetNode.x, targetNode.y)
)
); );
} }
export function RandomInt(a, b = 0) { export function RandomInt(a, b = 0) {
let max = Math.max(a, b); let max = Math.max(a, b);
let min = Math.min(a, b); let min = Math.min(a, b);
return Math.floor(Math.random() * (max - min) + min); return Math.floor(Math.random() * (max - min) + min);
} }
export function Between(a, b, c) { export function Between(a, b, c) {
return [a, b, c].sort((a, b) => a - b)[1]; return [a, b, c].sort((a, b) => a - b)[1];
} }
export function randomSortByArr(arr) { export function randomSortByArr(arr) {
const newArr = []; const newArr = [];
const tmpArr = arr.concat(); const tmpArr = arr.concat();
while (tmpArr.length > 0) { while (tmpArr.length > 0) {
...@@ -116,9 +115,9 @@ function initUtils_NGT_02() { ...@@ -116,9 +115,9 @@ function initUtils_NGT_02() {
tmpArr.splice(randomIndex, 1); tmpArr.splice(randomIndex, 1);
} }
return newArr; return newArr;
} }
export async function asyncTweenTo(node, duration, obj, ease = undefined) { export async function asyncTweenTo(node, duration, obj, ease = undefined) {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
cc.tween(node) cc.tween(node)
.to(duration, obj, ease) .to(duration, obj, ease)
...@@ -127,9 +126,9 @@ function initUtils_NGT_02() { ...@@ -127,9 +126,9 @@ function initUtils_NGT_02() {
}) })
.start(); .start();
}); });
} }
export async function asyncTweenBy(node, duration, obj, ease = undefined) { export async function asyncTweenBy(node, duration, obj, ease = undefined) {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
cc.tween(node) cc.tween(node)
.by(duration, obj, ease) .by(duration, obj, ease)
...@@ -138,14 +137,14 @@ function initUtils_NGT_02() { ...@@ -138,14 +137,14 @@ function initUtils_NGT_02() {
}) })
.start(); .start();
}); });
} }
export async function asyncPlayDragonBoneAnimation( export async function asyncPlayDragonBoneAnimation(
node, node,
animationName, animationName,
time = 1, time = 1,
onFrameEvent onFrameEvent
) { ) {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
node node
.getComponent(dragonBones.ArmatureDisplay) .getComponent(dragonBones.ArmatureDisplay)
...@@ -165,9 +164,9 @@ function initUtils_NGT_02() { ...@@ -165,9 +164,9 @@ function initUtils_NGT_02() {
.getComponent(dragonBones.ArmatureDisplay) .getComponent(dragonBones.ArmatureDisplay)
.playAnimation(animationName, time); .playAnimation(animationName, time);
}); });
} }
export async function asyncPlayEffectByUrl(url, loop = false) { export async function asyncPlayEffectByUrl(url, loop = false) {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
cc.assetManager.loadRemote(url, (err, clip) => { cc.assetManager.loadRemote(url, (err, clip) => {
console.log(clip); console.log(clip);
...@@ -175,9 +174,9 @@ function initUtils_NGT_02() { ...@@ -175,9 +174,9 @@ function initUtils_NGT_02() {
resolve(); resolve();
}); });
}); });
} }
export async function jelly(node) { export async function jelly(node) {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
cc.tween(node) cc.tween(node)
.to(0.1, { scaleX: 0.9, scaleY: 1.1 }) .to(0.1, { scaleX: 0.9, scaleY: 1.1 })
...@@ -186,24 +185,24 @@ function initUtils_NGT_02() { ...@@ -186,24 +185,24 @@ function initUtils_NGT_02() {
.call(resolve) .call(resolve)
.start(); .start();
}); });
} }
export async function asyncDelay(time) { export async function asyncDelay(time) {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
setTimeout(() => { setTimeout(() => {
resolve(); resolve();
}, time * 1000); }, time * 1000);
}); });
} }
export async function showFireworks( export async function showFireworks(
baseNode, baseNode,
nodeList, nodeList,
pos = cc.v2(0, 0), pos = cc.v2(0, 0),
side = cc.v2(0, 100), side = cc.v2(0, 100),
range = 50, range = 50,
number = 100 number = 100
) { ) {
new Array(number).fill(" ").forEach(async (_, i) => { new Array(number).fill(" ").forEach(async (_, i) => {
let rabbonNode = new cc.Node(); let rabbonNode = new cc.Node();
rabbonNode.parent = baseNode; rabbonNode.parent = baseNode;
...@@ -257,12 +256,11 @@ function initUtils_NGT_02() { ...@@ -257,12 +256,11 @@ function initUtils_NGT_02() {
}) })
.start(); .start();
}); });
} }
async function rabbonFall(node) { async function rabbonFall(node) {
const time = 1 + Math.random(); const time = 1 + Math.random();
const offsetX = RandomInt(-200, 200) * time; const offsetX = RandomInt(-200, 200) * time;
await asyncTweenBy(node, time, { x: offsetX, angle: (offsetX * 60) / 200 }); await asyncTweenBy(node, time, { x: offsetX, angle: (offsetX * 60) / 200 });
rabbonFall(node); rabbonFall(node);
}
} }
g.enum = { export function initEnumMgr_NGT_02() {
g.enum = {
E_Audio: { E_Audio: {
Star: 0,//大星星 Star: 0, //大星星
BtnCommom: 1,//按钮 BtnCommom: 1, //按钮
Flowers: 2,//撒花 Flowers: 2, //撒花
Right: 3,//正确 Right: 3, //正确
Error: 4,//错误 Error: 4, //错误
Question: 5,//题数 Question: 5, //题数
} },
};
} }
/** /**
* 音效 * 音效
...@@ -19,7 +21,7 @@ var speaker = cc.Class({ ...@@ -19,7 +21,7 @@ var speaker = cc.Class({
eff_audio: { eff_audio: {
default: [], default: [],
type: cc.AudioClip, type: cc.AudioClip,
displayName: "音效列表" displayName: "音效列表",
}, },
}, },
......
/** /**
* 数据管理器 * 数据管理器
*/ */
function initDataMgr_NGT_02() { export function initDataMgr_NGT_02() {
g.data_mgr = { g.data_mgr = {
data: null, //表所有数据 data: null, //表所有数据
......
function initEventMgr_NGT_02() { export function initEventMgr_NGT_02() {
// 事件汞 // 事件汞
let eventList = {}; // 响应列表(元素结构:eventName,[[target:cb]]) let eventList = {}; // 响应列表(元素结构:eventName,[[target:cb]])
g.event_mgr = { g.event_mgr = {
......
function initStorageMgr_NGT_02() { export function initStorageMgr_NGT_02() {
// localStorage封装 // localStorage封装
g.local_storage = { g.local_storage = {
// 背景音乐音量 // 背景音乐音量
......
function initResMgr_NGT_02() { export function initResMgr_NGT_02() {
/** /**
* 资源管理器 * 资源管理器
*/ */
......
function initSndMgr_NGT_02() { export function initSndMgr_NGT_02() {
// 声音管理器 // 声音管理器
g.snd_mgr = { g.snd_mgr = {
bgmId: -1, // 背景音乐的音频ID bgmId: -1, // 背景音乐的音频ID
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment