This commit is contained in:
2024-05-01 19:13:01 +08:00
parent cf008327aa
commit 80a32d9b1b
150 changed files with 8561 additions and 5045 deletions

View File

@@ -1,50 +1,53 @@
import { _decorator, Component, instantiate, Label, Node, tween, Vec3 } from 'cc'
import PrefabLoader from '../utils/PrefabLoader'
import { Component, Label, Node, Vec3, _decorator, instantiate, tween } from 'cc'
import { GameConfig } from '../../game/config/GameConfig'
import PrefabLoader from '../utils/PrefabLoader'
import DialogBase from './DialogBase'
const { ccclass, property } = _decorator
@ccclass('CommonTips')
export default class CommonTips extends Component {
public static TipsZorderIndex: number = 999
@property({ type: Label })
txtContent: Label | null = null
private tips: string = ''
private static showingNameList: Array<string> = []
onLoad() {}
public static TipsZorderIndex: number = 999
@property({ type: Label })
txtContent: Label | null = null
start() {
tween(this.node)
.by(1.5, { position: new Vec3(0, 100, 0) })
.to(0.2, { /* opacity: 0*/ scale: Vec3.ZERO })
.call(() => {
this.node.destroy()
})
.start()
}
private tips: string = ''
private static showingNameList: Array<string> = []
onDestroy() {
let index: number = CommonTips.showingNameList.indexOf(this.tips)
CommonTips.showingNameList.splice(index, 1)
this.unscheduleAllCallbacks()
}
onLoad() {}
public static showMsg(msg: string, parentNode: Node = null) {
PrefabLoader.loadPrefab(
`${GameConfig.GameName}/share/uicomponent/CommonTips`,
(loadedResource) => {
parentNode = parentNode || DialogBase.GetRootCanvas()
if (CommonTips.showingNameList.indexOf(msg) === -1) {
CommonTips.showingNameList.push(msg)
}
let dialogNode = instantiate(loadedResource)
dialogNode.setPosition(0, 0)
let dialogScript: CommonTips = dialogNode.getComponent(CommonTips)
dialogScript.tips = msg
dialogScript.txtContent.string = msg
parentNode.insertChild(dialogNode, CommonTips.TipsZorderIndex)
}
)
}
start() {
tween(this.node)
.by(1.5, { position: new Vec3(0, 100, 0) })
.to(0.2, { /* opacity: 0 */ scale: Vec3.ZERO })
.call(() => {
this.node.destroy()
})
.start()
}
onDestroy() {
const index: number = CommonTips.showingNameList.indexOf(this.tips)
CommonTips.showingNameList.splice(index, 1)
this.unscheduleAllCallbacks()
}
public static showMsg(msg: string, parentNode: Node = null) {
PrefabLoader.loadPrefab(
`${GameConfig.GameName}/share/uicomponent/CommonTips`,
(loadedResource) => {
parentNode = parentNode || DialogBase.GetRootCanvas()
if (!CommonTips.showingNameList.includes(msg)) CommonTips.showingNameList.push(msg)
const dialogNode = instantiate(loadedResource)
dialogNode.setPosition(0, 0)
const dialogScript: CommonTips = dialogNode.getComponent(CommonTips)
dialogScript.tips = msg
dialogScript.txtContent.string = msg
parentNode.insertChild(dialogNode, CommonTips.TipsZorderIndex)
},
)
}
}

View File

@@ -1,30 +1,36 @@
import { _decorator, Component, instantiate, Node, Prefab, Widget } from 'cc'
import PrefabLoader from '../utils/PrefabLoader'
import { Component, Node, Prefab, Widget, _decorator, instantiate } from 'cc'
import { GameConfig } from '../../game/config/GameConfig'
import PrefabLoader from '../utils/PrefabLoader'
import DialogBase from './DialogBase'
const { ccclass, property } = _decorator
@ccclass('DarkLayer')
export default class DarkLayer extends Component {
private static prefab: Prefab
onLoad() {
this.getComponent(Widget).target = DialogBase.GetRootCanvas()
}
start() {}
public static preLoad(): Promise<void> {
return new Promise((resolve, reject) => {
PrefabLoader.loadPrefab(
`${GameConfig.GameName}/share/uicomponent/DarkLayer`,
(loadedResource) => {
DarkLayer.prefab = loadedResource
resolve()
}
)
})
}
public static getDarkLayer() {
let dialogNode: Node = instantiate(DarkLayer.prefab)
return dialogNode
}
private static prefab: Prefab
onLoad() {
this.getComponent(Widget).target = DialogBase.GetRootCanvas()
}
start() {}
public static preLoad(): Promise<void> {
return new Promise((resolve, reject) => {
PrefabLoader.loadPrefab(
`${GameConfig.GameName}/share/uicomponent/DarkLayer`,
(loadedResource) => {
DarkLayer.prefab = loadedResource
resolve()
},
)
})
}
public static getDarkLayer() {
const dialogNode: Node = instantiate(DarkLayer.prefab)
return dialogNode
}
}

