Skip to content

Commit

Permalink
添加公共库文件
Browse files Browse the repository at this point in the history
  • Loading branch information
FlyQingfeng committed Oct 18, 2024
1 parent 6920d69 commit c8813e3
Show file tree
Hide file tree
Showing 7 changed files with 339 additions and 0 deletions.
80 changes: 80 additions & 0 deletions src/Public/Attribute.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
import { hasFun } from "./Stdlib";

//属性类
export class Attribute {
private properties: { [key: string]: any } = {};
_obj:any;
constructor(obj:any){

// 保证有这两个函数
if (hasFun(obj,"getDynamicProperty")
&&hasFun(obj,"setDynamicProperty")) {
this._obj=obj;
let data=obj.getDynamicProperty("Attribute");
if (data) {
this.properties=JSON.parse(data);
}else{
this.save()
}
}else{//抛出异常
throw new Error("SuperAPI[Attribute:constructor]:The constructor you want to pass using the <Attribute> class must have <getDynamicProperty><setDynamicProperty>two functions,");
}
}
private save(){
let data=JSON.stringify(this.properties);
if (this._obj) {
this._obj.setDynamicProperty("Attribute",data);
}
}
init(key: string, value: any){
let data=this._obj.getDynamicProperty("Attribute");
let json=JSON.parse(data);
if (!json[key]) {//只有在该值不存在的时候才能初始化,避免覆盖值
this.set(key,value);
}
}
// 添加或更新属性
set(key: string, value: any): void {
this.properties[key] = value;
this.save();
}

// 获取属性值
get(key: string): any {
return this.properties[key];
}

// 删除属性
delete(key: string): void {
delete this.properties[key];
this.save();
}

// 检查属性是否存在
has(key: string): boolean {
return key in this.properties;
}

// 获取所有属性键
keys(): string[] {
return Object.keys(this.properties);
}

// 获取所有属性值
values(): any[] {
return Object.values(this.properties);
}

// 获取所有属性键值对
entries(): [string, any][] {
return Object.entries(this.properties);
}

// 清空所有属性
clear(): void {
Object.keys(this.properties).forEach(key => {
delete this.properties[key];
});
this.save();
}
}
27 changes: 27 additions & 0 deletions src/Public/Axis.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import {Vec3} from "./Vec3";

export default class Axis {
x: Vec3; y: Vec3; z: Vec3;
constructor(x: Vec3, y: Vec3, z: Vec3) {
this.x = x
this.y = y
this.z = z
}
static bin(ang: Vec3, src: Vec3) {
let sx = Math.sin(ang.x)
let cx = Math.cos(ang.x)
let sy = Math.sin(ang.y)
let cy = Math.cos(ang.y)
let sz = Math.sin(ang.z)
let cz = Math.cos(ang.z)
return new Axis(new Vec3(cx * cy, sx * cy, sy).muln(src.x),
new Vec3(cx * sz * sy - sx * cz, sx * sz * sy + cx * cz, sz * cy).muln(src.y),
new Vec3(cx * cz * sy + sy * sz, sx * cz * sz + cx * sz, cz * cy).muln(src.z))
}
//入轴向量
dot(v: Vec3) { return this.x.muln(v.x).add(this.y.muln(v.y)).add(this.z.muln(v.z)) }
//叠加轴
plus(a: Axis) { return new Axis(this.dot(a.x), this.dot(a.y), this.dot(a.z)) }

toString() { return `[${this.x}] [${this.y}] [${this.z}]` }
}
17 changes: 17 additions & 0 deletions src/Public/Debug.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@

export class Debug {
static debug:boolean=true;
constructor() {
}

static log(...args:any[]){
if (this.debug) {
console.log("[SSAPI_DUBG] ",...args);
}
}
static debug_run(fun:()=>void){
if (this.debug) {
fun()
}
}
}
69 changes: 69 additions & 0 deletions src/Public/Stdlib.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
export function cast<T>(obj: any): T {//转换类型仅ts可用于强制类型转化,例如SuperEntity转SuperPlayer
return (obj as T);
}

export function hasFun(obj: any, funcName: string): boolean {
let currentProto = Object.getPrototypeOf(obj);
while (currentProto) {
if (typeof currentProto[funcName] === 'function') {
return true;
}
currentProto = Object.getPrototypeOf(currentProto);
}
return typeof obj[funcName] === 'function';
}

export function enumKeyToString(enumObj: any, enumValue: any): string {
const keys = Reflect.ownKeys(enumObj);
for (const key of keys) {
if (enumObj[key] === enumValue) {
return key.toString();
}
}
return '';
}

export function toJSON(value: any): string {
let cache: any[] = [];
let index: number = 0;
let str = JSON.stringify(value, function (key, value) {
if (typeof value === 'object' && value !== null) {
if (cache.indexOf(value) !== -1) {
// 循环引用,返回特殊标记
return `_CircularReference_${index++}`;
}
// 收集所有的值
cache.push(value);
}
return value;
});
// 清空变量,便于垃圾回收机制回收
cache = null;
return str;
}

export function fromJSON(value: string): any {
// 解析 JSON 字符串,但保留循环引用的标记
let parsed = JSON.parse(value);

let refs: any[] = [];
let refIndex: number = 0;

// 用于恢复循环引用的 reviver 函数
function revive(key, val) {
if (typeof val === 'string' && val.startsWith('_CircularReference_')) {
let refIndex = parseInt(val.split('_CircularReference_')[1], 10);

return refs[refIndex];
}
return val;
}
// 使用 reviver 函数解析数据,以恢复循环引用
return JSON.parse(JSON.stringify(parsed, function (key, val) {
if (typeof val === 'object' && val !== null) {
refs.push(val);
return refs[refIndex++];
}
return val;
}), revive);
}
60 changes: 60 additions & 0 deletions src/Public/Timer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import { system } from "@minecraft/server";

