-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
56 lines (48 loc) · 2.16 KB
/
index.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
var translateAsp = require('./src/asp-to-cygnus');
var rensa = require('./src/brain');
var ctp = require('./src/cygnus-to-phaser-brain');
var translatePhaserBrain = require('./src/phaser-brain-to-code');
function AspPhaserGenerator(generatedAsp, initialPhaserFile) {
// Read each line of the ASP game.
var lines = generatedAsp.split('\n');
// For each line read,
for (var i=0; i<lines.length;i++){
// Remove any extra spaces,and add a period at the end if there isn't one.
lines[i] = lines[i].replace(/\s+/g, '');
if (lines[i]!="" && lines[i].slice(-1)!="."){
lines[i] = lines[i]+".";
}
// Remove redundant button information.
lines[i] = lines[i].replace("control_event(button(mouse_button,held))", 'control_event(mouse_button,held)');
lines[i] = lines[i].replace("control_event(button(mouse_button,pressed))", 'control_event(mouse_button,pressed)');
}
// Store the ASP game.
this.aspGame = lines;
// Store the initial Phaser file as a brain.
this.initialPhaser = rensa.makeBrain(JSON.parse(initialPhaserFile));
}
AspPhaserGenerator.prototype.generate = function(debug) {
// Create a Rensa brain from literal ASP.
var cygnus = rensa.makeBrain(translateAsp(this.aspGame));
// Translate this brain into Phaser Abstract Syntax given some initial Phaser assertions.
var generatedPhaserBrain = ctp.cygnusToPhaser(this.initialPhaser, cygnus);
// Write a Phaser program using this brain.
var gameProgram = translatePhaserBrain.writePhaserProgram(generatedPhaserBrain);
// If debug flag is true, show output at each step.
if (debug){
console.log("\n------------------------------");
console.log("Initial Phaser Brain: ");
console.log("------------------------------");
this.initialPhaser.prettyprint();
console.log("\n------------------------------");
console.log("Cygnus Brain: ");
console.log("------------------------------");
cygnus.prettyprint();
console.log("\n------------------------------");
console.log("Generated Phaser Brain: ");
console.log("------------------------------");
generatedPhaserBrain.prettyprint();
}
return gameProgram;
};
module.exports = AspPhaserGenerator;