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
package/README.md
CHANGED
|
@@ -1,3 +1,61 @@
|
|
|
1
|
-
#
|
|
1
|
+
# phi-engine
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
The TypeScript SDK for games made with [Phi Engine](https://github.com/alexanderwanyoike/phi-engine).
|
|
4
|
+
|
|
5
|
+
A Phi game is a folder with a `phi.toml` file, scenes in JSON and TypeScript components. The `phi` command runs the game. This package gives the classes and types that the components use.
|
|
6
|
+
|
|
7
|
+
The package contains only TypeScript source files. Phi bundles the scripts of a game when it runs the game, so the package has no build step.
|
|
8
|
+
|
|
9
|
+
## Modules
|
|
10
|
+
|
|
11
|
+
Import each name from its module. The package root `phi-engine` exports nothing.
|
|
12
|
+
|
|
13
|
+
| Module | Exports |
|
|
14
|
+
| ------------------- | ---------------------------------------------------------------------------------------------------------------------- |
|
|
15
|
+
| `phi-engine/scene` | `Component`, `Entity`, `Scene`, `registerComponents`, `Transform2D`, `Transform3D`, `Sprite2D`, `Mesh3D`, `PointLight` |
|
|
16
|
+
| `phi-engine/math` | `Vector2`, `Vector3`, `Color` |
|
|
17
|
+
| `phi-engine/game` | `Game`, `Time` |
|
|
18
|
+
| `phi-engine/input` | `Input` |
|
|
19
|
+
| `phi-engine/audio` | `Audio`, `SoundName` |
|
|
20
|
+
| `phi-engine/render` | `Camera`, `PostProcess` |
|
|
21
|
+
| `phi-engine/ui` | `Layer`, `Row`, `Column`, `Text`, `Button`, `Toggle`, `Slider`, `Choice`, `Bar`, `Icon`, `Spacer`, `Theme`, `Settings` |
|
|
22
|
+
|
|
23
|
+
Use the version of the package that is the same as the version of `phi`. The package and `phi` must use the same script protocol. If the protocols are different, `phi` stops the scripts. The error tells you to run `yarn add phi-engine@<phi version>` or to update `phi`, and then to start the game again.
|
|
24
|
+
|
|
25
|
+
## Install
|
|
26
|
+
|
|
27
|
+
```bash
|
|
28
|
+
yarn add phi-engine
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
## Write a component
|
|
32
|
+
|
|
33
|
+
A component is a class that extends `Component`. Phi calls its lifecycle methods, such as `onUpdate`. Register each component in `scripts/index.ts`.
|
|
34
|
+
|
|
35
|
+
```ts
|
|
36
|
+
import { Component, registerComponents } from 'phi-engine/scene';
|
|
37
|
+
|
|
38
|
+
/** Turns its entity at a constant speed. */
|
|
39
|
+
export class Spinner extends Component {
|
|
40
|
+
public speed = 1.5;
|
|
41
|
+
|
|
42
|
+
override onUpdate(dt: number): void {
|
|
43
|
+
this.transform2d.rotation += this.speed * dt;
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
registerComponents({ Spinner });
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
An entity in a scene uses the component by its name. The `props` set the public fields.
|
|
51
|
+
|
|
52
|
+
```json
|
|
53
|
+
"scripts": [{ "component": "Spinner", "props": { "speed": 3 } }]
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
## Learn more
|
|
57
|
+
|
|
58
|
+
- [Website](https://alexanderwanyoike.github.io/phi-engine/)
|
|
59
|
+
- [Game guide](https://alexanderwanyoike.github.io/phi-engine/guide)
|
|
60
|
+
- [SDK reference](https://alexanderwanyoike.github.io/phi-engine/sdk)
|
|
61
|
+
- [Repository](https://github.com/alexanderwanyoike/phi-engine)
|
package/audio/Audio.ts
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import type { EngineOutputBus } from '../runtime/EngineOutputBus';
|
|
2
|
+
import type { SoundName } from './SoundName';
|
|
3
|
+
|
|
4
|
+
export class AudioService {
|
|
5
|
+
private bus: EngineOutputBus | null = null;
|
|
6
|
+
|
|
7
|
+
/** @internal */
|
|
8
|
+
attach(bus: EngineOutputBus): void {
|
|
9
|
+
this.bus = bus;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
play(sound: SoundName): void {
|
|
13
|
+
this.bus?.playSound(sound);
|
|
14
|
+
}
|
|
15
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export type SoundName = 'shoot' | 'hit' | 'explosion' | 'pickup' | 'dash' | 'level_up' | 'jump';
|
package/audio/index.ts
ADDED
package/game/Game.ts
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import type { EngineOutputBus } from '../runtime/EngineOutputBus';
|
|
2
|
+
import type { PauseReason } from './PauseReason';
|
|
3
|
+
|
|
4
|
+
export class GameService {
|
|
5
|
+
private bus: EngineOutputBus | null = null;
|
|
6
|
+
|
|
7
|
+
private reason: PauseReason | null = null;
|
|
8
|
+
|
|
9
|
+
private quitAllowed = false;
|
|
10
|
+
|
|
11
|
+
/** @internal */
|
|
12
|
+
attach(bus: EngineOutputBus): void {
|
|
13
|
+
this.bus = bus;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
/** @internal */
|
|
17
|
+
sync(pauseReason: PauseReason | null, canQuit: boolean): void {
|
|
18
|
+
this.reason = pauseReason;
|
|
19
|
+
this.quitAllowed = canQuit;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* True where `quit()` closes the game, as in a desktop window. A page cannot close its tab,
|
|
24
|
+
* so on the web it is false. Show a Quit button only when it is true.
|
|
25
|
+
*/
|
|
26
|
+
get canQuit(): boolean {
|
|
27
|
+
return this.quitAllowed;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* True while the game is paused. A paused game runs no fixed updates, its time does not
|
|
32
|
+
* increase, and only components with `runsWhenPaused` get `onUpdate()`.
|
|
33
|
+
*/
|
|
34
|
+
get paused(): boolean {
|
|
35
|
+
return this.reason !== null;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/** Why the game is paused, or null while it runs. */
|
|
39
|
+
get pauseReason(): PauseReason | null {
|
|
40
|
+
return this.reason;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/** Pauses the game. `paused` changes on the next step. */
|
|
44
|
+
pause(): void {
|
|
45
|
+
this.bus?.setPaused(true);
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/** Resumes the game. `paused` changes on the next step. */
|
|
49
|
+
resume(): void {
|
|
50
|
+
this.bus?.setPaused(false);
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/** Closes the game after this step. Where `canQuit` is false, it does nothing. */
|
|
54
|
+
quit(): void {
|
|
55
|
+
this.bus?.quit();
|
|
56
|
+
}
|
|
57
|
+
}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Why the game is paused. `'game'` means that a script called `Game.pause()`, or that the player
|
|
3
|
+
* pressed the `pause` action. `'focus'` means that the window or the page lost the focus, or that
|
|
4
|
+
* the player cannot see it. The game resumes from a focus pause when the focus comes back.
|
|
5
|
+
*/
|
|
6
|
+
export type PauseReason = 'game' | 'focus';
|
package/game/Time.ts
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
export class TimeService {
|
|
2
|
+
public elapsed = 0;
|
|
3
|
+
|
|
4
|
+
public delta = 1 / 60;
|
|
5
|
+
|
|
6
|
+
public fixedDelta = 1 / 60;
|
|
7
|
+
|
|
8
|
+
public frame = 0;
|
|
9
|
+
|
|
10
|
+
/** @internal */
|
|
11
|
+
sync(elapsed: number, delta: number, fixedDelta: number, frame: number): void {
|
|
12
|
+
this.elapsed = elapsed;
|
|
13
|
+
this.delta = delta;
|
|
14
|
+
this.fixedDelta = fixedDelta;
|
|
15
|
+
this.frame = frame;
|
|
16
|
+
}
|
|
17
|
+
}
|
package/game/index.ts
ADDED
package/input/Input.ts
ADDED
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
import { Vector2 } from '../math/Vector2';
|
|
2
|
+
import type { InputDevice } from './InputDevice';
|
|
3
|
+
|
|
4
|
+
export interface InputSnapshotWire {
|
|
5
|
+
actions_pressed: Record<string, boolean>;
|
|
6
|
+
actions_just_pressed: Record<string, boolean>;
|
|
7
|
+
actions_just_released: Record<string, boolean>;
|
|
8
|
+
axes: Record<string, number>;
|
|
9
|
+
keys_down: string[];
|
|
10
|
+
mouse_ndc: [number, number];
|
|
11
|
+
last_device: InputDevice;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Data-driven Input service (Godot `Input` / Unity `InputSystem` style).
|
|
16
|
+
* Queries actions and axes configured in the game's `phi.toml`.
|
|
17
|
+
*/
|
|
18
|
+
export class InputService {
|
|
19
|
+
private snap: InputSnapshotWire = {
|
|
20
|
+
actions_pressed: {},
|
|
21
|
+
actions_just_pressed: {},
|
|
22
|
+
actions_just_released: {},
|
|
23
|
+
axes: {},
|
|
24
|
+
keys_down: [],
|
|
25
|
+
mouse_ndc: [0, 0],
|
|
26
|
+
last_device: 'keyboard',
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
/** @internal */
|
|
30
|
+
sync(snapshot: InputSnapshotWire): void {
|
|
31
|
+
this.snap = snapshot;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
action(name: string): boolean {
|
|
35
|
+
return Boolean(this.snap.actions_pressed[name]);
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
actionJustPressed(name: string): boolean {
|
|
39
|
+
return Boolean(this.snap.actions_just_pressed[name]);
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
actionJustReleased(name: string): boolean {
|
|
43
|
+
return Boolean(this.snap.actions_just_released[name]);
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
axis(name: string): number {
|
|
47
|
+
return this.snap.axes[name] ?? 0;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
vector(axisX: string, axisY: string): Vector2 {
|
|
51
|
+
const v = new Vector2(this.axis(axisX), this.axis(axisY));
|
|
52
|
+
return v.lengthSquared() > 1.0 ? v.normalized() : v;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
keyDown(keyName: string): boolean {
|
|
56
|
+
const target = keyName.toLowerCase();
|
|
57
|
+
return this.snap.keys_down.some((k) => k.toLowerCase() === target);
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* The device of the last press or move of the player: `keyboard` before any input. Use it
|
|
62
|
+
* to show the control help of that device.
|
|
63
|
+
*/
|
|
64
|
+
get lastDevice(): InputDevice {
|
|
65
|
+
return this.snap.last_device;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
mouseNdc(): Vector2 {
|
|
69
|
+
return new Vector2(this.snap.mouse_ndc[0] ?? 0, this.snap.mouse_ndc[1] ?? 0);
|
|
70
|
+
}
|
|
71
|
+
}
|
package/input/index.ts
ADDED
package/math/Color.ts
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
export class Color {
|
|
2
|
+
public r: number;
|
|
3
|
+
|
|
4
|
+
public g: number;
|
|
5
|
+
|
|
6
|
+
public b: number;
|
|
7
|
+
|
|
8
|
+
public a: number;
|
|
9
|
+
|
|
10
|
+
constructor(r = 1, g = 1, b = 1, a = 1) {
|
|
11
|
+
this.r = r;
|
|
12
|
+
this.g = g;
|
|
13
|
+
this.b = b;
|
|
14
|
+
this.a = a;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
static readonly WHITE = new Color(1, 1, 1, 1);
|
|
18
|
+
|
|
19
|
+
static readonly BLACK = new Color(0, 0, 0, 1);
|
|
20
|
+
|
|
21
|
+
static readonly NEON_CYAN = new Color(0.06, 0.94, 1.0, 1.0);
|
|
22
|
+
|
|
23
|
+
static readonly NEON_MAGENTA = new Color(1.0, 0.1, 0.66, 1.0);
|
|
24
|
+
|
|
25
|
+
static readonly NEON_ORANGE = new Color(1.0, 0.38, 0.06, 1.0);
|
|
26
|
+
|
|
27
|
+
static readonly NEON_LIME = new Color(0.22, 1.0, 0.32, 1.0);
|
|
28
|
+
|
|
29
|
+
static readonly NEON_GOLD = new Color(1.0, 0.86, 0.18, 1.0);
|
|
30
|
+
|
|
31
|
+
static readonly DANGER_RED = new Color(1.0, 0.18, 0.28, 1.0);
|
|
32
|
+
|
|
33
|
+
static readonly SLATE_METAL = new Color(0.16, 0.2, 0.28, 1.0);
|
|
34
|
+
|
|
35
|
+
withAlpha(a: number): Color {
|
|
36
|
+
return new Color(this.r, this.g, this.b, a);
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
clone(): Color {
|
|
40
|
+
return new Color(this.r, this.g, this.b, this.a);
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
toArray(): [number, number, number, number] {
|
|
44
|
+
return [this.r, this.g, this.b, this.a];
|
|
45
|
+
}
|
|
46
|
+
}
|
package/math/Vector2.ts
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
export class Vector2 {
|
|
2
|
+
public x: number;
|
|
3
|
+
|
|
4
|
+
public y: number;
|
|
5
|
+
|
|
6
|
+
constructor(x = 0, y = 0) {
|
|
7
|
+
this.x = x;
|
|
8
|
+
this.y = y;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
static readonly ZERO = new Vector2(0, 0);
|
|
12
|
+
|
|
13
|
+
static readonly ONE = new Vector2(1, 1);
|
|
14
|
+
|
|
15
|
+
static readonly RIGHT = new Vector2(1, 0);
|
|
16
|
+
|
|
17
|
+
static readonly UP = new Vector2(0, 1);
|
|
18
|
+
|
|
19
|
+
static fromAngle(radians: number): Vector2 {
|
|
20
|
+
return new Vector2(Math.cos(radians), Math.sin(radians));
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
clone(): Vector2 {
|
|
24
|
+
return new Vector2(this.x, this.y);
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
length(): number {
|
|
28
|
+
return Math.hypot(this.x, this.y);
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
lengthSquared(): number {
|
|
32
|
+
return this.x * this.x + this.y * this.y;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
normalized(): Vector2 {
|
|
36
|
+
const len = this.length();
|
|
37
|
+
return len > 1e-6 ? new Vector2(this.x / len, this.y / len) : new Vector2(0, 0);
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
add(other: Vector2): Vector2 {
|
|
41
|
+
return new Vector2(this.x + other.x, this.y + other.y);
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
sub(other: Vector2): Vector2 {
|
|
45
|
+
return new Vector2(this.x - other.x, this.y - other.y);
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
scale(scalar: number): Vector2 {
|
|
49
|
+
return new Vector2(this.x * scalar, this.y * scalar);
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
distanceTo(other: Vector2): number {
|
|
53
|
+
return Math.hypot(this.x - other.x, this.y - other.y);
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
angle(): number {
|
|
57
|
+
return Math.atan2(this.y, this.x);
|
|
58
|
+
}
|
|
59
|
+
}
|
package/math/Vector3.ts
ADDED
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
export class Vector3 {
|
|
2
|
+
public x: number;
|
|
3
|
+
|
|
4
|
+
public y: number;
|
|
5
|
+
|
|
6
|
+
public z: number;
|
|
7
|
+
|
|
8
|
+
constructor(x = 0, y = 0, z = 0) {
|
|
9
|
+
this.x = x;
|
|
10
|
+
this.y = y;
|
|
11
|
+
this.z = z;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
static readonly ZERO = new Vector3(0, 0, 0);
|
|
15
|
+
|
|
16
|
+
static readonly ONE = new Vector3(1, 1, 1);
|
|
17
|
+
|
|
18
|
+
static readonly UP = new Vector3(0, 1, 0);
|
|
19
|
+
|
|
20
|
+
clone(): Vector3 {
|
|
21
|
+
return new Vector3(this.x, this.y, this.z);
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
length(): number {
|
|
25
|
+
return Math.hypot(this.x, this.y, this.z);
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
normalized(): Vector3 {
|
|
29
|
+
const len = this.length();
|
|
30
|
+
return len > 1e-6
|
|
31
|
+
? new Vector3(this.x / len, this.y / len, this.z / len)
|
|
32
|
+
: new Vector3(0, 0, 0);
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
add(other: Vector3): Vector3 {
|
|
36
|
+
return new Vector3(this.x + other.x, this.y + other.y, this.z + other.z);
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
sub(other: Vector3): Vector3 {
|
|
40
|
+
return new Vector3(this.x - other.x, this.y - other.y, this.z - other.z);
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
scale(scalar: number): Vector3 {
|
|
44
|
+
return new Vector3(this.x * scalar, this.y * scalar, this.z * scalar);
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
distanceTo(other: Vector3): number {
|
|
48
|
+
return Math.hypot(this.x - other.x, this.y - other.y, this.z - other.z);
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
lerp(target: Vector3, t: number): Vector3 {
|
|
52
|
+
const c = Math.max(0, Math.min(1, t));
|
|
53
|
+
return new Vector3(
|
|
54
|
+
this.x + (target.x - this.x) * c,
|
|
55
|
+
this.y + (target.y - this.y) * c,
|
|
56
|
+
this.z + (target.z - this.z) * c,
|
|
57
|
+
);
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
toTuple(): [number, number, number] {
|
|
61
|
+
return [this.x, this.y, this.z];
|
|
62
|
+
}
|
|
63
|
+
}
|
package/math/index.ts
ADDED
package/package.json
CHANGED
|
@@ -1,6 +1,25 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "phi-engine",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.3.0",
|
|
4
|
+
"description": "TypeScript SDK for games made with Phi Engine",
|
|
5
|
+
"license": "MIT OR Apache-2.0",
|
|
6
|
+
"repository": {
|
|
7
|
+
"type": "git",
|
|
8
|
+
"url": "git+https://github.com/alexanderwanyoike/phi-engine.git",
|
|
9
|
+
"directory": "sdk"
|
|
10
|
+
},
|
|
11
|
+
"type": "module",
|
|
12
|
+
"files": [
|
|
13
|
+
"**/*.ts",
|
|
14
|
+
"README.md"
|
|
15
|
+
],
|
|
16
|
+
"exports": {
|
|
17
|
+
"./audio": "./audio/index.ts",
|
|
18
|
+
"./game": "./game/index.ts",
|
|
19
|
+
"./input": "./input/index.ts",
|
|
20
|
+
"./math": "./math/index.ts",
|
|
21
|
+
"./render": "./render/index.ts",
|
|
22
|
+
"./scene": "./scene/index.ts",
|
|
23
|
+
"./ui": "./ui/index.ts"
|
|
24
|
+
}
|
|
25
|
+
}
|
package/render/Camera.ts
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import type { EngineOutputBus } from '../runtime/EngineOutputBus';
|
|
2
|
+
import type { Vector3 } from '../math/Vector3';
|
|
3
|
+
|
|
4
|
+
export class CameraService {
|
|
5
|
+
private bus: EngineOutputBus | null = null;
|
|
6
|
+
|
|
7
|
+
/** @internal */
|
|
8
|
+
attach(bus: EngineOutputBus): void {
|
|
9
|
+
this.bus = bus;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
set2D(x: number, y: number, halfHeight?: number): void {
|
|
13
|
+
this.bus?.setCamera2D(x, y, halfHeight);
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
set3D(position: Vector3, target: Vector3, fovDegrees?: number): void {
|
|
17
|
+
this.bus?.setCamera3D(position.toTuple(), target.toTuple(), fovDegrees);
|
|
18
|
+
}
|
|
19
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import type { EngineOutputBus } from '../runtime/EngineOutputBus';
|
|
2
|
+
|
|
3
|
+
export class PostProcessService {
|
|
4
|
+
private bus: EngineOutputBus | null = null;
|
|
5
|
+
|
|
6
|
+
/** @internal */
|
|
7
|
+
attach(bus: EngineOutputBus): void {
|
|
8
|
+
this.bus = bus;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
set(patch: { bloomIntensity?: number; saturation?: number; exposure?: number }): void {
|
|
12
|
+
this.bus?.setPostProcess(patch);
|
|
13
|
+
}
|
|
14
|
+
}
|
package/render/index.ts
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import type { SoundName } from '../audio/SoundName';
|
|
2
|
+
|
|
3
|
+
export interface EngineOutputBus {
|
|
4
|
+
playSound(sound: SoundName): void;
|
|
5
|
+
setCamera2D(x: number, y: number, halfHeight?: number): void;
|
|
6
|
+
setCamera3D(
|
|
7
|
+
pos: [number, number, number],
|
|
8
|
+
target: [number, number, number],
|
|
9
|
+
fovDegrees?: number,
|
|
10
|
+
): void;
|
|
11
|
+
setPostProcess(patch: { bloomIntensity?: number; saturation?: number; exposure?: number }): void;
|
|
12
|
+
setPaused(paused: boolean): void;
|
|
13
|
+
setTextScale(scale: number): void;
|
|
14
|
+
quit(): void;
|
|
15
|
+
changeScene(path: string): void;
|
|
16
|
+
log(message: string): void;
|
|
17
|
+
}
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
import type { Entity } from '../scene/Entity';
|
|
2
|
+
import type { Color } from '../math/Color';
|
|
3
|
+
|
|
4
|
+
type Value = number | string | boolean;
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* The last values of an entity that Rust and the scripts both had.
|
|
8
|
+
*
|
|
9
|
+
* This runs for each entity in each phase. It reads the fields directly because QuickJS was
|
|
10
|
+
* much slower when it listed the fields or made JSON text.
|
|
11
|
+
*/
|
|
12
|
+
export class EntityValues {
|
|
13
|
+
private readonly values: Value[] = [];
|
|
14
|
+
|
|
15
|
+
private index = 0;
|
|
16
|
+
|
|
17
|
+
private hasChanged = false;
|
|
18
|
+
|
|
19
|
+
/** Stores the current values. Returns true if one is different from the last call. */
|
|
20
|
+
update(entity: Entity): boolean {
|
|
21
|
+
this.index = 0;
|
|
22
|
+
this.readTransform2D(entity);
|
|
23
|
+
this.readSprite2D(entity);
|
|
24
|
+
this.readTransform3D(entity);
|
|
25
|
+
this.readMesh3D(entity);
|
|
26
|
+
this.readPointLight(entity);
|
|
27
|
+
|
|
28
|
+
// An error above keeps the flag, so the next call still reports the change.
|
|
29
|
+
const { hasChanged } = this;
|
|
30
|
+
this.hasChanged = false;
|
|
31
|
+
return hasChanged;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
private readTransform2D({ hasTransform2d, transform2d: t }: Entity): void {
|
|
35
|
+
this.read(hasTransform2d);
|
|
36
|
+
if (!hasTransform2d) return;
|
|
37
|
+
this.read(t.x);
|
|
38
|
+
this.read(t.y);
|
|
39
|
+
this.read(t.rotation);
|
|
40
|
+
this.read(t.scaleX);
|
|
41
|
+
this.read(t.scaleY);
|
|
42
|
+
this.read(t.z);
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
private readSprite2D({ sprite2d: s }: Entity): void {
|
|
46
|
+
this.read(s !== null);
|
|
47
|
+
if (!s) return;
|
|
48
|
+
this.readColor(s.color);
|
|
49
|
+
this.read(s.emissive);
|
|
50
|
+
this.read(s.shape);
|
|
51
|
+
this.read(s.visible);
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
private readTransform3D({ hasTransform3d, transform3d: t }: Entity): void {
|
|
55
|
+
this.read(hasTransform3d);
|
|
56
|
+
if (!hasTransform3d) return;
|
|
57
|
+
this.read(t.x);
|
|
58
|
+
this.read(t.y);
|
|
59
|
+
this.read(t.z);
|
|
60
|
+
this.read(t.rotX);
|
|
61
|
+
this.read(t.rotY);
|
|
62
|
+
this.read(t.rotZ);
|
|
63
|
+
this.read(t.scaleX);
|
|
64
|
+
this.read(t.scaleY);
|
|
65
|
+
this.read(t.scaleZ);
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
private readMesh3D({ mesh3d: m }: Entity): void {
|
|
69
|
+
this.read(m !== null);
|
|
70
|
+
if (!m) return;
|
|
71
|
+
this.read(m.shape);
|
|
72
|
+
this.readColor(m.albedo);
|
|
73
|
+
this.read(m.metallic);
|
|
74
|
+
this.read(m.roughness);
|
|
75
|
+
this.readColor(m.emissive);
|
|
76
|
+
this.read(m.emissiveStrength);
|
|
77
|
+
this.read(m.visible);
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
private readPointLight({ pointLight: p }: Entity): void {
|
|
81
|
+
this.read(p !== null);
|
|
82
|
+
if (!p) return;
|
|
83
|
+
this.readColor(p.color);
|
|
84
|
+
this.read(p.intensity);
|
|
85
|
+
this.read(p.radius);
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
private readColor({ r, g, b, a }: Color): void {
|
|
89
|
+
this.read(r);
|
|
90
|
+
this.read(g);
|
|
91
|
+
this.read(b);
|
|
92
|
+
this.read(a);
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
private read(value: Value): void {
|
|
96
|
+
// NaN is never equal to itself, so Rust receives and reports it in each phase.
|
|
97
|
+
if (this.values[this.index] !== value) {
|
|
98
|
+
this.values[this.index] = value;
|
|
99
|
+
this.hasChanged = true;
|
|
100
|
+
}
|
|
101
|
+
this.index += 1;
|
|
102
|
+
}
|
|
103
|
+
}
|