-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathScene.ts
139 lines (96 loc) · 2.98 KB
/
Scene.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
import { Camera } from './lib/juego/Camera.js'
import { Entity } from './lib/juego/Entity.js'
import { EntityManager } from './lib/juego/EntityManager.js'
import { Vec2 } from './lib/juego/Vec2.js'
import { Shape } from './lib/juego/Shape.js'
import { Sound } from './lib/juego/Sound.js'
import { PlayerStatus } from './Player.js'
import * as tp from './lib/toastpoint.js'
///////////
// SCENE //
///////////
/*
Parent class for cutscenes and levels
*/
export type SceneDrawOptions = {
noConsole?: boolean;
}
export class OmmatidiaScene {
name: string = '';
isLoaded: boolean = false;
camera: Camera = new Camera();
em: EntityManager = new EntityManager();
sounds: Array<Sound> = [];
final: boolean = false;
messages: Array<string> = [];
playerStatus: PlayerStatus;
discardFields: Array<string> = [];
//saveFields = [];
constructor( name: string ) {
this.name = name;
}
protected toToast( toaster: tp.Toaster ): any {
let fields = Object.keys( this );
// never save these fields (which are lists of other fields)
let exclude = ['editFields', 'saveFields', 'discardFields']
// fields for for serialization only (exclude the old value if left in by mistake)
exclude = exclude.concat( ['entities', '__entities'] );
exclude = exclude.concat( this.discardFields );
fields = fields.filter( x => !exclude.includes( x ) );
let flat: any = {};
tp.setMultiJSON( flat, fields, this, toaster );
tp.setJSON( flat, '__entities', this.em.entities, toaster );
return flat;
}
load(): Promise<any> {
// dummy load function
return new Promise<void>( ( resolve, reject ) => {
this.isLoaded = true;
resolve();
});
}
wake(): void {}
sleep(): void {}
reset() {}
getShapes(): Array<Shape> {
let shapes = [];
for ( let entity of this.em.entities ) {
for ( let shape of entity.getShapes( 0.0 ) ) {
shapes.push( shape );
}
}
return shapes;
}
pushMessage( msg: string ) {}
update() {}
updateSounds() {
for ( let sound of this.sounds ) {
this.updateSound( sound );
}
}
updateSound( source: Sound ) {
let dist = 0;
if ( source.pos ) {
// dist = this.player.pos.distTo( source.pos );
}
let vol = source.distScale / ( dist ** 2 + 1 );
source.audio.volume = Math.min( vol, 1.0 );
let atStart = source.audio.currentTime == 0 || source.audio.ended;
if ( atStart && source.count > 0 ) {
source.audio.play();
if ( !source.audio.loop ) source.count -= 1;
}
}
describe( entity: Entity, dir?: Vec2 ) {}
drawPauseOverlay( context: CanvasRenderingContext2D ) {
if ( typeof document === 'undefined' ) return;
context.fillStyle = 'hsl( 0, 0%, 90%)';
context.font = '24px Arial';
let text = 'P A U S E';
let meas = context.measureText( text );
let w = meas.width;
let h = meas.actualBoundingBoxAscent + meas.actualBoundingBoxDescent;
context.fillText( text, this.camera.viewportW / 2 - w / 2, this.camera.viewportH / 2 - 100 + h / 2 );
}
draw( context: CanvasRenderingContext2D, drawOptions: SceneDrawOptions={} ) {}
}