Skip to content

Commit 2498e9d

Browse files
author
Ben Jack
committed
imageLoader changed to assetLoader, sound loading/playing, manual scene changing
1 parent c2a88e9 commit 2498e9d

File tree

6 files changed

+89
-50
lines changed

6 files changed

+89
-50
lines changed

package.json

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,16 @@
2929
},
3030
"scripts": {
3131
"dev": "webpack-dev-server --config webpack.config.dev.js --mode=development -d",
32-
"build": "run-p prepare copy_p5 copy_ccapture copy_sample_code create_assets_folder copy_atom_live_server_script",
32+
"build": "run-p prepare copy_p5 copy_p5_sound copy_ccapture copy_sample_code create_assets_folder copy_atom_live_server_script create_images_folder create_sounds_folder",
3333
"prepare": "webpack --mode=production",
3434
"copy_p5": "cpx ./node_modules/p5/lib/p5.min.js ./build/libraries/",
35+
"copy_p5_sound": "cpx ./node_modules/p5/lib/addons/p5.sound.min.js ./build/libraries/",
3536
"copy_ccapture": "cpx ./node_modules/ccapture.js/build/CCapture.all.min.js ./build/libraries/ && cpx ./node_modules/ccapture.js/src/gif.worker.js ./build/libraries/",
3637
"copy_atom_live_server_script": "cpx ./sample_code/.atom-live-server.json ./build/",
3738
"copy_sample_code": "cpx ./sample_code/index.html ./build/ && cpx ./sample_code/my_vignettes.js ./build/",
38-
"create_assets_folder": "mkdirp ./build/assets"
39+
"create_assets_folder": "mkdirp ./build/assets",
40+
"create_images_folder": "mkdirp ./build/assets/images",
41+
"create_sounds_folder": "mkdirp ./build/assets/sounds"
3942
},
4043
"dependencies": {
4144
"@babel/polyfill": "^7.0.0",

sample_code/index.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
<script src="./libraries/CCapture.all.min.js"></script>
88
<script src="./libraries/p5.min.js"></script>
9+
<script src="./libraries/p5.sound.min.js"></script>
910

1011
<script src="./my_vignettes.js"></script>
1112
<script src="./libraries/interactive-vignettes.bundle.js"></script>

sample_code/my_vignettes.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ function scene1_click(){
2525

2626
function scene1_keypress(){
2727
console.log("scene1 key pressed!");
28+
vignettes.go_to_scene(2);
29+
2830
}
2931

3032

src/assetLoader.js

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
export default class AssetLoader{
2+
3+
constructor(){
4+
this._total_assets_to_load = 0;
5+
this._loaded_images = 0;
6+
this._images = {};
7+
this._image_sequences = {};
8+
this._sounds = {};
9+
}
10+
11+
load_image(name, file_type){
12+
this._total_assets_to_load++;
13+
this._images[name] = loadImage("./assets/images/"+name+"."+file_type, this.asset_loaded.bind(this));
14+
}
15+
16+
load_image_sequence(name, file_type, sequence_length){
17+
this._total_assets_to_load += sequence_length;
18+
for(var i = 0; i < image_count; ++i){
19+
this._image_sequences[name][i] = loadImage("assets/images/"+name+"/"+name+"_"+i+"."+file_type, this.asset_loaded.bind(this));
20+
}
21+
}
22+
23+
load_sound(name, file_type){
24+
this._total_assets_to_load++;
25+
this._sounds[name] = loadSound("./assets/sounds/"+name+"."+file_type, this.asset_loaded.bind(this));
26+
}
27+
28+
draw_image(name, x, y){
29+
image(this._images[name], x, y);
30+
}
31+
32+
draw_image_from_sequence(name, frame, x, y){
33+
image(this._image_sequences[name][frame], x, y);
34+
}
35+
36+
play_sound(name){
37+
this._sounds[name].play();
38+
}
39+
40+
stop_sound(name){
41+
if ( this._sounds[name].isPlaying() ) {
42+
this._sounds[name].stop();
43+
}
44+
}
45+
46+
all_assets_loaded(){
47+
return this._loaded_images >= this._total_assets_to_load;
48+
}
49+
50+
asset_loaded(){
51+
this._loaded_images++;
52+
}
53+
54+
}

src/imageLoader.js

Lines changed: 0 additions & 38 deletions
This file was deleted.

src/vignettes.js

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
import ImageLoader from "./imageLoader.js"
1+
import AssetLoader from "./assetLoader.js"
22

33
export default class Vignettes{
44

55
constructor(){
66
this._scenes = [];
7-
this._image_loader = new ImageLoader();
7+
this._asset_loader = new AssetLoader();
88
this._current_scene = 0;
99
this._recording = false;
1010
}
@@ -14,7 +14,7 @@ export default class Vignettes{
1414
}
1515

1616
draw(){
17-
if(this._scenes.length > 0){
17+
if(this._scenes.length > 0 && this._asset_loader.all_assets_loaded()){
1818
this._scenes[this._current_scene].draw();
1919
}
2020

@@ -41,13 +41,21 @@ export default class Vignettes{
4141
}
4242
}
4343

44+
manually_change_scenes(manually_change){
45+
this._manually_change_scenes = manually_change;
46+
}
47+
48+
go_to_scene(scene_number){
49+
this._current_scene = scene_number-1;
50+
}
51+
4452
key_pressed(){
4553

4654
console.log(keyCode, key);
4755

48-
if(key == "ArrowRight"){// 'rightArrow'
56+
if(key == "ArrowRight" && !this._manually_change_scenes){// 'rightArrow'
4957
this.increment_current_scene();
50-
}else if(key == "ArrowLeft"){// 'leftArrow'
58+
}else if(key == "ArrowLeft" && !this._manually_change_scenes){// 'leftArrow'
5159
this.decrement_current_scene();
5260
}else if(key == "r" && !this._recording){// 'r'
5361
this.begin_recording();
@@ -90,18 +98,27 @@ export default class Vignettes{
9098
this._recording = false;
9199
}
92100

93-
//-------------image loading and displaying------------------
101+
//-------------asset loading and displaying------------------
94102
load_image(name, file_type){
95-
this._image_loader.load_image(name, file_type);
103+
this._asset_loader.load_image(name, file_type);
96104
}
97105
load_image_sequence(name, file_type, sequence_length){
98-
this._image_loader.load_image_sequence(name, file_type, sequence_length);
106+
this._asset_loader.load_image_sequence(name, file_type, sequence_length);
107+
}
108+
load_sound(name, file_type){
109+
this._asset_loader.load_sound(name, file_type);
99110
}
100111
draw_image(name, x, y){
101-
this._image_loader.draw_image(name, x, y);
112+
this._asset_loader.draw_image(name, x, y);
102113
}
103114
draw_image_from_sequence(name, x, y, frame){
104-
this._image_loader.draw_image_from_sequence(name, frame, x, y);
115+
this._asset_loader.draw_image_from_sequence(name, frame, x, y);
116+
}
117+
play_sound(name){
118+
this._asset_loader.play_sound(name);
119+
}
120+
stop_sound(name){
121+
this._asset_loader.stop_sound(name);
105122
}
106123
//-----------------------------------------------------------
107124

0 commit comments

Comments
 (0)