View File

@@ -1,65 +1,65 @@
import { _decorator, Component, Node, Widget } from 'cc'
import DarkLayer from './DarkLayer'
import { Component, Node, Widget, _decorator } from 'cc'
import { UIRoot } from '../../game/utils/UIRoot'
import DarkLayer from './DarkLayer'
const { ccclass } = _decorator
@ccclass('DialogBase')
export default class DialogBase extends Component {
private static LocalZOrder: number = 5
private darkLayer: Node | null = null
private static LocalZOrder: number = 5
private darkLayer: Node | null = null
//private static _canvas: Node;
public static GetRootCanvas(): Node {
//if(DialogBase._canvas == null)
// DialogBase._canvas = director.getScene().getChildByName('Canvas');
//return DialogBase._canvas;
return UIRoot.Instance.node
}
onLoad() {
DialogBase.LocalZOrder += 1
let closeLayer: Node = this.node.getChildByName('closeLayer')
if (closeLayer) {
let closeLayerWidget: Widget = closeLayer.getComponent(Widget)
if (closeLayerWidget) {
closeLayerWidget.target = DialogBase.GetRootCanvas()
closeLayerWidget.left = 0
closeLayerWidget.right = 0
closeLayerWidget.top = 0
closeLayerWidget.bottom = 0
}
}
this.onLoadMe()
}
// private static _canvas: Node;
public static GetRootCanvas(): Node {
// if(DialogBase._canvas == null)
// DialogBase._canvas = director.getScene().getChildByName('Canvas');
// return DialogBase._canvas;
return UIRoot.Instance.node
}
onLoadMe() {}
onLoad() {
DialogBase.LocalZOrder += 1
const closeLayer: Node = this.node.getChildByName('closeLayer')
if (closeLayer) {
const closeLayerWidget: Widget = closeLayer.getComponent(Widget)
if (closeLayerWidget) {
closeLayerWidget.target = DialogBase.GetRootCanvas()
closeLayerWidget.left = 0
closeLayerWidget.right = 0
closeLayerWidget.top = 0
closeLayerWidget.bottom = 0
}
}
this.onLoadMe()
}
start(isPlayMv: boolean = false) {
this.darkLayer = DarkLayer.getDarkLayer()
this.node.insertChild(this.darkLayer, 0) //this.node.addChild(this.darkLayer, -1);
if (isPlayMv) {
this.node.setScale(0, 0)
} else {
this.onStartMe()
}
}
onLoadMe() {}
onStartMe() {}
start(isPlayMv: boolean = false) {
this.darkLayer = DarkLayer.getDarkLayer()
this.node.insertChild(this.darkLayer, 0) // this.node.addChild(this.darkLayer, -1);
if (isPlayMv) this.node.setScale(0, 0)
else this.onStartMe()
}
onClickClose() {
this.node.destroy()
}
onStartMe() {}
update(dt) {
this.onUpdateMe(dt)
}
onClickClose() {
this.node.destroy()
}
onUpdateMe(dt) {}
update(dt) {
this.onUpdateMe(dt)
}
onDestroy() {
DialogBase.LocalZOrder -= 1
this.onDestroyMe()
}
onUpdateMe(dt) {}
onDestroyMe() {}
onDestroy() {
DialogBase.LocalZOrder -= 1
this.onDestroyMe()
}
onDestroyMe() {}
}

View File

