phi-engine 0.0.0-stage → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +60 -2
- package/audio/Audio.ts +3 -0
- package/audio/AudioService.ts +15 -0
- package/audio/SoundName.ts +1 -0
- package/audio/index.ts +3 -0
- package/game/Game.ts +3 -0
- package/game/GameService.ts +57 -0
- package/game/PauseReason.ts +6 -0
- package/game/Time.ts +3 -0
- package/game/TimeService.ts +17 -0
- package/game/index.ts +5 -0
- package/input/Input.ts +3 -0
- package/input/InputDevice.ts +2 -0
- package/input/InputService.ts +71 -0
- package/input/index.ts +3 -0
- package/math/Color.ts +46 -0
- package/math/Vector2.ts +59 -0
- package/math/Vector3.ts +63 -0
- package/math/index.ts +3 -0
- package/package.json +23 -4
- package/render/Camera.ts +3 -0
- package/render/CameraService.ts +19 -0
- package/render/PostProcess.ts +3 -0
- package/render/PostProcessService.ts +14 -0
- package/render/index.ts +4 -0
- package/runtime/EngineOutputBus.ts +17 -0
- package/runtime/EntityValues.ts +103 -0
- package/runtime/PhiRuntime.ts +517 -0
- package/runtime/UiBridge.ts +178 -0
- package/runtime/UiCommand.ts +14 -0
- package/runtime/UiEventWire.ts +6 -0
- package/runtime/ViewportWire.ts +6 -0
- package/runtime/interfaceBridge.ts +3 -0
- package/runtime/protocol.ts +5 -0
- package/runtime/runtime.ts +9 -0
- package/scene/Component.ts +108 -0
- package/scene/Entity.ts +118 -0
- package/scene/Mesh3D.ts +37 -0
- package/scene/PointLight.ts +15 -0
- package/scene/Scene.ts +3 -0
- package/scene/SceneAccessor.ts +45 -0
- package/scene/SceneService.ts +21 -0
- package/scene/Sprite2D.ts +20 -0
- package/scene/Transform2D.ts +43 -0
- package/scene/Transform3D.ts +56 -0
- package/scene/index.ts +16 -0
- package/scene/registerComponents.ts +6 -0
- package/ui/Align.ts +2 -0
- package/ui/Anchor.ts +11 -0
- package/ui/Bar.ts +100 -0
- package/ui/Button.ts +93 -0
- package/ui/Choice.ts +80 -0
- package/ui/Column.ts +10 -0
- package/ui/ComponentUi.ts +70 -0
- package/ui/Container.ts +39 -0
- package/ui/ContainerOptions.ts +18 -0
- package/ui/Control.ts +94 -0
- package/ui/Element.ts +64 -0
- package/ui/ElementKind.ts +12 -0
- package/ui/Group.ts +110 -0
- package/ui/Icon.ts +63 -0
- package/ui/IconShape.ts +1 -0
- package/ui/Layer.ts +105 -0
- package/ui/Row.ts +10 -0
- package/ui/Settings.ts +3 -0
- package/ui/SettingsService.ts +48 -0
- package/ui/Slider.ts +158 -0
- package/ui/Spacer.ts +46 -0
- package/ui/Text.ts +62 -0
- package/ui/TextSize.ts +5 -0
- package/ui/Theme.ts +3 -0
- package/ui/ThemeValues.ts +58 -0
- package/ui/Toggle.ts +46 -0
- package/ui/Viewport.ts +11 -0
- package/ui/WireValue.ts +5 -0
- package/ui/finiteColor.ts +11 -0
- package/ui/finiteNumber.ts +10 -0
- package/ui/index.ts +26 -0
- package/ui/wholeNumber.ts +12 -0
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* An action of the player on one element, as Rust gives it in the step data. The value of a
|
|
3
|
+
* change is a boolean for a toggle, a number for a slider and an option index for a choice.
|
|
4
|
+
*/
|
|
5
|
+
export type UiEventWire =
|
|
6
|
+
{ id: number; kind: 'press' } | { id: number; kind: 'change'; value: boolean | number };
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { PhiRuntime } from './PhiRuntime';
|
|
2
|
+
import { PROTOCOL_VERSION } from './protocol';
|
|
3
|
+
|
|
4
|
+
export const runtime = new PhiRuntime();
|
|
5
|
+
|
|
6
|
+
// The Rust host finds the protocol version and the step function by these global names.
|
|
7
|
+
(globalThis as Record<string, unknown>).__phiProtocolVersion = PROTOCOL_VERSION;
|
|
8
|
+
(globalThis as Record<string, unknown>).__phiStep = (payloadJson: string): string =>
|
|
9
|
+
runtime.step(payloadJson);
|
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
import { ComponentUi } from '../ui/ComponentUi';
|
|
2
|
+
import { interfaceBridge } from '../runtime/interfaceBridge';
|
|
3
|
+
import type { Entity } from './Entity';
|
|
4
|
+
import type { Mesh3D } from './Mesh3D';
|
|
5
|
+
import type { PointLight } from './PointLight';
|
|
6
|
+
import type { SceneAccessor } from './SceneAccessor';
|
|
7
|
+
import type { Sprite2D } from './Sprite2D';
|
|
8
|
+
import type { Transform2D } from './Transform2D';
|
|
9
|
+
import type { Transform3D } from './Transform3D';
|
|
10
|
+
|
|
11
|
+
export type ComponentConstructor<T extends Component = Component> = new () => T;
|
|
12
|
+
|
|
13
|
+
/** A component class, also an abstract one. Lookups need only `instanceof`, not `new`. */
|
|
14
|
+
export type ComponentClass<T extends Component = Component> = abstract new () => T;
|
|
15
|
+
|
|
16
|
+
export interface ComponentSpec<T extends Component = Component> {
|
|
17
|
+
type: ComponentConstructor<T>;
|
|
18
|
+
/** NoInfer makes `type` alone set T, so the props must agree with that component. */
|
|
19
|
+
props?: Partial<NoInfer<T>>;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Base class for all gameplay behaviors attached to an `Entity`
|
|
24
|
+
* (analogous to Unity's `MonoBehaviour` or Godot's node script).
|
|
25
|
+
*
|
|
26
|
+
* Lifecycle methods. Usually `onStart()` runs first. A component can get an update before
|
|
27
|
+
* `onStart()` if another `onStart()` adds its entity, or if a script uses `addComponent()`
|
|
28
|
+
* on an existing entity.
|
|
29
|
+
* 1. `onStart()`: called once when the component enters the scene.
|
|
30
|
+
* 2. `onFixedUpdate(dt)`: called 60 times for each second of game time.
|
|
31
|
+
* 3. `onUpdate(dt)`: called once per rendered frame. While the game is paused, only on the
|
|
32
|
+
* components with `runsWhenPaused`.
|
|
33
|
+
* 4. `onDestroy()`: called before a script removes the owning entity, after another tool
|
|
34
|
+
* removes it, or at the end of the scene. After it, Phi removes the layers of `this.ui`.
|
|
35
|
+
*/
|
|
36
|
+
export abstract class Component {
|
|
37
|
+
/**
|
|
38
|
+
* Set it to true in a subclass to get `onUpdate()` while the game is paused, as a pause menu
|
|
39
|
+
* must. A paused game calls no `onFixedUpdate()`.
|
|
40
|
+
*/
|
|
41
|
+
static runsWhenPaused = false;
|
|
42
|
+
|
|
43
|
+
/** Owning scene entity (assigned automatically by Phi Runtime). */
|
|
44
|
+
public entity!: Entity;
|
|
45
|
+
|
|
46
|
+
/** Active scene query & spawning interface. */
|
|
47
|
+
public scene!: SceneAccessor;
|
|
48
|
+
|
|
49
|
+
private interfaceElements: ComponentUi | null = null;
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* The interface elements of this component. Add a layer with `this.ui.add()`. Phi removes
|
|
53
|
+
* the layers when the component is destroyed.
|
|
54
|
+
*/
|
|
55
|
+
get ui(): ComponentUi {
|
|
56
|
+
this.interfaceElements ??= new ComponentUi(interfaceBridge);
|
|
57
|
+
return this.interfaceElements;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
get transform2d(): Transform2D {
|
|
61
|
+
return this.entity.transform2d;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
get transform3d(): Transform3D {
|
|
65
|
+
return this.entity.transform3d;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
get sprite2d(): Sprite2D | null {
|
|
69
|
+
return this.entity.sprite2d;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
get mesh3d(): Mesh3D | null {
|
|
73
|
+
return this.entity.mesh3d;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
get pointLight(): PointLight | null {
|
|
77
|
+
return this.entity.pointLight;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/** @internal */
|
|
81
|
+
removeUi(): void {
|
|
82
|
+
this.interfaceElements?.removeAll();
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
protected getComponent<T extends Component>(ctor: ComponentClass<T>): T | null {
|
|
86
|
+
return this.entity.getComponent(ctor);
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
protected destroySelf(): void {
|
|
90
|
+
this.entity.destroy();
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
protected log(message: string): void {
|
|
94
|
+
this.entity.log(message);
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/** Called once when this component becomes active in the scene. */
|
|
98
|
+
onStart?(): void;
|
|
99
|
+
|
|
100
|
+
/** Called on fixed-timestep simulation ticks (`Stage::FixedUpdate`, default 60 Hz). */
|
|
101
|
+
onFixedUpdate?(dt: number): void;
|
|
102
|
+
|
|
103
|
+
/** Called once per rendered frame (`Stage::Update`). */
|
|
104
|
+
onUpdate?(dt: number): void;
|
|
105
|
+
|
|
106
|
+
/** Called immediately before the entity is despawned from the scene. */
|
|
107
|
+
onDestroy?(): void;
|
|
108
|
+
}
|
package/scene/Entity.ts
ADDED
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
import type { Component, ComponentClass, ComponentConstructor } from './Component';
|
|
2
|
+
import type { Mesh3D } from './Mesh3D';
|
|
3
|
+
import type { PointLight } from './PointLight';
|
|
4
|
+
import type { SceneAccessor } from './SceneAccessor';
|
|
5
|
+
import type { Sprite2D } from './Sprite2D';
|
|
6
|
+
import { Transform2D } from './Transform2D';
|
|
7
|
+
import { Transform3D } from './Transform3D';
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Live scene entity holding spatial transforms, visual/light components,
|
|
11
|
+
* and attached TypeScript `Component` behaviors.
|
|
12
|
+
*/
|
|
13
|
+
export class Entity {
|
|
14
|
+
public readonly id: number;
|
|
15
|
+
|
|
16
|
+
public name: string;
|
|
17
|
+
|
|
18
|
+
public tag: string;
|
|
19
|
+
|
|
20
|
+
public transform2d: Transform2D;
|
|
21
|
+
|
|
22
|
+
public hasTransform2d: boolean;
|
|
23
|
+
|
|
24
|
+
public sprite2d: Sprite2D | null;
|
|
25
|
+
|
|
26
|
+
public transform3d: Transform3D;
|
|
27
|
+
|
|
28
|
+
public hasTransform3d: boolean;
|
|
29
|
+
|
|
30
|
+
public mesh3d: Mesh3D | null;
|
|
31
|
+
|
|
32
|
+
public pointLight: PointLight | null;
|
|
33
|
+
|
|
34
|
+
/** @internal */
|
|
35
|
+
public readonly components: Component[] = [];
|
|
36
|
+
|
|
37
|
+
private alive = true;
|
|
38
|
+
|
|
39
|
+
private readonly scene: SceneAccessor;
|
|
40
|
+
|
|
41
|
+
private readonly logger: (msg: string) => void;
|
|
42
|
+
|
|
43
|
+
constructor(
|
|
44
|
+
id: number,
|
|
45
|
+
name: string,
|
|
46
|
+
tag: string,
|
|
47
|
+
scene: SceneAccessor,
|
|
48
|
+
logger: (msg: string) => void,
|
|
49
|
+
) {
|
|
50
|
+
this.id = id;
|
|
51
|
+
this.name = name;
|
|
52
|
+
this.tag = tag;
|
|
53
|
+
this.transform2d = new Transform2D();
|
|
54
|
+
this.hasTransform2d = false;
|
|
55
|
+
this.sprite2d = null;
|
|
56
|
+
this.transform3d = new Transform3D();
|
|
57
|
+
this.hasTransform3d = false;
|
|
58
|
+
this.mesh3d = null;
|
|
59
|
+
this.pointLight = null;
|
|
60
|
+
this.scene = scene;
|
|
61
|
+
this.logger = logger;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
get isAlive(): boolean {
|
|
65
|
+
return this.alive;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
addComponent<T extends Component>(ComponentType: ComponentConstructor<T>, props?: Partial<T>): T {
|
|
69
|
+
const instance = new ComponentType();
|
|
70
|
+
instance.entity = this;
|
|
71
|
+
instance.scene = this.scene;
|
|
72
|
+
if (props) {
|
|
73
|
+
Object.assign(instance, props);
|
|
74
|
+
}
|
|
75
|
+
this.components.push(instance);
|
|
76
|
+
return instance;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
getComponent<T extends Component>(ctor: ComponentClass<T>): T | null {
|
|
80
|
+
for (const c of this.components) {
|
|
81
|
+
if (c instanceof ctor) {
|
|
82
|
+
return c;
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
return null;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
destroy(): void {
|
|
89
|
+
if (this.alive) {
|
|
90
|
+
this.scene.destroy(this);
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/**
|
|
95
|
+
* Marks the entity as removed, runs the onDestroy() hook of each component and removes its
|
|
96
|
+
* interface elements. An error in one component does not stop the other components, and
|
|
97
|
+
* the first error is thrown after all of them.
|
|
98
|
+
* @internal
|
|
99
|
+
*/
|
|
100
|
+
markDestroyed(): void {
|
|
101
|
+
this.alive = false;
|
|
102
|
+
const errors: unknown[] = [];
|
|
103
|
+
for (const comp of this.components) {
|
|
104
|
+
try {
|
|
105
|
+
comp.onDestroy?.();
|
|
106
|
+
} catch (error) {
|
|
107
|
+
errors.push(error);
|
|
108
|
+
}
|
|
109
|
+
comp.removeUi();
|
|
110
|
+
}
|
|
111
|
+
if (errors.length > 0) throw errors[0];
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
/** @internal */
|
|
115
|
+
log(message: string): void {
|
|
116
|
+
this.logger(message);
|
|
117
|
+
}
|
|
118
|
+
}
|
package/scene/Mesh3D.ts
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { Color } from '../math/Color';
|
|
2
|
+
|
|
3
|
+
export type MeshShape3D = 'cube' | 'sphere' | 'plane' | 'cylinder' | 'pyramid';
|
|
4
|
+
|
|
5
|
+
export class Mesh3D {
|
|
6
|
+
public shape: MeshShape3D;
|
|
7
|
+
|
|
8
|
+
public albedo: Color;
|
|
9
|
+
|
|
10
|
+
public metallic: number;
|
|
11
|
+
|
|
12
|
+
public roughness: number;
|
|
13
|
+
|
|
14
|
+
public emissive: Color;
|
|
15
|
+
|
|
16
|
+
public emissiveStrength: number;
|
|
17
|
+
|
|
18
|
+
public visible: boolean;
|
|
19
|
+
|
|
20
|
+
constructor(init?: {
|
|
21
|
+
shape?: MeshShape3D;
|
|
22
|
+
albedo?: Color;
|
|
23
|
+
metallic?: number;
|
|
24
|
+
roughness?: number;
|
|
25
|
+
emissive?: Color;
|
|
26
|
+
emissiveStrength?: number;
|
|
27
|
+
visible?: boolean;
|
|
28
|
+
}) {
|
|
29
|
+
this.shape = init?.shape ?? 'cube';
|
|
30
|
+
this.albedo = init?.albedo ? init.albedo.clone() : Color.WHITE.clone();
|
|
31
|
+
this.metallic = init?.metallic ?? 0.2;
|
|
32
|
+
this.roughness = init?.roughness ?? 0.4;
|
|
33
|
+
this.emissive = init?.emissive ? init.emissive.clone() : this.albedo.clone();
|
|
34
|
+
this.emissiveStrength = init?.emissiveStrength ?? 0.0;
|
|
35
|
+
this.visible = init?.visible ?? true;
|
|
36
|
+
}
|
|
37
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { Color } from '../math/Color';
|
|
2
|
+
|
|
3
|
+
export class PointLight {
|
|
4
|
+
public color: Color;
|
|
5
|
+
|
|
6
|
+
public intensity: number;
|
|
7
|
+
|
|
8
|
+
public radius: number;
|
|
9
|
+
|
|
10
|
+
constructor(color: Color = Color.WHITE, intensity = 8.0, radius = 14.0) {
|
|
11
|
+
this.color = color.clone();
|
|
12
|
+
this.intensity = intensity;
|
|
13
|
+
this.radius = radius;
|
|
14
|
+
}
|
|
15
|
+
}
|
package/scene/Scene.ts
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import type { Color } from '../math/Color';
|
|
2
|
+
import type { Component, ComponentClass, ComponentSpec } from './Component';
|
|
3
|
+
import type { Entity } from './Entity';
|
|
4
|
+
import type { MeshShape3D } from './Mesh3D';
|
|
5
|
+
import type { Shape2D } from './Sprite2D';
|
|
6
|
+
import type { Transform2D } from './Transform2D';
|
|
7
|
+
import type { Transform3D } from './Transform3D';
|
|
8
|
+
|
|
9
|
+
/** C has the component type of each spec, so each spec checks its props against its own type. */
|
|
10
|
+
export interface EntityBlueprint<C extends readonly Component[] = Component[]> {
|
|
11
|
+
name?: string;
|
|
12
|
+
tag?: string;
|
|
13
|
+
transform2d?: Partial<Transform2D>;
|
|
14
|
+
sprite2d?: {
|
|
15
|
+
color?: Color;
|
|
16
|
+
emissive?: number;
|
|
17
|
+
shape?: Shape2D;
|
|
18
|
+
visible?: boolean;
|
|
19
|
+
};
|
|
20
|
+
transform3d?: Partial<Transform3D>;
|
|
21
|
+
mesh3d?: {
|
|
22
|
+
shape?: MeshShape3D;
|
|
23
|
+
albedo?: Color;
|
|
24
|
+
metallic?: number;
|
|
25
|
+
roughness?: number;
|
|
26
|
+
emissive?: Color;
|
|
27
|
+
emissiveStrength?: number;
|
|
28
|
+
visible?: boolean;
|
|
29
|
+
};
|
|
30
|
+
pointLight?: {
|
|
31
|
+
color: Color;
|
|
32
|
+
intensity: number;
|
|
33
|
+
radius: number;
|
|
34
|
+
};
|
|
35
|
+
components?: { [K in keyof C]: ComponentSpec<C[K]> };
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
export interface SceneAccessor {
|
|
39
|
+
spawn<const C extends readonly Component[]>(blueprint: EntityBlueprint<C>): Entity;
|
|
40
|
+
destroy(entity: Entity): void;
|
|
41
|
+
findByName(name: string): Entity | null;
|
|
42
|
+
findByTag(tag: string): Entity[];
|
|
43
|
+
findAllWith<T extends Component>(ctor: ComponentClass<T>): T[];
|
|
44
|
+
entityCount(): number;
|
|
45
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import type { EngineOutputBus } from '../runtime/EngineOutputBus';
|
|
2
|
+
|
|
3
|
+
export class SceneService {
|
|
4
|
+
private bus: EngineOutputBus | null = null;
|
|
5
|
+
|
|
6
|
+
/** @internal */
|
|
7
|
+
attach(bus: EngineOutputBus): void {
|
|
8
|
+
this.bus = bus;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Changes to the scene at `path`, such as `'scenes/title.scene.json'`, at the start of the
|
|
13
|
+
* next frame. Each component of the old scene gets `onDestroy()`, and all entities and
|
|
14
|
+
* interface elements go. The new scene then starts as at the start of the game. The path must
|
|
15
|
+
* be a relative path in the project, to a `.scene.json` file in the folder of the entry scene.
|
|
16
|
+
* If the scripts ask for more than one scene in a frame, the last valid scene loads.
|
|
17
|
+
*/
|
|
18
|
+
change(path: string): void {
|
|
19
|
+
this.bus?.changeScene(path);
|
|
20
|
+
}
|
|
21
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { Color } from '../math/Color';
|
|
2
|
+
|
|
3
|
+
export type Shape2D = 'quad' | 'circle' | 'ring' | 'diamond' | 'triangle';
|
|
4
|
+
|
|
5
|
+
export class Sprite2D {
|
|
6
|
+
public color: Color;
|
|
7
|
+
|
|
8
|
+
public emissive: number;
|
|
9
|
+
|
|
10
|
+
public shape: Shape2D;
|
|
11
|
+
|
|
12
|
+
public visible: boolean;
|
|
13
|
+
|
|
14
|
+
constructor(init?: { color?: Color; emissive?: number; shape?: Shape2D; visible?: boolean }) {
|
|
15
|
+
this.color = init?.color ? init.color.clone() : Color.WHITE.clone();
|
|
16
|
+
this.emissive = init?.emissive ?? 1.0;
|
|
17
|
+
this.shape = init?.shape ?? 'quad';
|
|
18
|
+
this.visible = init?.visible ?? true;
|
|
19
|
+
}
|
|
20
|
+
}
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import { Vector2 } from '../math/Vector2';
|
|
2
|
+
|
|
3
|
+
export class Transform2D {
|
|
4
|
+
public x: number;
|
|
5
|
+
|
|
6
|
+
public y: number;
|
|
7
|
+
|
|
8
|
+
public rotation: number;
|
|
9
|
+
|
|
10
|
+
public scaleX: number;
|
|
11
|
+
|
|
12
|
+
public scaleY: number;
|
|
13
|
+
|
|
14
|
+
public z: number;
|
|
15
|
+
|
|
16
|
+
constructor(init?: Partial<Transform2D>) {
|
|
17
|
+
this.x = init?.x ?? 0;
|
|
18
|
+
this.y = init?.y ?? 0;
|
|
19
|
+
this.rotation = init?.rotation ?? 0;
|
|
20
|
+
this.scaleX = init?.scaleX ?? 1;
|
|
21
|
+
this.scaleY = init?.scaleY ?? 1;
|
|
22
|
+
this.z = init?.z ?? 0;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
get position(): Vector2 {
|
|
26
|
+
return new Vector2(this.x, this.y);
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
set position(v: Vector2) {
|
|
30
|
+
this.x = v.x;
|
|
31
|
+
this.y = v.y;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
translate(dx: number, dy: number): void {
|
|
35
|
+
this.x += dx;
|
|
36
|
+
this.y += dy;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
setScale(sx: number, sy?: number): void {
|
|
40
|
+
this.scaleX = sx;
|
|
41
|
+
this.scaleY = sy ?? sx;
|
|
42
|
+
}
|
|
43
|
+
}
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import { Vector3 } from '../math/Vector3';
|
|
2
|
+
|
|
3
|
+
export class Transform3D {
|
|
4
|
+
public x: number;
|
|
5
|
+
|
|
6
|
+
public y: number;
|
|
7
|
+
|
|
8
|
+
public z: number;
|
|
9
|
+
|
|
10
|
+
/** Euler rotation in radians (XYZ). */
|
|
11
|
+
public rotX: number;
|
|
12
|
+
|
|
13
|
+
public rotY: number;
|
|
14
|
+
|
|
15
|
+
public rotZ: number;
|
|
16
|
+
|
|
17
|
+
public scaleX: number;
|
|
18
|
+
|
|
19
|
+
public scaleY: number;
|
|
20
|
+
|
|
21
|
+
public scaleZ: number;
|
|
22
|
+
|
|
23
|
+
constructor(init?: Partial<Transform3D>) {
|
|
24
|
+
this.x = init?.x ?? 0;
|
|
25
|
+
this.y = init?.y ?? 0;
|
|
26
|
+
this.z = init?.z ?? 0;
|
|
27
|
+
this.rotX = init?.rotX ?? 0;
|
|
28
|
+
this.rotY = init?.rotY ?? 0;
|
|
29
|
+
this.rotZ = init?.rotZ ?? 0;
|
|
30
|
+
this.scaleX = init?.scaleX ?? 1;
|
|
31
|
+
this.scaleY = init?.scaleY ?? 1;
|
|
32
|
+
this.scaleZ = init?.scaleZ ?? 1;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
get position(): Vector3 {
|
|
36
|
+
return new Vector3(this.x, this.y, this.z);
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
set position(v: Vector3) {
|
|
40
|
+
this.x = v.x;
|
|
41
|
+
this.y = v.y;
|
|
42
|
+
this.z = v.z;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
translate(dx: number, dy: number, dz: number): void {
|
|
46
|
+
this.x += dx;
|
|
47
|
+
this.y += dy;
|
|
48
|
+
this.z += dz;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
setScale(sx: number, sy?: number, sz?: number): void {
|
|
52
|
+
this.scaleX = sx;
|
|
53
|
+
this.scaleY = sy ?? sx;
|
|
54
|
+
this.scaleZ = sz ?? sx;
|
|
55
|
+
}
|
|
56
|
+
}
|
package/scene/index.ts
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
export {
|
|
2
|
+
Component,
|
|
3
|
+
type ComponentClass,
|
|
4
|
+
type ComponentConstructor,
|
|
5
|
+
type ComponentSpec,
|
|
6
|
+
} from './Component';
|
|
7
|
+
export { Entity } from './Entity';
|
|
8
|
+
export { Mesh3D, type MeshShape3D } from './Mesh3D';
|
|
9
|
+
export { PointLight } from './PointLight';
|
|
10
|
+
export { registerComponents } from './registerComponents';
|
|
11
|
+
export { Scene } from './Scene';
|
|
12
|
+
export type { EntityBlueprint, SceneAccessor } from './SceneAccessor';
|
|
13
|
+
export type { SceneService } from './SceneService';
|
|
14
|
+
export { Sprite2D, type Shape2D } from './Sprite2D';
|
|
15
|
+
export { Transform2D } from './Transform2D';
|
|
16
|
+
export { Transform3D } from './Transform3D';
|
package/ui/Align.ts
ADDED
package/ui/Anchor.ts
ADDED
package/ui/Bar.ts
ADDED
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
import { Element } from './Element';
|
|
2
|
+
import { finiteColor } from './finiteColor';
|
|
3
|
+
import { finiteNumber } from './finiteNumber';
|
|
4
|
+
import { Theme } from './Theme';
|
|
5
|
+
import type { Color } from '../math/Color';
|
|
6
|
+
import type { WireValue } from './WireValue';
|
|
7
|
+
|
|
8
|
+
export interface BarOptions {
|
|
9
|
+
/** The filled part, from 0 to 1. The default is 1. */
|
|
10
|
+
value?: number;
|
|
11
|
+
/** The default is `Theme.barWidth`. */
|
|
12
|
+
width?: number;
|
|
13
|
+
/** The default is `Theme.barHeight`. */
|
|
14
|
+
height?: number;
|
|
15
|
+
/** The default is `Theme.barFill`. */
|
|
16
|
+
fill?: Color;
|
|
17
|
+
/** The default is `Theme.barTrack`. */
|
|
18
|
+
track?: Color;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/** A bar that shows a value from 0 to 1, such as the oil of a lantern. Lengths are in UI units. */
|
|
22
|
+
export class Bar extends Element {
|
|
23
|
+
private level: number;
|
|
24
|
+
|
|
25
|
+
private widthValue: number;
|
|
26
|
+
|
|
27
|
+
private heightValue: number;
|
|
28
|
+
|
|
29
|
+
private fillValue: Color;
|
|
30
|
+
|
|
31
|
+
private trackValue: Color;
|
|
32
|
+
|
|
33
|
+
constructor(options: BarOptions = {}) {
|
|
34
|
+
super('bar');
|
|
35
|
+
this.level = finiteNumber('Bar.value', options.value ?? 1);
|
|
36
|
+
this.widthValue = finiteNumber('Bar.width', options.width ?? Theme.barWidth);
|
|
37
|
+
this.heightValue = finiteNumber('Bar.height', options.height ?? Theme.barHeight);
|
|
38
|
+
this.fillValue = finiteColor('Bar.fill', options.fill ?? Theme.barFill);
|
|
39
|
+
this.trackValue = finiteColor('Bar.track', options.track ?? Theme.barTrack);
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/** The filled part, from 0 to 1. */
|
|
43
|
+
get value(): number {
|
|
44
|
+
return this.level;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
set value(value: number) {
|
|
48
|
+
this.level = finiteNumber('Bar.value', value);
|
|
49
|
+
this.changed();
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
get width(): number {
|
|
53
|
+
return this.widthValue;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
set width(value: number) {
|
|
57
|
+
this.widthValue = finiteNumber('Bar.width', value);
|
|
58
|
+
this.changed();
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
get height(): number {
|
|
62
|
+
return this.heightValue;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
set height(value: number) {
|
|
66
|
+
this.heightValue = finiteNumber('Bar.height', value);
|
|
67
|
+
this.changed();
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/** A copy of the fill colour. Set a new colour to change it. */
|
|
71
|
+
get fill(): Color {
|
|
72
|
+
return this.fillValue.clone();
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
set fill(value: Color) {
|
|
76
|
+
this.fillValue = finiteColor('Bar.fill', value);
|
|
77
|
+
this.changed();
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/** A copy of the track colour. Set a new colour to change it. */
|
|
81
|
+
get track(): Color {
|
|
82
|
+
return this.trackValue.clone();
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
set track(value: Color) {
|
|
86
|
+
this.trackValue = finiteColor('Bar.track', value);
|
|
87
|
+
this.changed();
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/** @internal */
|
|
91
|
+
wireProps(): Record<string, WireValue> {
|
|
92
|
+
return {
|
|
93
|
+
value: this.level,
|
|
94
|
+
width: this.widthValue,
|
|
95
|
+
height: this.heightValue,
|
|
96
|
+
fill: this.fillValue.toArray(),
|
|
97
|
+
track: this.trackValue.toArray(),
|
|
98
|
+
};
|
|
99
|
+
}
|
|
100
|
+
}
|