移除数学库
This commit is contained in:
@@ -14,7 +14,7 @@
|
|||||||
- 🔍 **查询系统** - 基于位掩码的高性能实体查询
|
- 🔍 **查询系统** - 基于位掩码的高性能实体查询
|
||||||
- 🛠️ **性能监控** - 内置性能监控工具,帮助优化游戏性能
|
- 🛠️ **性能监控** - 内置性能监控工具,帮助优化游戏性能
|
||||||
- 🎯 **对象池** - 内存管理优化,减少垃圾回收压力
|
- 🎯 **对象池** - 内存管理优化,减少垃圾回收压力
|
||||||
- 📊 **数学库** - 完整的 2D 数学运算支持
|
- 🎯 **纯ECS架构** - 专注于实体组件系统核心逻辑
|
||||||
|
|
||||||
## 📦 安装
|
## 📦 安装
|
||||||
|
|
||||||
|
|||||||
4
source/package-lock.json
generated
4
source/package-lock.json
generated
@@ -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,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",
|
||||||
|
|||||||
@@ -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 是否启用
|
||||||
|
|||||||
@@ -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),
|
||||||
|
|||||||
@@ -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,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';
|
||||||
|
|||||||
@@ -1,25 +0,0 @@
|
|||||||
/**
|
|
||||||
* 边缘枚举
|
|
||||||
* 表示矩形的四个边
|
|
||||||
*/
|
|
||||||
export enum Edge {
|
|
||||||
/**
|
|
||||||
* 顶边
|
|
||||||
*/
|
|
||||||
top = 0,
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 底边
|
|
||||||
*/
|
|
||||||
bottom = 1,
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 左边
|
|
||||||
*/
|
|
||||||
left = 2,
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 右边
|
|
||||||
*/
|
|
||||||
right = 3
|
|
||||||
}
|
|
||||||
@@ -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));
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 将数值限制在0到1之间
|
|
||||||
* @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 如果两个数值在容差范围内相等返回true,否则返回false
|
|
||||||
*/
|
|
||||||
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;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -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 如果点在矩形内返回true,否则返回false
|
|
||||||
*/
|
|
||||||
public contains(x: number, y: number): boolean;
|
|
||||||
/**
|
|
||||||
* 检查指定点是否在矩形内
|
|
||||||
* @param point 要检查的点
|
|
||||||
* @returns 如果点在矩形内返回true,否则返回false
|
|
||||||
*/
|
|
||||||
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 如果两个矩形相交返回true,否则返回false
|
|
||||||
*/
|
|
||||||
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 如果矩形为空返回true,否则返回false
|
|
||||||
*/
|
|
||||||
public isEmpty(): boolean {
|
|
||||||
return this.width === 0 && this.height === 0 && this.x === 0 && this.y === 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 比较两个矩形是否相等
|
|
||||||
* @param other 要比较的矩形
|
|
||||||
* @returns 如果两个矩形相等返回true,否则返回false
|
|
||||||
*/
|
|
||||||
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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -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));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,5 +0,0 @@
|
|||||||
// 数学库导出
|
|
||||||
export { Vector2 } from './Vector2';
|
|
||||||
export { MathHelper } from './MathHelper';
|
|
||||||
export { Rectangle } from './Rectangle';
|
|
||||||
export { Edge } from './Edge';
|
|
||||||
@@ -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 如果是水平方向返回true,否则返回false
|
|
||||||
*/
|
|
||||||
public static isHorizontal(self: Edge): boolean {
|
|
||||||
return self == Edge.right || self == Edge.left;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 检查边缘是否为垂直方向(上或下)
|
|
||||||
* @param self 边缘
|
|
||||||
* @returns 如果是垂直方向返回true,否则返回false
|
|
||||||
*/
|
|
||||||
public static isVertical(self: Edge): boolean {
|
|
||||||
return self == Edge.top || self == Edge.bottom;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,4 +1,3 @@
|
|||||||
// 扩展工具类导出
|
// 扩展工具类导出
|
||||||
export { TypeUtils } from './TypeUtils';
|
export { TypeUtils } from './TypeUtils';
|
||||||
export { NumberExtension } from './NumberExtension';
|
export { NumberExtension } from './NumberExtension';
|
||||||
export { EdgeExt } from './EdgeExt';
|
|
||||||
@@ -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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -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';
|
||||||
@@ -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';
|
||||||
|
|||||||
Reference in New Issue
Block a user