@@ -1,78 +1,76 @@
import { _decorator, Component, instantiate, Node, Prefab, Quat, Vec3 } from 'cc'
import PrefabLoader from '../utils/PrefabLoader'
import { Component, Node, Prefab, Quat, Vec3, _decorator, instantiate } from 'cc'
import { GameConfig } from '../../game/config/GameConfig'
import PrefabLoader from '../utils/PrefabLoader'
import DialogBase from './DialogBase'
const { ccclass, property } = _decorator
@ccclass('LoadingPrefab')
export default class LoadingPrefab extends Component {
public static instance: Node
private static prefab: Prefab
public static LoadingZorderIndex: number = 99
@property({ type: Node })
loadingSp: Node | null = null
public static instance: Node
private static prefab: Prefab
public static LoadingZorderIndex: number = 99
@property({ type: Node })
loadingSp: Node | null = null
private _quatCache: Quat
private _vec3Cache: Vec3
onLoad() {
this._quatCache = new Quat()
this._vec3Cache = new Vec3()
}
private _quatCache: Quat
private _vec3Cache: Vec3
start() {}
onLoad() {
this._quatCache = new Quat()
this._vec3Cache = new Vec3()
}
public static close() {
if (!LoadingPrefab.instance) {
return
}
LoadingPrefab.instance.removeFromParent()
LoadingPrefab.instance.destroy()
LoadingPrefab.instance = null
}
start() {}
public static preLoad(): Promise<void> {
return new Promise((resolve, reject) => {
PrefabLoader.loadPrefab(
GameConfig.GameName + '/' + 'share/uicomponent/LoadingPrefab',
(loadedResource) => {
LoadingPrefab.prefab = loadedResource
resolve()
}
)
})
}
public static close() {
if (!LoadingPrefab.instance) return
private static createLoadingPrefab(parentNode: Node = null) {
let dialogNode: Node = instantiate(LoadingPrefab.prefab)
LoadingPrefab.instance = dialogNode
if (!parentNode) {
parentNode = DialogBase.GetRootCanvas()
}
parentNode.insertChild(dialogNode, LoadingPrefab.LoadingZorderIndex)
dialogNode.setPosition(0, 0)
}
LoadingPrefab.instance.removeFromParent()
LoadingPrefab.instance.destroy()
LoadingPrefab.instance = null
}
public static async show(parentNode: Node = null) {
if (LoadingPrefab.instance) return
if (!LoadingPrefab.prefab) {
await LoadingPrefab.preLoad()
}
this.createLoadingPrefab(parentNode)
}
public static preLoad(): Promise<void> {
return new Promise((resolve, reject) => {
PrefabLoader.loadPrefab(
`${GameConfig.GameName}/share/uicomponent/LoadingPrefab`,
(loadedResource) => {
LoadingPrefab.prefab = loadedResource
resolve()
},
)
})
}
update(dt) {
this.loadingSp.getRotation(this._quatCache)
Quat.toEuler(this._vec3Cache, this._quatCache)
this._vec3Cache.z += 10
this.loadingSp.setRotationFromEuler(this._vec3Cache)
if (this._vec3Cache.z >= 360) {
this.loadingSp.getRotation(Quat.IDENTITY)
}
}
private static createLoadingPrefab(parentNode: Node = null) {
const dialogNode: Node = instantiate(LoadingPrefab.prefab)
LoadingPrefab.instance = dialogNode
if (!parentNode) parentNode = DialogBase.GetRootCanvas()
public static clear() {
LoadingPrefab.instance = null
LoadingPrefab.prefab = null
}
parentNode.insertChild(dialogNode, LoadingPrefab.LoadingZorderIndex)
dialogNode.setPosition(0, 0)
}
public static async show(parentNode: Node = null) {
if (LoadingPrefab.instance) return
if (!LoadingPrefab.prefab) await LoadingPrefab.preLoad()
this.createLoadingPrefab(parentNode)
}
update(dt) {
this.loadingSp.getRotation(this._quatCache)
Quat.toEuler(this._vec3Cache, this._quatCache)
this._vec3Cache.z += 10
this.loadingSp.setRotationFromEuler(this._vec3Cache)
if (this._vec3Cache.z >= 360) this.loadingSp.getRotation(Quat.IDENTITY)
}
public static clear() {
LoadingPrefab.instance = null
LoadingPrefab.prefab = null
}
}

View File

