测试用例更新
This commit is contained in:
@@ -7,8 +7,12 @@ import { ITimer } from '../src/Utils/Timers/ITimer';
|
|||||||
|
|
||||||
// 测试组件
|
// 测试组件
|
||||||
class TestComponent extends Component {
|
class TestComponent extends Component {
|
||||||
constructor(public value: number = 0) {
|
public value: number;
|
||||||
|
|
||||||
|
constructor(...args: unknown[]) {
|
||||||
super();
|
super();
|
||||||
|
const [value = 0] = args as [number?];
|
||||||
|
this.value = value;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -5,14 +5,20 @@ import { Component } from '../../../src/ECS/Component';
|
|||||||
|
|
||||||
// 测试用组件
|
// 测试用组件
|
||||||
class TestComponent extends Component {
|
class TestComponent extends Component {
|
||||||
constructor(public value: number = 0) {
|
public value: number = 0;
|
||||||
|
|
||||||
|
constructor(...args: unknown[]) {
|
||||||
super();
|
super();
|
||||||
|
if (args.length >= 1) this.value = args[0] as number;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class AnotherTestComponent extends Component {
|
class AnotherTestComponent extends Component {
|
||||||
constructor(public name: string = 'test') {
|
public name: string = 'test';
|
||||||
|
|
||||||
|
constructor(...args: unknown[]) {
|
||||||
super();
|
super();
|
||||||
|
if (args.length >= 1) this.name = args[0] as string;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -9,26 +9,48 @@ import { BigIntFactory } from '../../../src/ECS/Utils/BigIntCompatibility';
|
|||||||
|
|
||||||
// 测试组件类(默认使用原始存储)
|
// 测试组件类(默认使用原始存储)
|
||||||
class TestComponent extends Component {
|
class TestComponent extends Component {
|
||||||
constructor(public value: number = 0) {
|
public value: number;
|
||||||
|
|
||||||
|
constructor(...args: unknown[]) {
|
||||||
super();
|
super();
|
||||||
|
const [value = 0] = args as [number?];
|
||||||
|
this.value = value;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class PositionComponent extends Component {
|
class PositionComponent extends Component {
|
||||||
constructor(public x: number = 0, public y: number = 0) {
|
public x: number;
|
||||||
|
public y: number;
|
||||||
|
|
||||||
|
constructor(...args: unknown[]) {
|
||||||
super();
|
super();
|
||||||
|
const [x = 0, y = 0] = args as [number?, number?];
|
||||||
|
this.x = x;
|
||||||
|
this.y = y;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class VelocityComponent extends Component {
|
class VelocityComponent extends Component {
|
||||||
constructor(public vx: number = 0, public vy: number = 0) {
|
public vx: number;
|
||||||
|
public vy: number;
|
||||||
|
|
||||||
|
constructor(...args: unknown[]) {
|
||||||
super();
|
super();
|
||||||
|
const [vx = 0, vy = 0] = args as [number?, number?];
|
||||||
|
this.vx = vx;
|
||||||
|
this.vy = vy;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class HealthComponent extends Component {
|
class HealthComponent extends Component {
|
||||||
constructor(public health: number = 100, public maxHealth: number = 100) {
|
public health: number;
|
||||||
|
public maxHealth: number;
|
||||||
|
|
||||||
|
constructor(...args: unknown[]) {
|
||||||
super();
|
super();
|
||||||
|
const [health = 100, maxHealth = 100] = args as [number?, number?];
|
||||||
|
this.health = health;
|
||||||
|
this.maxHealth = maxHealth;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -7,20 +7,37 @@ import { EventBus } from '../../../src/ECS/Core/EventBus';
|
|||||||
|
|
||||||
// 测试组件
|
// 测试组件
|
||||||
class TransformComponent extends Component {
|
class TransformComponent extends Component {
|
||||||
constructor(public x: number = 0, public y: number = 0, public rotation: number = 0) {
|
public x: number = 0;
|
||||||
|
public y: number = 0;
|
||||||
|
public rotation: number = 0;
|
||||||
|
|
||||||
|
constructor(...args: unknown[]) {
|
||||||
super();
|
super();
|
||||||
|
if (args.length >= 1) this.x = args[0] as number;
|
||||||
|
if (args.length >= 2) this.y = args[1] as number;
|
||||||
|
if (args.length >= 3) this.rotation = args[2] as number;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class VelocityComponent extends Component {
|
class VelocityComponent extends Component {
|
||||||
constructor(public vx: number = 0, public vy: number = 0) {
|
public vx: number = 0;
|
||||||
|
public vy: number = 0;
|
||||||
|
|
||||||
|
constructor(...args: unknown[]) {
|
||||||
super();
|
super();
|
||||||
|
if (args.length >= 1) this.vx = args[0] as number;
|
||||||
|
if (args.length >= 2) this.vy = args[1] as number;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class HealthComponent extends Component {
|
class HealthComponent extends Component {
|
||||||
constructor(public health: number = 100, public maxHealth: number = 100) {
|
public health: number = 100;
|
||||||
|
public maxHealth: number = 100;
|
||||||
|
|
||||||
|
constructor(...args: unknown[]) {
|
||||||
super();
|
super();
|
||||||
|
if (args.length >= 1) this.health = args[0] as number;
|
||||||
|
if (args.length >= 2) this.maxHealth = args[1] as number;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -4,38 +4,70 @@ import { Component } from '../../../src/ECS/Component';
|
|||||||
|
|
||||||
// 测试组件
|
// 测试组件
|
||||||
class PositionComponent extends Component {
|
class PositionComponent extends Component {
|
||||||
constructor(public x: number = 0, public y: number = 0) {
|
public x: number;
|
||||||
|
public y: number;
|
||||||
|
|
||||||
|
constructor(...args: unknown[]) {
|
||||||
super();
|
super();
|
||||||
|
const [x = 0, y = 0] = args as [number?, number?];
|
||||||
|
this.x = x;
|
||||||
|
this.y = y;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class VelocityComponent extends Component {
|
class VelocityComponent extends Component {
|
||||||
constructor(public vx: number = 0, public vy: number = 0) {
|
public vx: number;
|
||||||
|
public vy: number;
|
||||||
|
|
||||||
|
constructor(...args: unknown[]) {
|
||||||
super();
|
super();
|
||||||
|
const [vx = 0, vy = 0] = args as [number?, number?];
|
||||||
|
this.vx = vx;
|
||||||
|
this.vy = vy;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class HealthComponent extends Component {
|
class HealthComponent extends Component {
|
||||||
constructor(public health: number = 100, public maxHealth: number = 100) {
|
public health: number;
|
||||||
|
public maxHealth: number;
|
||||||
|
|
||||||
|
constructor(...args: unknown[]) {
|
||||||
super();
|
super();
|
||||||
|
const [health = 100, maxHealth = 100] = args as [number?, number?];
|
||||||
|
this.health = health;
|
||||||
|
this.maxHealth = maxHealth;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class RenderComponent extends Component {
|
class RenderComponent extends Component {
|
||||||
constructor(public visible: boolean = true, public color: string = 'white') {
|
public visible: boolean;
|
||||||
|
public color: string;
|
||||||
|
|
||||||
|
constructor(...args: unknown[]) {
|
||||||
super();
|
super();
|
||||||
|
const [visible = true, color = 'white'] = args as [boolean?, string?];
|
||||||
|
this.visible = visible;
|
||||||
|
this.color = color;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class AIComponent extends Component {
|
class AIComponent extends Component {
|
||||||
constructor(public intelligence: number = 50) {
|
public intelligence: number;
|
||||||
|
|
||||||
|
constructor(...args: unknown[]) {
|
||||||
super();
|
super();
|
||||||
|
const [intelligence = 50] = args as [number?];
|
||||||
|
this.intelligence = intelligence;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class PlayerComponent extends Component {
|
class PlayerComponent extends Component {
|
||||||
constructor(public name: string = 'Player') {
|
public name: string;
|
||||||
|
|
||||||
|
constructor(...args: unknown[]) {
|
||||||
super();
|
super();
|
||||||
|
const [name = 'Player'] = args as [string?];
|
||||||
|
this.name = name;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -18,20 +18,36 @@ import { Matcher } from '../../../src/ECS/Utils/Matcher';
|
|||||||
|
|
||||||
// 测试组件
|
// 测试组件
|
||||||
class TestComponent extends Component {
|
class TestComponent extends Component {
|
||||||
constructor(public value: number = 0) {
|
public value: number;
|
||||||
|
|
||||||
|
constructor(...args: unknown[]) {
|
||||||
super();
|
super();
|
||||||
|
const [value = 0] = args as [number?];
|
||||||
|
this.value = value;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class PositionComponent extends Component {
|
class PositionComponent extends Component {
|
||||||
constructor(public x: number = 0, public y: number = 0) {
|
public x: number;
|
||||||
|
public y: number;
|
||||||
|
|
||||||
|
constructor(...args: unknown[]) {
|
||||||
super();
|
super();
|
||||||
|
const [x = 0, y = 0] = args as [number?, number?];
|
||||||
|
this.x = x;
|
||||||
|
this.y = y;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class VelocityComponent extends Component {
|
class VelocityComponent extends Component {
|
||||||
constructor(public vx: number = 0, public vy: number = 0) {
|
public vx: number;
|
||||||
|
public vy: number;
|
||||||
|
|
||||||
|
constructor(...args: unknown[]) {
|
||||||
super();
|
super();
|
||||||
|
const [vx = 0, vy = 0] = args as [number?, number?];
|
||||||
|
this.vx = vx;
|
||||||
|
this.vy = vy;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -5,38 +5,70 @@ import { ComponentRegistry, ComponentType } from '../../../src/ECS/Core/Componen
|
|||||||
|
|
||||||
// 测试组件
|
// 测试组件
|
||||||
class PositionComponent extends Component {
|
class PositionComponent extends Component {
|
||||||
constructor(public x: number = 0, public y: number = 0) {
|
public x: number;
|
||||||
|
public y: number;
|
||||||
|
|
||||||
|
constructor(...args: unknown[]) {
|
||||||
super();
|
super();
|
||||||
|
const [x = 0, y = 0] = args as [number?, number?];
|
||||||
|
this.x = x;
|
||||||
|
this.y = y;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class VelocityComponent extends Component {
|
class VelocityComponent extends Component {
|
||||||
constructor(public vx: number = 0, public vy: number = 0) {
|
public vx: number;
|
||||||
|
public vy: number;
|
||||||
|
|
||||||
|
constructor(...args: unknown[]) {
|
||||||
super();
|
super();
|
||||||
|
const [vx = 0, vy = 0] = args as [number?, number?];
|
||||||
|
this.vx = vx;
|
||||||
|
this.vy = vy;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class HealthComponent extends Component {
|
class HealthComponent extends Component {
|
||||||
constructor(public health: number = 100, public maxHealth: number = 100) {
|
public health: number;
|
||||||
|
public maxHealth: number;
|
||||||
|
|
||||||
|
constructor(...args: unknown[]) {
|
||||||
super();
|
super();
|
||||||
|
const [health = 100, maxHealth = 100] = args as [number?, number?];
|
||||||
|
this.health = health;
|
||||||
|
this.maxHealth = maxHealth;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class RenderComponent extends Component {
|
class RenderComponent extends Component {
|
||||||
constructor(public visible: boolean = true, public layer: number = 0) {
|
public visible: boolean;
|
||||||
|
public layer: number;
|
||||||
|
|
||||||
|
constructor(...args: unknown[]) {
|
||||||
super();
|
super();
|
||||||
|
const [visible = true, layer = 0] = args as [boolean?, number?];
|
||||||
|
this.visible = visible;
|
||||||
|
this.layer = layer;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class AIComponent extends Component {
|
class AIComponent extends Component {
|
||||||
constructor(public behavior: string = 'idle') {
|
public behavior: string;
|
||||||
|
|
||||||
|
constructor(...args: unknown[]) {
|
||||||
super();
|
super();
|
||||||
|
const [behavior = 'idle'] = args as [string?];
|
||||||
|
this.behavior = behavior;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class PhysicsComponent extends Component {
|
class PhysicsComponent extends Component {
|
||||||
constructor(public mass: number = 1.0) {
|
public mass: number;
|
||||||
|
|
||||||
|
constructor(...args: unknown[]) {
|
||||||
super();
|
super();
|
||||||
|
const [mass = 1.0] = args as [number?];
|
||||||
|
this.mass = mass;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -6,26 +6,42 @@ import { Matcher } from '../../../src/ECS/Utils/Matcher';
|
|||||||
|
|
||||||
// 测试组件
|
// 测试组件
|
||||||
class PositionComponent extends Component {
|
class PositionComponent extends Component {
|
||||||
constructor(public x: number = 0, public y: number = 0) {
|
public x: number;
|
||||||
|
public y: number;
|
||||||
|
|
||||||
|
constructor(...args: unknown[]) {
|
||||||
super();
|
super();
|
||||||
|
this.x = (args[0] as number) ?? 0;
|
||||||
|
this.y = (args[1] as number) ?? 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class VelocityComponent extends Component {
|
class VelocityComponent extends Component {
|
||||||
constructor(public vx: number = 0, public vy: number = 0) {
|
public vx: number;
|
||||||
|
public vy: number;
|
||||||
|
|
||||||
|
constructor(...args: unknown[]) {
|
||||||
super();
|
super();
|
||||||
|
this.vx = (args[0] as number) ?? 0;
|
||||||
|
this.vy = (args[1] as number) ?? 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class HealthComponent extends Component {
|
class HealthComponent extends Component {
|
||||||
constructor(public health: number = 100) {
|
public health: number;
|
||||||
|
|
||||||
|
constructor(...args: unknown[]) {
|
||||||
super();
|
super();
|
||||||
|
this.health = (args[0] as number) ?? 100;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class TagComponent extends Component {
|
class TagComponent extends Component {
|
||||||
constructor(public tag: string = '') {
|
public tag: string;
|
||||||
|
|
||||||
|
constructor(...args: unknown[]) {
|
||||||
super();
|
super();
|
||||||
|
this.tag = (args[0] as string) ?? '';
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -5,8 +5,12 @@ import { EntitySystem } from '../../../src/ECS/Systems/EntitySystem';
|
|||||||
import { ComponentTypeManager } from '../../../src/ECS/Utils/ComponentTypeManager';
|
import { ComponentTypeManager } from '../../../src/ECS/Utils/ComponentTypeManager';
|
||||||
|
|
||||||
class TestComponent extends Component {
|
class TestComponent extends Component {
|
||||||
constructor(public value: number = 0) {
|
public value: number = 0;
|
||||||
|
|
||||||
|
constructor(...args: unknown[]) {
|
||||||
super();
|
super();
|
||||||
|
const [value = 0] = args as [number?];
|
||||||
|
this.value = value;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -26,7 +30,7 @@ class TrackingSystem extends EntitySystem {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public override onChanged(entity: Entity): void {
|
public onChanged(entity: Entity): void {
|
||||||
this.onChangedCallCount++;
|
this.onChangedCallCount++;
|
||||||
if (this.isInterestedEntity(entity)) {
|
if (this.isInterestedEntity(entity)) {
|
||||||
if (!this.trackedEntities.includes(entity)) {
|
if (!this.trackedEntities.includes(entity)) {
|
||||||
|
|||||||
@@ -6,20 +6,36 @@ import { Matcher } from '../../../src/ECS/Utils/Matcher';
|
|||||||
|
|
||||||
// 测试组件
|
// 测试组件
|
||||||
class PositionComponent extends Component {
|
class PositionComponent extends Component {
|
||||||
constructor(public x: number = 0, public y: number = 0) {
|
public x: number = 0;
|
||||||
|
public y: number = 0;
|
||||||
|
|
||||||
|
constructor(...args: unknown[]) {
|
||||||
super();
|
super();
|
||||||
|
const [x = 0, y = 0] = args as [number?, number?];
|
||||||
|
this.x = x;
|
||||||
|
this.y = y;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class VelocityComponent extends Component {
|
class VelocityComponent extends Component {
|
||||||
constructor(public vx: number = 0, public vy: number = 0) {
|
public vx: number = 0;
|
||||||
|
public vy: number = 0;
|
||||||
|
|
||||||
|
constructor(...args: unknown[]) {
|
||||||
super();
|
super();
|
||||||
|
const [vx = 0, vy = 0] = args as [number?, number?];
|
||||||
|
this.vx = vx;
|
||||||
|
this.vy = vy;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class HealthComponent extends Component {
|
class HealthComponent extends Component {
|
||||||
constructor(public health: number = 100) {
|
public health: number = 100;
|
||||||
|
|
||||||
|
constructor(...args: unknown[]) {
|
||||||
super();
|
super();
|
||||||
|
const [health = 100] = args as [number?];
|
||||||
|
this.health = health;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -6,8 +6,9 @@ class TestPositionComponent extends Component {
|
|||||||
public x: number = 0;
|
public x: number = 0;
|
||||||
public y: number = 0;
|
public y: number = 0;
|
||||||
|
|
||||||
constructor(x: number = 0, y: number = 0) {
|
constructor(...args: unknown[]) {
|
||||||
super();
|
super();
|
||||||
|
const [x = 0, y = 0] = args as [number?, number?];
|
||||||
this.x = x;
|
this.x = x;
|
||||||
this.y = y;
|
this.y = y;
|
||||||
}
|
}
|
||||||
@@ -16,8 +17,9 @@ class TestPositionComponent extends Component {
|
|||||||
class TestHealthComponent extends Component {
|
class TestHealthComponent extends Component {
|
||||||
public health: number = 100;
|
public health: number = 100;
|
||||||
|
|
||||||
constructor(health: number = 100) {
|
constructor(...args: unknown[]) {
|
||||||
super();
|
super();
|
||||||
|
const [health = 100] = args as [number?];
|
||||||
this.health = health;
|
this.health = health;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -26,8 +28,9 @@ class TestVelocityComponent extends Component {
|
|||||||
public vx: number = 0;
|
public vx: number = 0;
|
||||||
public vy: number = 0;
|
public vy: number = 0;
|
||||||
|
|
||||||
constructor(vx: number = 0, vy: number = 0) {
|
constructor(...args: unknown[]) {
|
||||||
super();
|
super();
|
||||||
|
const [vx = 0, vy = 0] = args as [number?, number?];
|
||||||
this.vx = vx;
|
this.vx = vx;
|
||||||
this.vy = vy;
|
this.vy = vy;
|
||||||
}
|
}
|
||||||
@@ -36,8 +39,9 @@ class TestVelocityComponent extends Component {
|
|||||||
class TestRenderComponent extends Component {
|
class TestRenderComponent extends Component {
|
||||||
public visible: boolean = true;
|
public visible: boolean = true;
|
||||||
|
|
||||||
constructor(visible: boolean = true) {
|
constructor(...args: unknown[]) {
|
||||||
super();
|
super();
|
||||||
|
const [visible = true] = args as [boolean?];
|
||||||
this.visible = visible;
|
this.visible = visible;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,26 +6,46 @@ import { Matcher } from '../../src/ECS/Utils/Matcher';
|
|||||||
|
|
||||||
// 测试组件
|
// 测试组件
|
||||||
class PositionComponent extends Component {
|
class PositionComponent extends Component {
|
||||||
constructor(public x: number = 0, public y: number = 0) {
|
public x: number;
|
||||||
|
public y: number;
|
||||||
|
|
||||||
|
constructor(...args: unknown[]) {
|
||||||
super();
|
super();
|
||||||
|
const [x = 0, y = 0] = args as [number?, number?];
|
||||||
|
this.x = x;
|
||||||
|
this.y = y;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class VelocityComponent extends Component {
|
class VelocityComponent extends Component {
|
||||||
constructor(public vx: number = 0, public vy: number = 0) {
|
public vx: number;
|
||||||
|
public vy: number;
|
||||||
|
|
||||||
|
constructor(...args: unknown[]) {
|
||||||
super();
|
super();
|
||||||
|
const [vx = 0, vy = 0] = args as [number?, number?];
|
||||||
|
this.vx = vx;
|
||||||
|
this.vy = vy;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class HealthComponent extends Component {
|
class HealthComponent extends Component {
|
||||||
constructor(public health: number = 100) {
|
public health: number;
|
||||||
|
|
||||||
|
constructor(...args: unknown[]) {
|
||||||
super();
|
super();
|
||||||
|
const [health = 100] = args as [number?];
|
||||||
|
this.health = health;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class RenderComponent extends Component {
|
class RenderComponent extends Component {
|
||||||
constructor(public visible: boolean = true) {
|
public visible: boolean;
|
||||||
|
|
||||||
|
constructor(...args: unknown[]) {
|
||||||
super();
|
super();
|
||||||
|
const [visible = true] = args as [boolean?];
|
||||||
|
this.visible = visible;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -9,14 +9,22 @@ import { Matcher } from '../../../src/ECS/Utils/Matcher';
|
|||||||
|
|
||||||
// 测试组件
|
// 测试组件
|
||||||
class TestComponent extends Component {
|
class TestComponent extends Component {
|
||||||
constructor(public value: number = 0) {
|
public value: number = 0;
|
||||||
|
|
||||||
|
constructor(...args: unknown[]) {
|
||||||
super();
|
super();
|
||||||
|
const [value = 0] = args as [number?];
|
||||||
|
this.value = value;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class AnotherComponent extends Component {
|
class AnotherComponent extends Component {
|
||||||
constructor(public name: string = 'test') {
|
public name: string = 'test';
|
||||||
|
|
||||||
|
constructor(...args: unknown[]) {
|
||||||
super();
|
super();
|
||||||
|
const [name = 'test'] = args as [string?];
|
||||||
|
this.name = name;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
/**
|
/**
|
||||||
* Matcher完整测试套件
|
* Matcher完整测试套件
|
||||||
* 包含功能测试、性能测试和向后兼容性测试
|
* 测试新的Matcher条件构建功能和QuerySystem集成
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import { Scene } from '../../../src/ECS/Scene';
|
import { Scene } from '../../../src/ECS/Scene';
|
||||||
@@ -10,20 +10,36 @@ import { Matcher } from '../../../src/ECS/Utils/Matcher';
|
|||||||
|
|
||||||
// 测试组件
|
// 测试组件
|
||||||
class Position extends Component {
|
class Position extends Component {
|
||||||
constructor(public x: number = 0, public y: number = 0) {
|
public x: number = 0;
|
||||||
|
public y: number = 0;
|
||||||
|
|
||||||
|
constructor(...args: unknown[]) {
|
||||||
super();
|
super();
|
||||||
|
const [x = 0, y = 0] = args as [number?, number?];
|
||||||
|
this.x = x;
|
||||||
|
this.y = y;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class Velocity extends Component {
|
class Velocity extends Component {
|
||||||
constructor(public vx: number = 0, public vy: number = 0) {
|
public vx: number = 0;
|
||||||
|
public vy: number = 0;
|
||||||
|
|
||||||
|
constructor(...args: unknown[]) {
|
||||||
super();
|
super();
|
||||||
|
const [vx = 0, vy = 0] = args as [number?, number?];
|
||||||
|
this.vx = vx;
|
||||||
|
this.vy = vy;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class Health extends Component {
|
class Health extends Component {
|
||||||
constructor(public hp: number = 100) {
|
public hp: number = 100;
|
||||||
|
|
||||||
|
constructor(...args: unknown[]) {
|
||||||
super();
|
super();
|
||||||
|
const [hp = 100] = args as [number?];
|
||||||
|
this.hp = hp;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -71,293 +87,202 @@ describe('Matcher测试套件', () => {
|
|||||||
scene.end();
|
scene.end();
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('新API测试', () => {
|
describe('Matcher条件构建测试', () => {
|
||||||
test('create()应该创建有效的matcher', () => {
|
test('Matcher.all()应该创建正确的查询条件', () => {
|
||||||
const matcher = Matcher.create(scene.querySystem);
|
const matcher = Matcher.all(Position, Health);
|
||||||
expect(matcher).toBeInstanceOf(Matcher);
|
const condition = matcher.getCondition();
|
||||||
|
|
||||||
|
expect(condition.all).toContain(Position);
|
||||||
|
expect(condition.all).toContain(Health);
|
||||||
|
expect(condition.all.length).toBe(2);
|
||||||
});
|
});
|
||||||
|
|
||||||
test('all()查询应该正确工作', () => {
|
test('Matcher.any()应该创建正确的查询条件', () => {
|
||||||
const matcher = Matcher.create(scene.querySystem)
|
const matcher = Matcher.any(Health, Dead);
|
||||||
.all(Position, Health);
|
const condition = matcher.getCondition();
|
||||||
|
|
||||||
const result = matcher.query();
|
expect(condition.any).toContain(Health);
|
||||||
expect(result).toHaveLength(2);
|
expect(condition.any).toContain(Dead);
|
||||||
expect(result.map(e => e.name).sort()).toEqual(['MovingAlive', 'StillAlive']);
|
expect(condition.any.length).toBe(2);
|
||||||
});
|
});
|
||||||
|
|
||||||
test('any()查询应该正确工作', () => {
|
test('Matcher.none()应该创建正确的查询条件', () => {
|
||||||
const matcher = Matcher.create(scene.querySystem)
|
const matcher = Matcher.none(Dead);
|
||||||
.any(Health, Dead);
|
const condition = matcher.getCondition();
|
||||||
|
|
||||||
const result = matcher.query();
|
expect(condition.none).toContain(Dead);
|
||||||
expect(result).toHaveLength(4); // 所有实体
|
expect(condition.none.length).toBe(1);
|
||||||
});
|
});
|
||||||
|
|
||||||
test('none()查询应该正确工作', () => {
|
test('链式调用应该正确工作', () => {
|
||||||
const matcher = Matcher.create(scene.querySystem)
|
const matcher = Matcher.all(Position)
|
||||||
.all(Position)
|
|
||||||
.none(Dead);
|
|
||||||
|
|
||||||
const result = matcher.query();
|
|
||||||
expect(result).toHaveLength(2);
|
|
||||||
expect(result.map(e => e.name).sort()).toEqual(['MovingAlive', 'StillAlive']);
|
|
||||||
});
|
|
||||||
|
|
||||||
test('复合查询应该正确工作', () => {
|
|
||||||
const matcher = Matcher.create(scene.querySystem)
|
|
||||||
.all(Position)
|
|
||||||
.any(Health, Velocity)
|
.any(Health, Velocity)
|
||||||
.none(Dead);
|
.none(Dead);
|
||||||
|
|
||||||
const result = matcher.query();
|
const condition = matcher.getCondition();
|
||||||
expect(result).toHaveLength(2);
|
expect(condition.all).toContain(Position);
|
||||||
expect(result.map(e => e.name).sort()).toEqual(['MovingAlive', 'StillAlive']);
|
expect(condition.any).toContain(Health);
|
||||||
|
expect(condition.any).toContain(Velocity);
|
||||||
|
expect(condition.none).toContain(Dead);
|
||||||
});
|
});
|
||||||
|
|
||||||
test('matches()应该正确检查单个实体', () => {
|
test('byComponent()应该创建单组件查询条件', () => {
|
||||||
const matcher = Matcher.create(scene.querySystem)
|
const matcher = Matcher.byComponent(Position);
|
||||||
.all(Position, Velocity);
|
const condition = matcher.getCondition();
|
||||||
|
|
||||||
expect(matcher.matches(entities[0])).toBe(true); // MovingAlive
|
expect(condition.component).toBe(Position);
|
||||||
expect(matcher.matches(entities[1])).toBe(false); // StillAlive
|
|
||||||
expect(matcher.matches(entities[2])).toBe(true); // MovingDead
|
|
||||||
expect(matcher.matches(entities[3])).toBe(false); // StillDead
|
|
||||||
});
|
});
|
||||||
|
|
||||||
test('count()和exists()应该正确工作', () => {
|
test('byTag()应该创建标签查询条件', () => {
|
||||||
const matcher = Matcher.create(scene.querySystem)
|
const matcher = Matcher.byTag(123);
|
||||||
.all(Health);
|
const condition = matcher.getCondition();
|
||||||
|
|
||||||
expect(matcher.count()).toBe(2);
|
expect(condition.tag).toBe(123);
|
||||||
expect(matcher.exists()).toBe(true);
|
|
||||||
|
|
||||||
const emptyMatcher = Matcher.create(scene.querySystem)
|
|
||||||
.all(Health, Dead);
|
|
||||||
|
|
||||||
expect(emptyMatcher.count()).toBe(0);
|
|
||||||
expect(emptyMatcher.exists()).toBe(false);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
test('clone()应该创建独立的matcher', () => {
|
test('byName()应该创建名称查询条件', () => {
|
||||||
const baseMatcher = Matcher.create(scene.querySystem)
|
const matcher = Matcher.byName('TestEntity');
|
||||||
.all(Position);
|
const condition = matcher.getCondition();
|
||||||
|
|
||||||
const livingMatcher = baseMatcher.clone()
|
expect(condition.name).toBe('TestEntity');
|
||||||
.all(Health)
|
|
||||||
.none(Dead);
|
|
||||||
|
|
||||||
const deadMatcher = baseMatcher.clone()
|
|
||||||
.all(Dead);
|
|
||||||
|
|
||||||
expect(livingMatcher.count()).toBe(2);
|
|
||||||
expect(deadMatcher.count()).toBe(2);
|
|
||||||
expect(baseMatcher.count()).toBe(4); // 原始matcher不受影响
|
|
||||||
});
|
|
||||||
|
|
||||||
test('reset()应该清空所有条件', () => {
|
|
||||||
const matcher = Matcher.create(scene.querySystem)
|
|
||||||
.all(Position)
|
|
||||||
.any(Health)
|
|
||||||
.none(Dead);
|
|
||||||
|
|
||||||
expect(matcher.count()).toBe(2);
|
|
||||||
|
|
||||||
matcher.reset();
|
|
||||||
expect(matcher.count()).toBe(4); // 所有实体
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('向后兼容性测试', () => {
|
describe('QuerySystem集成测试', () => {
|
||||||
test('empty()和withQuerySystem()应该正常工作', () => {
|
test('使用QuerySystem的queryAll()查询所有匹配实体', () => {
|
||||||
const consoleSpy = jest.spyOn(console, 'warn').mockImplementation();
|
const result = scene.querySystem.queryAll(Position, Health);
|
||||||
|
expect(result.entities.map(e => e.name).sort()).toEqual(['MovingAlive', 'StillAlive']);
|
||||||
|
});
|
||||||
|
|
||||||
const matcher = Matcher.empty()
|
test('使用QuerySystem的queryAny()查询任一匹配实体', () => {
|
||||||
.all(Position, Health)
|
const result = scene.querySystem.queryAny(Health, Dead);
|
||||||
.withQuerySystem(scene.querySystem);
|
expect(result.entities.length).toBe(4); // 所有实体都有Health或Dead
|
||||||
|
});
|
||||||
|
|
||||||
const result = matcher.query();
|
test('使用QuerySystem的queryNone()查询排除实体', () => {
|
||||||
expect(result).toHaveLength(2);
|
const result = scene.querySystem.queryNone(Dead);
|
||||||
|
const aliveEntities = result.entities.filter(e => e.hasComponent(Position));
|
||||||
|
expect(aliveEntities.map(e => e.name).sort()).toEqual(['MovingAlive', 'StillAlive']);
|
||||||
|
});
|
||||||
|
|
||||||
// 应该有deprecation警告
|
test('QuerySystem查询性能统计', () => {
|
||||||
expect(consoleSpy).toHaveBeenCalledWith(
|
scene.querySystem.queryAll(Position, Velocity);
|
||||||
'withQuerySystem() is deprecated. Use Matcher.create(querySystem) instead.'
|
const stats = scene.querySystem.getStats();
|
||||||
|
|
||||||
|
expect(stats.queryStats.totalQueries).toBeGreaterThan(0);
|
||||||
|
expect(stats.queryStats.cacheHits).toBeGreaterThanOrEqual(0);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('实际使用场景测试', () => {
|
||||||
|
test('游戏系统中的移动实体查询', () => {
|
||||||
|
// 查询所有可移动的实体(有位置和速度的)
|
||||||
|
const movableEntities = scene.querySystem.queryAll(Position, Velocity);
|
||||||
|
expect(movableEntities.entities.length).toBe(2); // MovingAlive, MovingDead
|
||||||
|
|
||||||
|
movableEntities.entities.forEach(entity => {
|
||||||
|
const pos = entity.getComponent(Position)!;
|
||||||
|
const vel = entity.getComponent(Velocity)!;
|
||||||
|
expect(pos).toBeDefined();
|
||||||
|
expect(vel).toBeDefined();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
test('游戏系统中的活体实体查询', () => {
|
||||||
|
// 查询所有活体实体(有血量,没有死亡标记的)
|
||||||
|
const aliveEntitiesAll = scene.querySystem.queryAll(Health);
|
||||||
|
const deadEntitiesAll = scene.querySystem.queryAll(Dead);
|
||||||
|
|
||||||
|
expect(aliveEntitiesAll.entities.length).toBe(2); // MovingAlive, StillAlive
|
||||||
|
expect(deadEntitiesAll.entities.length).toBe(2); // MovingDead, StillDead
|
||||||
|
});
|
||||||
|
|
||||||
|
test('复杂查询:查找活着的移动实体', () => {
|
||||||
|
// 首先获取所有有位置和速度的实体
|
||||||
|
const movableEntities = scene.querySystem.queryAll(Position, Velocity);
|
||||||
|
|
||||||
|
// 然后过滤出活着的(有血量的)
|
||||||
|
const aliveMovableEntities = movableEntities.entities.filter(entity =>
|
||||||
|
entity.hasComponent(Health)
|
||||||
);
|
);
|
||||||
|
|
||||||
consoleSpy.mockRestore();
|
expect(aliveMovableEntities.length).toBe(1); // 只有MovingAlive
|
||||||
|
expect(aliveMovableEntities[0].name).toBe('MovingAlive');
|
||||||
});
|
});
|
||||||
|
|
||||||
test('deprecated方法应该工作并显示警告', () => {
|
test('复合查询条件应用', () => {
|
||||||
const consoleSpy = jest.spyOn(console, 'warn').mockImplementation();
|
// 使用Matcher建立复杂条件,然后用QuerySystem执行
|
||||||
|
const matcher = Matcher.all(Position).any(Health, Dead);
|
||||||
|
const condition = matcher.getCondition();
|
||||||
|
|
||||||
const matcher = Matcher.empty()
|
// 这里演示如何用条件,实际执行需要QuerySystem支持复合条件
|
||||||
.all(Position)
|
expect(condition.all).toContain(Position);
|
||||||
.withQuerySystem(scene.querySystem);
|
expect(condition.any).toContain(Health);
|
||||||
|
expect(condition.any).toContain(Dead);
|
||||||
// 测试deprecated方法
|
});
|
||||||
expect(matcher.isInterestedEntity(entities[0])).toBe(true);
|
|
||||||
const result = matcher.queryEntities();
|
|
||||||
expect(result).toHaveLength(4);
|
|
||||||
|
|
||||||
// 测试getter方法
|
|
||||||
expect(matcher.getAllSet()).toEqual([Position]);
|
|
||||||
expect(matcher.getExclusionSet()).toEqual([]);
|
|
||||||
expect(matcher.getOneSet()).toEqual([]);
|
|
||||||
|
|
||||||
// 验证警告
|
|
||||||
expect(consoleSpy).toHaveBeenCalledWith(
|
|
||||||
'isInterestedEntity() is deprecated. Use matches() instead.'
|
|
||||||
);
|
|
||||||
expect(consoleSpy).toHaveBeenCalledWith(
|
|
||||||
'queryEntities() is deprecated. Use query() instead.'
|
|
||||||
);
|
|
||||||
|
|
||||||
consoleSpy.mockRestore();
|
|
||||||
});
|
});
|
||||||
|
|
||||||
test('无QuerySystem时应该抛出错误', () => {
|
describe('性能测试', () => {
|
||||||
const consoleSpy = jest.spyOn(console, 'warn').mockImplementation();
|
test('大量简单查询的性能', () => {
|
||||||
|
const startTime = performance.now();
|
||||||
|
|
||||||
const matcher = Matcher.empty()
|
for (let i = 0; i < 1000; i++) {
|
||||||
.all(Position, Health);
|
scene.querySystem.queryAll(Position);
|
||||||
|
|
||||||
// 应该抛出错误而不是回退
|
|
||||||
expect(() => matcher.matches(entities[0])).toThrow(
|
|
||||||
'Matcher requires QuerySystem. Use Matcher.create(querySystem) or call withQuerySystem() first.'
|
|
||||||
);
|
|
||||||
|
|
||||||
expect(() => matcher.query()).toThrow(
|
|
||||||
'Matcher requires QuerySystem. Use Matcher.create(querySystem) or call withQuerySystem() first.'
|
|
||||||
);
|
|
||||||
|
|
||||||
consoleSpy.mockRestore();
|
|
||||||
});
|
|
||||||
|
|
||||||
test('新旧API应该产生相同结果', () => {
|
|
||||||
const consoleSpy = jest.spyOn(console, 'warn').mockImplementation();
|
|
||||||
|
|
||||||
// 旧API
|
|
||||||
const oldMatcher = Matcher.empty()
|
|
||||||
.all(Position)
|
|
||||||
.exclude(Dead)
|
|
||||||
.withQuerySystem(scene.querySystem);
|
|
||||||
|
|
||||||
// 新API
|
|
||||||
const newMatcher = Matcher.create(scene.querySystem)
|
|
||||||
.all(Position)
|
|
||||||
.none(Dead);
|
|
||||||
|
|
||||||
// 结果应该相同
|
|
||||||
const oldResult = oldMatcher.query().sort((a, b) => a.id - b.id);
|
|
||||||
const newResult = newMatcher.query().sort((a, b) => a.id - b.id);
|
|
||||||
|
|
||||||
expect(oldResult).toEqual(newResult);
|
|
||||||
|
|
||||||
// 单个实体检查也应该相同
|
|
||||||
for (const entity of entities) {
|
|
||||||
expect(oldMatcher.matches(entity)).toBe(newMatcher.matches(entity));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
consoleSpy.mockRestore();
|
const executionTime = performance.now() - startTime;
|
||||||
});
|
expect(executionTime).toBeLessThan(100); // 应该在100ms内完成
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('缓存机制测试', () => {
|
test('复杂查询的性能', () => {
|
||||||
test('条件变更应该使缓存失效', () => {
|
const startTime = performance.now();
|
||||||
const matcher = Matcher.create(scene.querySystem)
|
|
||||||
.all(Position);
|
|
||||||
|
|
||||||
const result1 = matcher.query();
|
for (let i = 0; i < 100; i++) {
|
||||||
|
scene.querySystem.queryAll(Position, Health);
|
||||||
|
scene.querySystem.queryAny(Health, Dead);
|
||||||
|
scene.querySystem.queryNone(Dead);
|
||||||
|
}
|
||||||
|
|
||||||
// 添加条件
|
const executionTime = performance.now() - startTime;
|
||||||
matcher.all(Health);
|
expect(executionTime).toBeLessThan(50);
|
||||||
const result2 = matcher.query();
|
|
||||||
|
|
||||||
// 结果应该不同
|
|
||||||
expect(result2.length).toBeLessThan(result1.length);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
test('QuerySystem版本变更应该使缓存失效', () => {
|
test('不存在组件的查询性能', () => {
|
||||||
const matcher = Matcher.create(scene.querySystem)
|
class NonExistentComponent extends Component {
|
||||||
.all(Position);
|
constructor(...args: unknown[]) {
|
||||||
|
super();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
const result1 = matcher.query();
|
const result = scene.querySystem.queryAll(NonExistentComponent);
|
||||||
|
expect(result.entities.length).toBe(0);
|
||||||
// 添加新实体触发版本变更
|
|
||||||
const newEntity = scene.createEntity('NewEntity');
|
|
||||||
newEntity.addComponent(new Position(100, 100));
|
|
||||||
|
|
||||||
const result2 = matcher.query();
|
|
||||||
|
|
||||||
// 结果应该包含新实体
|
|
||||||
expect(result2.length).toBe(result1.length + 1);
|
|
||||||
});
|
|
||||||
|
|
||||||
test('重复查询应该使用缓存', () => {
|
|
||||||
const matcher = Matcher.create(scene.querySystem)
|
|
||||||
.all(Position);
|
|
||||||
|
|
||||||
const result1 = matcher.query();
|
|
||||||
const result2 = matcher.query();
|
|
||||||
|
|
||||||
// 结果应该相同(功能测试,不测性能)
|
|
||||||
expect(result1).toEqual(result2);
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('边界情况测试', () => {
|
describe('边界情况测试', () => {
|
||||||
test('空条件应该返回所有实体', () => {
|
test('空查询应该返回所有实体', () => {
|
||||||
const matcher = Matcher.create(scene.querySystem);
|
const result = scene.querySystem.queryAll();
|
||||||
const result = matcher.query();
|
expect(result.entities.length).toBe(entities.length);
|
||||||
expect(result.length).toBeGreaterThan(0);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
test('不存在的组件查询应该返回空结果', () => {
|
test('查询不存在的组件应该返回空结果', () => {
|
||||||
class NonExistentComponent extends Component {}
|
class NonExistentComponent extends Component {
|
||||||
|
constructor(...args: unknown[]) {
|
||||||
|
super();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
const matcher = Matcher.create(scene.querySystem)
|
const result = scene.querySystem.queryAll(NonExistentComponent);
|
||||||
.all(NonExistentComponent);
|
expect(result.entities.length).toBe(0);
|
||||||
|
|
||||||
expect(matcher.query()).toEqual([]);
|
|
||||||
expect(matcher.count()).toBe(0);
|
|
||||||
expect(matcher.exists()).toBe(false);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
test('复杂的排除条件应该正确工作', () => {
|
test('Matcher条件构建的边界情况', () => {
|
||||||
const matcher = Matcher.create(scene.querySystem)
|
const emptyMatcher = Matcher.complex();
|
||||||
.all(Position)
|
const condition = emptyMatcher.getCondition();
|
||||||
.none(Health, Dead); // 排除有血量或死亡的
|
|
||||||
|
|
||||||
// 应该没有结果,因为所有有Position的实体都有Health或Dead
|
expect(condition.all.length).toBe(0);
|
||||||
expect(matcher.query()).toEqual([]);
|
expect(condition.any.length).toBe(0);
|
||||||
});
|
expect(condition.none.length).toBe(0);
|
||||||
|
|
||||||
test('toString()应该提供有用的描述', () => {
|
|
||||||
const matcher = Matcher.create(scene.querySystem)
|
|
||||||
.all(Position, Health)
|
|
||||||
.any(Velocity)
|
|
||||||
.none(Dead);
|
|
||||||
|
|
||||||
const description = matcher.toString();
|
|
||||||
expect(description).toContain('all(Position, Health)');
|
|
||||||
expect(description).toContain('any(Velocity)');
|
|
||||||
expect(description).toContain('none(Dead)');
|
|
||||||
});
|
|
||||||
|
|
||||||
test('getCondition()应该返回只读条件', () => {
|
|
||||||
const matcher = Matcher.create(scene.querySystem)
|
|
||||||
.all(Position)
|
|
||||||
.any(Health)
|
|
||||||
.none(Dead);
|
|
||||||
|
|
||||||
const condition = matcher.getCondition();
|
|
||||||
expect(condition.all).toEqual([Position]);
|
|
||||||
expect(condition.any).toEqual([Health]);
|
|
||||||
expect(condition.none).toEqual([Dead]);
|
|
||||||
|
|
||||||
// 修改返回的条件不应该影响原matcher
|
|
||||||
condition.all.push(Velocity as any);
|
|
||||||
expect(matcher.getCondition().all).toEqual([Position]);
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
@@ -3,11 +3,19 @@ import { TypeUtils } from '../../../src/Utils/Extensions/TypeUtils';
|
|||||||
describe('TypeUtils - 类型工具类测试', () => {
|
describe('TypeUtils - 类型工具类测试', () => {
|
||||||
// 测试用的类和对象
|
// 测试用的类和对象
|
||||||
class TestClass {
|
class TestClass {
|
||||||
constructor(public value: number = 0) {}
|
public value: number = 0;
|
||||||
|
|
||||||
|
constructor(...args: unknown[]) {
|
||||||
|
if (args.length >= 1) this.value = args[0] as number;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class AnotherTestClass {
|
class AnotherTestClass {
|
||||||
constructor(public name: string = '') {}
|
public name: string = '';
|
||||||
|
|
||||||
|
constructor(...args: unknown[]) {
|
||||||
|
if (args.length >= 1) this.name = args[0] as string;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function TestFunction() {
|
function TestFunction() {
|
||||||
@@ -106,12 +114,19 @@ describe('TypeUtils - 类型工具类测试', () => {
|
|||||||
|
|
||||||
it('应该处理继承关系', () => {
|
it('应该处理继承关系', () => {
|
||||||
class Parent {
|
class Parent {
|
||||||
constructor(public value: number = 0) {}
|
public value: number = 0;
|
||||||
|
|
||||||
|
constructor(...args: unknown[]) {
|
||||||
|
if (args.length >= 1) this.value = args[0] as number;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class Child extends Parent {
|
class Child extends Parent {
|
||||||
constructor(value: number = 0, public name: string = '') {
|
public name: string = '';
|
||||||
super(value);
|
|
||||||
|
constructor(...args: unknown[]) {
|
||||||
|
super(args[0]);
|
||||||
|
if (args.length >= 2) this.name = args[1] as string;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -232,18 +247,28 @@ describe('TypeUtils - 类型工具类测试', () => {
|
|||||||
|
|
||||||
it('应该能够用于多态类型识别', () => {
|
it('应该能够用于多态类型识别', () => {
|
||||||
class Animal {
|
class Animal {
|
||||||
constructor(public name: string) {}
|
public name: string = '';
|
||||||
|
|
||||||
|
constructor(...args: unknown[]) {
|
||||||
|
if (args.length >= 1) this.name = args[0] as string;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class Dog extends Animal {
|
class Dog extends Animal {
|
||||||
constructor(name: string, public breed: string) {
|
public breed: string = '';
|
||||||
super(name);
|
|
||||||
|
constructor(...args: unknown[]) {
|
||||||
|
super(args[0]);
|
||||||
|
if (args.length >= 2) this.breed = args[1] as string;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class Cat extends Animal {
|
class Cat extends Animal {
|
||||||
constructor(name: string, public color: string) {
|
public color: string = '';
|
||||||
super(name);
|
|
||||||
|
constructor(...args: unknown[]) {
|
||||||
|
super(args[0]);
|
||||||
|
if (args.length >= 2) this.color = args[1] as string;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -11,26 +11,42 @@ import { Matcher } from '../../src/ECS/Utils/Matcher';
|
|||||||
|
|
||||||
// 测试组件
|
// 测试组件
|
||||||
class Position extends Component {
|
class Position extends Component {
|
||||||
constructor(public x: number = 0, public y: number = 0) {
|
public x: number = 0;
|
||||||
|
public y: number = 0;
|
||||||
|
|
||||||
|
constructor(...args: unknown[]) {
|
||||||
super();
|
super();
|
||||||
|
if (args.length >= 1) this.x = args[0] as number;
|
||||||
|
if (args.length >= 2) this.y = args[1] as number;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class Velocity extends Component {
|
class Velocity extends Component {
|
||||||
constructor(public vx: number = 0, public vy: number = 0) {
|
public vx: number = 0;
|
||||||
|
public vy: number = 0;
|
||||||
|
|
||||||
|
constructor(...args: unknown[]) {
|
||||||
super();
|
super();
|
||||||
|
if (args.length >= 1) this.vx = args[0] as number;
|
||||||
|
if (args.length >= 2) this.vy = args[1] as number;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class Health extends Component {
|
class Health extends Component {
|
||||||
constructor(public hp: number = 100) {
|
public hp: number = 100;
|
||||||
|
|
||||||
|
constructor(...args: unknown[]) {
|
||||||
super();
|
super();
|
||||||
|
if (args.length >= 1) this.hp = args[0] as number;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class Weapon extends Component {
|
class Weapon extends Component {
|
||||||
constructor(public damage: number = 10) {
|
public damage: number = 10;
|
||||||
|
|
||||||
|
constructor(...args: unknown[]) {
|
||||||
super();
|
super();
|
||||||
|
if (args.length >= 1) this.damage = args[0] as number;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user