添加伪随机对象

This commit is contained in:
donggang
2024-06-21 15:51:22 +08:00
parent 6d0dfb174d
commit c22d31ad29
3 changed files with 39 additions and 31 deletions

View File

@@ -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();
}
/**

View 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!;
}
}

View File

@@ -0,0 +1,9 @@
{
"ver": "4.0.24",
"importer": "typescript",
"imported": true,
"uuid": "9b02b9cd-c306-445a-af45-b79f41442201",
"files": [],
"subMetas": {},
"userData": {}
}