forked from ElementalSystems/complicit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
objects.js
234 lines (204 loc) · 6.99 KB
/
objects.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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
function r_cycle(count,time) { return (count%time)/time; }
function r_pingpong(count,time) { a=r_cycle(count,time*2); return (a<.5)?a*2:2-a*2; }
function r_limit(r,s,e) { return (r<s)?0:((r>e)?1:(r-s)/(e-s));}
function r_siso(r) { return 3*r*r-2*r*r*r;}
function r_i(r,s,e) { return s*(1-r)+e*r; }
function idiv(n,d) { return Math.floor(n/d);}
function step(x,step) {return Math.floor(x/step)*step;}
function adjustTowards(source, target, change)
{
var diff = target - source;
if (Math.abs(diff) < change)
return target;
if (diff > 0)
return source + change;
else
return source - change;
}
function weaveAction(timestart,xstart,ystart,xthrow,yhop,len)
{
return function(gameTime,frameTime) {
gameTime-=timestart;
this.x=xstart+r_i(r_siso(r_pingpong(gameTime,len)),0,xthrow);
this.y=ystart+r_i((r_limit(r_cycle(gameTime,len),.8,1)),0,yhop)+
idiv(gameTime,len)*yhop;
}
}
function wiggleAction(timestart,xstart,ystart,xthrow,yhop,len)
{
return function(gameTime,frameTime) {
gameTime-=timestart;
this.x=xstart+r_i(r_siso(r_pingpong(gameTime,len)),0,xthrow);
this.y=ystart+(gameTime/len)*yhop;
}
}
function slideAction(timestart,xstart,ystart,xthrow,ythrow,len)
{
return function(gameTime,frameTime) {
gameTime-=timestart;
this.x=xstart+r_i(r_siso(r_pingpong(gameTime,len)),0,xthrow);
this.y=ystart+r_i(r_cycle(gameTime,len),0,ythrow)+
idiv(gameTime,len)*ythrow;
}
}
function followControllerAction()
{
return function(gameTime,frameTime){
if (this.a===undefined) {
this.a=0;
this.x=50;
this.y=130;
board.avatar=this;
}
this.fireNow=false;
if (board.control_active) {
//preprocess the co-ordinates
var x=(board.control_x-50)/100;
if (x<-.45) x=-.45;
if (x>.45) x=.45;
var a=(board.control_y<100)?1:(150-board.control_y)/50;
this.x=adjustTowards(this.x,50+x*100,.05*frameTime);
this.a=adjustTowards(this.a,a,frameTime/1000);
this.fireNow=true;
}
if (board.keyList[68]||board.keyList[39]) {
this.x=adjustTowards(this.x,97,.05*frameTime);
this.fireNow=true;
}
if (board.keyList[65]||board.keyList[37]) {
this.x=adjustTowards(this.x,3,.05*frameTime);
this.fireNow=true;
}
if (board.keyList[87]||board.keyList[38]) {
this.a=adjustTowards(this.a,1,frameTime/1000);
this.fireNow=true;
}
if (board.keyList[83]||board.keyList[40]) {
this.a=adjustTowards(this.a,0,frameTime/1000);
this.fireNow=true;
}
if (board.keyList[32]||board.keyList[13]) {
this.fireNow=true;
}
if (this.fireNow) { //we are active
x=(this.x-50)/100; //go back with our actual point
var y=this.a*x*x+this.a; //basic space mapping y=ax^2+a from a-space to y-space
this.y=135-y*50;
this.pointAng=-Math.atan(2*this.a*x)-Math.PI/2; //gets the angle of the tangent from dy/dx=2ax
this.display.style.transform='rotate('+(this.pointAng+Math.PI/2)+'rad)';
}
}
}
function avatarFireAction()
{
return function(gameTime,fireCount){
if (this.fireNow)
addOb(sprites.aBullet,bulletAction(this.x,this.y,this.pointAng,100),null);
//record the action
board.shadowRec.push({x:this.x, y: this.y, pAng: this.pointAng, fire: this.fireNow });
}
}
function ghostFollowAction(followArray)
{
return function(gameTime,frameTime) {
var f=idiv(gameTime,250); //frame number in the array
var r=(gameTime%250)/250;
if (this.isDead) return;
if (f+1>=followArray.length) {
//we've run out of life so kill us
killOb(this);
return;
}
this.x=r_i(r,followArray[f].x,followArray[f+1].x);
this.y=r_i(r,followArray[f].y,followArray[f+1].y);
this.pointAng=r_i(r,followArray[f].pAng,followArray[f+1].pAng);
if (!isNaN(this.pointAng))
this.display.style.transform='rotate('+(this.pointAng+Math.PI/2)+'rad)';
}
}
function ghostFireAction(followArray)
{
return function(gameTime,fireCount){
if (this.isDead) return;
var f=idiv(gameTime,250); //frame number in the array
if (followArray[f].fire)
addOb(sprites.gBullet,bulletAction(this.x,this.y,this.pointAng,100),null);
}
}
function regularFireAction(fireOff,fireCycle,bullet,speed)
{
return function(gameTime,fireCount) {
if (!((fireCount+fireOff)%fireCycle))
addOb(bullet,bulletAction(this.x,this.y,3.14/2,speed),null);
}
}
function diveBombAction(fireOff,fireCycle,speed)
{
return function(gameTime,fireCount) {
if (!((fireCount+fireOff)%fireCycle)) {
if (this.y>-50) //only start the dive if you are low enough
programOb(this,diveAction(this.x,this.y,speed),null);
}
}
}
function diveAction(xstart,ystart,speed)
{ var xs=-xstart+board.avatar.x;
var ys=speed;
return function(gameTime,frameTime) {
this.x=xstart+xs*gameTime/1000;
this.y=ystart+ys*gameTime/1000;
}
}
function regularFireAngleAction(fireOff,fireCycle,bullet,speed,angle)
{
return function(gameTime,fireCount) {
if (!((fireCount+fireOff)%fireCycle)) {
var ang=angle*Math.random();
if (board.avatar.x>this.x)
ang*=-1;
addOb(bullet,bulletAction(this.x,this.y,3.14/2+ang/180*3.14,speed),null);
}
}
}
function dualFireAngleAction(fireOff,fireCycle,bullet,speed,angle)
{
return function(gameTime,fireCount) {
if (!((fireCount+fireOff)%fireCycle)) {
var ang=angle*Math.random();
if (board.avatar.x>this.x)
ang*=-1;
addOb(bullet,bulletAction(this.x,this.y,3.14/2+ang/180*3.14,speed),null);
addOb(bullet,bulletAction(this.x,this.y,3.14/2+ang/180*3.14/2,speed*.9),null);
}
}
}
function xZoneFireAction(zone,fireOff,fireCycle,bullet,speed)
{
return function(gameTime,fireCount) {
if (Math.abs(board.avatar.x-this.x)>zone) return;
if (!((fireCount+fireOff)%fireCycle))
addOb(bullet,bulletAction(this.x,this.y,3.14/2,speed),null);
}
}
function bulletAction(xstart,ystart,angle,speed)
{
var xs=Math.cos(angle)*speed;
var ys=Math.sin(angle)*speed;
return function(gameTime,frameTime) {
this.x=xstart+xs*gameTime/1000;
this.y=ystart+ys*gameTime/1000;
}
}
function sqAction(timestart,xstart,ystart,xthrow,ythrow,ytravel,len)
{
return function(gameTime,frameTime) {
gameTime-=timestart;
this.x=xstart
+r_i(r_siso(r_limit(r_cycle(gameTime,len),0,.25)),0,xthrow)
-r_i(r_siso(r_limit(r_cycle(gameTime,len),.5,.75)),0,xthrow);
this.y=ystart
+r_i(r_siso(r_limit(r_cycle(gameTime,len),.25,.5)),0,ythrow)
-r_i(r_siso(r_limit(r_cycle(gameTime,len),.75,1)),0,ythrow)
+(gameTime/len)*ytravel;
}
}