lecodes-sdk 0.20.2 → 1.0.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.
Files changed (77) hide show
  1. package/dist/global.d.ts +17 -5
  2. package/dist/types/canvas/Canvas.d.ts +2 -0
  3. package/dist/types/gl/DecalSet.d.ts +48 -3
  4. package/dist/types/gl/Geometry.d.ts +12 -0
  5. package/dist/types/gl/Lightmap.d.ts +9 -0
  6. package/dist/types/gl/Material.d.ts +4 -0
  7. package/dist/types/gl/Mesh.d.ts +7 -1
  8. package/dist/types/gl/Model.d.ts +34 -0
  9. package/dist/types/gl/Particles.d.ts +13 -0
  10. package/dist/types/gl/Scene.d.ts +1 -1
  11. package/dist/types/gl/animation/DynamicBone.d.ts +173 -0
  12. package/dist/types/gl/{IK.d.ts → animation/IK.d.ts} +4 -4
  13. package/dist/types/gl/{Locomotion.d.ts → animation/Locomotion.d.ts} +6 -6
  14. package/dist/types/gl/{AudioSource.d.ts → audio/AudioSource.d.ts} +6 -6
  15. package/dist/types/gl/{AudioZone.d.ts → audio/AudioZone.d.ts} +4 -4
  16. package/dist/types/gl/{SceneAudio.d.ts → audio/SceneAudio.d.ts} +1 -1
  17. package/dist/types/gl/{NavAgent.d.ts → nav/NavAgent.d.ts} +4 -4
  18. package/dist/types/gl/{NavMesh.d.ts → nav/NavMesh.d.ts} +4 -4
  19. package/dist/types/gl/{CharacterController.d.ts → physics/CharacterController.d.ts} +5 -5
  20. package/dist/types/gl/{Physics.d.ts → physics/Physics.d.ts} +6 -5
  21. package/dist/types/gl/{Ragdoll.d.ts → physics/Ragdoll.d.ts} +4 -4
  22. package/dist/types/gl/{Shape.d.ts → physics/Shape.d.ts} +3 -3
  23. package/dist/types/gl/{Trigger.d.ts → physics/Trigger.d.ts} +2 -2
  24. package/dist/types/gl/{Terrain.d.ts → terrain/Terrain.d.ts} +6 -6
  25. package/dist/types/gl/{terrainMesh.d.ts → terrain/terrainMesh.d.ts} +1 -1
  26. package/dist/types/gl/vehicle/Vehicle.d.ts +300 -0
  27. package/dist/types/gl/vehicle/Wheel.d.ts +147 -0
  28. package/dist/types/inject.d.ts +28 -26
  29. package/dist/types/runtime/files.d.ts +24 -1
  30. package/dist/types/scene/defineScene.d.ts +10 -3
  31. package/dist/types.json +1 -1
  32. package/package.json +1 -1
  33. package/prompts/README.md +142 -142
  34. package/src/bridges.d.ts +1599 -1481
  35. package/src/canvas/Canvas.ts +21 -0
  36. package/src/compile/__tests__/compile.test.ts +11 -0
  37. package/src/compile/header.ts +6 -3
  38. package/src/compile/index.ts +1 -0
  39. package/src/compile/sceneEditor.ts +42 -1
  40. package/src/gl/DecalSet.ts +132 -5
  41. package/src/gl/Geometry.ts +348 -284
  42. package/src/gl/Lightmap.ts +32 -5
  43. package/src/gl/Material.ts +21 -0
  44. package/src/gl/Mesh.ts +120 -102
  45. package/src/gl/Model.ts +33 -1
  46. package/src/gl/Node.ts +1 -1
  47. package/src/gl/Particles.ts +21 -3
  48. package/src/gl/Scene.ts +1 -1
  49. package/src/gl/animation/DynamicBone.ts +459 -0
  50. package/src/gl/{IK.ts → animation/IK.ts} +4 -4
  51. package/src/gl/{Locomotion.ts → animation/Locomotion.ts} +7 -7
  52. package/src/gl/{AudioSource.ts → audio/AudioSource.ts} +7 -7
  53. package/src/gl/{AudioZone.ts → audio/AudioZone.ts} +75 -75
  54. package/src/gl/{SceneAudio.ts → audio/SceneAudio.ts} +2 -2
  55. package/src/gl/{NavAgent.ts → nav/NavAgent.ts} +5 -5
  56. package/src/gl/{NavMesh.ts → nav/NavMesh.ts} +8 -8
  57. package/src/gl/{CharacterController.ts → physics/CharacterController.ts} +5 -5
  58. package/src/gl/{Physics.ts → physics/Physics.ts} +12 -5
  59. package/src/gl/{Ragdoll.ts → physics/Ragdoll.ts} +8 -6
  60. package/src/gl/{Shape.ts → physics/Shape.ts} +3 -3
  61. package/src/gl/{Trigger.ts → physics/Trigger.ts} +45 -45
  62. package/src/gl/{physicsEvents.ts → physics/physicsEvents.ts} +1 -1
  63. package/src/gl/{Terrain.ts → terrain/Terrain.ts} +10 -10
  64. package/src/gl/{terrainMesh.ts → terrain/terrainMesh.ts} +1 -1
  65. package/src/gl/vehicle/Vehicle.ts +666 -0
  66. package/src/gl/vehicle/Wheel.ts +290 -0
  67. package/src/inject.ts +226 -224
  68. package/src/runtime/files.ts +32 -2
  69. package/src/scene/defineScene.ts +26 -10
  70. package/src/scene/level.ts +2 -2
  71. package/dist/types/gl/Gearbox.d.ts +0 -86
  72. package/dist/types/gl/Vehicle.d.ts +0 -191
  73. package/dist/types/gl/Wheel.d.ts +0 -95
  74. package/src/gl/Gearbox.ts +0 -212
  75. package/src/gl/Vehicle.ts +0 -473
  76. package/src/gl/Wheel.ts +0 -240
  77. /package/dist/types/gl/{physicsEvents.d.ts → physics/physicsEvents.d.ts} +0 -0