@@ -1,90 +1,76 @@
import { _decorator, Component, instantiate, Node, Prefab } from 'cc'
import PrefabLoader from '../utils/PrefabLoader'
import Progress from './Progress'
import { Component, Node, Prefab, _decorator, instantiate } from 'cc'
import { GameConfig } from '../../game/config/GameConfig'
import PrefabLoader from '../utils/PrefabLoader'
import DialogBase from './DialogBase'
import Progress from './Progress'
const { ccclass, property } = _decorator
@ccclass('LoadingScenePrefab')
export default class LoadingScenePrefab extends Component {
public static instance: Node
private static prefab: Prefab
public static LoadingZorderIndex: number = 99
@property({ type: Node })
private progressNode: Node | null = null
onLoad() {}
public static instance: Node
private static prefab: Prefab
public static LoadingZorderIndex: number = 99
@property({ type: Node })
private progressNode: Node | null = null
start() {}
onLoad() {}
public updateProgress(
completedCount: number,
totalCount: number,
item: any = null
) {
this.progressNode
.getComponent(Progress)
.updateProgress(
completedCount,
totalCount,
'消耗流量,预下载所有"鱼"类中,请耐心等待...'
)
}
start() {}
public static updateLoading(
completedCount: number,
totalCount: number,
item: any = null
) {
if (LoadingScenePrefab.instance) {
let nodeTs: LoadingScenePrefab =
LoadingScenePrefab.instance.getComponent(LoadingScenePrefab)
if (nodeTs) {
nodeTs.updateProgress(completedCount, totalCount, item)
}
}
}
public updateProgress(completedCount: number, totalCount: number, item: any = null) {
this.progressNode
.getComponent(Progress)
.updateProgress(completedCount, totalCount, '消耗流量,预下载所有"鱼"类中,请耐心等待...')
}
private static createPrefab(parentNode: Node = null) {
let dialogNode: Node = instantiate(LoadingScenePrefab.prefab)
LoadingScenePrefab.instance = dialogNode
if (!parentNode) {
parentNode = DialogBase.GetRootCanvas()
}
parentNode.insertChild(dialogNode, LoadingScenePrefab.LoadingZorderIndex)
dialogNode.setPosition(0, 0)
}
public static updateLoading(completedCount: number, totalCount: number, item: any = null) {
if (LoadingScenePrefab.instance) {
const nodeTs: LoadingScenePrefab =
LoadingScenePrefab.instance.getComponent(LoadingScenePrefab)
if (nodeTs) nodeTs.updateProgress(completedCount, totalCount, item)
}
}
public static preLoad(): Promise<void> {
return new Promise((resolve, reject) => {
PrefabLoader.loadPrefab(
GameConfig.GameName + '/' + 'share/uicomponent/LoadingScenePrefab',
(loadedResource: Prefab) => {
LoadingScenePrefab.prefab = loadedResource
resolve()
}
)
})
}
private static createPrefab(parentNode: Node = null) {
const dialogNode: Node = instantiate(LoadingScenePrefab.prefab)
LoadingScenePrefab.instance = dialogNode
if (!parentNode) parentNode = DialogBase.GetRootCanvas()
public static close() {
if (!LoadingScenePrefab.instance) {
return
}
LoadingScenePrefab.instance.destroy()
LoadingScenePrefab.instance = null
}
parentNode.insertChild(dialogNode, LoadingScenePrefab.LoadingZorderIndex)
dialogNode.setPosition(0, 0)
}
public static async show(parentNode: Node = null) {
if (LoadingScenePrefab.instance) return
if (!LoadingScenePrefab.prefab) {
await LoadingScenePrefab.preLoad()
}
this.createPrefab(parentNode)
}
public static preLoad(): Promise<void> {
return new Promise((resolve, reject) => {
PrefabLoader.loadPrefab(
`${GameConfig.GameName}/share/uicomponent/LoadingScenePrefab`,
(loadedResource: Prefab) => {
LoadingScenePrefab.prefab = loadedResource
resolve()
},
)
})
}
public static clear() {
LoadingScenePrefab.instance = null
LoadingScenePrefab.prefab = null
}
public static close() {
if (!LoadingScenePrefab.instance) return
LoadingScenePrefab.instance.destroy()
LoadingScenePrefab.instance = null
}
public static async show(parentNode: Node = null) {
if (LoadingScenePrefab.instance) return
if (!LoadingScenePrefab.prefab) await LoadingScenePrefab.preLoad()
this.createPrefab(parentNode)
}
public static clear() {
LoadingScenePrefab.instance = null
LoadingScenePrefab.prefab = null
}
}

View File

