Skip to content

Commit 5f93c58

Browse files
authored
Refactor Script and useScript types (#72)
* Refactor Script and useScript to enforce type safety for script constructors * Add __name property to AutoRotator class for better identification * Refactor Script and useScript to improve type definitions and remove unused code
1 parent 0a4e7f7 commit 5f93c58

File tree

4 files changed

+29
-12
lines changed

4 files changed

+29
-12
lines changed

packages/lib/src/components/Script.tsx

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,18 @@
1-
import { Script as PcScript } from "playcanvas";
1+
import { AppBase, Entity, Script as PcScript } from "playcanvas";
22
import { useScript } from "../hooks"
33
import { FC, memo, useMemo } from "react";
44
import { shallowEquals } from "../utils/shallow-equals";
55

6+
// type PcScriptWithoutPrivateName = Omit<typeof PcScript, '__name'> & {
7+
// __name: string;
8+
// };
9+
// type PcScriptWithoutPrivateName = {
10+
// new (args: { app: AppBase; entity: Entity; }): PcScript
11+
// __name: string;
12+
// };
13+
614
interface ScriptProps {
7-
script: new (...args: unknown[]) => PcScript;
15+
script: new (args: { app: AppBase; entity: Entity; }) => PcScript;
816
[key: string]: unknown;
917
}
1018

packages/lib/src/hooks/use-script.tsx

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,27 @@
11
import { useEffect, useRef } from 'react';
22
import { useParent } from './use-parent';
33
import { useApp } from './use-app';
4-
import { Application, Entity, Script, ScriptComponent } from 'playcanvas';
4+
import { AppBase, Application, Entity, Script, ScriptComponent } from 'playcanvas';
55

66
const toLowerCamelCase = (str: string) : string => str[0].toLowerCase() + str.substring(1);
77

88
interface Props {
99
[key: string]: unknown;
1010
}
1111

12-
export const useScript = (scriptConstructor: new (...args: unknown[]) => Script, props: Props) : void => {
12+
// type PcScriptWithName = Omit<typeof Script, '__name'> & {
13+
// __name: string;
14+
// } & {
15+
// __name: string,
16+
// name: string
17+
// };
18+
// type PcScriptWithName = {
19+
// new (args: { app: AppBase; entity: Entity; }): Script
20+
// __name: string;
21+
// }
22+
23+
24+
export const useScript = (scriptConstructor: new (args: { app: AppBase; entity: Entity; }) => Script, props: Props) : void => {
1325
const parent: Entity = useParent();
1426
const app: Application = useApp();
1527
const scriptName: string = toLowerCamelCase(scriptConstructor.name);
@@ -29,7 +41,7 @@ export const useScript = (scriptConstructor: new (...args: unknown[]) => Script
2941
if (!scriptRef.current) {
3042
// Create the script instance with the provided attributes
3143
const scriptComponent : ScriptComponent = parent.script as ScriptComponent;
32-
const scriptInstance = scriptComponent.create(scriptConstructor as typeof Script, {
44+
const scriptInstance = scriptComponent.create(scriptConstructor as unknown as typeof Script, {
3345
properties: { ...props },
3446
preloading: false,
3547
});

packages/lib/src/scripts/auto-rotator/auto-rotator.tsx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ const smoothStep = (x: number): number =>
55

66

77
class AutoRotator extends Script {
8-
98
speed: number = 4;
109
pitchSpeed: number = 0;
1110
pitchAmount: number = 1;

packages/lib/src/scripts/orbit-controls/index.tsx

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Entity, Vec3, Script as PcScript } from "playcanvas";
1+
import { Entity, Vec3 } from "playcanvas";
22
import { Script } from "../../components/Script";
33
import { OrbitCamera, OrbitCameraInputMouse, OrbitCameraInputTouch } from "./orbit-camera";
44

@@ -24,8 +24,6 @@ type OrbitControls = OrbitCameraProps & {
2424
touch?: OrbitCameraInputProps;
2525
};
2626

27-
type PcScriptType = new (...args: unknown[]) => PcScript
28-
2927
export const OrbitControls = ({
3028
distanceMax = 20, distanceMin = 18, pitchAngleMax = 90, pitchAngleMin = 0, inertiaFactor = 0.0, focusEntity = null, pivotPoint = new Vec3(), frameOnStart = true, distance = 0,
3129
mouse = { orbitSensitivity: 0.3, distanceSensitivity: 0.15 },
@@ -35,8 +33,8 @@ export const OrbitControls = ({
3533
const orbitCameraProps : OrbitCameraProps = { distanceMax, distanceMin, pitchAngleMax, pitchAngleMin, inertiaFactor, focusEntity, pivotPoint, frameOnStart, distance }
3634

3735
return <>
38-
<Script script={OrbitCamera as PcScriptType} {...orbitCameraProps}/>
39-
<Script script={OrbitCameraInputMouse as PcScriptType} {...mouse}/>
40-
<Script script={OrbitCameraInputTouch as PcScriptType} {...touch} />
36+
<Script script={OrbitCamera} {...orbitCameraProps}/>
37+
<Script script={OrbitCameraInputMouse} {...mouse}/>
38+
<Script script={OrbitCameraInputTouch} {...touch} />
4139
</>
4240
}

0 commit comments

Comments
 (0)