-
Notifications
You must be signed in to change notification settings - Fork 0
/
world.js
178 lines (148 loc) · 5.01 KB
/
world.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
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
exports.World = World
var sp = getSpotifyApi(1);
var constants = sp.require("constants");
var box = sp.require("box");
var spectrum = sp.require('spectrum');
var bands = spectrum.BAND10;
var obstacle = sp.require('obstacle');
function World() {
this.upper = new Array();
this.lower = new Array();
this.lastSounds = new Array();
this.lastSum = 0;
this.songBPM = constants.DEFAULT_BPM;
this.obstacles = new Array();
for(var i = 0; i<constants.STEPS; ++i) {
this.upper[i] = new box.Box(5);
this.lower[i] = new box.Box(5);
this.obstacles[i] = new obstacle.Obstacle(0,false);
}
this.mapGeneratorValue = 0;
this.upperDB = 0;
this.offset = 0;
this.lightningEffectValue = 0;
this.amplitude = 1;
this.sinoffset = 0;
console.log("adding event handler");
(function(t) {
spectrum.init(function(s) {
if(isNaN(t.mapGeneratorValue)) {
//Some weird stuff that we have to prevent here
console.log("It happend again!!");
t.mapGeneratorValue = 0;
}
var data = spectrum.normalize(s, 100);
var spectrumSum = 0;
for(var i=0; i<data.spectruml.length; ++i) {
spectrumSum += data.spectruml[i];
}
t.setMapGeneratorValue(spectrumSum);
t.setLightningEffectValue(spectrumSum);
}, bands);
})(this);
}
World.prototype.setBPM = function(val) {
console.log('BPM being changed to: ',val);
this.songBPM = val;
var t = this;
this.bpmTimer = window.setInterval(function(){t.bpmTick()}, (60/(this.songBPM/constants.BPM_DIVIDER))*1000/constants.PERIOD_SLICE, false);
}
World.prototype.getBPM = function() {
return this.songBPM;
}
World.prototype.setMapGeneratorValue = function(spectrumSum) {
var currentValue = this.mapGeneratorValue;
var nextValue = spectrumSum*spectrumSum/3000;
var down = nextValue < currentValue;
var change = Math.min(Math.abs(nextValue - currentValue),20+constants.DIFFICULTY_MULT*this.getDifficulty());
this.mapGeneratorValue = currentValue + (down?-change:change);
// this.mapGeneratorValue = Math.min(this.mapGeneratorValue,200);
}
World.prototype.setLightningEffectValue = function(spectrumSum) {
this.lightningEffectValue = spectrumSum*spectrumSum/600;
}
World.prototype.bpmTick = function() {
if(this.sinoffset == undefined) this.sinoffset = 0;
this.sinoffset += 2*Math.PI / constants.PERIOD_SLICE;
if(this.sinoffset > 2*Math.PI) {
this.sinoffset -= 2*Math.PI;
console.log("TICK");
}
}
World.prototype.update = function() {
this.fetchValues()
}
World.prototype.reset = function() {
this.upper = new Array();
this.lower = new Array();
this.mapGeneratorValue = 0;
this.upperDB = 0;
this.offset = 0;
this.lightningEffectValue = 0;
this.amplitude = 1;
}
World.prototype.setDifficulty = function(difficulty) {
this.difficulty = difficulty;
}
World.prototype.getDifficulty = function() {
return this.difficulty;
}
World.prototype.render = function(context) {
var dx = constants.WIDTH/constants.STEPS;
var frequency = Math.PI/constants.STEPS;
++this.offset;
for(var i = 0; i<constants.STEPS; ++i) {
var j = i+this.offset%constants.STEPS*2;
red = Math.floor(Math.sin(frequency*j + 0) * 127 + 128);
green = Math.floor(Math.sin(frequency*j + 2) * 127 + 128);
blue = Math.floor(Math.sin(frequency*j + 4) * 127 + 128);
red -= 50;
green -= 50;
blue -= 50;
red += Math.floor(255*(this.lightningEffectValue-300)/500);
green += Math.floor(255*(this.lightningEffectValue-300)/500);
blue += Math.floor(255*(this.lightningEffectValue-300)/500);
context.fillStyle = "rgb("+red+","+green+","+blue+")";
context.fillRect(dx*i,0,dx, this.upper[i].height);
context.fillRect(dx*i,constants.HEIGHT-this.lower[i].height,dx,this.lower[i].height);
if (this.obstacles[i].exist) {
context.fillRect(dx*i,this.obstacles[i].y, this.obstacles[i].width, this.obstacles[i].height);
}
}
}
World.prototype.getLowerHeight = function(x) {
var i = Math.floor(x/constants.WIDTH*constants.STEPS);
return this.lower[i].height;
}
World.prototype.getUpperHeight = function(x) {
var i = Math.floor(x/constants.WIDTH*constants.STEPS);
return this.upper[i].height;
}
World.prototype.getLowerValue = function(x) {
return x+this.getDifficulty()*constants.DIFFICULTY_MULT;
}
World.prototype.getUpperValue = function(x) {
return constants.HEIGHT-x-constants.INITIAL_WIDTH+Math.floor(10*Math.random()-5);
}
/**
* Fetches new values from the map generator local variable, and
* puts them into the world data structures, making use of
* difficulty functions etc, etc.
*
*/
World.prototype.fetchValues = function() {
// use latest value of private variable
var usedValue = this.mapGeneratorValue;
usedValue += (usedValue*constants.AMPLITUDE_MULT)*Math.sin(this.sinoffset);// replace with song BPM
// fetch values for lower & upper
var newLower = new box.Box(this.getLowerValue(usedValue));
var newUpper = new box.Box(this.getUpperValue(usedValue));
// update values
delete this.upper.shift();
delete this.lower.shift();
delete this.obstacles.shift();
this.upper.push(newUpper);
this.lower.push(newLower);
this.obstacles.push(newObstacle);
// console.log("lower len: "+this.lower.length);
}