@@ -1,9 +1,18 @@
import { _decorator, AssetManager, AudioClip, AudioSource, Component, instantiate, Prefab } from 'cc'
import {
AssetManager,
AudioClip,
AudioSource,
Component,
Prefab,
_decorator,
instantiate,
} from 'cc'
import { GameConfig } from '../../game/config/GameConfig'
import MusicConfig from '../config/MusicConfig'
import LocalStorage from '../utils/LocalStorage'
import { Logger } from '../utils/Logger'
import PrefabLoader from '../utils/PrefabLoader'
import LocalStorage from '../utils/LocalStorage'
import MusicConfig from '../config/MusicConfig'
import { GameConfig } from '../../game/config/GameConfig'
const { ccclass, property } = _decorator
@@ -13,71 +22,59 @@ const { ccclass, property } = _decorator
@ccclass('MusicPrefab')
export default class MusicPrefab extends Component {
private static instance: MusicPrefab
private static MUSIC_VOLUMN_KEY: string = 'musicVolumn'
public static musicVolumn: number = 1
public static play(key: string) {
let url: string = MusicConfig.musicKey2Path.get(key)
if (url) {
AssetManager.instance.resources.load(
url,
AudioClip,
(error: Error, clip: AudioClip) => {
if (error) {
Logger.warn(this, 'load music error===', error.message)
} else {
if (clip) {
this.instance.node.getComponent(AudioSource).clip = clip
this.instance.node.getComponent(AudioSource).volume =
this.musicVolumn
this.instance.node.getComponent(AudioSource).play()
this.instance.node.getComponent(AudioSource).loop = true
}
}
}
)
} else {
Logger.warn(this, '播放不存在的music=', key)
}
}
private static instance: MusicPrefab
private static MUSIC_VOLUMN_KEY: string = 'musicVolumn'
public static musicVolumn: number = 1
public static changeVolumn(nowVolumn: number) {
this.musicVolumn = nowVolumn
this.instance.node.getComponent(AudioSource).volume = nowVolumn
LocalStorage.setItem(
MusicPrefab.MUSIC_VOLUMN_KEY,
this.musicVolumn.toString()
)
}
public static play(key: string) {
const url: string = MusicConfig.musicKey2Path.get(key)
if (url) {
AssetManager.instance.resources.load(url, AudioClip, (error: Error, clip: AudioClip) => {
if (error) {
Logger.warn(this, 'load music error===', error.message)
} else {
if (clip) {
this.instance.node.getComponent(AudioSource).clip = clip
this.instance.node.getComponent(AudioSource).volume = this.musicVolumn
this.instance.node.getComponent(AudioSource).play()
this.instance.node.getComponent(AudioSource).loop = true
}
}
})
} else {
Logger.warn(this, '播放不存在的music=', key)
}
}
private static preInit() {
this.musicVolumn = parseFloat(
LocalStorage.getItem(MusicPrefab.MUSIC_VOLUMN_KEY)
)
if (isNaN(this.musicVolumn)) {
this.musicVolumn = 1
}
}
public static changeVolumn(nowVolumn: number) {
this.musicVolumn = nowVolumn
this.instance.node.getComponent(AudioSource).volume = nowVolumn
LocalStorage.setItem(MusicPrefab.MUSIC_VOLUMN_KEY, this.musicVolumn.toString())
}
public static preLoad(): Promise<void> {
return new Promise((resolve, reject) => {
PrefabLoader.loadPrefab(
GameConfig.GameName + '/' + 'share/uicomponent/MusicPrefab',
(loadedResource: Prefab) => {
MusicPrefab.instance =
instantiate(loadedResource).getComponent(MusicPrefab)
this.preInit()
resolve()
}
)
})
}
private static preInit() {
this.musicVolumn = Number.parseFloat(LocalStorage.getItem(MusicPrefab.MUSIC_VOLUMN_KEY))
if (Number.isNaN(this.musicVolumn)) this.musicVolumn = 1
}
public static destory() {
if (MusicPrefab.instance) {
MusicPrefab.instance.getComponent(AudioSource).stop()
MusicPrefab.instance.destroy()
}
MusicPrefab.instance = null
}
public static preLoad(): Promise<void> {
return new Promise((resolve, reject) => {
PrefabLoader.loadPrefab(
`${GameConfig.GameName}/share/uicomponent/MusicPrefab`,
(loadedResource: Prefab) => {
MusicPrefab.instance = instantiate(loadedResource).getComponent(MusicPrefab)
this.preInit()
resolve()
},
)
})
}
public static destory() {
if (MusicPrefab.instance) {
MusicPrefab.instance.getComponent(AudioSource).stop()
MusicPrefab.instance.destroy()
}
MusicPrefab.instance = null
}
}

