-
Notifications
You must be signed in to change notification settings - Fork 0
/
Player.js
235 lines (207 loc) · 9.7 KB
/
Player.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
235
var playerType = 2857; //signifies player parts
var playerShieldType = 4781 //used for ring around player
/*
var pvers = [ //vertices of all bodies of player
[[0.143,-.060], [-.143,-.060], [-.025,0.058], [0.025,0.058]],
[[0.060,-.143], [-.060,-.143], [-.143,-.060], [0.143,-.060]],
[[-.143,-.060], [-.500,-.210], [-.500,0.210], [-.058,0.025]],
[[-.058,0.025], [-.500,0.210], [-.210,0.500], [-.025,0.058]],
[[0.025,0.058], [-.025,0.058], [-.210,0.500], [0.210,0.500]],
[[0.058,0.025], [0.025,0.058], [0.210,0.500], [0.500,0.210]],
[[0.500,-.210], [0.143,-.060], [0.058,0.025], [0.500,0.210]],
[[0.500,-.210], [0.210,-.500], [0.151,-.360], [0.360,-.151]],
[[0.360,-.151], [0.151,-.360], [0.060,-.143], [0.143,-.060]],
[[0.210,-.500], [-.210,-.500], [-.151,-.360], [0.151,-.360]],
[[0.151,-.360], [-.151,-.360], [-.060,-.143], [0.060,-.143]],
[[-.210,-.500], [-.500,-.210], [-.360,-.151], [-.151,-.360]],
[[-.151,-.360], [-.360,-.151], [-.143,-.060], [-.060,-.143]],
];*/
/*
var pvers = [ //vertices of all bodies of player
[new b2Vec2(0.025,0.058), new b2Vec2(-.025,0.058), new b2Vec2(-.143,-.060), new b2Vec2(0.143,-.060)],
[new b2Vec2(0.143,-.060), new b2Vec2(-.143,-.060), new b2Vec2(-.060,-.143), new b2Vec2(0.060,-.143)],
[new b2Vec2(-.058,0.025), new b2Vec2(-.500,0.210), new b2Vec2(-.500,-.210), new b2Vec2(-.143,-.060)],
[new b2Vec2(-.025,0.058), new b2Vec2(-.210,0.500), new b2Vec2(-.500,0.210), new b2Vec2(-.058,0.025)],
[new b2Vec2(0.210,0.500), new b2Vec2(-.210,0.500), new b2Vec2(-.025,0.058), new b2Vec2(0.025,0.058)],
[new b2Vec2(0.500,0.210), new b2Vec2(0.210,0.500), new b2Vec2(0.025,0.058), new b2Vec2(0.058,0.025)],
[new b2Vec2(0.500,0.210), new b2Vec2(0.058,0.025), new b2Vec2(0.143,-.060), new b2Vec2(0.500,-.210)],
[new b2Vec2(0.360,-.151), new b2Vec2(0.151,-.360), new b2Vec2(0.210,-.500), new b2Vec2(0.500,-.210)],
[new b2Vec2(0.143,-.060), new b2Vec2(0.060,-.143), new b2Vec2(0.151,-.360), new b2Vec2(0.360,-.151)],
[new b2Vec2(0.151,-.360), new b2Vec2(-.151,-.360), new b2Vec2(-.210,-.500), new b2Vec2(0.210,-.500)],
[new b2Vec2(0.060,-.143), new b2Vec2(-.060,-.143), new b2Vec2(-.151,-.360), new b2Vec2(0.151,-.360)],
[new b2Vec2(-.151,-.360), new b2Vec2(-.360,-.151), new b2Vec2(-.500,-.210), new b2Vec2(-.210,-.500)],
[new b2Vec2(-.060,-.143), new b2Vec2(-.143,-.060), new b2Vec2(-.360,-.151), new b2Vec2(-.151,-.360)],
];
*/
var pvers = [ //vertices of all bodies of player
[new b2Vec2(0.025,0.058), new b2Vec2(-.025,0.058), new b2Vec2(-.143,-.060), new b2Vec2(0.143,-.060)],
[new b2Vec2(0.143,-.060), new b2Vec2(-.143,-.060), new b2Vec2(-.060,-.143), new b2Vec2(0.060,-.143)],
[new b2Vec2(-.058,0.025), new b2Vec2(-.500,0.210), new b2Vec2(-.500,-.210), new b2Vec2(-.143,-.060)],
[new b2Vec2(-.025,0.058), new b2Vec2(-.210,0.500), new b2Vec2(-.500,0.210), new b2Vec2(-.058,0.025)],
[new b2Vec2(0.210,0.500), new b2Vec2(-.210,0.500), new b2Vec2(-.025,0.058), new b2Vec2(0.025,0.058)],
[new b2Vec2(0.500,0.210), new b2Vec2(0.210,0.500), new b2Vec2(0.025,0.058), new b2Vec2(0.058,0.025)],
[new b2Vec2(0.500,0.210), new b2Vec2(0.058,0.025), new b2Vec2(0.143,-.060), new b2Vec2(0.500,-.210)],
[new b2Vec2(0.360,-.151), new b2Vec2(0.151,-.360), new b2Vec2(0.210,-.500), new b2Vec2(0.500,-.210)],
[new b2Vec2(0.143,-.060), new b2Vec2(0.060,-.143), new b2Vec2(0.151,-.360), new b2Vec2(0.360,-.151)],
[new b2Vec2(0.151,-.360), new b2Vec2(-.151,-.360), new b2Vec2(-.210,-.500), new b2Vec2(0.210,-.500)],
[new b2Vec2(0.060,-.143), new b2Vec2(-.060,-.143), new b2Vec2(-.151,-.360), new b2Vec2(0.151,-.360)],
[new b2Vec2(-.151,-.360), new b2Vec2(-.360,-.151), new b2Vec2(-.500,-.210), new b2Vec2(-.210,-.500)],
[new b2Vec2(-.060,-.143), new b2Vec2(-.143,-.060), new b2Vec2(-.360,-.151), new b2Vec2(-.151,-.360)],
];
var Player = {
"size" : 5, //controls size of player
"thrustMod" : 10000, //maximum thrust force in any direction
"maxHealth" : 500, //player maximum health
"health" : 0, //player health
"shield" : null, //holds player "forcefield", also what holds player together
"parts" : [], //parts comprising player, parts[0] position used as player center
"joints" : [], //joints holding player character together
"alive" : true, //so the dead routine doesn't repeat
init : function() { //initializes the player at starting point
this.health = this.maxHealth; //set health to maximum
for(i = 0; i < pvers.length; i++) { //scales vertex coordinates to size of object
for(j = 0; j < pvers[i].length; j++) {
pvers[i][j].Multiply(this.size);
}
}
this.shield = makeObject(playerShieldType, 20, 40, 0, this.size * 0.65);
for(i = 0; i < pvers.length; i++) {
this.parts.push(makeObject(playerType, 20, 40, 0, pvers[i]));
objectList.push(this.parts[i]);
jdef.Initialize(this.shield.body, this.parts[i].body, this.shield.body.GetWorldCenter());
jdef.collideConnected = false;
this.joints.push(world.CreateJoint(jdef));
}
},
draw : function() {
var scale = 2;
var pos = this.parts[0].body.GetPosition();
var cor;
//var theta = this.body.GetAngle();
var dims = [2,2];
//FIXME: add colors
var myVertices = [];
for(i = 0; i < pvers.length; i++){
myVertices = [];
for(j = 0; j < pvers[i].length; j++){
cor = coordTrans(pvers[i][j].x,pvers[i][j].y,this.parts[0].body.GetPosition().x,this.parts[0].body.GetPosition().y,this.parts[0].body.GetAngle());
//cor = coordTrans(pvers[i][j].x,pvers[i][j].y,0,0,this.parts[0].body.GetAngle());
myVertices.push(cor[0]/scale);
myVertices.push(cor[1]/scale);
myVertices.push(0/scale);
}
//pos.x = this.parts[0].body.GetPosition().x * 5;
//pos.y = -this.parts[0].body.GetPosition().y * 5;
pos.x = 0;
pos.y = 0;
addSquare2(pos, myVertices);
}
/*
var myVertices = [ -dims[0]/2/scale, dims[1]/2/scale, 0, //left-top
dims[0]/2/scale, dims[1]/2/scale, 0, //right-top
dims[0]/2/scale, -dims[1]/2/scale, 0, //right-bottom
-dims[0]/2/scale, -dims[1]/2/scale, 0 ]; //left-bottom
pos.x = pos.x * 10;
pos.y = pos.y * 10;
addSquare(pos, myVertices);
*/
},
drawShield : function() {
var scale = 2;
var pos = this.parts[0].body.GetPosition();
var cor;
var dims = [2,2];
var myVertices = [];
myVertices.push(pos.x - 1.0);
myVertices.push(pos.y + 1.0);
myVertices.push(0/scale);
myVertices.push(pos.x + 1.0);
myVertices.push(pos.y + 1.0);
myVertices.push(0/scale);
myVertices.push(pos.x + 1.0);
myVertices.push(pos.y - 1.0);
myVertices.push(0/scale);
myVertices.push(pos.x - 1.0);
myVertices.push(pos.y - 1.0);
myVertices.push(0/scale);
addSquare2(pos, myVertices);
},
action : function() {
if (this.health <= 0 && this.alive) {this.die();}
if (this.alive) {
this.shield.body.ApplyTorque((-this.shield.body.GetAngle()*20-this.shield.body.GetAngularVelocity())*100000); //corrects angle
pos = this.shield.body.GetPosition();
if (pos.y < 5) { //Up correction
this.shield.ctrForce(new b2Vec2(this.thrustMod*(5-pos.y)*-Math.sin(theta),this.thrustMod*(5-pos.y)*Math.cos(theta)));
}
if (pos.y > 75) { //Down correction
this.shield.ctrForce(new b2Vec2(-this.thrustMod*(pos.y-75)*-Math.sin(theta),-this.thrustMod*(pos.y-75)*Math.cos(theta)));
}
if (pos.x > 135) { //Left correction
this.shield.ctrForce(new b2Vec2(-this.thrustMod*(pos.x-135)*Math.cos(theta),-this.thrustMod*(pos.x-135)*Math.sin(theta)));
}
if (pos.x < 5) { //Right correction
this.shield.ctrForce(new b2Vec2(this.thrustMod*(5-pos.x)*Math.cos(theta),this.thrustMod*(5-pos.x)*Math.sin(theta)));
}
theta = this.parts[0].body.GetAngle();
if (38 in keysDown || 87 in keysDown) { //Up
this.shield.ctrForce(new b2Vec2(-this.thrustMod*-Math.sin(theta),-this.thrustMod*Math.cos(theta)));
}
if (40 in keysDown || 83 in keysDown) { //Down
this.shield.ctrForce(new b2Vec2(this.thrustMod*-Math.sin(theta),this.thrustMod*Math.cos(theta)));
}
if (37 in keysDown || 65 in keysDown) { //Left
this.shield.ctrForce(new b2Vec2(-this.thrustMod*Math.cos(theta),-this.thrustMod*Math.sin(theta)));
}
if (39 in keysDown || 68 in keysDown) { //Right
this.shield.ctrForce(new b2Vec2(this.thrustMod*Math.cos(theta),this.thrustMod*Math.sin(theta)));
}
if (32 in keysDown) {
//TODO: Space bar action
}
}
},
die : function() { //activates on player death
/*
for(i = this.joints.length - 1; i >= 0; i--) { //destroy joints on player model
world.DestroyJoint(this.joints[i]);
this.alive = false;
}*/
//this.shield.body.DestroyFixture(this.shield.fixture);
world.DestroyBody(this.shield.body);
var temp;
this.shield = null;
this.alive = false;
for(i = 0; i < this.parts.length; i++){
temp = pvers[i][0].Copy();
temp.Add(pvers[i][1]);
temp.Add(pvers[i][2]);
temp.Add(pvers[i][3]);
temp.Multiply((Math.random())*this.parts[i].body.GetMass());
this.parts[i].ctrForce(temp);
}
/*
draw : function() { //draws the player character
var cor;
theContext.strokeStyle = "#000000";
for(i = 0; i < pvers.length; i++){
theContext.beginPath();
cor = coordTrans(pvers[i][0].x,pvers[i][0].y,this.parts[0].body.GetPosition().x,this.parts[0].body.GetPosition().y,this.parts[0].body.GetAngle());
theContext.moveTo(cor[0]*10,cor[1]*10);
for(j = 1; j < pvers[i].length; j++){
cor = coordTrans(pvers[i][j].x,pvers[i][j].y,this.parts[0].body.GetPosition().x,this.parts[0].body.GetPosition().y,this.parts[0].body.GetAngle());
theContext.lineTo(cor[0]*10,cor[1]*10);
}
theContext.closePath();
theContext.stroke();
}
},*/
}
};
function playerPartDraw() {
theContext.beginPath();
theContext.arc(this.body.GetPosition().x, this.body.GetPosition().y,10,0,Math.PI /2,true);
theContext.closePath();
theContext.stroke();
theContext.fill();
}