-
Notifications
You must be signed in to change notification settings - Fork 2
/
Gruntfile.js
142 lines (123 loc) · 3.64 KB
/
Gruntfile.js
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
module.exports = function(grunt) {
// Project configuration.
grunt.initConfig({
pkg: grunt.file.readJSON('package.json'),
"connect": {
server: {
options: {
port: 8080,
open: 'http://localhost:8080/game/'
}
}
},
"sfx": {
all: {
options: {
src: "game/assets/sfx/*.wav"
}
}
},
"atlas": {
sprites: {
options: {
src: "game/assets/gfx/*.png",
name: "sprites",
path: "game/assets/gfx/atlas/",
format: "json",
powerOfTwo: true,
padding: 1,
scale: '100%',
trim: true
}
},
},
"watch": {
"atlas": {
files: ['**/assets/gfx/*.png' ],
tasks: ['atlas']
},
"scripts": {
files: ['game/index.html', 'game/assets/css/**', 'game/src/**', 'game/assets/gfx/atlas/**'],
options: {
livereload: true
}
},
"sfx": {
files: ['game/assets/sfx/*.wav'],
tasks: ['sfx'],
options: {
livereload: true
}
}
},
"clean": {
build: [ 'build/', 'game/assets/gfx/atlas/' ]
}
});
// Load the plugin that provides the "uglify" task.
grunt.loadNpmTasks('grunt-contrib-clean');
grunt.loadNpmTasks('grunt-contrib-connect');
grunt.loadNpmTasks('grunt-contrib-watch');
// Task to run webserver and watch
grunt.registerTask('run', ['connect', 'watch']);
// Task to build a texture atlas (spritesheet-js)
// https://github.com/krzysztof-o/spritesheet.js
// https://www.npmjs.org/package/spritesheet-js
grunt.task.registerMultiTask('atlas', 'Generate texture atlas', function() {
var done = this.async(); // Force into async mode and return handle to done() function
var options = this.options();
// Build new atlas
var spritesheet = require('spritesheet-js');
spritesheet(options.src, options, function (err) {
if (err) throw err;
console.log("Generated " + options.name + ".json");
done(); // Call done() when the async spritesheet generator is finished
});
});
/**
* Task to build mp3 and ogg format sfx resources from wav files
*/
grunt.task.registerMultiTask('sfx', 'Create ogg files from mp3s', function() {
var done = this.async();
var glob = require("glob");
var options = this.options();
// Build sounds
var fs = require('fs');
var exec = require('child_process').exec;
glob(options.src, {}, function (er, files) {
for(var i = 0; i < files.length; i++) {
var f = files[i];
f = f.replace('.wav', '');
var stat = fs.statSync(f + ".wav");
var updateMp3 = false;
var updateOgg = false;
try {
var statmp3 = fs.statSync(f + ".mp3");
if (stat.mtime > statmp3.mtime) updateMp3 = true;
} catch (ex) {
console.log(ex);
updateMp3 = true;
}
try {
var statogg = fs.statSync(f + ".ogg");
if (stat.mtime > statogg.mtime) updateOgg = true;
} catch(ex) {
console.log(ex);
updateOgg = true;
}
var cmd;
if (updateMp3) {
console.log("Updating " + f + ".mp3");
cmd = exec("avconv -y -v quiet -i " + f + ".wav -acodec libmp3lame -ab 128k " + f + ".mp3");
}
if (updateOgg) {
console.log("Updating " + f + ".ogg");
cmd = exec("avconv -y -v quiet -i " + f + ".wav -acodec libvorbis -ab 128k " + f + ".ogg");
}
}
});
done();
});
// Default task(s).
grunt.registerTask('default', ['clean:build', 'atlas', 'sfx']);
};