移除数学库

This commit is contained in:
YHH
2025-06-07 21:45:11 +08:00
parent 50420f9052
commit 082c2b46d0
17 changed files with 31 additions and 1228 deletions
+1 -1
View File
@@ -14,7 +14,7 @@
- 🔍 **查询系统** - 基于位掩码的高性能实体查询 - 🔍 **查询系统** - 基于位掩码的高性能实体查询
- 🛠️ **性能监控** - 内置性能监控工具,帮助优化游戏性能 - 🛠️ **性能监控** - 内置性能监控工具,帮助优化游戏性能
- 🎯 **对象池** - 内存管理优化,减少垃圾回收压力 - 🎯 **对象池** - 内存管理优化,减少垃圾回收压力
- 📊 **数学库** - 完整的 2D 数学运算支持 - 🎯 **纯ECS架构** - 专注于实体组件系统核心逻辑
## 📦 安装 ## 📦 安装
+2 -2
View File
@@ -1,12 +1,12 @@
{ {
"name": "@esengine/ecs-framework", "name": "@esengine/ecs-framework",
"version": "2.0.4", "version": "2.0.5",
"lockfileVersion": 3, "lockfileVersion": 3,
"requires": true, "requires": true,
"packages": { "packages": {
"": { "": {
"name": "@esengine/ecs-framework", "name": "@esengine/ecs-framework",
"version": "2.0.4", "version": "2.0.5",
"license": "MIT", "license": "MIT",
"devDependencies": { "devDependencies": {
"@babel/core": "^7.27.4", "@babel/core": "^7.27.4",
+1 -1
View File
@@ -1,6 +1,6 @@
{ {
"name": "@esengine/ecs-framework", "name": "@esengine/ecs-framework",
"version": "2.0.4", "version": "2.0.5",
"description": "用于Laya、Cocos等游戏引擎的高性能ECS框架", "description": "用于Laya、Cocos等游戏引擎的高性能ECS框架",
"main": "bin/index.js", "main": "bin/index.js",
"types": "bin/index.d.ts", "types": "bin/index.d.ts",
-36
View File
@@ -102,42 +102,6 @@ export class EntityBuilder {
return this; return this;
} }
/**
* 设置实体位置(如果有Transform组件)
* @param x X坐标
* @param y Y坐标
* @param z Z坐标(可选)
* @returns 实体构建器
*/
public at(x: number, y: number, z: number = 0): EntityBuilder {
// 直接使用Entity的position属性
this.entity.position.x = x;
this.entity.position.y = y;
return this;
}
/**
* 设置实体旋转(如果有Transform组件)
* @param rotation 旋转角度
* @returns 实体构建器
*/
public rotated(rotation: number): EntityBuilder {
this.entity.rotation = rotation;
return this;
}
/**
* 设置实体缩放(如果有Transform组件)
* @param scaleX X轴缩放
* @param scaleY Y轴缩放(可选,默认与X轴相同)
* @returns 实体构建器
*/
public scaled(scaleX: number, scaleY?: number): EntityBuilder {
this.entity.scale.x = scaleX;
this.entity.scale.y = scaleY !== undefined ? scaleY : scaleX;
return this;
}
/** /**
* 设置实体为启用状态 * 设置实体为启用状态
* @param enabled 是否启用 * @param enabled 是否启用
+25 -87
View File
@@ -1,5 +1,3 @@
import { Vector2 } from '../Math/Vector2';
import { Transform } from './Transform';
import { Component } from './Component'; import { Component } from './Component';
import { ComponentRegistry, ComponentType } from './Core/ComponentStorage'; import { ComponentRegistry, ComponentType } from './Core/ComponentStorage';
@@ -204,35 +202,28 @@ export class Entity {
/** /**
* 实体唯一标识符 * 实体唯一标识符
* *
* 在整个游戏生命周期中唯一的数字ID * 在场景中唯一的数字标识符
*/ */
public readonly id: number; public readonly id: number;
/**
* 变换组件
*
* 管理实体的位置、旋转和缩放信息。
*/
public readonly transform: Transform;
/** /**
* 组件集合 * 组件集合
* *
* 存储附加到此实体的所有组件。 * 存储实体拥有的所有组件。
*/ */
public readonly components: Component[] = []; public readonly components: Component[] = [];
/** /**
* 所属场景 * 所属场景引用
* *
* 实体所在的场景引用 * 指向实体所在的场景实例
*/ */
public scene: any; // 使用any避免循环依赖 public scene: any; // 使用any避免循环依赖
/** /**
* 更新间隔 * 更新间隔
* *
* 控制实体更新的频率。 * 控制实体更新的频率,值越大更新越不频繁
*/ */
public updateInterval: number = 1; public updateInterval: number = 1;
@@ -244,66 +235,72 @@ export class Entity {
public _isDestroyed: boolean = false; public _isDestroyed: boolean = false;
/** /**
* 父实体 * 父实体引用
* *
* 此实体的父实体引用,如果为null则表示是根实体 * 指向父级实体,用于构建实体层次结构
*/ */
private _parent: Entity | null = null; private _parent: Entity | null = null;
/** /**
* 子实体集合 * 子实体集合
* *
* 存储此实体的所有子实体。 * 存储所有子实体的数组
*/ */
private _children: Entity[] = []; private _children: Entity[] = [];
/** /**
* 活状态 * 活状态
* *
* 控制实体及其子实体是否参与更新和渲染 * 控制实体是否处于激活状态
*/ */
private _active: boolean = true; private _active: boolean = true;
/** /**
* 实体标签 * 实体标签
* *
* 用于分类和快速查找的数字标签。 * 用于分类和查询的数字标签。
*/ */
private _tag: number = 0; private _tag: number = 0;
/** /**
* 启用状态 * 启用状态
* *
* 控制实体是否参与更新循环 * 控制实体是否启用更新和处理
*/ */
private _enabled: boolean = true; private _enabled: boolean = true;
/** /**
* 更新顺序 * 更新顺序
* *
* 决定实体在更新循环中的执行顺序 * 控制实体在系统中的更新优先级
*/ */
private _updateOrder: number = 0; private _updateOrder: number = 0;
/** /**
* 组件位掩码 * 组件位掩码
* *
* 用于快速查实体拥有哪些组件类型。 * 用于快速查实体拥有组件类型。
*/ */
private _componentMask: bigint = BigInt(0); private _componentMask: bigint = BigInt(0);
/** /**
* 组件类型到索引的映射 * 组件类型到索引的映射
*
* 用于快速定位组件在数组中的位置。
*/ */
private _componentTypeToIndex = new Map<ComponentType, number>(); private _componentTypeToIndex = new Map<ComponentType, number>();
/** /**
* 组件缓存系统 * 组件缓存
*
* 高性能组件访问缓存。
*/ */
private _componentCache: ComponentCache; private _componentCache: ComponentCache;
/** /**
* 组件访问统计 * 组件访问统计
*
* 记录组件访问的性能统计信息。
*/ */
private _componentAccessStats = new Map<ComponentType, { private _componentAccessStats = new Map<ComponentType, {
accessCount: number; accessCount: number;
@@ -313,7 +310,7 @@ export class Entity {
}>(); }>();
/** /**
* 创建实体实例 * 构造函数
* *
* @param name - 实体名称 * @param name - 实体名称
* @param id - 实体唯一标识符 * @param id - 实体唯一标识符
@@ -321,7 +318,8 @@ export class Entity {
constructor(name: string, id: number) { constructor(name: string, id: number) {
this.name = name; this.name = name;
this.id = id; this.id = id;
this.transform = new Transform();
// 初始化组件缓存
this._componentCache = new ComponentCache(); this._componentCache = new ComponentCache();
} }
@@ -460,60 +458,6 @@ export class Entity {
return this._componentMask; return this._componentMask;
} }
/**
* 获取实体位置
*
* @returns 实体的位置向量
*/
public get position(): Vector2 {
return this.transform.position;
}
/**
* 设置实体位置
*
* @param value - 新的位置向量
*/
public set position(value: Vector2) {
this.transform.position = value;
}
/**
* 获取实体旋转角度
*
* @returns 实体的旋转角度(弧度)
*/
public get rotation(): number {
return this.transform.rotation;
}
/**
* 设置实体旋转角度
*
* @param value - 新的旋转角度(弧度)
*/
public set rotation(value: number) {
this.transform.rotation = value;
}
/**
* 获取实体缩放
*
* @returns 实体的缩放向量
*/
public get scale(): Vector2 {
return this.transform.scale;
}
/**
* 设置实体缩放
*
* @param value - 新的缩放向量
*/
public set scale(value: Vector2) {
this.transform.scale = value;
}
/** /**
* 创建并添加组件 * 创建并添加组件
* *
@@ -1298,9 +1242,6 @@ export class Entity {
componentCount: number; componentCount: number;
componentTypes: string[]; componentTypes: string[];
componentMask: string; componentMask: string;
position: { x: number; y: number };
rotation: number;
scale: { x: number; y: number };
parentId: number | null; parentId: number | null;
childCount: number; childCount: number;
childIds: number[]; childIds: number[];
@@ -1341,9 +1282,6 @@ export class Entity {
componentCount: this.components.length, componentCount: this.components.length,
componentTypes: this.components.map(c => c.constructor.name), componentTypes: this.components.map(c => c.constructor.name),
componentMask: this._componentMask.toString(2), // 二进制表示 componentMask: this._componentMask.toString(2), // 二进制表示
position: { x: this.position.x, y: this.position.y },
rotation: this.rotation,
scale: { x: this.scale.x, y: this.scale.y },
parentId: this._parent?.id || null, parentId: this._parent?.id || null,
childCount: this._children.length, childCount: this._children.length,
childIds: this._children.map(c => c.id), childIds: this._children.map(c => c.id),
-285
View File
@@ -1,285 +0,0 @@
import { Vector2 } from '../Math/Vector2';
import { MathHelper } from '../Math/MathHelper';
/**
*
*
*
*
*
* @example
* ```typescript
* const transform = new Transform();
* transform.setPosition(100, 200);
* transform.setRotationDegrees(45);
* transform.setScale(2, 2);
*
* // 设置父子关系
* childTransform.setParent(transform);
* ```
*/
export class Transform {
/**
*
*
*
*/
public position: Vector2 = Vector2.zero;
/**
*
*
*
*/
public rotation: number = 0;
/**
*
*
*
*/
public scale: Vector2 = Vector2.one;
/**
*
*
* null则表示这是根变换
*/
public parent: Transform | null = null;
/**
*
*
*
*/
private _children: Transform[] = [];
/**
*
*
* @param position -
* @param rotation - 0
* @param scale -
*/
constructor(position?: Vector2, rotation: number = 0, scale?: Vector2) {
if (position) this.position = position.clone();
this.rotation = rotation;
if (scale) this.scale = scale.clone();
}
/**
*
*
* @returns
*/
public get rotationDegrees(): number {
return MathHelper.toDegrees(this.rotation);
}
/**
*
*
* @param value -
*/
public set rotationDegrees(value: number) {
this.rotation = MathHelper.toRadians(value);
}
/**
*
*
*
*
* @returns
*/
public get worldPosition(): Vector2 {
if (!this.parent) {
return this.position.clone();
}
// 计算世界位置
const parentWorld = this.parent.worldPosition;
const cos = Math.cos(this.parent.worldRotation);
const sin = Math.sin(this.parent.worldRotation);
const parentScale = this.parent.worldScale;
const scaledPos = Vector2.multiply(this.position, parentScale);
const rotatedX = scaledPos.x * cos - scaledPos.y * sin;
const rotatedY = scaledPos.x * sin + scaledPos.y * cos;
return new Vector2(parentWorld.x + rotatedX, parentWorld.y + rotatedY);
}
/**
*
*
*
*
* @returns
*/
public get worldRotation(): number {
if (!this.parent) {
return this.rotation;
}
return this.parent.worldRotation + this.rotation;
}
/**
*
*
*
*
* @returns
*/
public get worldScale(): Vector2 {
if (!this.parent) {
return this.scale.clone();
}
return Vector2.multiply(this.parent.worldScale, this.scale);
}
/**
*
*
* @returns
*/
public get childCount(): number {
return this._children.length;
}
/**
*
*
* @param index -
* @returns null
*/
public getChild(index: number): Transform | null {
if (index >= 0 && index < this._children.length) {
return this._children[index];
}
return null;
}
/**
*
*
*
*
* @param parent - null表示断开父子关系
*/
public setParent(parent: Transform | null): void {
if (this.parent === parent) return;
// 从旧父级移除
if (this.parent) {
const index = this.parent._children.indexOf(this);
if (index >= 0) {
this.parent._children.splice(index, 1);
}
}
// 设置新父级
this.parent = parent;
if (parent) {
parent._children.push(this);
}
}
/**
*
*
* @param x - X坐标
* @param y - Y坐标
*/
public setPosition(x: number, y: number): void {
this.position.set(x, y);
}
/**
*
*
* @param radians -
*/
public setRotation(radians: number): void {
this.rotation = radians;
}
/**
*
*
* @param degrees -
*/
public setRotationDegrees(degrees: number): void {
this.rotation = MathHelper.toRadians(degrees);
}
/**
*
*
* @param scale -
*/
public setScale(scale: Vector2): void;
/**
*
*
* @param x - X轴缩放比例
* @param y - Y轴缩放比例
*/
public setScale(x: number, y: number): void;
public setScale(scaleOrX: Vector2 | number, y?: number): void {
if (typeof scaleOrX === 'number') {
this.scale.set(scaleOrX, y!);
} else {
this.scale.copyFrom(scaleOrX);
}
}
/**
*
*
* 使
*
* @param target -
*/
public lookAt(target: Vector2): void {
const direction = target.subtract(this.worldPosition);
this.rotation = Math.atan2(direction.y, direction.x);
}
/**
*
*
*
*
* @param offset -
*/
public translate(offset: Vector2): void {
this.position = this.position.add(offset);
}
/**
*
* @param radians
*/
public rotate(radians: number): void {
this.rotation += radians;
}
/**
*
* @param other
*/
public copyFrom(other: Transform): void {
this.position.copyFrom(other.position);
this.rotation = other.rotation;
this.scale.copyFrom(other.scale);
}
/**
*
*/
public clone(): Transform {
const transform = new Transform(this.position, this.rotation, this.scale);
return transform;
}
}
+1 -2
View File
@@ -1,7 +1,6 @@
// Core目录已删除,直接导出核心类 // 导出核心ECS
export { Entity } from './Entity'; export { Entity } from './Entity';
export { Component } from './Component'; export { Component } from './Component';
export { Transform } from './Transform';
export { CoreEvents } from './CoreEvents'; export { CoreEvents } from './CoreEvents';
export * from './Systems'; export * from './Systems';
export * from './Utils'; export * from './Utils';
-25
View File
@@ -1,25 +0,0 @@
/**
*
*
*/
export enum Edge {
/**
*
*/
top = 0,
/**
*
*/
bottom = 1,
/**
*
*/
left = 2,
/**
*
*/
right = 3
}
-100
View File
@@ -1,100 +0,0 @@
/**
*
*
*/
export class MathHelper {
/**
*
*/
public static readonly DEG_TO_RAD = Math.PI / 180;
/**
*
*/
public static readonly RAD_TO_DEG = 180 / Math.PI;
/**
*
*/
public static readonly EPSILON = 0.00001;
/**
*
* @param degrees
* @returns
*/
public static toRadians(degrees: number): number {
return degrees * MathHelper.DEG_TO_RAD;
}
/**
*
* @param radians
* @returns
*/
public static toDegrees(radians: number): number {
return radians * MathHelper.RAD_TO_DEG;
}
/**
*
* @param value
* @param min
* @param max
* @returns
*/
public static clamp(value: number, min: number, max: number): number {
return Math.max(min, Math.min(max, value));
}
/**
* 01
* @param value
* @returns [0,1]
*/
public static clamp01(value: number): number {
return MathHelper.clamp(value, 0, 1);
}
/**
* 线
* @param a
* @param b
* @param t [0,1]
* @returns
*/
public static lerp(a: number, b: number, t: number): number {
return a + (b - a) * MathHelper.clamp01(t);
}
/**
*
* @param a
* @param b
* @param tolerance EPSILON
* @returns truefalse
*/
public static approximately(a: number, b: number, tolerance: number = MathHelper.EPSILON): boolean {
return Math.abs(a - b) < tolerance;
}
/**
*
* @param min 0
* @param max 1
* @returns
*/
public static random(min: number = 0, max: number = 1): number {
return Math.random() * (max - min) + min;
}
/**
*
* @param min
* @param max
* @returns
*/
public static randomInt(min: number, max: number): number {
return Math.floor(Math.random() * (max - min + 1)) + min;
}
}
-219
View File
@@ -1,219 +0,0 @@
import { Vector2 } from './Vector2';
/**
*
*
*/
export class Rectangle {
/**
* X坐标
*/
public x: number = 0;
/**
* Y坐标
*/
public y: number = 0;
/**
*
*/
public width: number = 0;
/**
*
*/
public height: number = 0;
/**
*
* @param x X坐标
* @param y Y坐标
* @param width
* @param height
*/
constructor(x: number = 0, y: number = 0, width: number = 0, height: number = 0) {
this.x = x;
this.y = y;
this.width = width;
this.height = height;
}
/**
*
*/
public static get empty(): Rectangle {
return new Rectangle();
}
/**
* X坐标
*/
public get left(): number {
return this.x;
}
/**
* X坐标
*/
public get right(): number {
return this.x + this.width;
}
/**
* Y坐标
*/
public get top(): number {
return this.y;
}
/**
* Y坐标
*/
public get bottom(): number {
return this.y + this.height;
}
/**
*
*/
public get center(): Vector2 {
return new Vector2(this.x + this.width / 2, this.y + this.height / 2);
}
/**
*
*/
public get location(): Vector2 {
return new Vector2(this.x, this.y);
}
public set location(value: Vector2) {
this.x = value.x;
this.y = value.y;
}
/**
*
*/
public get size(): Vector2 {
return new Vector2(this.width, this.height);
}
public set size(value: Vector2) {
this.width = value.x;
this.height = value.y;
}
/**
*
* @param x X坐标
* @param y Y坐标
* @returns truefalse
*/
public contains(x: number, y: number): boolean;
/**
*
* @param point
* @returns truefalse
*/
public contains(point: Vector2): boolean;
public contains(xOrPoint: number | Vector2, y?: number): boolean {
if (typeof xOrPoint === 'number') {
return xOrPoint >= this.x && xOrPoint < this.right && y! >= this.y && y! < this.bottom;
} else {
return this.contains(xOrPoint.x, xOrPoint.y);
}
}
/**
*
* @param other
* @returns truefalse
*/
public intersects(other: Rectangle): boolean {
return other.left < this.right && this.left < other.right &&
other.top < this.bottom && this.top < other.bottom;
}
/**
*
* @param other
* @returns
*/
public intersection(other: Rectangle): Rectangle {
const x1 = Math.max(this.x, other.x);
const x2 = Math.min(this.right, other.right);
const y1 = Math.max(this.y, other.y);
const y2 = Math.min(this.bottom, other.bottom);
if (x2 >= x1 && y2 >= y1) {
return new Rectangle(x1, y1, x2 - x1, y2 - y1);
}
return Rectangle.empty;
}
/**
*
* @param rect1
* @param rect2
* @returns
*/
public static union(rect1: Rectangle, rect2: Rectangle): Rectangle {
const x = Math.min(rect1.x, rect2.x);
const y = Math.min(rect1.y, rect2.y);
const right = Math.max(rect1.right, rect2.right);
const bottom = Math.max(rect1.bottom, rect2.bottom);
return new Rectangle(x, y, right - x, bottom - y);
}
/**
*
* @param offsetX X轴偏移量
* @param offsetY Y轴偏移量
*/
public offset(offsetX: number, offsetY: number): void {
this.x += offsetX;
this.y += offsetY;
}
/**
*
* @param horizontalAmount
* @param verticalAmount
*/
public inflate(horizontalAmount: number, verticalAmount: number): void {
this.x -= horizontalAmount;
this.y -= verticalAmount;
this.width += horizontalAmount * 2;
this.height += verticalAmount * 2;
}
/**
* 0
* @returns truefalse
*/
public isEmpty(): boolean {
return this.width === 0 && this.height === 0 && this.x === 0 && this.y === 0;
}
/**
*
* @param other
* @returns truefalse
*/
public equals(other: Rectangle): boolean {
return this.x === other.x && this.y === other.y &&
this.width === other.width && this.height === other.height;
}
/**
*
* @returns
*/
public clone(): Rectangle {
return new Rectangle(this.x, this.y, this.width, this.height);
}
}
-397
View File
@@ -1,397 +0,0 @@
/**
*
*
*/
export class Vector2 {
/**
* X坐标
*/
public x: number;
/**
* Y坐标
*/
public y: number;
/**
*
* @param x X坐标0
* @param y Y坐标0
*/
constructor(x: number = 0, y: number = 0) {
this.x = x;
this.y = y;
}
/**
*
*/
public static get zero(): Vector2 {
return new Vector2(0, 0);
}
/**
* (1, 1)
*/
public static get one(): Vector2 {
return new Vector2(1, 1);
}
/**
* X向量(1, 0)
*/
public static get unitX(): Vector2 {
return new Vector2(1, 0);
}
/**
* Y向量(0, 1)
*/
public static get unitY(): Vector2 {
return new Vector2(0, 1);
}
/**
* (0, -1)
*/
public static get up(): Vector2 {
return new Vector2(0, -1);
}
/**
* (0, 1)
*/
public static get down(): Vector2 {
return new Vector2(0, 1);
}
/**
* (-1, 0)
*/
public static get left(): Vector2 {
return new Vector2(-1, 0);
}
/**
* (1, 0)
*/
public static get right(): Vector2 {
return new Vector2(1, 0);
}
/**
*
*/
public get length(): number {
return Math.sqrt(this.x * this.x + this.y * this.y);
}
/**
*
*/
public get lengthSquared(): number {
return this.x * this.x + this.y * this.y;
}
/**
*
* @param x X坐标
* @param y Y坐标
*/
public set(x: number, y: number): Vector2 {
this.x = x;
this.y = y;
return this;
}
/**
*
* @param other
*/
public copyFrom(other: Vector2): Vector2 {
this.x = other.x;
this.y = other.y;
return this;
}
/**
*
*/
public clone(): Vector2 {
return new Vector2(this.x, this.y);
}
/**
*
* @param other
*/
public add(other: Vector2): Vector2 {
return new Vector2(this.x + other.x, this.y + other.y);
}
/**
*
* @param other
*/
public subtract(other: Vector2): Vector2 {
return new Vector2(this.x - other.x, this.y - other.y);
}
/**
*
* @param scalar
*/
public multiply(scalar: number): Vector2 {
return new Vector2(this.x * scalar, this.y * scalar);
}
/**
*
* @param scalar
*/
public divide(scalar: number): Vector2 {
return new Vector2(this.x / scalar, this.y / scalar);
}
/**
*
* @param other
*/
public dot(other: Vector2): number {
return this.x * other.x + this.y * other.y;
}
/**
* 2D中返回标量
* @param other
*/
public cross(other: Vector2): number {
return this.x * other.y - this.y * other.x;
}
/**
*
*/
public normalize(): Vector2 {
const length = this.length;
if (length === 0) {
return Vector2.zero;
}
return new Vector2(this.x / length, this.y / length);
}
/**
*
* @param other
*/
public distanceTo(other: Vector2): number {
const dx = this.x - other.x;
const dy = this.y - other.y;
return Math.sqrt(dx * dx + dy * dy);
}
/**
*
* @param other
*/
public distanceSquaredTo(other: Vector2): number {
const dx = this.x - other.x;
const dy = this.y - other.y;
return dx * dx + dy * dy;
}
/**
*
* @param other
* @param tolerance
*/
public equals(other: Vector2, tolerance: number = 0.0001): boolean {
return Math.abs(this.x - other.x) < tolerance && Math.abs(this.y - other.y) < tolerance;
}
/**
*
*/
public angle(): number {
return Math.atan2(this.y, this.x);
}
/**
*
* @param radians
*/
public rotate(radians: number): Vector2 {
const cos = Math.cos(radians);
const sin = Math.sin(radians);
return new Vector2(
this.x * cos - this.y * sin,
this.x * sin + this.y * cos
);
}
/**
* 线
* @param other
* @param t (0-1)
*/
public lerp(other: Vector2, t: number): Vector2 {
return new Vector2(
this.x + (other.x - this.x) * t,
this.y + (other.y - this.y) * t
);
}
/**
*
*/
public round(): Vector2 {
return new Vector2(Math.round(this.x), Math.round(this.y));
}
/**
*
*/
public floor(): Vector2 {
return new Vector2(Math.floor(this.x), Math.floor(this.y));
}
/**
*
*/
public ceil(): Vector2 {
return new Vector2(Math.ceil(this.x), Math.ceil(this.y));
}
/**
*
*/
public toString(): string {
return `(${this.x.toFixed(2)}, ${this.y.toFixed(2)})`;
}
// 静态方法
/**
*
* @param a A
* @param b B
*/
public static add(a: Vector2, b: Vector2): Vector2 {
return new Vector2(a.x + b.x, a.y + b.y);
}
/**
*
* @param a A
* @param b B
*/
public static subtract(a: Vector2, b: Vector2): Vector2 {
return new Vector2(a.x - b.x, a.y - b.y);
}
/**
*
* @param a A
* @param b B或标量
*/
public static multiply(a: Vector2, b: Vector2 | number): Vector2 {
if (typeof b === 'number') {
return new Vector2(a.x * b, a.y * b);
} else {
return new Vector2(a.x * b.x, a.y * b.y);
}
}
/**
*
* @param a A
* @param b B或标量
*/
public static divide(a: Vector2, b: Vector2 | number): Vector2 {
if (typeof b === 'number') {
return new Vector2(a.x / b, a.y / b);
} else {
return new Vector2(a.x / b.x, a.y / b.y);
}
}
/**
*
* @param a A
* @param b B
*/
public static dot(a: Vector2, b: Vector2): number {
return a.x * b.x + a.y * b.y;
}
/**
*
* @param a A
* @param b B
*/
public static cross(a: Vector2, b: Vector2): number {
return a.x * b.y - a.y * b.x;
}
/**
*
* @param a A
* @param b B
*/
public static distance(a: Vector2, b: Vector2): number {
const dx = a.x - b.x;
const dy = a.y - b.y;
return Math.sqrt(dx * dx + dy * dy);
}
/**
*
* @param a A
* @param b B
*/
public static distanceSquared(a: Vector2, b: Vector2): number {
const dx = a.x - b.x;
const dy = a.y - b.y;
return dx * dx + dy * dy;
}
/**
* 线
* @param a
* @param b
* @param t (0-1)
*/
public static lerp(a: Vector2, b: Vector2, t: number): Vector2 {
return new Vector2(
a.x + (b.x - a.x) * t,
a.y + (b.y - a.y) * t
);
}
/**
*
* @param vector
*/
public static round(vector: Vector2): Vector2 {
return new Vector2(Math.round(vector.x), Math.round(vector.y));
}
/**
*
* @param radians
* @param length 1
*/
public static fromAngle(radians: number, length: number = 1): Vector2 {
return new Vector2(Math.cos(radians) * length, Math.sin(radians) * length);
}
/**
*
* @param vector
* @param normal
*/
public static reflect(vector: Vector2, normal: Vector2): Vector2 {
const dot = Vector2.dot(vector, normal);
return Vector2.subtract(vector, Vector2.multiply(normal, 2 * dot));
}
}
-5
View File
@@ -1,5 +0,0 @@
// 数学库导出
export { Vector2 } from './Vector2';
export { MathHelper } from './MathHelper';
export { Rectangle } from './Rectangle';
export { Edge } from './Edge';
-43
View File
@@ -1,43 +0,0 @@
import { Edge } from '../../Math/Edge';
/**
*
*
*/
export class EdgeExt {
/**
*
* @param self
* @returns
*/
public static oppositeEdge(self: Edge): Edge {
switch (self) {
case Edge.bottom:
return Edge.top;
case Edge.top:
return Edge.bottom;
case Edge.left:
return Edge.right;
case Edge.right:
return Edge.left;
}
}
/**
*
* @param self
* @returns truefalse
*/
public static isHorizontal(self: Edge): boolean {
return self == Edge.right || self == Edge.left;
}
/**
*
* @param self
* @returns truefalse
*/
public static isVertical(self: Edge): boolean {
return self == Edge.top || self == Edge.bottom;
}
}
+1 -2
View File
@@ -1,4 +1,3 @@
// 扩展工具类导出 // 扩展工具类导出
export { TypeUtils } from './TypeUtils'; export { TypeUtils } from './TypeUtils';
export { NumberExtension } from './NumberExtension'; export { NumberExtension } from './NumberExtension';
export { EdgeExt } from './EdgeExt';
-18
View File
@@ -1,18 +0,0 @@
import { Vector2 } from '../Math/Vector2';
/**
*
*
*/
export class Screen {
public static width: number;
public static height: number;
public static get size(): Vector2 {
return new Vector2(this.width, this.height);
}
public static get center(): Vector2 {
return new Vector2(this.width / 2, this.height / 2);
}
}
-1
View File
@@ -2,5 +2,4 @@
export * from './Extensions'; export * from './Extensions';
export * from './Pool'; export * from './Pool';
export * from './PerformanceMonitor'; export * from './PerformanceMonitor';
export { Screen } from './Screen';
export { Time } from './Time'; export { Time } from './Time';
-4
View File
@@ -19,11 +19,7 @@ export { Timer } from './Utils/Timers/Timer';
// ECS核心 // ECS核心
export * from './ECS'; export * from './ECS';
// 数学库
export * from './Math';
// 工具类 // 工具类
export { Screen } from './Utils/Screen';
export * from './Utils/Pool'; export * from './Utils/Pool';
export * from './Utils/PerformanceMonitor'; export * from './Utils/PerformanceMonitor';
export * from './Utils/Extensions'; export * from './Utils/Extensions';