Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor Script and useScript types #72

Merged
merged 5 commits into from
Feb 19, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions packages/lib/src/components/Script.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,18 @@
import { Script as PcScript } from "playcanvas";
import { AppBase, Entity, Script as PcScript } from "playcanvas";
import { useScript } from "../hooks"
import { FC, memo, useMemo } from "react";
import { shallowEquals } from "../utils/shallow-equals";

// type PcScriptWithoutPrivateName = Omit<typeof PcScript, '__name'> & {
// __name: string;
// };
// type PcScriptWithoutPrivateName = {
// new (args: { app: AppBase; entity: Entity; }): PcScript
// __name: string;
// };

interface ScriptProps {
script: new (...args: unknown[]) => PcScript;
script: new (args: { app: AppBase; entity: Entity; }) => PcScript;
[key: string]: unknown;
}

Expand Down
18 changes: 15 additions & 3 deletions packages/lib/src/hooks/use-script.tsx
Original file line number Diff line number Diff line change
@@ -1,15 +1,27 @@
import { useEffect, useRef } from 'react';
import { useParent } from './use-parent';
import { useApp } from './use-app';
import { Application, Entity, Script, ScriptComponent } from 'playcanvas';
import { AppBase, Application, Entity, Script, ScriptComponent } from 'playcanvas';

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

interface Props {
[key: string]: unknown;
}

export const useScript = (scriptConstructor: new (...args: unknown[]) => Script, props: Props) : void => {
// type PcScriptWithName = Omit<typeof Script, '__name'> & {
// __name: string;
// } & {
// __name: string,
// name: string
// };
// type PcScriptWithName = {
// new (args: { app: AppBase; entity: Entity; }): Script
// __name: string;
// }


export const useScript = (scriptConstructor: new (args: { app: AppBase; entity: Entity; }) => Script, props: Props) : void => {
const parent: Entity = useParent();
const app: Application = useApp();
const scriptName: string = toLowerCamelCase(scriptConstructor.name);
Expand All @@ -29,7 +41,7 @@ export const useScript = (scriptConstructor: new (...args: unknown[]) => Script
if (!scriptRef.current) {
// Create the script instance with the provided attributes
const scriptComponent : ScriptComponent = parent.script as ScriptComponent;
const scriptInstance = scriptComponent.create(scriptConstructor as typeof Script, {
const scriptInstance = scriptComponent.create(scriptConstructor as unknown as typeof Script, {
properties: { ...props },
preloading: false,
});
Expand Down
1 change: 0 additions & 1 deletion packages/lib/src/scripts/auto-rotator/auto-rotator.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ const smoothStep = (x: number): number =>


class AutoRotator extends Script {

speed: number = 4;
pitchSpeed: number = 0;
pitchAmount: number = 1;
Expand Down
10 changes: 4 additions & 6 deletions packages/lib/src/scripts/orbit-controls/index.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Entity, Vec3, Script as PcScript } from "playcanvas";
import { Entity, Vec3 } from "playcanvas";
import { Script } from "../../components/Script";
import { OrbitCamera, OrbitCameraInputMouse, OrbitCameraInputTouch } from "./orbit-camera";

Expand All @@ -24,8 +24,6 @@ type OrbitControls = OrbitCameraProps & {
touch?: OrbitCameraInputProps;
};

type PcScriptType = new (...args: unknown[]) => PcScript

export const OrbitControls = ({
distanceMax = 20, distanceMin = 18, pitchAngleMax = 90, pitchAngleMin = 0, inertiaFactor = 0.0, focusEntity = null, pivotPoint = new Vec3(), frameOnStart = true, distance = 0,
mouse = { orbitSensitivity: 0.3, distanceSensitivity: 0.15 },
Expand All @@ -35,8 +33,8 @@ export const OrbitControls = ({
const orbitCameraProps : OrbitCameraProps = { distanceMax, distanceMin, pitchAngleMax, pitchAngleMin, inertiaFactor, focusEntity, pivotPoint, frameOnStart, distance }

return <>
<Script script={OrbitCamera as PcScriptType} {...orbitCameraProps}/>
<Script script={OrbitCameraInputMouse as PcScriptType} {...mouse}/>
<Script script={OrbitCameraInputTouch as PcScriptType} {...touch} />
<Script script={OrbitCamera} {...orbitCameraProps}/>
<Script script={OrbitCameraInputMouse} {...mouse}/>
<Script script={OrbitCameraInputTouch} {...touch} />
</>
}
Loading