mirror of
https://gitee.com/dgflash/oops-plugin-framework.git
synced 2026-05-30 18:39:18 +08:00
添加伪随机对象
This commit is contained in:
@@ -3,48 +3,24 @@
|
||||
/** 随机管理 */
|
||||
export class RandomManager {
|
||||
private static _instance: RandomManager;
|
||||
|
||||
/** 是否运行在客户端环境 */
|
||||
isClient: boolean = true;
|
||||
/** 是否为全局伪随机 */
|
||||
isGlobal: boolean = false;
|
||||
|
||||
private random: any = null;
|
||||
private random: any = null!;
|
||||
|
||||
/** 随机数管理单例对象 */
|
||||
static get instance(): RandomManager {
|
||||
if (this._instance == null) {
|
||||
this._instance = new RandomManager();
|
||||
this._instance.random = Math.random;
|
||||
this._instance.setRandom(Math.random);
|
||||
}
|
||||
return this._instance;
|
||||
}
|
||||
|
||||
private getRandom(): number {
|
||||
return this.isGlobal ? Math.random() : this.random();
|
||||
/** 设置第三方随机库 */
|
||||
setRandom(random: any) {
|
||||
this.random = random;
|
||||
}
|
||||
|
||||
/** 设置随机种子 */
|
||||
setSeed(seed: number) {
|
||||
if (this.isClient) {
|
||||
//注:seedrandom.min.js文件在Cocos Creator中导入为插件生效
|
||||
//@ts-ignore
|
||||
if (Math.seedrandom) {
|
||||
if (this.isGlobal)
|
||||
//@ts-ignore
|
||||
new Math.seedrandom(seed, { global: true });
|
||||
else
|
||||
//@ts-ignore
|
||||
this.random = new Math.seedrandom(seed);
|
||||
}
|
||||
}
|
||||
else {
|
||||
var seedrandom = require('seedrandom');
|
||||
if (this.isGlobal)
|
||||
new seedrandom(seed, { global: true });
|
||||
else
|
||||
this.random = new seedrandom(seed);
|
||||
}
|
||||
private getRandom(): number {
|
||||
return this.random();
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
23
assets/core/common/random/SeedRandom.ts
Normal file
23
assets/core/common/random/SeedRandom.ts
Normal file
@@ -0,0 +1,23 @@
|
||||
import { RandomManager } from "./RandomManager";
|
||||
|
||||
/** 伪随机 */
|
||||
export class SeedRandom {
|
||||
private rm: RandomManager;
|
||||
private sr: any;
|
||||
|
||||
get random(): RandomManager {
|
||||
return this.rm;
|
||||
}
|
||||
|
||||
constructor(seed: string) {
|
||||
//@ts-ignore
|
||||
this.sr = new Math.seedrandom(seed);
|
||||
this.rm = new RandomManager();
|
||||
this.rm.setRandom(this.sr);
|
||||
}
|
||||
|
||||
destroy() {
|
||||
this.rm = null!;
|
||||
this.sr = null!;
|
||||
}
|
||||
}
|
||||
9
assets/core/common/random/SeedRandom.ts.meta
Normal file
9
assets/core/common/random/SeedRandom.ts.meta
Normal file
@@ -0,0 +1,9 @@
|
||||
{
|
||||
"ver": "4.0.24",
|
||||
"importer": "typescript",
|
||||
"imported": true,
|
||||
"uuid": "9b02b9cd-c306-445a-af45-b79f41442201",
|
||||
"files": [],
|
||||
"subMetas": {},
|
||||
"userData": {}
|
||||
}
|
||||
Reference in New Issue
Block a user