Commit b719705e authored by Tt's avatar Tt

pg修改

parent 5eed1804
import { asyncDelay, onHomeworkFinish, RandomInt } from "../script/util";
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";
const { ccclass, property } = cc._decorator;
......@@ -10,24 +10,24 @@ export default class SceneComponent extends MyCocosSceneComponent {
addPreloadImage() {
// TODO 根据自己的配置预加载图片资源
let arr = [];
this.data.questions.forEach(question => {
arr.push({ url: question.bgItem.url })
question.hotZoneItemArr.forEach(zone => {
// this.data.questions.forEach(question => {
// arr.push({ url: question.bgItem.url })
// question.hotZoneItemArr.forEach(zone => {
})
})
this._imageResList.push(...arr);
// })
// })
// this._imageResList.push(...arr);
}
addPreloadAudio() {
// TODO 根据自己的配置预加载音频资源
let arr = [];
this.data.questions.forEach(question => {
question.hotZoneItemArr.forEach(zone => {
arr.push({ url: zone.audio_url })
})
})
this._audioResList.push(...arr);
// this.data.questions.forEach(question => {
// question.hotZoneItemArr.forEach(zone => {
// arr.push({ url: zone.audio_url })
// })
// })
// this._audioResList.push(...arr);
}
addPreloadAnima() {
......@@ -241,7 +241,6 @@ export default class SceneComponent extends MyCocosSceneComponent {
this.openDouble = false;
this.openSingle = false;
// this.onPlayAllAudio();
this.preLoadAudio();
}
onTouchBtnPre() {
this.onStopAllAudio();
......@@ -330,15 +329,6 @@ export default class SceneComponent extends MyCocosSceneComponent {
} else {
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 {
this.updateTips('开始连续播放音频');
this.openDouble = false;
......@@ -350,15 +340,6 @@ export default class SceneComponent extends MyCocosSceneComponent {
}
}
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() {
return new Promise(async (resolve, reject) => {
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 {
public page;
public audio;
......@@ -80,52 +38,6 @@ export class Item {
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 {
WAIT,
RUNNING,
......@@ -139,16 +51,12 @@ export default class Game {
}
private data: any;
public lists: Array<Item>
public player: Player;
public robot: Robot;
public state: GAME_STATE;
public total: number;
constructor() {
this.start = false;
this.lists = [];
this.player = new Player();
this.robot = new Robot();
this.state = GAME_STATE.WAIT;
}
get len() {
......@@ -158,14 +66,12 @@ export default class Game {
public isCanClick: boolean;
public failCount;
public dispelCount;
public aniEnter;
public startAudio: string;
public init(data) {
this.startAudio = data.startAudio || "";
this.start = false;
this.lists = [];
this.data = data.questions;
this.aniEnter = data.imgAni;
}
public start: boolean;
public page: number;
......@@ -173,8 +79,6 @@ export default class Game {
this.isCanClick = false;
this.failCount = 0;
this.dispelCount = 0;
this.player.reset();
this.robot.reset();
this.page = 0;
this.start = true;
this.lists = [];
......@@ -215,13 +119,8 @@ export default class Game {
hasNext() {
return this.page < this.lists.length - 1;
}
get getTotla() {
get getTotal() {
return this.lists.length;
// this.total = 0;
// this.lists.forEach((item) => {
// this.total += item.rNum;
// })
// return this.total;
}
get isOver() {
return this.page > this.lists.length;
......
......@@ -16,7 +16,8 @@ class Emitter {
clear() {
this._callbacks = {};
}
on(event, fn) {
on(event, fn: any) {
this.off(event);
if (!this._callbacks[event]) this._callbacks[event] = [];
this._callbacks[event].push(fn);
};
......@@ -28,7 +29,7 @@ class Emitter {
on.fn = fn;
this.on(event, on);
};
off(event, fn) {
off(event, fn?: any) {
// all
if (0 == arguments.length) {
this._callbacks = {};
......@@ -75,6 +76,32 @@ class Emitter {
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 {
private current: number;
......@@ -131,7 +158,7 @@ let pg = {
return node;
},
//添加节点
addChild(item, child, zIndex) {
addChild(item, child, zIndex = 0) {
if (!child) return console.log("addChild error ->请传入子节点");
if (!item) return console.log("addChild error ->请传入父节点");
let node = item.node ? item.node : item;
......@@ -250,6 +277,7 @@ let pg = {
}
if (!node) return pg.logger.w("图片更换失败,传入了错误的item");
if (node.net_url == res) return;
let nodeActive = node.active;
node.active = false;//
pg.load.loadNetImg(res).then((texture: cc.Texture2D) => {
if (!cc.isValid(node)) return pg.logger.i("节点已销毁");
......@@ -264,11 +292,11 @@ let pg = {
if (w && h && custom) {
let scaleX = w / nw;
let scaleY = h / nh;
// let scale = scaleX <= scaleY ? scaleX : scaleY;
node.width = nw * scaleX;
node.height = nh * scaleY;
let scale = scaleX <= scaleY ? scaleX : scaleY;
node.width = nw * scale;
node.height = nh * scale;
}
node.active = true;
node.active = nodeActive;
resolve({ w: nw, h: nh });
})
})
......@@ -324,6 +352,7 @@ let pg = {
if (!node) return;
let component = node.getComponent(cc.Label);//组件功能 非node的功能
component.string = text;
// component.cacheMode = cc.Label.CacheMode.CHAR;
return true;
},
//更变颜色
......@@ -368,16 +397,38 @@ let pg = {
skl.setAnimation(0, aniName, loop);
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) {
if (!item || !cc.isValid(item)) return pg.logger.w("动画播放失败,传入了错误的item");
if (!aniName) return pg.logger.w("动画播放失败,传入了错误的aniName");
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.playAnimation(aniName, loop);
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) {
return cc.instantiate(node)
}
......@@ -518,10 +569,27 @@ let pg = {
}
},
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) {
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, 0.8);
cc.audioEngine.setFinishCallback(audioId, () => {
cb && cb();
......@@ -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) {
if (!audioId) return;
cc.audioEngine.stopEffect(audioId);
......@@ -556,35 +636,27 @@ let pg = {
cb && cb();
});
}
return audioId;
}
},
playLocalAudio(audio: cc.Node) {
playLocalAudio(audio: cc.Node, volume = 1) {
return new Promise((resolve, reject) => {
const id = cc.audioEngine.playEffect(audio.getComponent(cc.AudioSource).clip, false);
cc.audioEngine.setVolume(id, volume);
cc.audioEngine.setFinishCallback(id, () => {
resolve(id);
});
})
},
getAudioClip(audio_url): Promise<cc.AudioClip> {
getAudioClip(audio_url) {
return new Promise((resolve, reject) => {
if (!audio_url) return resolve(null);
cc.assetManager.loadRemote(audio_url, (err, audioClip: cc.AudioClip) => {
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);
}
cc.assetManager.loadRemote(audio_url, (err, audioClip: any) => {
resolve(audioClip);
});
});
}
},
time: {
delay(time) {
......@@ -606,8 +678,24 @@ let pg = {
const id = cc.audioEngine.playEffect(audio.clip, loop);
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()
};
export default pg;
\ No newline at end of file
export default pg;
......@@ -4588,6 +4588,7 @@ declare namespace cc {
/** !#en Returns a normalized vector representing the forward direction (Z axis) of the node in world space.
!#zh 获取节点正前方(z 轴)面对的方向,返回值为世界坐标系下的归一化向量 */
forward: Vec3;
data: import("f:/work/cocos/huiwan/dg_book_voice/assets/dg04_dyxx/scene/tool/Game").Item;
/**
@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