View File

@@ -1,56 +1,55 @@
import { _decorator, Component, Label, ProgressBar } from 'cc'
import { Component, Label, ProgressBar, _decorator } from 'cc'
const { ccclass, property } = _decorator
@ccclass('Progress')
export default class Progress extends Component {
@property(Label)
public percentLable: Label
@property(Label)
public percentLable: Label
@property(ProgressBar)
public bar: ProgressBar
@property(ProgressBar)
public bar: ProgressBar
onLoad() {
this.bar.node.active = false
this.bar.progress = 0
}
onLoad() {
this.bar.node.active = false
this.bar.progress = 0
}
start() {}
start() {}
updatePercent(current, filePercent) {
//this.percentLable.string = filePercent.toFixed(2);
}
updatePercent(current, filePercent) {
// this.percentLable.string = filePercent.toFixed(2);
}
updatefileTotal(current, filePercent) {
if (!this.bar.node.active) this.bar.node.active = true
var nowPercent = Math.round((current / filePercent) * 100)
var curMB = this.getMB(current)
var totalMB = this.getMB(filePercent)
// this.percentLable.string = "正在更新 " + nowPercent + "%" + " ( " + curMB + " / "+totalMB +" MB)";
nowPercent = Math.min(nowPercent, 100)
this.percentLable.string = '正在更新 ' + nowPercent + '%'
var percent = current / filePercent
this.bar.progress = percent
}
updatefileTotal(current: number, filePercent: number) {
if (!this.bar.node.active) this.bar.node.active = true
let nowPercent = Math.round((current / filePercent) * 100)
const curMB = this.getMB(current)
const totalMB = this.getMB(filePercent)
// this.percentLable.string = "正在更新 " + nowPercent + "%" + " ( " + curMB + " / "+totalMB +" MB)";
nowPercent = Math.min(nowPercent, 100)
this.percentLable.string = `正在更新 ${nowPercent}%`
this.bar.progress = current / filePercent
}
public updateProgress(
current,
total,
msg: string = '正在加载资源,此过程不消耗流量...'
) {
this.bar.node.active = true
// this.setMsg(msg+ current + "/" + total);
this.setMsg(msg)
this.bar.progress = current / total
}
public updateProgress(
current: number,
total: number,
msg: string = '正在加载资源,此过程不消耗流量...',
) {
this.bar.node.active = true
// this.setMsg(msg+ current + "/" + total);
this.setMsg(msg)
this.bar.progress = current / total
}
getMB(bytes) {
bytes /= 1024
bytes /= 1024
return bytes.toFixed(2)
}
getMB(bytes: number) {
bytes /= 1024
bytes /= 1024
return bytes.toFixed(2)
}
public setMsg(msg: string = '游戏加载中,请稍后...') {
this.percentLable.string = msg
}
public setMsg(msg: string = '游戏加载中,请稍后...') {
this.percentLable.string = msg
}
}

View File

