From af2464ceb1271091ba39792fd667f54ef5065b16 Mon Sep 17 00:00:00 2001 From: GuoLei1990 Date: Tue, 28 Mar 2023 17:28:26 +0800 Subject: [PATCH] feat: opt ComponentsDependencies --- packages/core/src/Camera.ts | 4 ++-- packages/core/src/ComponentsDependencies.ts | 18 ++++++++++++++++-- packages/core/src/Renderer.ts | 7 ++++--- packages/core/src/physics/Collider.ts | 4 ++-- packages/core/src/physics/joint/Joint.ts | 4 ++-- tests/src/core/CompomentDependencies.test.ts | 2 +- tests/src/core/Script.test.ts | 4 ++-- 7 files changed, 29 insertions(+), 14 deletions(-) diff --git a/packages/core/src/Camera.ts b/packages/core/src/Camera.ts index f207582d1f..93a423cd8b 100644 --- a/packages/core/src/Camera.ts +++ b/packages/core/src/Camera.ts @@ -27,9 +27,9 @@ class MathTemp { /** * Camera component, as the entrance to the three-dimensional world. - * @decorator `@dependentComponents(DependentMode.CheckOnly, Transform)` + * @decorator `@dependentComponents(Transform, DependentMode.CheckOnly)` */ -@dependentComponents(DependentMode.CheckOnly, Transform) +@dependentComponents(Transform, DependentMode.CheckOnly) export class Camera extends Component { /** @internal */ private static _inverseViewMatrixProperty = ShaderProperty.getByName("u_viewInvMat"); diff --git a/packages/core/src/ComponentsDependencies.ts b/packages/core/src/ComponentsDependencies.ts index 7deaedb1e5..fdcb6c741d 100644 --- a/packages/core/src/ComponentsDependencies.ts +++ b/packages/core/src/ComponentsDependencies.ts @@ -85,11 +85,25 @@ export class ComponentsDependencies { } /** - * Declare dependent components. + * Declare dependent component. + * @param component - Dependent component * @param dependentMode - Dependent mode + */ +export function dependentComponents(component: ComponentConstructor, dependentMode?: DependentMode); + +/** + * Declare dependent components. * @param components - Dependent components + * @param dependentMode - Dependent mode */ -export function dependentComponents(dependentMode: DependentMode, ...components: ComponentConstructor[]) { +export function dependentComponents(components: ComponentConstructor[], dependentMode?: DependentMode); + +export function dependentComponents( + componentOrComponents: ComponentConstructor | ComponentConstructor[], + dependentMode: DependentMode = DependentMode.CheckOnly +) { + const components = Array.isArray(componentOrComponents) ? componentOrComponents : [componentOrComponents]; + return function (target: T): void { ComponentsDependencies._dependenciesMap.set(target, { mode: dependentMode, components }); components.forEach((component) => ComponentsDependencies._addInvDependency(component, target)); diff --git a/packages/core/src/Renderer.ts b/packages/core/src/Renderer.ts index 6a6b1de0e0..7558a0666e 100644 --- a/packages/core/src/Renderer.ts +++ b/packages/core/src/Renderer.ts @@ -1,3 +1,4 @@ +// @ts-ignore import { BoundingBox, Matrix, Vector3 } from "@oasis-engine/math"; import { assignmentClone, deepClone, ignoreClone, shallowClone } from "./clone/CloneManager"; import { Component } from "./Component"; @@ -5,7 +6,7 @@ import { dependentComponents, DependentMode } from "./ComponentsDependencies"; import { Entity } from "./Entity"; import { Material } from "./material/Material"; import { RenderContext } from "./RenderPipeline/RenderContext"; -import { Shader, ShaderProperty } from "./shader"; +import { ShaderProperty } from "./shader"; import { ShaderDataGroup } from "./shader/enums/ShaderDataGroup"; import { ShaderData } from "./shader/ShaderData"; import { ShaderMacro } from "./shader/ShaderMacro"; @@ -14,9 +15,9 @@ import { Transform, TransformModifyFlags } from "./Transform"; /** * Basis for all renderers. - * @decorator `@dependentComponents(DependentMode.CheckOnly, Transform)` + * @decorator `@dependentComponents(Transform, DependentMode.CheckOnly)` */ -@dependentComponents(DependentMode.CheckOnly, Transform) +@dependentComponents(Transform, DependentMode.CheckOnly) export class Renderer extends Component { private static _tempVector0 = new Vector3(); diff --git a/packages/core/src/physics/Collider.ts b/packages/core/src/physics/Collider.ts index 44eddf1e2e..72d3652980 100644 --- a/packages/core/src/physics/Collider.ts +++ b/packages/core/src/physics/Collider.ts @@ -9,9 +9,9 @@ import { ColliderShape } from "./shape/ColliderShape"; /** * Base class for all colliders. - * @decorator `@dependentComponents(Transform)` + * @decorator `@dependentComponents(Transform, DependentMode.CheckOnly)` */ -@dependentComponents(DependentMode.CheckOnly, Transform) +@dependentComponents(Transform, DependentMode.CheckOnly) export class Collider extends Component { /** @internal */ @ignoreClone diff --git a/packages/core/src/physics/joint/Joint.ts b/packages/core/src/physics/joint/Joint.ts index 5ad9f1743e..86a353cbad 100644 --- a/packages/core/src/physics/joint/Joint.ts +++ b/packages/core/src/physics/joint/Joint.ts @@ -7,9 +7,9 @@ import { Collider } from "../Collider"; /** * A base class providing common functionality for joints. - * @decorator `@dependentComponents(DependentMode.CheckOnly,Collider)` + * @decorator `@dependentComponents(Collider, DependentMode.CheckOnly)` */ -@dependentComponents(DependentMode.CheckOnly, Collider) +@dependentComponents(Collider, DependentMode.CheckOnly) export class Joint extends Component { protected _connectedCollider = new JointCollider(); protected _collider = new JointCollider(); diff --git a/tests/src/core/CompomentDependencies.test.ts b/tests/src/core/CompomentDependencies.test.ts index 659df2916f..b098686cf9 100644 --- a/tests/src/core/CompomentDependencies.test.ts +++ b/tests/src/core/CompomentDependencies.test.ts @@ -30,7 +30,7 @@ describe("Component dependencies test", function () { }); }); -@dependentComponents(DependentMode.CheckOnly, MeshRenderer) +@dependentComponents(MeshRenderer, DependentMode.CheckOnly) export class CustomScriptA extends Script {} export class CustomScriptB extends CustomScriptA {} diff --git a/tests/src/core/Script.test.ts b/tests/src/core/Script.test.ts index 3f6e9bdd7a..27fd234315 100644 --- a/tests/src/core/Script.test.ts +++ b/tests/src/core/Script.test.ts @@ -252,10 +252,10 @@ describe("Script", () => { }); it("Dependent components", () => { - @dependentComponents(DependentMode.CheckOnly, Camera) + @dependentComponents(Camera, DependentMode.CheckOnly) class CheckScript extends Script {} - @dependentComponents(DependentMode.AutoAdd, Camera) + @dependentComponents(Camera, DependentMode.AutoAdd) class AutoAddScript extends Script {} const engine = new WebGLEngine(document.createElement("canvas"));