export class Timer {
private timeoutId?: number;
private intervalId?: number;
constructor() { }

// 设置一个单次定时器
public setTimeout(callback: () => void, delay: number): void {
system.run(()=>{
if (this.timeoutId) {
system.clearRun(this.timeoutId);
}
this.timeoutId = system.runTimeout(callback, delay);
})
}

// 设置一个重复定时器
public setInterval(callback: () => void, interval: number): void {
if (this.intervalId) {
system.clearRun(this.intervalId);
}
this.intervalId = system.runInterval(callback, interval);
}

// 取消单次定时器
public clearTimeout(): void {
if (this.timeoutId) {
system.clearRun(this.timeoutId);
this.timeoutId = undefined;
}
}

// 取消重复定时器
public clearInterval(): void {
if (this.intervalId) {
system.clearRun(this.intervalId);
this.intervalId = undefined;
}
}
//持久定时器,在持续时间内持续触发
public setLasting(interval:number,loop_event:()=>void,duration:number,out_event:()=>void):void{
if (interval>duration) {
throw new Error("Interval cannot be greater than duration.");
}
this.setInterval(()=>{
loop_event();
},interval)
this.setTimeout(()=>{
out_event();
this.clearAll();
},duration);
}
// 清除所有定时器
public clearAll(): void {
this.clearTimeout();
this.clearInterval();
}
}

41 changes: 41 additions & 0 deletions src/Public/Vec2.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import {Vec3} from "./Vec3";
export class Vec2 {
x: number; y: number;
constructor(x: any, y: number) {
this.x = x
this.y = y
}
static fromObj(obj: any) {
return new Vec2(obj.x, obj.y)
}
//投影
mod(v: Vec2) { return v.muln(this.dot(v) / v.dot(v)) }
//点积
dot(v: Vec2) { return this.x * v.x + this.y * v.y }
//加
add(v: Vec2) { return new Vec2(this.x + v.x, this.y + v.y) }
//减
sub(v: Vec2) { return new Vec2(this.x - v.x, this.y - v.y) }
//向量乘
mul(v: Vec2) { return new Vec2(this.x * v.x, this.y * v.y) }
//向量除
div(v: Vec2) { return new Vec2(this.x / v.x, this.y / v.y) }
//夹角
ang(v: Vec2) { return Math.atan2(this.x * v.y - this.y * v.x, this.dot(v)) }
//数乘
muln(v: number) { return new Vec2(this.x * v, this.y * v) }
//数除
divn(v: number) { return new Vec2(this.x / v, this.y / v) }
//旋转
rotate(ang: number) { return this.muln(Math.cos(ang)).add(new Vec2(-this.y, this.x).muln(Math.sin(ang))) }
//旋转至
rotate2(v: Vec2, t: number) { return this.rotate(this.ang(v) * t) }
//长度1
normal() { return this.divn(this.len()) }
//长
len() { return Math.sqrt(this.dot(this)) }
//转文本
toString() { return `${this.x} ${this.y}` }
//角转ve3
ator() { return new Vec3(Math.cos(this.x) * Math.cos(this.y), Math.sin(this.x) * Math.cos(this.y), Math.sin(this.y)) }
}
45 changes: 45 additions & 0 deletions src/Public/Vec3.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import {Vec2} from "./Vec2";
export class Vec3 {
x: number; y: number; z: number;

constructor(x: any, y: number, z: number) {
this.x = x
this.y = y
this.z = z
}
static fromObj(obj: any) {
return new Vec3(obj.x, obj.y, obj.z)
}
//投影
mod(v: Vec3) { return v.muln(this.dot(v) / v.dot(v)) }
//点积
dot(v: Vec3) { return this.x * v.x + this.y * v.y + this.z * v.z }
//加
add(v: Vec3) { return new Vec3(this.x + v.x, this.y + v.y, this.z + v.z) }
//减
sub(v: Vec3) { return new Vec3(this.x - v.x, this.y - v.y, this.z - v.z) }
//向量乘
mul(v: Vec3) { return new Vec3(this.x * v.x, this.y * v.y, this.z * v.z) }
//向量除
div(v: Vec3) { return new Vec3(this.x / v.x, this.y / v.y, this.z / v.z) }
//叉乘
pow(v: Vec3) { return new Vec3(this.y * this.z - v.y * v.z, this.z * this.x - v.z * v.x, this.x * this.y - v.x * v.y) }
//夹角
ang(v: Vec3) { return Math.acos(this.normal().dot(v.normal())) }
//数乘
muln(v: number) { return new Vec3(this.x * v, this.y * v, this.z * v) }
//数除
divn(v: number) { return new Vec3(this.x / v, this.y / v, this.z / v) }
//绕轴旋转
rotate(ax: Vec3, ang: number) { let di = this.mod(ax), rx = this.sub(di); return rx.muln(Math.cos(ang)).add(rx.pow(di.normal()).muln(Math.sin(ang))).add(di) }
//旋转至
rotate2(v: Vec3, t: number) { return this.rotate(this.pow(v), this.ang(v) * t) }
//长度1
normal() { return this.divn(this.len()) }
//长
len() { return Math.sqrt(this.dot(this)) }
//转文本
toString() { return `${this.x} ${this.y} ${this.z}` }
//转角向量
tor() { return new Vec2(Math.atan2(this.z, this.x), Math.atan2(this.y, Math.sqrt(this.x * this.x + this.z + this.z))) }
}

0 comments on commit c8813e3

Please sign in to comment.