init
This commit is contained in:
@@ -0,0 +1,80 @@
|
||||
import { _decorator, Component, js, JsonAsset, Node, resources, TypeScript } from 'cc';
|
||||
import { SingletonBase } from '../SingletonBase';
|
||||
import { setClassName } from '../../data/ConfigData';
|
||||
|
||||
const { ccclass, property } = _decorator;
|
||||
|
||||
@ccclass('DataMgr')
|
||||
export class DataMgr extends SingletonBase<DataMgr> {
|
||||
|
||||
protected start(): void {
|
||||
setClassName()
|
||||
this.initAllConfig()
|
||||
}
|
||||
|
||||
datas: Map<string, Array<any>> = new Map();
|
||||
|
||||
|
||||
createInstance(className: string): any {
|
||||
const Constructor = (window as any).ConfigData[className];
|
||||
|
||||
if (typeof Constructor === 'function') {
|
||||
return new Constructor();
|
||||
} else {
|
||||
console.error(`Class ${className} is not a constructor`);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
initAllConfig() {
|
||||
resources.loadDir(`data`, null, null, (err, assets) => {
|
||||
for (let i = 0; i < assets.length; i++) {
|
||||
let jsonAsset = assets[i] as JsonAsset
|
||||
let className = jsonAsset.name
|
||||
let array = new Array<any>()
|
||||
// this.datas.set((window as any)[className].name, array)
|
||||
console.log("是否有类", (window as any).ConfigData, className, (window as any).ConfigData[className])
|
||||
this.datas.set(js.getClassName((window as any).ConfigData[className]), array)
|
||||
|
||||
console.log("设置data", className, js.getClassName((window as any).ConfigData[className]))
|
||||
|
||||
for (let j = 0; j < jsonAsset.json.length; j++) {
|
||||
let myClassInstance = new (window as any).ConfigData[className]();
|
||||
let targetData: Record<string, any> = jsonAsset.json[j]
|
||||
this.initDataItem(myClassInstance, targetData)
|
||||
array.push(myClassInstance)
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public GetDataById<T>(dataClass: new () => T, id: number): T {
|
||||
let name = js.getClassName(dataClass)
|
||||
console.log("GetDataById", name)
|
||||
let datas = this.datas.get(name) as Array<T>
|
||||
for (let i = 0; i < datas.length; i++) {
|
||||
if (datas[i].id == id) {
|
||||
return datas[i]
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
initDataItem(obj, record: Record<string, any>) {
|
||||
for (const key in record) {
|
||||
if (record.hasOwnProperty(key) && obj.hasOwnProperty(key)) {
|
||||
obj[key] = record[key];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public GetDatas<T>(dataClass: new () => T): Array<T> {
|
||||
let name = js.getClassName(dataClass)
|
||||
console.log(this.datas.get(name))
|
||||
console.log(dataClass)
|
||||
console.log(name)
|
||||
console.log(this.datas)
|
||||
return this.datas.get(name) as Array<T>
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
{
|
||||
"ver": "4.0.24",
|
||||
"importer": "typescript",
|
||||
"imported": true,
|
||||
"uuid": "70a16c4c-5e41-460a-87b0-4b4090db6c3c",
|
||||
"files": [],
|
||||
"subMetas": {},
|
||||
"userData": {}
|
||||
}
|
||||
@@ -0,0 +1,199 @@
|
||||
import { _decorator, Component, math, Node, EventTarget } from 'cc';
|
||||
import { EventCenter } from '../../EventCenter';
|
||||
import { AudioMgr } from '../audio/AudioMgr';
|
||||
import { Config } from '../../data/GameConfig';
|
||||
|
||||
|
||||
const { ccclass, property } = _decorator;
|
||||
const eventTarget = new EventTarget();
|
||||
@ccclass('LocalSaveMgr')
|
||||
export class LocalSaveMgr extends Component {
|
||||
static instance: LocalSaveMgr;
|
||||
playerData: PlayerData = null
|
||||
|
||||
protected onLoad(): void {
|
||||
LocalSaveMgr.instance = this
|
||||
let value = localStorage.getItem("playerData")
|
||||
|
||||
if (value != null || value != "") {
|
||||
|
||||
this.playerData = new PlayerData()
|
||||
try {
|
||||
this.playerData.jsonDataInit(JSON.parse(value))
|
||||
} catch (err) {
|
||||
//…
|
||||
}
|
||||
} else {
|
||||
this.playerData = new PlayerData()
|
||||
}
|
||||
this.initSound()
|
||||
}
|
||||
|
||||
passLevel() {
|
||||
this.playerData.levelIndex += 1
|
||||
this.save()
|
||||
}
|
||||
|
||||
addCoin(count: number) {
|
||||
console.log("addCoin", count)
|
||||
this.playerData.coin += count
|
||||
EventCenter.coinCountChange.emit("coinCountChange")
|
||||
this.save()
|
||||
}
|
||||
|
||||
|
||||
save() {
|
||||
localStorage.setItem("playerData", JSON.stringify(this.playerData))
|
||||
}
|
||||
|
||||
clearData() {
|
||||
localStorage.clear()
|
||||
}
|
||||
|
||||
initSound() {
|
||||
AudioMgr.inst.activeSound(!this.playerData.muteSound)
|
||||
AudioMgr.inst.activeMusic(!this.playerData.muteMusic)
|
||||
}
|
||||
|
||||
activeSound(active: boolean) {
|
||||
this.playerData.muteSound = !active
|
||||
AudioMgr.inst.activeSound(active)
|
||||
this.save()
|
||||
|
||||
}
|
||||
activeMusic(active: boolean) {
|
||||
this.playerData.muteMusic = !active
|
||||
AudioMgr.inst.activeMusic(active)
|
||||
this.save()
|
||||
}
|
||||
|
||||
equipWeapon(index: number) {
|
||||
this.playerData.equipedWeapon = index
|
||||
this.save()
|
||||
}
|
||||
|
||||
buyWeapon(index: number) {
|
||||
this.playerData.ownWeapon.push(index)
|
||||
console.log("购买武器", index, this.playerData.ownWeapon[this.playerData.ownWeapon.length - 1])
|
||||
this.save()
|
||||
}
|
||||
|
||||
timerAddPower(count: number) {
|
||||
this.playerData.power += count
|
||||
if (this.playerData.power > Config.maxHp) {
|
||||
this.playerData.power = Config.maxHp
|
||||
}
|
||||
EventCenter.powerChange.emit("powerChange")
|
||||
this.save()
|
||||
}
|
||||
|
||||
usePower() {
|
||||
this.playerData.power -= 1
|
||||
EventCenter.powerChange.emit("powerChange")
|
||||
this.save()
|
||||
}
|
||||
|
||||
setSideTip() {
|
||||
this.playerData.haveGetSideReward = true
|
||||
this.save()
|
||||
}
|
||||
|
||||
adsAddPower() {
|
||||
this.playerData.power += 3
|
||||
EventCenter.powerChange.emit("powerChange")
|
||||
this.save()
|
||||
}
|
||||
|
||||
completeTutorial() {
|
||||
this.playerData.haveTutorial = true
|
||||
this.save()
|
||||
}
|
||||
|
||||
getPowerCD() {
|
||||
let passTime = (Date.now() - this.playerData.lastAddPowerTimer) / 1000
|
||||
return Config.powerCD - passTime
|
||||
}
|
||||
|
||||
setShareTime() {
|
||||
const date: Date = new Date();
|
||||
const dayOfMonth: number = date.getDate();
|
||||
this.playerData.day = dayOfMonth
|
||||
this.save()
|
||||
console.log(dayOfMonth)
|
||||
}
|
||||
|
||||
protected update(dt: number): void {
|
||||
if (this.playerData.power < Config.maxHp) {
|
||||
if (this.playerData.isOnTimerRun == false) {
|
||||
this.playerData.isOnTimerRun = true
|
||||
this.playerData.lastAddPowerTimer = Date.now()
|
||||
this.save()
|
||||
}
|
||||
|
||||
if (this.playerData.isOnTimerRun) {
|
||||
let passTime = (Date.now() - this.playerData.lastAddPowerTimer) / 1000
|
||||
// console.log(passTime)
|
||||
if ((passTime > Config.powerCD)) {
|
||||
console.log("倒计时足够")
|
||||
this.playerData.lastAddPowerTimer = Date.now()
|
||||
this.timerAddPower(Math.floor(passTime / Config.powerCD))
|
||||
}
|
||||
|
||||
if (this.playerData.power >= Config.maxHp) {
|
||||
console.log("体力超过上线")
|
||||
this.playerData.isOnTimerRun = false
|
||||
this.save()
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
class PlayerData {
|
||||
muteMusic: boolean = false
|
||||
muteSound: boolean = false
|
||||
levelIndex: number = 1
|
||||
equipedWeapon: number = 1
|
||||
power: number = Config.maxHp
|
||||
coin: number = 100
|
||||
ownWeapon: number[] = [1]
|
||||
lastAddPowerTimer: number = null
|
||||
isOnTimerRun: boolean = false
|
||||
haveGetSideReward: boolean = false
|
||||
haveShare: boolean = false
|
||||
haveTutorial: boolean = false
|
||||
day: number = -1
|
||||
jsonDataInit(obj) {
|
||||
if (obj == null) return
|
||||
if (obj.muteMusic != null)
|
||||
this.muteMusic = obj.muteMusic
|
||||
if (obj.muteSound != null)
|
||||
this.muteSound = obj.muteSound
|
||||
if (obj.levelIndex != null)
|
||||
this.levelIndex = obj.levelIndex
|
||||
if (obj.equipedWeapon != null)
|
||||
this.equipedWeapon = obj.equipedWeapon
|
||||
if (obj.ownWeapon != null)
|
||||
this.ownWeapon = obj.ownWeapon
|
||||
if (obj.coin != null)
|
||||
this.coin = obj.coin
|
||||
if (obj.lastAddPowerTimer != null)
|
||||
this.lastAddPowerTimer = obj.lastAddPowerTimer
|
||||
if (obj.power != null)
|
||||
this.power = obj.power
|
||||
if (obj.isOnTimerRun != null)
|
||||
this.isOnTimerRun = obj.isOnTimerRun
|
||||
if (obj.haveGetSideReward != null)
|
||||
this.haveGetSideReward = obj.haveGetSideReward
|
||||
if (obj.haveShare != null)
|
||||
this.haveShare = obj.haveShare
|
||||
if (obj.day != null)
|
||||
this.day = obj.day
|
||||
if (obj.haveTutorial != null)
|
||||
this.haveTutorial = obj.haveTutorial
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,9 @@
|
||||
{
|
||||
"ver": "4.0.24",
|
||||
"importer": "typescript",
|
||||
"imported": true,
|
||||
"uuid": "f0143096-0558-4925-a73a-0b91753e5038",
|
||||
"files": [],
|
||||
"subMetas": {},
|
||||
"userData": {}
|
||||
}
|
||||
Reference in New Issue
Block a user