-
Notifications
You must be signed in to change notification settings - Fork 0
/
director.js
executable file
·135 lines (123 loc) · 3.85 KB
/
director.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
var Director = function(directorStat) {
/*
The director handles the game logic
*/
this.difficulty = directorStat.diff; // should be between (0,1), 0 is easiest, 1 most difficult
this.comboTime = (2-this.difficulty) * directorStat.comboTime;
this.playerLives = directorStat.lives;
this.triggers = directorStat.triggers; // Array of triggers. Trigger is a literal w/ attributes: time, callback
this.songDetector = new BeatDetector();
this.inputDetector = new BeatDetector();
};
Director.prototype = {
init: function() {
Game.actors = [];
Game.playerScore = 0;
var player = new PlayerShip(Game.playerStat);
Game.actors.push(player);
Game.player = Game.actors[Game.actors.length-1];
Game.projectiles = [];
},
adjustDifficulty: function(diffDelta) {
this.difficulty = Math.max(this.difficulty + diffDelta, 1);
},
resolvePhysics: function() {
var physicals = Game.actors.concat(Game.projectiles)
for (var i=0; i < physicals.length; i++) {
var physical = physicals[i];
for (var j = i+1; j < physicals.length; j++) {
var otherPhysical = physicals[j];
if (physical.hasConflict(otherPhysical)) {
physical.resolveConflict(otherPhysical);
}
}
physical.move();
}
},
spawnEnemy: function(enemyType,numEnemies) {
var enInfo = enemyInfo[enemyType];
var xDelta = Game.width / (numEnemies + 1);
var R = enInfo.r;
var x = 0;
var y = 1.5*R;
var okay = Game.isAreaClear(x,y,R);
for(var i=0; i<numEnemies; i++){
if(Game.actors.length <= Game.maxEnemies){
x += xDelta;
var attempts = 0;
while(!okay && attempts < 20){
x += (Math.random() - 0.5) * R;
y += (Math.random() - 0.5) * R;
okay = Game.isAreaClear(x,y,R);
attempts += 1
}
var enemy = new EnemyShip(x,y,enInfo);
Game.actors.push(enemy);
}
}
},
nextTrigger: function(timestamp) {
if(this.triggers.length > 0){
trigger = this.triggers[0];
if (trigger.time < timestamp){
this.triggers.shift();
return trigger;
}
}
return null;
},
resolveAlive: function() {
for(var i = Game.actors.length-1; i>=0; i--){
if(!Game.actors[i].alive){
Game.display.animateDeath(Game.actors[i]);
if(Game.actors[i] != Game.player){
this.updateScore(Game.actors[i].pointValue);
Game.actors.splice(i,1); //remove dead actors from list
}
else{
Game.player.reset();
this.playerLives -= 1;
this.updateScore(-1 * Game.playerScore/4)
if(this.playerLives < 0){
Game.over(true);
}
}
}
}
for(var i = Game.projectiles.length-1; i>=0; i--){
if(Game.projectiles[i].alive = false){
Game.projectiles.splice(i,1);
}
}
},
resolveLogic: function() {
for(var i = Game.actors.length-1; i>=0; i--){
Game.actors[i].logic();
}
},
updateScore: function(scoreDelta) {
Game.playerScore += scoreDelta * (1+this.difficulty);
},
detect: function() {
this.songDetector.sample(audio.analyser);
if(this.songDetector.beatChance > Game.beatThreshold) {
this.triggers.push({time: Game.gameTime + 10, callback: function(){Game.director.spawnEnemy(ENUM.MINOR,5)}} )
}
this.inputDetector.sample(inputAnalyser);
if(this.inputDetector.beatChance > Game.inputThreshold) {
Game.player.basicLaser();
Game.player.boom();
Game.player.gun();
}
},
run: function() {
this.resolvePhysics();
this.resolveAlive();
this.resolveLogic();
this.detect();
next = this.nextTrigger(Game.gameTime);
if(next != null){
next.callback();
}
}
};