@@ -1,11 +1,22 @@
import { _decorator, AssetManager, AudioClip, AudioSource, Component, instantiate, Node, NodePool, Prefab } from 'cc'
import { Logger } from '../utils/Logger'
import PrefabLoader from '../utils/PrefabLoader'
import LocalStorage from '../utils/LocalStorage'
import EventManager from '../utils/EventManager'
import {
AssetManager,
AudioClip,
AudioSource,
Component,
Node,
NodePool,
Prefab,
_decorator,
instantiate,
} from 'cc'
import { GameConfig } from '../../game/config/GameConfig'
import CommonEvent from '../config/CommonEvent'
import MusicConfig from '../config/MusicConfig'
import { GameConfig } from '../../game/config/GameConfig'
import EventManager from '../utils/EventManager'
import LocalStorage from '../utils/LocalStorage'
import { Logger } from '../utils/Logger'
import PrefabLoader from '../utils/PrefabLoader'
const { ccclass, property } = _decorator
@@ -16,120 +27,102 @@ const { ccclass, property } = _decorator
@ccclass('SoundPrefab')
export default class SoundPrefab extends Component {
private static prefab: Prefab | null = null
private static SOUND_VOLUMN_KEY: string = 'soundVolumn'
public static soundVolumn: number = 1
private static Pool_Init_Num: number = 30
private static pool: NodePool = new NodePool()
private static nowAudioNodeList: Array<Node> = []
private audioName: string = ''
private audioUrl: string = ''
private static getAudioNode() {
let node: Node = null
// if (this.pool.size() > 0) {
private static prefab: Prefab | null = null
private static SOUND_VOLUMN_KEY: string = 'soundVolumn'
public static soundVolumn: number = 1
private static Pool_Init_Num: number = 30
private static pool: NodePool = new NodePool()
private static nowAudioNodeList: Array<Node> = []
private audioName: string = ''
private audioUrl: string = ''
// node = this.pool.get();
// } else {
node = instantiate(this.prefab)
// }
return node
}
private static getAudioNode() {
let node: Node = null
// if (this.pool.size() > 0) {
public static play(key: string) {
let url: string = MusicConfig.musicKey2Path.get(key)
if (url) {
AssetManager.instance.resources.load(url, AudioClip, (error: Error, clip: AudioClip) => {
if (error) {
Logger.warn(this, 'load sound error===', error.message)
} else {
if (clip) {
let audioNode: Node = this.getAudioNode()
if (audioNode) {
audioNode.getComponent(AudioSource).clip = clip
audioNode.getComponent(AudioSource).volume =
SoundPrefab.soundVolumn
audioNode.getComponent(AudioSource).loop = false
audioNode.getComponent(AudioSource).currentTime = 0 //rewind();
audioNode.getComponent(AudioSource).play()
audioNode.getComponent(SoundPrefab).audioName = key
audioNode.getComponent(SoundPrefab).audioUrl = url
this.nowAudioNodeList.push(audioNode)
}
}
}
})
} else {
Logger.warn(this, '播放不存在的music=', key)
}
}
// node = this.pool.get();
// } else {
node = instantiate(this.prefab)
// }
return node
}
public static changeVolumn(nowVolumn: number) {
this.soundVolumn = nowVolumn
for (let i = 0; i < this.nowAudioNodeList.length; i++) {
let audioNode: Node = this.nowAudioNodeList[i]
let audioSource: AudioSource = audioNode.getComponent(AudioSource)
if (audioSource.playing) {
audioSource.volume = nowVolumn
}
}
LocalStorage.setItem(
SoundPrefab.SOUND_VOLUMN_KEY,
SoundPrefab.soundVolumn.toString()
)
}
public static play(key: string) {
const url: string = MusicConfig.musicKey2Path.get(key)
if (url) {
AssetManager.instance.resources.load(url, AudioClip, (error: Error, clip: AudioClip) => {
if (error) {
Logger.warn(this, 'load sound error===', error.message)
} else {
if (clip) {
const audioNode: Node = this.getAudioNode()
if (audioNode) {
audioNode.getComponent(AudioSource).clip = clip
audioNode.getComponent(AudioSource).volume = SoundPrefab.soundVolumn
audioNode.getComponent(AudioSource).loop = false
audioNode.getComponent(AudioSource).currentTime = 0 // rewind();
audioNode.getComponent(AudioSource).play()
audioNode.getComponent(SoundPrefab).audioName = key
audioNode.getComponent(SoundPrefab).audioUrl = url
this.nowAudioNodeList.push(audioNode)
}
}
}
})
} else {
Logger.warn(this, '播放不存在的music=', key)
}
}
private static preInit() {
EventManager.instance.addListener(
CommonEvent.Event_FrameUpdate,
this.updateFrame,
this
)
SoundPrefab.soundVolumn = parseFloat(
LocalStorage.getItem(SoundPrefab.SOUND_VOLUMN_KEY)
)
if (isNaN(SoundPrefab.soundVolumn)) {
SoundPrefab.soundVolumn = 1
}
}
public static changeVolumn(nowVolumn: number) {
this.soundVolumn = nowVolumn
for (let i = 0; i < this.nowAudioNodeList.length; i++) {
const audioNode: Node = this.nowAudioNodeList[i]
const audioSource: AudioSource = audioNode.getComponent(AudioSource)
if (audioSource.playing) audioSource.volume = nowVolumn
}
LocalStorage.setItem(SoundPrefab.SOUND_VOLUMN_KEY, SoundPrefab.soundVolumn.toString())
}
private static updateFrame() {
for (let i = 0; i < this.nowAudioNodeList.length; i++) {
let audioNode: Node = this.nowAudioNodeList[i]
let audioSource: AudioSource = audioNode.getComponent(AudioSource)
if (!audioSource.playing) {
SoundPrefab.nowAudioNodeList.splice(i, 1)
}
}
}
private static preInit() {
EventManager.instance.addListener(CommonEvent.Event_FrameUpdate, this.updateFrame, this)
SoundPrefab.soundVolumn = Number.parseFloat(LocalStorage.getItem(SoundPrefab.SOUND_VOLUMN_KEY))
if (Number.isNaN(SoundPrefab.soundVolumn)) SoundPrefab.soundVolumn = 1
}
public static preLoad(): Promise<void> {
return new Promise((resolve, reject) => {
PrefabLoader.loadPrefab(
GameConfig.GameName + '/' + 'share/uicomponent/SoundPrefab',
(loadedResource: Prefab) => {
SoundPrefab.prefab = loadedResource
this.preInit()
// for (let i = 0; i < this.Pool_Init_Num; i++) {
// let tempNode: cc.Node = cc.instantiate(loadedResource);
// this.pool.put(tempNode);
// }
resolve()
}
)
})
}
private static updateFrame() {
for (let i = 0; i < this.nowAudioNodeList.length; i++) {
const audioNode: Node = this.nowAudioNodeList[i]
const audioSource: AudioSource = audioNode.getComponent(AudioSource)
if (!audioSource.playing) SoundPrefab.nowAudioNodeList.splice(i, 1)
}
}
public static destory() {
EventManager.instance.removeListener(
CommonEvent.Event_FrameUpdate,
this.updateFrame
)
for (let i = 0; i < this.nowAudioNodeList.length; i++) {
let audioNode: Node = this.nowAudioNodeList[i]
audioNode.getComponent(AudioSource).stop()
audioNode.getComponent(AudioSource).destroy()
}
this.nowAudioNodeList = []
this.pool.clear()
}
public static preLoad(): Promise<void> {
return new Promise((resolve, reject) => {
PrefabLoader.loadPrefab(
`${GameConfig.GameName}/share/uicomponent/SoundPrefab`,
(loadedResource: Prefab) => {
SoundPrefab.prefab = loadedResource
this.preInit()
// for (let i = 0; i < this.Pool_Init_Num; i++) {
// let tempNode: cc.Node = cc.instantiate(loadedResource);
// this.pool.put(tempNode);
// }
resolve()
},
)
})
}
public static destory() {
EventManager.instance.removeListener(CommonEvent.Event_FrameUpdate, this.updateFrame)
for (let i = 0; i < this.nowAudioNodeList.length; i++) {
const audioNode: Node = this.nowAudioNodeList[i]
audioNode.getComponent(AudioSource).stop()
audioNode.getComponent(AudioSource).destroy()
}
this.nowAudioNodeList = []
this.pool.clear()
}
}

View File

@@ -1,21 +1,25 @@
import { _decorator, Component, SpriteFrame } from 'cc'
import { Component, SpriteFrame, _decorator } from 'cc'
const { ccclass, property } = _decorator
@ccclass('TextureMgr')
export default class TextureMgr extends Component {
@property({ type: [SpriteFrame] })
public Spriteset: SpriteFrame[] = []
@property({ type: [SpriteFrame] })
public Spriteset1: SpriteFrame[] = []
@property({ type: [SpriteFrame] })
public Spriteset2: SpriteFrame[] = []
@property({ type: [SpriteFrame] })
public Spriteset3: SpriteFrame[] = []
@property({ type: [SpriteFrame] })
public Spriteset4: SpriteFrame[] = []
@property({ type: [SpriteFrame] })
public Spriteset: SpriteFrame[] = []
onLoad() {
// // init logic
}
@property({ type: [SpriteFrame] })
public Spriteset1: SpriteFrame[] = []
@property({ type: [SpriteFrame] })
public Spriteset2: SpriteFrame[] = []
@property({ type: [SpriteFrame] })
public Spriteset3: SpriteFrame[] = []
@property({ type: [SpriteFrame] })
public Spriteset4: SpriteFrame[] = []
onLoad() {
// init logic
}
}