This repository has been archived by the owner on Sep 16, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 74
/
Copy pathMain.hx
300 lines (215 loc) · 8.03 KB
/
Main.hx
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
import luxe.Input;
import luxe.Sprite;
import luxe.Parcel;
import luxe.ParcelProgress;
import luxe.Text;
import luxe.Vector;
import luxe.Color;
import luxe.resource.Resource;
import phoenix.BitmapFont;
import phoenix.Texture;
import snow.types.Types.AudioHandle;
//A simple message object,
//time to stay on screen,
//message to show,
//and optional event to fire
typedef Message = {
msg : String,
time : Float,
? event : String
}
class Main extends luxe.Game {
//the text object
var text : Text;
//the font we will use
var montez_font : BitmapFont;
//the screen sized color overlay
var overlay : Sprite;
//the image background
var apartment : Sprite;
//a list of messages to show
var messages : Array<Message>;
var music_id = 'assets/sound/189175__triangelx__emotional-piano.ogg';
var thunder_id = 'assets/sound/244053__lennyboy__thunder.ogg';
var rain_id = 'assets/sound/244028__lennyboy__rain001.ogg';
//The audio sources
var thunder : AudioResource;
var music : AudioResource;
var rain : AudioResource;
//The audio instances (i.e the one that is playing)
var thunder_handle : AudioHandle;
var music_handle : AudioHandle;
var rain_handle : AudioHandle;
override function ready() {
//as with guide 3, we just create a list of assets to load
var parcel = new Parcel({
textures : [{ id : 'assets/apartment.png' }],
fonts : [{ id : 'assets/montez/montez.fnt' }],
sounds : [
{ id : thunder_id, is_stream : false },
{ id : rain_id, is_stream : false },
{ id : music_id, is_stream : false }
]
});
//and a simpe progress bar
new ParcelProgress({
parcel : parcel,
background : new Color(1,1,1,0.85),
oncomplete : assets_loaded
});
//go!
parcel.load();
} //ready
function assets_loaded(_) {
thunder = Luxe.resources.audio(thunder_id);
music = Luxe.resources.audio(music_id);
rain = Luxe.resources.audio(rain_id);
create_apartment();
create_overlay();
create_text();
connect_events();
//once all created, start the chain
start();
} //assets_loaded
function start() {
//now we tween over time, to fade in the color alpha
//notice the times are all in seconds, always
//for the apartment, it fades to full alpha
apartment.color.tween(1, { a:1 });
//for the overlay, partially full
overlay.color.tween(2, { a:0.95 }).delay(0.5);
//now, a little after that, start the text!
Luxe.timer.schedule(2.8, show_message);
rain_handle = Luxe.audio.loop(rain.source);
} //start
function reset() {
//flush the existing stuff
Luxe.scene.empty();
//kill the audio
Luxe.audio.stop(music_handle);
Luxe.audio.stop(thunder_handle);
Luxe.audio.stop(rain_handle);
//reset the timers
Luxe.timer.reset();
//start over
assets_loaded(null);
} //reset
function create_overlay() {
//create a large screen sized rectangle,
//and set its depth to 2, so its above the apartment
//notice the alpha is set to 0, we want to fade it in
//later on as well
overlay = new Sprite({
name : 'overlay',
centered : false,
size : Luxe.screen.size,
color : new Color(1,1,1,0),
depth : 1
});
} //create_overlay
function create_text() {
//since the parcel has created the font, its in the resource manager
montez_font = Luxe.resources.font('assets/montez/montez.fnt');
//scale to fit the screen nicely, but max at the font default size of 48,
//and the 12 is a ratio I made up based on the default window size
var text_size = Math.min( Math.round(Luxe.screen.h/12), 48);
//again, depth 3 > 2, so its above everything
text = new Text({
pos : Luxe.screen.mid,
point_size : text_size,
depth : 3,
align : TextAlign.center,
font : montez_font,
text : 'no message yet',
color : new Color(0,0,0,0).rgb(0x242424)
});
messages = [];
messages.push({ msg:'An unfortunate story...', time:4 });
messages.push({ msg:'a person seeking answers', time:3, event:'thunder' });
messages.push({ msg:'searching the edges of time', time:5.5, event:'darkness' });
messages.push({ msg:'but finding only darkness.', time:6, event:'music' });
messages.push({ msg:'as the tale is written...', time:3 });
messages.push({ msg:'it all started with a backpack', time:5, event:'finish' });
} //create_text
function create_apartment() {
//fetch the previously loaded texture
var image = Luxe.resources.texture('assets/apartment.png');
//this makes sure the pixels stay crisp when scaling, like in guide 3
image.filter_min = image.filter_mag = FilterType.nearest;
//this calculates how wide the image should be on screen,
//if we make the image as high as the view itself
var height = Luxe.screen.h;
var width = (height/image.height) * image.width;
//we also set the alpha to zero, so it can be faded in
apartment = new Sprite({
name: 'apartment',
texture: image,
size: new Vector( width, height ),
centered: false,
color : new Color(1,1,1,0)
});
} //create_apartment
function show_message() {
if(messages.length == 0) {
return;
}
//take the first message off the list
var message = messages[0];
//update the text itself
text.text = message.msg;
//schedule the timer for on complete,
//we use the + here because the fade in time is added
//so that the text has its whole time shown
Luxe.timer.schedule( message.time + 0.45, message_done);
text.color.tween(0.45, { a:1 });
} //show_message
function message_done() {
//make sure we remove the message we used
var message = messages.shift();
//hide the text, and when done,
text.color.tween(0.55, { a:0 }).onComplete(function(){
//if it has an event, fire it
if(message.event != null) {
Luxe.events.fire('intro.' + message.event);
}
//continue the queue (if any)
show_message();
}); //tween
} //message_done
function connect_events() {
Luxe.events.listen('intro.darkness', ondarkness);
Luxe.events.listen('intro.thunder', onthunder);
Luxe.events.listen('intro.music', onmusic);
Luxe.events.listen('intro.finish', onfinish);
} //connect_events
function onthunder(_) {
thunder_handle = Luxe.audio.play(thunder.source);
} //onthunder
function onmusic(_) {
music_handle = Luxe.audio.play(music.source);
Luxe.audio.volume(music_handle, 0.1);
} //onmusic
function ondarkness(_) {
//dramatic effect
overlay.color.tween(0.3, { r:0.05, g:0.05, b:0.05 });
text.color = new Color().rgb(0xefefef);
} //ondarkness
function onfinish(_) {
overlay.color.tween( 4, { a:0 });
} //onfinish
override function onkeyup( e:KeyEvent ) {
if(e.keycode == Key.key_r) {
reset();
}
if(e.keycode == Key.escape) {
Luxe.shutdown();
}
} //onkeyup
override function config( config:luxe.GameConfig ) {
#if luxe_doc_sample
config.window.width = 640;
config.window.height = 427;
#end
return config;
} //config
} //Main