Commit b719705e authored by Tt's avatar Tt

pg修改

parent 5eed1804
import { asyncDelay, onHomeworkFinish, RandomInt } from "../script/util"; import { asyncDelay, onHomeworkFinish, RandomInt } from "../script/util";
import { MyCocosSceneComponent } from "../script/MyCocosSceneComponent"; import { MyCocosSceneComponent } from "../script/MyCocosSceneComponent";
import Game, { FISH_OUT, GAME_STATE, Option } from "./tool/Game"; import Game from "./tool/Game";
import pg from "./tool/pg"; import pg from "./tool/pg";
const { ccclass, property } = cc._decorator; const { ccclass, property } = cc._decorator;
...@@ -10,24 +10,24 @@ export default class SceneComponent extends MyCocosSceneComponent { ...@@ -10,24 +10,24 @@ export default class SceneComponent extends MyCocosSceneComponent {
addPreloadImage() { addPreloadImage() {
// TODO 根据自己的配置预加载图片资源 // TODO 根据自己的配置预加载图片资源
let arr = []; let arr = [];
this.data.questions.forEach(question => { // this.data.questions.forEach(question => {
arr.push({ url: question.bgItem.url }) // arr.push({ url: question.bgItem.url })
question.hotZoneItemArr.forEach(zone => { // question.hotZoneItemArr.forEach(zone => {
}) // })
}) // })
this._imageResList.push(...arr); // this._imageResList.push(...arr);
} }
addPreloadAudio() { addPreloadAudio() {
// TODO 根据自己的配置预加载音频资源 // TODO 根据自己的配置预加载音频资源
let arr = []; let arr = [];
this.data.questions.forEach(question => { // this.data.questions.forEach(question => {
question.hotZoneItemArr.forEach(zone => { // question.hotZoneItemArr.forEach(zone => {
arr.push({ url: zone.audio_url }) // arr.push({ url: zone.audio_url })
}) // })
}) // })
this._audioResList.push(...arr); // this._audioResList.push(...arr);
} }
addPreloadAnima() { addPreloadAnima() {
...@@ -241,7 +241,6 @@ export default class SceneComponent extends MyCocosSceneComponent { ...@@ -241,7 +241,6 @@ export default class SceneComponent extends MyCocosSceneComponent {
this.openDouble = false; this.openDouble = false;
this.openSingle = false; this.openSingle = false;
// this.onPlayAllAudio(); // this.onPlayAllAudio();
this.preLoadAudio();
} }
onTouchBtnPre() { onTouchBtnPre() {
this.onStopAllAudio(); this.onStopAllAudio();
...@@ -330,15 +329,6 @@ export default class SceneComponent extends MyCocosSceneComponent { ...@@ -330,15 +329,6 @@ export default class SceneComponent extends MyCocosSceneComponent {
} else { } else {
cc.audioEngine.resumeAllEffects(); cc.audioEngine.resumeAllEffects();
} }
// if (this.currentPlay && this.currentPlay.audioId) {
// cc.audioEngine.pause(this.currentPlay.audioId);
// cc.audioEngine.resume(this.currentPlay.audioId);
// }
// if (this.pauseing) {
// cc.director.pause();
// } else {
// cc.director.resume();
// }
} else { } else {
this.updateTips('开始连续播放音频'); this.updateTips('开始连续播放音频');
this.openDouble = false; this.openDouble = false;
...@@ -350,15 +340,6 @@ export default class SceneComponent extends MyCocosSceneComponent { ...@@ -350,15 +340,6 @@ export default class SceneComponent extends MyCocosSceneComponent {
} }
} }
private lastTime: number; private lastTime: number;
preLoadAudio() {
// for (let i = 0; i < this.currentTouchAreas.length; i++) {
// let node = this.currentTouchAreas[i];
// if (!node) return;
// let data = node.data;
// if (!node) return;
// pg.audio.getAudioClip(data.audio);
// }
}
onPlayAllAudio() { onPlayAllAudio() {
return new Promise(async (resolve, reject) => { return new Promise(async (resolve, reject) => {
this.openPlay = true; this.openPlay = true;
......
export enum FISH_OUT {
RUNNING,
TOUCH,
CATCH,
ESCAP,
OUT
}
export class Fish {
public isOut: number;
public node: cc.Node;
public isLeft: boolean;//在左侧 向右运动
constructor() {
this.isOut = 0;
this.isLeft = Math.random() < 0.5;
}
}
export class Option {
public type;
public txt;
public picUrl;
public audioUrl;
// public right: boolean;
public data;
public id: number;
public count: number;
public time: number;
public touchRight: boolean;
public parent;
public pairKey;
constructor(data, id) {
this.id = id;
this.type = data.type;
// this.count = 0;
this.txt = data.text;
// this.time = data.duration;
this.picUrl = data.image;
this.audioUrl = data.audio;
// this.right = data.right;
// this.touchRight = false;
}
}
export class Item { export class Item {
public page; public page;
public audio; public audio;
...@@ -80,52 +38,6 @@ export class Item { ...@@ -80,52 +38,6 @@ export class Item {
this.pointsArr = arr.concat(); this.pointsArr = arr.concat();
} }
} }
class Role {
public score: number;
public right: number;
public error: number;
public voices: Array<any>;
constructor() {
this.score = 0;
this.error = 0;
this.right = 0;
this.voices = [];
}
addScore(score: number = 1) {
this.score += score;
}
addError(score: number = 1) {
this.error += score;
}
addRight() {
this.right += 1;
}
addVoice(obj: any) {
this.voices.push(obj);
}
}
export class Player extends Role {
constructor() {
super();
}
reset() {
this.score = 0;
this.error = 0;
this.right = 0;
this.voices = [];
}
}
export class Robot extends Role {
constructor() {
super();
}
reset() {
this.score = 0;
this.error = 0;
this.right = 0;
this.voices = [];
}
}
export enum GAME_STATE { export enum GAME_STATE {
WAIT, WAIT,
RUNNING, RUNNING,
...@@ -139,16 +51,12 @@ export default class Game { ...@@ -139,16 +51,12 @@ export default class Game {
} }
private data: any; private data: any;
public lists: Array<Item> public lists: Array<Item>
public player: Player;
public robot: Robot;
public state: GAME_STATE; public state: GAME_STATE;
public total: number; public total: number;
constructor() { constructor() {
this.start = false; this.start = false;
this.lists = []; this.lists = [];
this.player = new Player();
this.robot = new Robot();
this.state = GAME_STATE.WAIT; this.state = GAME_STATE.WAIT;
} }
get len() { get len() {
...@@ -158,14 +66,12 @@ export default class Game { ...@@ -158,14 +66,12 @@ export default class Game {
public isCanClick: boolean; public isCanClick: boolean;
public failCount; public failCount;
public dispelCount; public dispelCount;
public aniEnter;
public startAudio: string; public startAudio: string;
public init(data) { public init(data) {
this.startAudio = data.startAudio || ""; this.startAudio = data.startAudio || "";
this.start = false; this.start = false;
this.lists = []; this.lists = [];
this.data = data.questions; this.data = data.questions;
this.aniEnter = data.imgAni;
} }
public start: boolean; public start: boolean;
public page: number; public page: number;
...@@ -173,8 +79,6 @@ export default class Game { ...@@ -173,8 +79,6 @@ export default class Game {
this.isCanClick = false; this.isCanClick = false;
this.failCount = 0; this.failCount = 0;
this.dispelCount = 0; this.dispelCount = 0;
this.player.reset();
this.robot.reset();
this.page = 0; this.page = 0;
this.start = true; this.start = true;
this.lists = []; this.lists = [];
...@@ -215,13 +119,8 @@ export default class Game { ...@@ -215,13 +119,8 @@ export default class Game {
hasNext() { hasNext() {
return this.page < this.lists.length - 1; return this.page < this.lists.length - 1;
} }
get getTotla() { get getTotal() {
return this.lists.length; return this.lists.length;
// this.total = 0;
// this.lists.forEach((item) => {
// this.total += item.rNum;
// })
// return this.total;
} }
get isOver() { get isOver() {
return this.page > this.lists.length; return this.page > this.lists.length;
......
...@@ -16,7 +16,8 @@ class Emitter { ...@@ -16,7 +16,8 @@ class Emitter {
clear() { clear() {
this._callbacks = {}; this._callbacks = {};
} }
on(event, fn) { on(event, fn: any) {
this.off(event);
if (!this._callbacks[event]) this._callbacks[event] = []; if (!this._callbacks[event]) this._callbacks[event] = [];
this._callbacks[event].push(fn); this._callbacks[event].push(fn);
}; };
...@@ -28,7 +29,7 @@ class Emitter { ...@@ -28,7 +29,7 @@ class Emitter {
on.fn = fn; on.fn = fn;
this.on(event, on); this.on(event, on);
}; };
off(event, fn) { off(event, fn?: any) {
// all // all
if (0 == arguments.length) { if (0 == arguments.length) {
this._callbacks = {}; this._callbacks = {};
...@@ -75,6 +76,32 @@ class Emitter { ...@@ -75,6 +76,32 @@ class Emitter {
return !!this.listeners(event).length; return !!this.listeners(event).length;
}; };
} }
class Signal<T = void> {
private handlers: ((data: T) => void)[] = [];
private thisArgs: any[] = [];
public on(handler: (data: T) => void, thisArg: any): void {
this.off(handler);
this.handlers.push(handler);
this.thisArgs.push(thisArg);
}
public off(handler: (data: T) => void): void {
const index: number = this.handlers.indexOf(handler);
if (index == -1) return;
this.handlers.splice(index, 1);
this.thisArgs.splice(index, 1);
}
public emit(data: T): void {
// protect from trigger >> off
const handlers: ((data: T) => void)[] = [...this.handlers];
const thisArgs: any[] = [...this.thisArgs];
for (let i = 0; i < handlers.length; i++) {
handlers[i].call(thisArgs[i], data);
}
}
}
class TimeCtrl { class TimeCtrl {
private current: number; private current: number;
...@@ -131,7 +158,7 @@ let pg = { ...@@ -131,7 +158,7 @@ let pg = {
return node; return node;
}, },
//添加节点 //添加节点
addChild(item, child, zIndex) { addChild(item, child, zIndex = 0) {
if (!child) return console.log("addChild error ->请传入子节点"); if (!child) return console.log("addChild error ->请传入子节点");
if (!item) return console.log("addChild error ->请传入父节点"); if (!item) return console.log("addChild error ->请传入父节点");
let node = item.node ? item.node : item; let node = item.node ? item.node : item;
...@@ -250,6 +277,7 @@ let pg = { ...@@ -250,6 +277,7 @@ let pg = {
} }
if (!node) return pg.logger.w("图片更换失败,传入了错误的item"); if (!node) return pg.logger.w("图片更换失败,传入了错误的item");
if (node.net_url == res) return; if (node.net_url == res) return;
let nodeActive = node.active;
node.active = false;// node.active = false;//
pg.load.loadNetImg(res).then((texture: cc.Texture2D) => { pg.load.loadNetImg(res).then((texture: cc.Texture2D) => {
if (!cc.isValid(node)) return pg.logger.i("节点已销毁"); if (!cc.isValid(node)) return pg.logger.i("节点已销毁");
...@@ -264,11 +292,11 @@ let pg = { ...@@ -264,11 +292,11 @@ let pg = {
if (w && h && custom) { if (w && h && custom) {
let scaleX = w / nw; let scaleX = w / nw;
let scaleY = h / nh; let scaleY = h / nh;
// let scale = scaleX <= scaleY ? scaleX : scaleY; let scale = scaleX <= scaleY ? scaleX : scaleY;
node.width = nw * scaleX; node.width = nw * scale;
node.height = nh * scaleY; node.height = nh * scale;
} }
node.active = true; node.active = nodeActive;
resolve({ w: nw, h: nh }); resolve({ w: nw, h: nh });
}) })
}) })
...@@ -324,6 +352,7 @@ let pg = { ...@@ -324,6 +352,7 @@ let pg = {
if (!node) return; if (!node) return;
let component = node.getComponent(cc.Label);//组件功能 非node的功能 let component = node.getComponent(cc.Label);//组件功能 非node的功能
component.string = text; component.string = text;
// component.cacheMode = cc.Label.CacheMode.CHAR;
return true; return true;
}, },
//更变颜色 //更变颜色
...@@ -368,16 +397,38 @@ let pg = { ...@@ -368,16 +397,38 @@ let pg = {
skl.setAnimation(0, aniName, loop); skl.setAnimation(0, aniName, loop);
return skl; return skl;
}, },
addEventSpineAnimation(item, callback, target) {
if (!item || !cc.isValid(item)) return pg.logger.w("动画播放失败,传入了错误的item");
let node = item.node ? item.node : item;
if (!cc.isValid(node)) return pg.logger.w("节点已销毁");
let skl: sp.Skeleton = node.getComponent(sp.Skeleton);
if (!skl) return pg.logger.w("无动画实体");
// 添加循环完成的监听器
skl.setCompleteListener(function (trackEntry, loopCount) {
// 在此处编写循环完成时要执行的代码
callback.call(target);
});
return skl;
},
playDBAnimation(item, aniName, loop = -1) { playDBAnimation(item, aniName, loop = -1) {
if (!item || !cc.isValid(item)) return pg.logger.w("动画播放失败,传入了错误的item"); if (!item || !cc.isValid(item)) return pg.logger.w("动画播放失败,传入了错误的item");
if (!aniName) return pg.logger.w("动画播放失败,传入了错误的aniName"); if (!aniName) return pg.logger.w("动画播放失败,传入了错误的aniName");
let node = item.node ? item.node : item; let node = item.node ? item.node : item;
if (!cc.isValid(node)) return pg.logger.w("节点已销毁"); if (!cc.isValid(node)) return pg.logger.w("节点已销毁");
let skl: dragonBones.ArmatureDisplay = node.getComponent(dragonBones.ArmatureDisplay); let skl: dragonBones.ArmatureDisplay = node.getComponent(dragonBones.ArmatureDisplay);
if (!skl) return pg.logger.w("无动画实体");
skl.playAnimation(aniName, loop); skl.playAnimation(aniName, loop);
return skl; return skl;
}, },
addEventDBAnimation(item, callback, target) {
if (!item || !cc.isValid(item)) return pg.logger.w("动画播放失败,传入了错误的item");
let node = item.node ? item.node : item;
if (!cc.isValid(node)) return pg.logger.w("节点已销毁");
let skl: dragonBones.ArmatureDisplay = node.getComponent(dragonBones.ArmatureDisplay);
if (!skl) return pg.logger.w("无动画实体");
skl.on(dragonBones.EventObject.LOOP_COMPLETE, callback, target)
return skl;
},
clone(node: cc.Node) { clone(node: cc.Node) {
return cc.instantiate(node) return cc.instantiate(node)
} }
...@@ -518,10 +569,27 @@ let pg = { ...@@ -518,10 +569,27 @@ let pg = {
} }
}, },
audio: { audio: {
playAudioByUrlVolume(audio_url, volume = 1, cb = null, loadCb = null) {
return new Promise((resolve, reject) => {
if (!audio_url) return resolve(null);
cc.assetManager.loadRemote(audio_url, (err, audioClip: any) => {
cc.audioEngine.stopAll();
const audioId = cc.audioEngine.play(audioClip, false, volume);
cc.audioEngine.setFinishCallback(audioId, () => {
cb && cb();
resolve(audioId);
});
loadCb && loadCb(audioId);
});
});
},
playAudioByUrl(audio_url, cb = null, loadCb = null) { playAudioByUrl(audio_url, cb = null, loadCb = null) {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
if (!audio_url) return resolve(null); if (!audio_url) return resolve(null);
cc.assetManager.loadRemote(audio_url, (err, audioClip: any) => { cc.assetManager.loadRemote(audio_url, (err, audioClip: any) => {
cc.audioEngine.stopAll();
const audioId = cc.audioEngine.play(audioClip, false, 0.8); const audioId = cc.audioEngine.play(audioClip, false, 0.8);
cc.audioEngine.setFinishCallback(audioId, () => { cc.audioEngine.setFinishCallback(audioId, () => {
cb && cb(); cb && cb();
...@@ -544,6 +612,18 @@ let pg = { ...@@ -544,6 +612,18 @@ let pg = {
}); });
}); });
}, },
playLocalBgm(audio: cc.Node, volume = 0.7) {
return new Promise((resolve, reject) => {
const id = cc.audioEngine.playMusic(audio.getComponent(cc.AudioSource).clip, true);
cc.audioEngine.setVolume(id, volume);
cc.audioEngine.setFinishCallback(id, () => {
resolve(id);
});
})
},
stopLocalBgm() {
cc.audioEngine.stopMusic();
},
stopAudio(audioId) { stopAudio(audioId) {
if (!audioId) return; if (!audioId) return;
cc.audioEngine.stopEffect(audioId); cc.audioEngine.stopEffect(audioId);
...@@ -556,35 +636,27 @@ let pg = { ...@@ -556,35 +636,27 @@ let pg = {
cb && cb(); cb && cb();
}); });
} }
return audioId; return audioId;
} }
}, },
playLocalAudio(audio: cc.Node) { playLocalAudio(audio: cc.Node, volume = 1) {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
const id = cc.audioEngine.playEffect(audio.getComponent(cc.AudioSource).clip, false); const id = cc.audioEngine.playEffect(audio.getComponent(cc.AudioSource).clip, false);
cc.audioEngine.setVolume(id, volume);
cc.audioEngine.setFinishCallback(id, () => { cc.audioEngine.setFinishCallback(id, () => {
resolve(id); resolve(id);
}); });
}) })
}, },
getAudioClip(audio_url): Promise<cc.AudioClip> { getAudioClip(audio_url) {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
if (!audio_url) return resolve(null); if (!audio_url) return resolve(null);
cc.assetManager.loadRemote(audio_url, (err, audioClip: cc.AudioClip) => { cc.assetManager.loadRemote(audio_url, (err, audioClip: any) => {
if (audioClip.duration === undefined) {
const audioId = cc.audioEngine.play(audioClip, false, 0);
setTimeout(() => {
audioClip.duration = cc.audioEngine.getDuration(audioId)
cc.audioEngine.pause(audioId)
resolve(audioClip);
}, 50);
} else {
resolve(audioClip); resolve(audioClip);
}
}); });
}); });
} }
}, },
time: { time: {
delay(time) { delay(time) {
...@@ -606,8 +678,24 @@ let pg = { ...@@ -606,8 +678,24 @@ let pg = {
const id = cc.audioEngine.playEffect(audio.clip, loop); const id = cc.audioEngine.playEffect(audio.clip, loop);
resolve(id); resolve(id);
}) })
},
records: [],
addRecord(data) {
pg.hw.records.push(data)
},
getRecord() {
return pg.hw.records.concat();
}
},
signal: {
createSignal(): Signal {
let event = new Signal<any>();
return event;
} }
}, },
event: Emitter.getInstance() event: Emitter.getInstance()
}; };
export default pg; export default pg;
...@@ -4588,6 +4588,7 @@ declare namespace cc { ...@@ -4588,6 +4588,7 @@ declare namespace cc {
/** !#en Returns a normalized vector representing the forward direction (Z axis) of the node in world space. /** !#en Returns a normalized vector representing the forward direction (Z axis) of the node in world space.
!#zh 获取节点正前方(z 轴)面对的方向,返回值为世界坐标系下的归一化向量 */ !#zh 获取节点正前方(z 轴)面对的方向,返回值为世界坐标系下的归一化向量 */
forward: Vec3; forward: Vec3;
data: import("f:/work/cocos/huiwan/dg_book_voice/assets/dg04_dyxx/scene/tool/Game").Item;
/** /**
@param name name @param name name
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