package/src/gl/Gearbox.ts DELETED
@@ -1,212 +0,0 @@
1
- // The gearbox of a `Vehicle`, as an aspect on the same node. It is deliberately NOT in the engine:
2
- // picking a gear is policy, not physics — two RPM thresholds and a latency timer — and every game
3
- // wants its own. The engine keeps only the physical half (the clutch's stiffness and the coupled
4
- // engine/wheel solve), and is told one ratio and one clutch scalar per frame.
5
- //
6
- // car.aspect(Gearbox, { ratios: [2.66, 1.78, 1.3, 1.0, 0.74], reverse: -2.9 })
7
- //
8
- // A `Vehicle` with no `Gearbox` attaches this one with its defaults, so a car that never mentions
9
- // gears still drives.
10
- //
11
- // Custom behaviour is a subclass overriding ONE method:
12
- //
13
- // class SportBox extends Gearbox {
14
- // select(v: Vehicle): number { return v.rpm > 6200 ? this.gear + 1 : this.gear }
15
- // }
16
- //
17
- // The clutch is two things multiplied. The SHIFT envelope (`shiftTime`/`clutchTime`) stays with the
18
- // base class on purpose: a forgotten ramp breaks a car silently — no drive at all, or a slammed
19
- // clutch. The TAKE-UP (`takeUp`) is the left-foot half, and is overridable for a car that launches
20
- // unusually — a torque converter, a rally launch control.
21
-
22
- import { Aspect } from "../core/Aspect"
23
- import type { FieldMeta } from "../core/fields"
24
- import type { Node } from "./Node"
25
- import type { Vehicle } from "./Vehicle"
26
-
27
- // How far above idle counts as "revs to spare". A simulated engine cannot fall below its idle — the
28
- // physics clamps it — so being AT idle is the observable form of "the wheels are dragging me under".
29
- // This is the tolerance for reading that clamp, not a tuning knob.
30
- const IDLE_MARGIN = 1.05
31
-
32
- // The car this gearbox belongs to. Read through the aspect accessor rather than node.get(Vehicle),
33
- // so this file needs Vehicle only as a TYPE and the two do not import each other at runtime.
34
- const carOf = (n: Node): Vehicle | null => (n as unknown as { vehicle?: Vehicle }).vehicle ?? null
35
-
36
- export class Gearbox extends Aspect<"gearbox", Node> {
37
- static readonly aspect = "gearbox"
38
-
39
- /** Forward gear ratios, first gear first. Engine RPM = wheel speed × ratio × the differential. */
40
- ratios: number[] = [ 2.66, 1.78, 1.3, 1.0, 0.74 ]
41
- /** Reverse ratio — negative, because a ratio's SIGN is what reverse means to the engine. */
42
- reverse = -2.9
43
- /** Shift up when the engine passes this fraction of its redline. */
44
- shiftUp = 0.67
45
- /** Shift down when it falls below this fraction. */
46
- shiftDown = 0.33
47
- /** How long a shift takes, in seconds — the clutch is out for this long. */
48
- shiftTime = 0.2
49
- /** How long the clutch takes to re-engage afterwards. */
50
- clutchTime = 0.3
51
- /** How long to wait after a shift before another is allowed — without it a car sitting on a
52
- * threshold hunts between two gears. */
53
- latency = 0.5
54
-
55
- /** How far the clutch bites the instant drive is asked for from rest, 0..1. It only has to COUPLE
56
- * the shafts — how much torque that passes is the engine's business, so a feathered throttle
57
- * still pulls away gently at the same bite point. */
58
- bite = 0.4
59
-
60
- static fields: FieldMeta<Gearbox> = {
61
- reverse: { max: -0.1, step: 0.1 },
62
- shiftUp: { min: 0.1, max: 1, step: 0.01 },
63
- shiftDown: { min: 0, max: 0.9, step: 0.01 },
64
- shiftTime: { min: 0, max: 3, step: 0.05 },
65
- clutchTime: { min: 0, max: 3, step: 0.05 },
66
- latency: { min: 0, max: 3, step: 0.05 },
67
- bite: { min: 0, max: 1, step: 0.05 },
68
- ratios: { hidden: true },
69
- }
70
-
71
- /** Current gear: −1 reverse, 0 neutral, 1… forward. Read it for a HUD. */
72
- gear = 0
73
- /** True while a shift is in progress — the clutch is out and the engine is not driving the wheels. */
74
- get shifting(): boolean { return this._shiftLeft > 0 }
75
-
76
- private _shiftLeft = 0 // seconds of clutch-out left
77
- private _engageLeft = 0 // seconds of clutch re-engagement left
78
- private _waitLeft = 0 // anti-hunting
79
- private _take = 0 // how far the CAR's own state wants the clutch in, latched per frame
80
-
81
- /** EARLY phase: the pair has to be latched before this frame's step consumes it. */
82
- updateBefore(dt: number): void {
83
- const v = carOf(this.node)
84
- if (!v || !v.id) return
85
-
86
- this._waitLeft = Math.max(0, this._waitLeft - dt)
87
-
88
- if (this._shiftLeft > 0) {
89
- // Mid-shift: clutch fully out, and the moment it ends the re-engagement ramp starts.
90
- this._shiftLeft = Math.max(0, this._shiftLeft - dt)
91
- if (this._shiftLeft === 0) this._engageLeft = this.clutchTime
92
- } else {
93
- const want = this.select(v)
94
- if (want !== this.gear) this.shift(want)
95
- else this._engageLeft = Math.max(0, this._engageLeft - dt)
96
- }
97
-
98
- // The pedal only moves while no shift is in flight. During one the revs say nothing about
99
- // whether the wheels can sustain them: an engine still spinning down from the last gear reads as
100
- // "revs to spare" and would drop the clutch straight back in — enough to shove a stopped car
101
- // several km/h with no throttle at all. Freezing it is what a driver does anyway; the foot is
102
- // already down, and the envelope owns the clutch until the gear is in.
103
- if (this._shiftLeft === 0 && this._engageLeft === 0) {
104
- this._take = Math.max(0, Math.min(1, this.takeUp(v, dt)))
105
- }
106
-
107
- v._setTransmission(this.ratio, this.clutch)
108
- }
109
-
110
- /**
111
- * The driver's left foot: where the clutch pedal is, ignoring shifts. Returns the new engagement.
112
- *
113
- * A driver does not compute anything. They watch the tacho: **revs sinking to idle means the
114
- * wheels are about to drag the engine under, and that is when you push the clutch in.** That
115
- * single test replaces every ratio and threshold — the crossover it finds is the exact speed where
116
- * engine braking stops and the engine would start PUSHING instead, and it finds it per gear
117
- * without being told the gear.
118
- *
119
- * The pedal comes back UP only for the throttle. Healthy revs are not on their own a reason to
120
- * engage: a car standing still shows healthy revs too — the wheels simply are not turning to say
121
- * otherwise — and a clutch dropped on that evidence lurches the car off with no throttle at all.
122
- *
123
- * Three things fall out of it for free:
124
- *
125
- * - **A braked car stops.** The engine cannot stall, so a clutch left in at 0 km/h feeds idle
126
- * torque to the wheels forever and the car creeps against its own brakes. Here the revs pin at
127
- * idle, so the pedal goes down and the brakes have nothing to fight.
128
- * - **Reverse is not a special case.** No ratio takes part, so it needs no separate number — which
129
- * is exactly why a fixed road-speed threshold got reverse wrong.
130
- * - **Hill starts judder instead of stalling.** Let the pedal out, the load pulls the revs down,
131
- * the rule pushes it straight back in, and the clutch sits slipping at the bite point until the
132
- * car moves. So the test deliberately ignores the throttle — gating it on "no throttle" would
133
- * stall the car under load. Throttle only holds the floor at `bite`.
134
- *
135
- * The cost is honest and small: once the pedal is down the revs sit at idle and cannot rise on
136
- * their own, so **coasting downhill gives no engine braking until you touch the throttle** — which
137
- * is precisely what a car with the clutch in does. Slipping it permanently to keep listening would
138
- * bring the creep straight back.
139
- */
140
- protected takeUp(v: Vehicle, dt: number): number {
141
- if (this.gear === 0) return 0
142
- const idle = v.engine.idleRpm ?? 1000
143
- const step = dt / Math.max(this.clutchTime, dt)
144
- const asked = v.throttle !== 0
145
- let take = this._take
146
- if (v.rpm <= idle * IDLE_MARGIN) take -= step // being dragged under: pedal goes down
147
- else if (asked) take += step // revs to spare AND somewhere to go: let it up
148
- // Asking for drive also holds the pedal at the bite point however far down the rule wants it;
149
- // that is what makes a launch — and a hill start — slip instead of collapsing to nothing.
150
- return asked ? Math.max(take, this.bite) : take
151
- }
152
-
153
- /**
154
- * Which gear the car should be in. The default is an ordinary automatic: pull away from neutral in
155
- * the direction of the throttle, shift up past `shiftUp` of the redline and down below `shiftDown`,
156
- * and refuse to shift again for `latency` seconds. Override this and nothing else.
157
- */
158
- select(v: Vehicle): number {
159
- const throttle = v.throttle
160
- // Neutral, or asked to go the other way: engage once the car has (nearly) stopped.
161
- if (this.gear === 0 || throttle * this.gear < 0) {
162
- if (Math.abs(v.speed) > 1 && this.gear !== 0) return this.gear
163
- return throttle > 0 ? 1 : throttle < 0 ? -1 : this.gear
164
- }
165
- if (this.gear < 0 || this._waitLeft > 0 || this.shifting) return this.gear
166
- const rpm = v.rpm
167
- const max = v.engine.maxRpm ?? 6000
168
- // Upshifting needs COUPLED revs. With the clutch out the engine is free, so a blip at a
169
- // standstill reads as 6000 rpm and the box would climb to top gear without the car moving —
170
- // then launch in it. Downshifting stays open either way: it can only take you to a lower gear,
171
- // which is where a car with dying revs belongs.
172
- if (rpm > max * this.shiftUp && this.clutch >= 1 && this.gear < this.ratios.length) return this.gear + 1
173
- if (rpm < max * this.shiftDown && this.gear > 1) return this.gear - 1
174
- return this.gear
175
- }
176
-
177
- /** Change gear now, with the clutch envelope. Safe to call from `select()` or from game code
178
- * (a sequential manual is `if (Input.key('KeyE')) car.gearbox.shift(car.gearbox.gear + 1)`). */
179
- shift(gear: number): void {
180
- const clamped = Math.max(-1, Math.min(this.ratios.length, Math.round(gear)))
181
- if (clamped === this.gear) return
182
- // The envelope is for swapping one ENGAGED gear for another. If nothing is coupled there is
183
- // nothing to disengage, so it is skipped: neutral at either end, and equally a car sitting still
184
- // with the pedal already down — picking reverse there should pull away the moment it is asked
185
- // to, not sit out a shift it never had to make.
186
- const swapping = this.gear !== 0 && clamped !== 0 && this._take > 0
187
- this.gear = clamped
188
- this._shiftLeft = swapping ? this.shiftTime : 0
189
- this._engageLeft = swapping ? this.clutchTime : 0
190
- this._waitLeft = this.latency + this._shiftLeft
191
- }
192
-
193
- /** The ratio the engine should be running: 0 in neutral, negative in reverse. */
194
- get ratio(): number {
195
- if (this.gear === 0) return 0
196
- if (this.gear < 0) return this.reverse
197
- return this.ratios[this.gear - 1] ?? 1
198
- }
199
-
200
- /**
201
- * The clutch scalar 0..1 the engine is given: the SHIFT envelope (out during a shift, ramping back
202
- * over `clutchTime`) times the driver's pedal (`takeUp`). Two independent reasons for the clutch
203
- * to be out, so they multiply: mid-shift is out however healthy the revs are, and an engine being
204
- * dragged to idle is out whatever gear it just picked.
205
- */
206
- get clutch(): number {
207
- if (this._shiftLeft > 0) return 0
208
- const envelope =
209
- this._engageLeft > 0 && this.clutchTime > 0 ? 1 - this._engageLeft / this.clutchTime : 1
210
- return envelope * this._take
211
- }
212
- }