@inglorious/engine 24.15.0 → 24.17.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/package.json +2 -2
- package/src/ai/movement/dynamic/align.js +16 -17
- package/src/ai/movement/dynamic/alignment.js +25 -0
- package/src/ai/movement/dynamic/arrive.js +13 -5
- package/src/ai/movement/dynamic/avoid-walls.js +93 -0
- package/src/ai/movement/dynamic/cohesion.js +23 -0
- package/src/ai/movement/dynamic/evade.js +13 -31
- package/src/ai/movement/dynamic/face.js +10 -7
- package/src/ai/movement/dynamic/flee.js +11 -35
- package/src/ai/movement/dynamic/look-where-youre-going.js +10 -4
- package/src/ai/movement/dynamic/match-velocity.js +17 -33
- package/src/ai/movement/dynamic/pursue.js +20 -9
- package/src/ai/movement/dynamic/seek.js +11 -34
- package/src/ai/movement/dynamic/separation.js +49 -0
- package/src/ai/movement/dynamic/steering.js +151 -0
- package/src/ai/movement/dynamic/wander.js +17 -5
- package/src/behaviors/film.js +269 -0
- package/types/ai/movement/dynamic/align.d.ts +8 -1
- package/types/ai/movement/dynamic/alignment.d.ts +8 -0
- package/types/ai/movement/dynamic/arrive.d.ts +12 -0
- package/types/ai/movement/dynamic/avoid-walls.d.ts +11 -0
- package/types/ai/movement/dynamic/cohesion.d.ts +7 -0
- package/types/ai/movement/dynamic/evade.d.ts +8 -0
- package/types/ai/movement/dynamic/face.d.ts +13 -1
- package/types/ai/movement/dynamic/flee.d.ts +7 -0
- package/types/ai/movement/dynamic/look-where-youre-going.d.ts +7 -1
- package/types/ai/movement/dynamic/match-velocity.d.ts +8 -1
- package/types/ai/movement/dynamic/pursue.d.ts +15 -0
- package/types/ai/movement/dynamic/seek.d.ts +7 -0
- package/types/ai/movement/dynamic/separation.d.ts +11 -0
- package/types/ai/movement/dynamic/steering.d.ts +34 -0
- package/types/ai/movement/dynamic/wander.d.ts +20 -2
- package/types/behaviors/film.d.ts +124 -0
- package/types/index.d.ts +11 -0
|
@@ -0,0 +1,151 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Steering as data, after Ian Millington, *AI for Games* (3rd ed., CRC Press,
|
|
3
|
+
* 2019), chapter 3 "Movement": a steering behavior only says how it wants to
|
|
4
|
+
* accelerate, `{ linear, angular }`, and one update applies it. Kept apart, the
|
|
5
|
+
* accelerations of several behaviors can be blended or arbitrated before
|
|
6
|
+
* anything moves ("Combining Steering Behaviors"), which is what flocking needs.
|
|
7
|
+
* https://www.oreilly.com/library/view/ai-for-games/9781351053280/
|
|
8
|
+
*
|
|
9
|
+
* Every dynamic behavior has an `…Steering` function returning that object (or
|
|
10
|
+
* `null`, the book's "no steering", when it has nothing to ask), and the older
|
|
11
|
+
* name (`seek`, `arrive`…) is that steering passed through `integrate`.
|
|
12
|
+
*/
|
|
13
|
+
import { abs, clamp as clampNumber } from "@inglorious/utils/math/number.js"
|
|
14
|
+
import {
|
|
15
|
+
angle,
|
|
16
|
+
clamp,
|
|
17
|
+
magnitude,
|
|
18
|
+
multiply,
|
|
19
|
+
sum,
|
|
20
|
+
zero,
|
|
21
|
+
} from "@inglorious/utils/vector.js"
|
|
22
|
+
|
|
23
|
+
export const DEFAULT_EPSILON = 0.1
|
|
24
|
+
|
|
25
|
+
const DEFAULT_ANGULAR_SPEED = 0
|
|
26
|
+
const DEFAULT_MAX_ACCELERATION = 0
|
|
27
|
+
const DEFAULT_MAX_ANGULAR_ACCELERATION = 0
|
|
28
|
+
const DEFAULT_MAX_SPEED = 0
|
|
29
|
+
const DEFAULT_ORIENTATION = 0
|
|
30
|
+
const MIN_SPEED = 0
|
|
31
|
+
|
|
32
|
+
const HALF = 0.5
|
|
33
|
+
const NONE = 0
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* Applies a steering to the entity for `dt` seconds, the book's
|
|
37
|
+
* `Kinematic.update`. The linear part changes velocity (at most `maxSpeed`) and
|
|
38
|
+
* position, and turns the entity where it goes; the angular part changes
|
|
39
|
+
* `angularSpeed` and `orientation`, and wins over the linear turn when both are
|
|
40
|
+
* given. `null` leaves the entity as it is, as when a behavior has arrived.
|
|
41
|
+
*/
|
|
42
|
+
export function integrate(entity, steering, dt) {
|
|
43
|
+
if (!steering) {
|
|
44
|
+
return entity
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
const { linear, angular } = steering
|
|
48
|
+
const result = {}
|
|
49
|
+
|
|
50
|
+
if (linear) {
|
|
51
|
+
const maxSpeed = entity.maxSpeed ?? DEFAULT_MAX_SPEED
|
|
52
|
+
|
|
53
|
+
let velocity = entity.velocity ?? zero()
|
|
54
|
+
velocity = sum(velocity, multiply(linear, dt))
|
|
55
|
+
velocity = clamp(velocity, MIN_SPEED, maxSpeed)
|
|
56
|
+
|
|
57
|
+
result.acceleration = linear
|
|
58
|
+
result.velocity = velocity
|
|
59
|
+
result.position = sum(
|
|
60
|
+
entity.position,
|
|
61
|
+
multiply(velocity, dt),
|
|
62
|
+
multiply(linear, HALF * dt * dt),
|
|
63
|
+
)
|
|
64
|
+
result.orientation = magnitude(velocity)
|
|
65
|
+
? angle(velocity)
|
|
66
|
+
: (entity.orientation ?? DEFAULT_ORIENTATION)
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
if (angular != null) {
|
|
70
|
+
const angularSpeed =
|
|
71
|
+
(entity.angularSpeed ?? DEFAULT_ANGULAR_SPEED) + angular * dt
|
|
72
|
+
const orientation = entity.orientation ?? DEFAULT_ORIENTATION
|
|
73
|
+
|
|
74
|
+
result.angularSpeed = angularSpeed
|
|
75
|
+
result.orientation =
|
|
76
|
+
orientation + angularSpeed * dt + angular * HALF * dt * dt
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
return result
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/**
|
|
83
|
+
* Weighted blending (*AI for Games*, "Combining Steering Behaviors"): adds up
|
|
84
|
+
* `[steering, weight]` pairs, then caps the sum at the entity's
|
|
85
|
+
* `maxAcceleration` and `maxAngularAcceleration`. Flocking is separation,
|
|
86
|
+
* cohesion and alignment blended this way. `null` steerings are skipped; all
|
|
87
|
+
* `null` gives `null`.
|
|
88
|
+
*
|
|
89
|
+
* Rejected: normalising the weights to sum to 1, which would let a behavior that
|
|
90
|
+
* asks nothing (a far wall) water down the others.
|
|
91
|
+
*/
|
|
92
|
+
export function blend(entity, weightedSteerings) {
|
|
93
|
+
let linear = null
|
|
94
|
+
let angular = null
|
|
95
|
+
|
|
96
|
+
for (const [steering, weight] of weightedSteerings) {
|
|
97
|
+
if (!steering) continue
|
|
98
|
+
|
|
99
|
+
if (steering.linear) {
|
|
100
|
+
const weighted = multiply(steering.linear, weight)
|
|
101
|
+
linear = linear ? sum(linear, weighted) : weighted
|
|
102
|
+
}
|
|
103
|
+
if (steering.angular != null) {
|
|
104
|
+
angular = (angular ?? NONE) + steering.angular * weight
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
if (!linear && angular == null) {
|
|
109
|
+
return null
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
const result = {}
|
|
113
|
+
if (linear) {
|
|
114
|
+
const maxAcceleration = entity.maxAcceleration ?? DEFAULT_MAX_ACCELERATION
|
|
115
|
+
result.linear = clamp(linear, MIN_SPEED, maxAcceleration)
|
|
116
|
+
}
|
|
117
|
+
if (angular != null) {
|
|
118
|
+
const maxAngularAcceleration =
|
|
119
|
+
entity.maxAngularAcceleration ?? DEFAULT_MAX_ANGULAR_ACCELERATION
|
|
120
|
+
result.angular = clampNumber(
|
|
121
|
+
angular,
|
|
122
|
+
-maxAngularAcceleration,
|
|
123
|
+
maxAngularAcceleration,
|
|
124
|
+
)
|
|
125
|
+
}
|
|
126
|
+
return result
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
/**
|
|
130
|
+
* Priorities (*AI for Games*, "Combining Steering Behaviors"): takes steerings
|
|
131
|
+
* from most to least urgent (usually each a `blend` of a group) and returns the
|
|
132
|
+
* first one asking for more than `epsilon`, so avoiding a wall is not averaged
|
|
133
|
+
* away by the wish to flock. When none does, the last one is returned, as in the
|
|
134
|
+
* book.
|
|
135
|
+
*/
|
|
136
|
+
export function prioritize(steerings, { epsilon = DEFAULT_EPSILON } = {}) {
|
|
137
|
+
let last = null
|
|
138
|
+
|
|
139
|
+
for (const steering of steerings) {
|
|
140
|
+
last = steering
|
|
141
|
+
if (!steering) continue
|
|
142
|
+
|
|
143
|
+
const linear = steering.linear ? magnitude(steering.linear) : NONE
|
|
144
|
+
const angular = abs(steering.angular ?? NONE)
|
|
145
|
+
if (linear > epsilon || angular > epsilon) {
|
|
146
|
+
return steering
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
return last
|
|
151
|
+
}
|
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
import { randomBinomial } from "@inglorious/utils/math/rng.js"
|
|
2
2
|
import { createVector, sum } from "@inglorious/utils/vector.js"
|
|
3
3
|
|
|
4
|
-
import {
|
|
4
|
+
import { seekSteering } from "./seek.js"
|
|
5
|
+
import { integrate } from "./steering.js"
|
|
5
6
|
|
|
6
7
|
export const DEFAULT_WANDER_OFFSET = 100
|
|
7
8
|
export const DEFAULT_WANDER_RADIUS = 100
|
|
@@ -10,22 +11,33 @@ const DEFAULT_MAX_ANGULAR_SPEED = 0
|
|
|
10
11
|
|
|
11
12
|
const DEFAULT_ORIENTATION = 0
|
|
12
13
|
|
|
13
|
-
|
|
14
|
+
/**
|
|
15
|
+
* Seeks a point on a circle `wanderRadius` wide, `wanderOffset` ahead, moved a
|
|
16
|
+
* little at random each frame (*AI for Games*, "Wander"). Pass a seeded `rng`
|
|
17
|
+
* (`createRng` from `@inglorious/utils/math/rng.js`) to keep a simulation
|
|
18
|
+
* deterministic.
|
|
19
|
+
*/
|
|
20
|
+
export function wanderSteering(
|
|
14
21
|
entity,
|
|
15
|
-
dt,
|
|
16
22
|
{
|
|
17
23
|
wanderOffset = DEFAULT_WANDER_OFFSET,
|
|
18
24
|
wanderRadius = DEFAULT_WANDER_RADIUS,
|
|
25
|
+
rng,
|
|
19
26
|
} = {},
|
|
20
27
|
) {
|
|
21
28
|
const maxAngularSpeed = entity.maxAngularSpeed ?? DEFAULT_MAX_ANGULAR_SPEED
|
|
29
|
+
const binomial = rng ? rng.randomBinomial() : randomBinomial()
|
|
22
30
|
|
|
23
31
|
let orientation = entity.orientation ?? DEFAULT_ORIENTATION
|
|
24
32
|
|
|
25
33
|
let position = sum(entity.position, createVector(wanderOffset, orientation))
|
|
26
34
|
|
|
27
|
-
orientation +=
|
|
35
|
+
orientation += binomial * maxAngularSpeed
|
|
28
36
|
position = sum(position, createVector(wanderRadius, orientation))
|
|
29
37
|
|
|
30
|
-
return
|
|
38
|
+
return seekSteering(entity, { position })
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export function wander(entity, dt, options) {
|
|
42
|
+
return integrate(entity, wanderSteering(entity, options), dt)
|
|
31
43
|
}
|
|
@@ -0,0 +1,269 @@
|
|
|
1
|
+
import { v } from "@inglorious/utils/v.js"
|
|
2
|
+
|
|
3
|
+
// A film grammar for story-driven games, from the AGAIN hackathon game: one still picture
|
|
4
|
+
// per shot, cuts that dip through black, a slow push-in on every shot, and title cards.
|
|
5
|
+
// Cheap, and it makes a change of place feel directed rather than swapped.
|
|
6
|
+
//
|
|
7
|
+
// - A dip to black fades the outgoing shot out and the next one in, with black in between:
|
|
8
|
+
// the fade-out/fade-in of film editing (Wikipedia, "Dissolve (filmmaking)", which covers
|
|
9
|
+
// fades to and from black: https://en.wikipedia.org/wiki/Dissolve_(filmmaking)).
|
|
10
|
+
// - A push-in is a slow zoom on a still, the Ken Burns effect (Wikipedia, "Ken Burns
|
|
11
|
+
// effect": "slow zooming and panning effects, and fading transitions between frames",
|
|
12
|
+
// https://en.wikipedia.org/wiki/Ken_Burns_effect).
|
|
13
|
+
// - A title card is a line of text shown over the picture for a moment, the intertitle of
|
|
14
|
+
// silent film (https://en.wikipedia.org/wiki/Intertitle).
|
|
15
|
+
//
|
|
16
|
+
// The state lives in entities, not in the renderer, so that it is saved, replayed and
|
|
17
|
+
// tested like everything else, and so that other entities can read it: a character drawn
|
|
18
|
+
// over the shot fades with it by reading `filmBlack`. Nothing here is random.
|
|
19
|
+
//
|
|
20
|
+
// Like every effect, a cut answers what happened in the game (`lifeStart`, `cardChosen`):
|
|
21
|
+
// the film's own type calls `cut` from its handlers. There is no `cut` event on purpose,
|
|
22
|
+
// for the reason given in `particles.js` for `burst`.
|
|
23
|
+
//
|
|
24
|
+
// Rejected: a crossfade (one shot dissolving into the next). Two pictures half-visible at
|
|
25
|
+
// once read as muddy for busy art, and it costs two full-screen draws per frame for the
|
|
26
|
+
// whole cut; the dip draws one.
|
|
27
|
+
|
|
28
|
+
const DEFAULT_FILM_ID = "film"
|
|
29
|
+
const FILM_TYPE = "Film"
|
|
30
|
+
const DEFAULT_CARD_ID = "titleCard"
|
|
31
|
+
const TITLE_CARD_TYPE = "TitleCard"
|
|
32
|
+
const NOTHING = 0
|
|
33
|
+
const WHOLE = 1
|
|
34
|
+
const HALF = 0.5
|
|
35
|
+
const TWICE = 2
|
|
36
|
+
|
|
37
|
+
// A dip to black lasting 0.6 s in all, as AGAIN's did: long enough to read as a cut,
|
|
38
|
+
// short enough not to hold up play, which is what a game's cuts must not do. Film editors
|
|
39
|
+
// use longer fades (a second or more) because nobody is waiting to press a button.
|
|
40
|
+
const DEFAULT_CUT_SECONDS = 0.6
|
|
41
|
+
// A 6% zoom over 12 s: slow enough to be felt rather than seen, as a Ken Burns move is. A
|
|
42
|
+
// larger zoom or a shorter time reads as the camera moving on purpose, which pulls the eye
|
|
43
|
+
// away from the play. After `pushSeconds` the shot holds.
|
|
44
|
+
const DEFAULT_PUSH_IN = 0.06
|
|
45
|
+
const DEFAULT_PUSH_SECONDS = 12
|
|
46
|
+
// A card stays 2 s: a short line read at a glance. It fades in quickly, so it lands with
|
|
47
|
+
// the moment it names, and out more slowly, so it does not vanish while being read.
|
|
48
|
+
const DEFAULT_CARD_SECONDS = 2
|
|
49
|
+
const CARD_FADE_IN = 0.25
|
|
50
|
+
const CARD_FADE_OUT = 0.5
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* An entity showing one shot at a time: a still picture filling its frame, slowly pushing
|
|
54
|
+
* in, that changes through black when the game calls `cut`. A shot is an image id, as
|
|
55
|
+
* entities name images (see `behaviors/images.js`); `null` is black.
|
|
56
|
+
*
|
|
57
|
+
* Pair it with `film()` and draw it with `renderFilm` from
|
|
58
|
+
* `@inglorious/renderer-2d/film.js`, which reads `position` as the frame's bottom left
|
|
59
|
+
* corner and `size` as its width and height (the whole game when there is none), and
|
|
60
|
+
* `focus` as the point the push-in closes on.
|
|
61
|
+
*
|
|
62
|
+
* @example
|
|
63
|
+
* ```js
|
|
64
|
+
* types: {
|
|
65
|
+
* Film: [
|
|
66
|
+
* { render: renderFilm },
|
|
67
|
+
* film(),
|
|
68
|
+
* { lifeStart(entity) { cut(entity, "bedroom") }, personDie(entity) { cut(entity, "grave") } },
|
|
69
|
+
* ],
|
|
70
|
+
* },
|
|
71
|
+
* entities: { film: createFilm({ layer: -20 }) },
|
|
72
|
+
* ```
|
|
73
|
+
*
|
|
74
|
+
* @param {Object} [options]
|
|
75
|
+
* @param {string} [options.id="film"]
|
|
76
|
+
* @param {number} [options.cutSeconds=0.6] - How long a cut lasts, out and in together.
|
|
77
|
+
* @param {number} [options.pushIn=0.06] - How much a shot zooms in: 0.06 is 6%.
|
|
78
|
+
* @param {number} [options.pushSeconds=12] - Over how long; then it holds.
|
|
79
|
+
* @returns {Object} The entity, of type `Film` unless `type` says otherwise, showing black.
|
|
80
|
+
* Any other option (`size`, `focus`, `layer`...) is kept on it for the renderer.
|
|
81
|
+
*/
|
|
82
|
+
export function createFilm({
|
|
83
|
+
id = DEFAULT_FILM_ID,
|
|
84
|
+
cutSeconds = DEFAULT_CUT_SECONDS,
|
|
85
|
+
pushIn = DEFAULT_PUSH_IN,
|
|
86
|
+
pushSeconds = DEFAULT_PUSH_SECONDS,
|
|
87
|
+
...rest
|
|
88
|
+
} = {}) {
|
|
89
|
+
return {
|
|
90
|
+
id,
|
|
91
|
+
type: FILM_TYPE,
|
|
92
|
+
position: v(NOTHING, NOTHING, NOTHING),
|
|
93
|
+
shot: null,
|
|
94
|
+
previous: null,
|
|
95
|
+
shotTime: NOTHING,
|
|
96
|
+
previousTime: NOTHING,
|
|
97
|
+
cutTime: cutSeconds,
|
|
98
|
+
cutSeconds,
|
|
99
|
+
pushIn,
|
|
100
|
+
pushSeconds,
|
|
101
|
+
...rest,
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
/**
|
|
106
|
+
* The behaviour of a film entity: it keeps time for the shot and the cut. Shots change
|
|
107
|
+
* with `cut`, called by the film type's own handlers.
|
|
108
|
+
*/
|
|
109
|
+
export function film() {
|
|
110
|
+
return {
|
|
111
|
+
update(entity, dt) {
|
|
112
|
+
entity.shotTime += dt
|
|
113
|
+
entity.cutTime += dt
|
|
114
|
+
},
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
/**
|
|
119
|
+
* Changes the shot through black: the shot on screen fades out over the first half of
|
|
120
|
+
* the cut, the new one fades in over the second. From black (the first shot, or after a
|
|
121
|
+
* cut to `null`) it only fades in; to `null` it only fades out. Cutting to the shot
|
|
122
|
+
* already showing does nothing, so a handler can call it without checking.
|
|
123
|
+
*
|
|
124
|
+
* @param {Object} entity - A film entity, from `createFilm`.
|
|
125
|
+
* @param {string | null} shot - An image id, or `null` for black.
|
|
126
|
+
*/
|
|
127
|
+
export function cut(entity, shot) {
|
|
128
|
+
if (shot === entity.shot) return
|
|
129
|
+
|
|
130
|
+
entity.previous = entity.shot
|
|
131
|
+
entity.previousTime = entity.shotTime
|
|
132
|
+
entity.shot = shot
|
|
133
|
+
entity.shotTime = NOTHING
|
|
134
|
+
entity.cutTime = NOTHING
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
/**
|
|
138
|
+
* How black the frame is now, from 0 (the shot fully visible) to 1. Things drawn over the
|
|
139
|
+
* shot multiply their opacity by `1 - filmBlack(film)` to fade with it.
|
|
140
|
+
*
|
|
141
|
+
* @param {Object} entity - A film entity.
|
|
142
|
+
* @returns {number}
|
|
143
|
+
*/
|
|
144
|
+
export function filmBlack(entity) {
|
|
145
|
+
const progress = entity.cutTime / entity.cutSeconds
|
|
146
|
+
if (progress >= WHOLE) return entity.shot === null ? WHOLE : NOTHING
|
|
147
|
+
|
|
148
|
+
// From black there is nothing to fade out: the whole cut fades in.
|
|
149
|
+
if (entity.previous === null) return WHOLE - progress
|
|
150
|
+
|
|
151
|
+
if (progress < HALF) return progress * TWICE
|
|
152
|
+
if (entity.shot === null) return WHOLE
|
|
153
|
+
return TWICE - progress * TWICE
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
/**
|
|
157
|
+
* The shot to draw now and how long it has been on screen: during the first half of a
|
|
158
|
+
* cut, the one going out, still pushing in; otherwise the current one.
|
|
159
|
+
*
|
|
160
|
+
* @param {Object} entity - A film entity.
|
|
161
|
+
* @returns {{ shot: string | null, time: number }}
|
|
162
|
+
*/
|
|
163
|
+
export function shownShot(entity) {
|
|
164
|
+
const progress = entity.cutTime / entity.cutSeconds
|
|
165
|
+
if (progress < HALF && entity.previous !== null) {
|
|
166
|
+
return { shot: entity.previous, time: entity.previousTime + entity.cutTime }
|
|
167
|
+
}
|
|
168
|
+
return { shot: entity.shot, time: entity.shotTime }
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
/**
|
|
172
|
+
* How much a shot that has been on screen for `time` seconds is zoomed: 1 when it
|
|
173
|
+
* starts, growing evenly to `1 + pushIn` over `pushSeconds`, then holding.
|
|
174
|
+
*
|
|
175
|
+
* @param {Object} entity - A film entity.
|
|
176
|
+
* @param {number} time - Seconds, from `shownShot`.
|
|
177
|
+
* @returns {number}
|
|
178
|
+
*/
|
|
179
|
+
export function pushZoom(entity, time) {
|
|
180
|
+
return WHOLE + entity.pushIn * Math.min(time / entity.pushSeconds, WHOLE)
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
/**
|
|
184
|
+
* An entity showing a title card for a moment: a title, and a smaller subtitle under it
|
|
185
|
+
* if there is one ("Life #3", "Age 18"). A separate entity from the film, so it can be
|
|
186
|
+
* drawn over what stands in front of the shot.
|
|
187
|
+
*
|
|
188
|
+
* Pair it with `titleCard()` and draw it with `renderTitleCard` from
|
|
189
|
+
* `@inglorious/renderer-2d/film.js`, centred on its `position`, with its `size` (the
|
|
190
|
+
* title's font size), `font`, `weight`, `color` and `shade` (the band behind the text).
|
|
191
|
+
*
|
|
192
|
+
* @example
|
|
193
|
+
* ```js
|
|
194
|
+
* types: {
|
|
195
|
+
* TitleCard: [
|
|
196
|
+
* { render: renderTitleCard },
|
|
197
|
+
* titleCard(),
|
|
198
|
+
* { lifeStart(entity, life) { showTitleCard(entity, `Life #${life.number}`, `Age ${life.age}`) } },
|
|
199
|
+
* ],
|
|
200
|
+
* },
|
|
201
|
+
* entities: { titleCard: createTitleCard({ position: v(640, 480, 0), layer: 10 }) },
|
|
202
|
+
* ```
|
|
203
|
+
*
|
|
204
|
+
* @param {Object} [options]
|
|
205
|
+
* @param {string} [options.id="titleCard"]
|
|
206
|
+
* @param {number} [options.cardSeconds=2] - How long a card stays, fades included.
|
|
207
|
+
* @returns {Object} The entity, of type `TitleCard` unless `type` says otherwise, showing
|
|
208
|
+
* nothing. Any other option is kept on it for the renderer.
|
|
209
|
+
*/
|
|
210
|
+
export function createTitleCard({
|
|
211
|
+
id = DEFAULT_CARD_ID,
|
|
212
|
+
cardSeconds = DEFAULT_CARD_SECONDS,
|
|
213
|
+
...rest
|
|
214
|
+
} = {}) {
|
|
215
|
+
return {
|
|
216
|
+
id,
|
|
217
|
+
type: TITLE_CARD_TYPE,
|
|
218
|
+
position: v(NOTHING, NOTHING, NOTHING),
|
|
219
|
+
title: null,
|
|
220
|
+
subtitle: null,
|
|
221
|
+
cardTime: cardSeconds,
|
|
222
|
+
cardSeconds,
|
|
223
|
+
...rest,
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
/** The behaviour of a title card entity: it keeps time for the card showing. */
|
|
228
|
+
export function titleCard() {
|
|
229
|
+
return {
|
|
230
|
+
update(entity, dt) {
|
|
231
|
+
entity.cardTime += dt
|
|
232
|
+
},
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
/**
|
|
237
|
+
* Shows a card from now, replacing the one showing if any. Call it from the card type's
|
|
238
|
+
* own handlers.
|
|
239
|
+
*
|
|
240
|
+
* @param {Object} entity - A title card entity, from `createTitleCard`.
|
|
241
|
+
* @param {string} title
|
|
242
|
+
* @param {string | null} [subtitle=null]
|
|
243
|
+
*/
|
|
244
|
+
export function showTitleCard(entity, title, subtitle = null) {
|
|
245
|
+
entity.title = title
|
|
246
|
+
entity.subtitle = subtitle
|
|
247
|
+
entity.cardTime = NOTHING
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
/**
|
|
251
|
+
* How opaque the card is now: it fades in over a quarter of a second, holds, and fades
|
|
252
|
+
* out over the last half second of `cardSeconds`. 0 when no card is showing.
|
|
253
|
+
*
|
|
254
|
+
* @param {Object} entity - A title card entity.
|
|
255
|
+
* @returns {number}
|
|
256
|
+
*/
|
|
257
|
+
export function titleCardOpacity(entity) {
|
|
258
|
+
const { title, cardTime, cardSeconds } = entity
|
|
259
|
+
if (title === null || cardTime >= cardSeconds) return NOTHING
|
|
260
|
+
|
|
261
|
+
return Math.max(
|
|
262
|
+
NOTHING,
|
|
263
|
+
Math.min(
|
|
264
|
+
cardTime / CARD_FADE_IN,
|
|
265
|
+
WHOLE,
|
|
266
|
+
(cardSeconds - cardTime) / CARD_FADE_OUT,
|
|
267
|
+
),
|
|
268
|
+
)
|
|
269
|
+
}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { Kinematic, Steering } from "../../../index"
|
|
1
|
+
import type { Kinematic, Steering, SteeringOutput } from "../../../index"
|
|
2
2
|
|
|
3
3
|
export const DEFAULT_TARGET_RADIUS: number
|
|
4
4
|
export const DEFAULT_SLOW_RADIUS: number
|
|
@@ -17,3 +17,10 @@ export function align<TEntity extends Omit<Kinematic, "position">>(
|
|
|
17
17
|
dt: number,
|
|
18
18
|
options?: AlignOptions,
|
|
19
19
|
): Steering<TEntity, { angularSpeed: number; orientation: number }>
|
|
20
|
+
|
|
21
|
+
/** The angular acceleration `align` applies; `null` within `targetRadius`. */
|
|
22
|
+
export function alignSteering(
|
|
23
|
+
entity: Omit<Kinematic, "position">,
|
|
24
|
+
target: { orientation: number },
|
|
25
|
+
options?: AlignOptions,
|
|
26
|
+
): SteeringOutput | null
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import type { Kinematic, SteeringOutput } from "../../../index"
|
|
2
|
+
|
|
3
|
+
/** Matches the neighbors' average velocity (flocking's alignment; `align` turns to an orientation instead). */
|
|
4
|
+
export function alignmentSteering(
|
|
5
|
+
entity: Kinematic,
|
|
6
|
+
neighbors: Pick<Kinematic, "velocity">[],
|
|
7
|
+
options?: { timeToTarget?: number },
|
|
8
|
+
): SteeringOutput | null
|
|
@@ -2,6 +2,7 @@ import type {
|
|
|
2
2
|
DynamicMovement,
|
|
3
3
|
Kinematic,
|
|
4
4
|
Steering,
|
|
5
|
+
SteeringOutput,
|
|
5
6
|
SteeringTarget,
|
|
6
7
|
} from "../../../index"
|
|
7
8
|
|
|
@@ -20,3 +21,14 @@ export function arrive<TEntity extends Kinematic>(
|
|
|
20
21
|
timeToTarget?: number
|
|
21
22
|
},
|
|
22
23
|
): Steering<TEntity, DynamicMovement>
|
|
24
|
+
|
|
25
|
+
/** The acceleration `arrive` applies; `null` within `targetRadius`. */
|
|
26
|
+
export function arriveSteering(
|
|
27
|
+
entity: Kinematic,
|
|
28
|
+
target: Pick<SteeringTarget, "position">,
|
|
29
|
+
options?: {
|
|
30
|
+
targetRadius?: number
|
|
31
|
+
slowRadius?: number
|
|
32
|
+
timeToTarget?: number
|
|
33
|
+
},
|
|
34
|
+
): SteeringOutput | null
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import type { Kinematic, SteeringOutput } from "../../../index"
|
|
2
|
+
|
|
3
|
+
export const DEFAULT_LOOKAHEAD: number
|
|
4
|
+
export const DEFAULT_AVOID_DISTANCE: number
|
|
5
|
+
|
|
6
|
+
/** Seeks a point `avoidDistance` inside the arena's wall when a ray `lookahead` long along the velocity would cross it. */
|
|
7
|
+
export function avoidWallsSteering(
|
|
8
|
+
entity: Kinematic,
|
|
9
|
+
size: [width: number, depth: number],
|
|
10
|
+
options?: { lookahead?: number; avoidDistance?: number },
|
|
11
|
+
): SteeringOutput | null
|
|
@@ -2,6 +2,7 @@ import type {
|
|
|
2
2
|
DynamicMovement,
|
|
3
3
|
Kinematic,
|
|
4
4
|
Steering,
|
|
5
|
+
SteeringOutput,
|
|
5
6
|
SteeringTarget,
|
|
6
7
|
} from "../../../index"
|
|
7
8
|
|
|
@@ -14,3 +15,10 @@ export function evade<TEntity extends Kinematic>(
|
|
|
14
15
|
dt: number,
|
|
15
16
|
options?: { maxPrediction?: number },
|
|
16
17
|
): Steering<TEntity, DynamicMovement>
|
|
18
|
+
|
|
19
|
+
/** The acceleration `evade` applies. */
|
|
20
|
+
export function evadeSteering(
|
|
21
|
+
entity: Kinematic,
|
|
22
|
+
target: Pick<SteeringTarget, "position" | "velocity">,
|
|
23
|
+
options?: { maxPrediction?: number },
|
|
24
|
+
): SteeringOutput | null
|
|
@@ -1,4 +1,9 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type {
|
|
2
|
+
Kinematic,
|
|
3
|
+
Steering,
|
|
4
|
+
SteeringOutput,
|
|
5
|
+
SteeringTarget,
|
|
6
|
+
} from "../../../index"
|
|
2
7
|
import type { AlignOptions } from "./align"
|
|
3
8
|
|
|
4
9
|
/** Turns to face the target. */
|
|
@@ -8,3 +13,10 @@ export function face<TEntity extends Kinematic>(
|
|
|
8
13
|
dt: number,
|
|
9
14
|
options?: AlignOptions,
|
|
10
15
|
): Steering<TEntity, { angularSpeed: number; orientation: number }>
|
|
16
|
+
|
|
17
|
+
/** The angular acceleration `face` applies. */
|
|
18
|
+
export function faceSteering(
|
|
19
|
+
entity: Kinematic,
|
|
20
|
+
target: Pick<SteeringTarget, "position">,
|
|
21
|
+
options?: AlignOptions,
|
|
22
|
+
): SteeringOutput | null
|
|
@@ -2,6 +2,7 @@ import type {
|
|
|
2
2
|
DynamicMovement,
|
|
3
3
|
Kinematic,
|
|
4
4
|
Steering,
|
|
5
|
+
SteeringOutput,
|
|
5
6
|
SteeringTarget,
|
|
6
7
|
} from "../../../index"
|
|
7
8
|
|
|
@@ -11,3 +12,9 @@ export function flee<TEntity extends Kinematic>(
|
|
|
11
12
|
target: Pick<SteeringTarget, "position">,
|
|
12
13
|
dt: number,
|
|
13
14
|
): Steering<TEntity, DynamicMovement>
|
|
15
|
+
|
|
16
|
+
/** The acceleration `flee` applies: `maxAcceleration` away from the target. */
|
|
17
|
+
export function fleeSteering(
|
|
18
|
+
entity: Kinematic,
|
|
19
|
+
target: Pick<SteeringTarget, "position">,
|
|
20
|
+
): SteeringOutput | null
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { Kinematic, Steering } from "../../../index"
|
|
1
|
+
import type { Kinematic, Steering, SteeringOutput } from "../../../index"
|
|
2
2
|
import type { AlignOptions } from "./align"
|
|
3
3
|
|
|
4
4
|
/** Turns to face where it is going. */
|
|
@@ -9,3 +9,9 @@ export function lookWhereYoureGoing<
|
|
|
9
9
|
dt: number,
|
|
10
10
|
options?: AlignOptions,
|
|
11
11
|
): Steering<TEntity, { angularSpeed: number; orientation: number }>
|
|
12
|
+
|
|
13
|
+
/** The angular acceleration `lookWhereYoureGoing` applies. */
|
|
14
|
+
export function lookWhereYoureGoingSteering(
|
|
15
|
+
entity: Omit<Kinematic, "position">,
|
|
16
|
+
options?: AlignOptions,
|
|
17
|
+
): SteeringOutput | null
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { DynamicMovement, Kinematic } from "../../../index"
|
|
1
|
+
import type { DynamicMovement, Kinematic, SteeringOutput } from "../../../index"
|
|
2
2
|
|
|
3
3
|
export const DEFAULT_TIME_TO_TARGET: number
|
|
4
4
|
|
|
@@ -9,3 +9,10 @@ export function matchVelocity(
|
|
|
9
9
|
dt: number,
|
|
10
10
|
options?: { timeToTarget?: number },
|
|
11
11
|
): Required<DynamicMovement>
|
|
12
|
+
|
|
13
|
+
/** The acceleration `matchVelocity` applies. */
|
|
14
|
+
export function matchVelocitySteering(
|
|
15
|
+
entity: Kinematic,
|
|
16
|
+
target: { velocity: import("@inglorious/utils/vector.js").Vector3 },
|
|
17
|
+
options?: { timeToTarget?: number },
|
|
18
|
+
): SteeringOutput
|
|
@@ -2,6 +2,7 @@ import type {
|
|
|
2
2
|
DynamicMovement,
|
|
3
3
|
Kinematic,
|
|
4
4
|
Steering,
|
|
5
|
+
SteeringOutput,
|
|
5
6
|
SteeringTarget,
|
|
6
7
|
} from "../../../index"
|
|
7
8
|
|
|
@@ -14,3 +15,17 @@ export function pursue<TEntity extends Kinematic>(
|
|
|
14
15
|
dt: number,
|
|
15
16
|
options?: { maxPrediction?: number },
|
|
16
17
|
): Steering<TEntity, DynamicMovement>
|
|
18
|
+
|
|
19
|
+
/** The acceleration `pursue` applies. */
|
|
20
|
+
export function pursueSteering(
|
|
21
|
+
entity: Kinematic,
|
|
22
|
+
target: Pick<SteeringTarget, "position" | "velocity">,
|
|
23
|
+
options?: { maxPrediction?: number },
|
|
24
|
+
): SteeringOutput | null
|
|
25
|
+
|
|
26
|
+
/** Where the target will be when the entity, at its speed, could reach it: at most `maxPrediction` seconds ahead. `null` when they are in the same place. */
|
|
27
|
+
export function predict(
|
|
28
|
+
entity: Kinematic,
|
|
29
|
+
target: Pick<SteeringTarget, "position" | "velocity">,
|
|
30
|
+
maxPrediction: number,
|
|
31
|
+
): import("@inglorious/utils/vector.js").Vector3 | null
|
|
@@ -2,6 +2,7 @@ import type {
|
|
|
2
2
|
DynamicMovement,
|
|
3
3
|
Kinematic,
|
|
4
4
|
Steering,
|
|
5
|
+
SteeringOutput,
|
|
5
6
|
SteeringTarget,
|
|
6
7
|
} from "../../../index"
|
|
7
8
|
|
|
@@ -11,3 +12,9 @@ export function seek<TEntity extends Kinematic>(
|
|
|
11
12
|
target: Pick<SteeringTarget, "position">,
|
|
12
13
|
dt: number,
|
|
13
14
|
): Steering<TEntity, DynamicMovement>
|
|
15
|
+
|
|
16
|
+
/** The acceleration `seek` applies: `maxAcceleration` towards the target. */
|
|
17
|
+
export function seekSteering(
|
|
18
|
+
entity: Kinematic,
|
|
19
|
+
target: Pick<SteeringTarget, "position">,
|
|
20
|
+
): SteeringOutput | null
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import type { Kinematic, SteeringOutput } from "../../../index"
|
|
2
|
+
|
|
3
|
+
export const DEFAULT_THRESHOLD: number
|
|
4
|
+
export const DEFAULT_DECAY_COEFFICIENT: number
|
|
5
|
+
|
|
6
|
+
/** Pushes away from neighbors closer than `threshold`, by `decayCoefficient / distance²`, at most `maxAcceleration` each. */
|
|
7
|
+
export function separationSteering(
|
|
8
|
+
entity: Kinematic,
|
|
9
|
+
neighbors: Pick<Kinematic, "position">[],
|
|
10
|
+
options?: { threshold?: number; decayCoefficient?: number },
|
|
11
|
+
): SteeringOutput | null
|