-
Notifications
You must be signed in to change notification settings - Fork 0
/
rockmanL_v0.3.html
463 lines (392 loc) · 15 KB
/
rockmanL_v0.3.html
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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
<!doctype html>
<html lang="cn">
<head>
<meta charset="UTF-8">
<title>Rockman L</title>
<!-- 注意js的引入顺序,有依赖关系的 -->
<script src="js/Box2dWeb-2.1.a.3.js"></script>
<script src="iio-sdk/iioEngine-1.2.2.js"></script>
<script src="js/bone.js"></script>
</head>
<body></body>
<script>
var player_io = {};
function gameApp(io) {
var b2Vec2 = Box2D.Common.Math.b2Vec2
, b2AABB = Box2D.Collision.b2AABB
, b2BodyDef = Box2D.Dynamics.b2BodyDef
, b2Body = Box2D.Dynamics.b2Body
, b2FixtureDef = Box2D.Dynamics.b2FixtureDef
, b2Fixture = Box2D.Dynamics.b2Fixture
, b2World = Box2D.Dynamics.b2World
, b2PolygonShape = Box2D.Collision.Shapes.b2PolygonShape
, b2CircleShape = Box2D.Collision.Shapes.b2CircleShape
, b2MouseJointDef = Box2D.Dynamics.Joints.b2MouseJointDef;
var ctx = io.context,
canvas = io.canvas;
var GameOffsetX = 50,
GameOffsetY = 10,
GameWidth = canvas.width - GameOffsetX*2,
GameHeight = canvas.height - GameOffsetY*2;
var Default ={
fill:'black',
stroke:'white'
}
var player = {};
var gameObj = {}; //除player之外的所有obj
var PI = Math.PI,
deg = PI/180,//角度换弧度
px = 30, //物理世界度量单位换像素单位 15*px
bp = 1/30; //像素单位换物理世界度量单位 400*bp
//封装了io和box2d create body直接的关系
//由于目前需要获取box2d的body对象,很可能这方法要废弃
function createB2dAndIO(bodyDef,fixDef,getb2Body){
var obj,body;
body = world.CreateBody(bodyDef);
obj = io.addObj(body,-1).CreateFixture(fixDef)
.GetShape()
.prepGraphics(io.b2Scale);
return getb2Body ? body : obj;
}
function renderShape(body,fixDef){
return io.addObj(body,-1).CreateFixture(fixDef)
.GetShape()
.prepGraphics(io.b2Scale);
}
var world = io.addB2World(new b2World(
new b2Vec2(0, 20) //gravity
,true //allow sleep
),-1);
var createFixture = function(shape) {
var fixture = new b2FixtureDef();
fixture.density = 3;
fixture.friction = 0.8;
fixture.restitution = .3;
fixture.shape = shape;
return fixture;
};
var createBody = function(x, y) {
var b = new b2BodyDef();
b.position.Set(x, y);
b.type = b2Body.b2_dynamicBody;
b.linearDamping = .01; //线性阻尼
b.angularDamping = .01; //角阻尼
return b;
};
var initOuterWall = function(){
var thickness = 0.1; //墙壁的厚度的一半
var sysFill = Default.fill;
var sysStrok = Default.stroke;
var fixDef = createFixture(new b2PolygonShape);//不能链式编程
gameObj.outerWall = {};
fixDef.shape.SetAsBox( GameWidth*bp/2, thickness);//width,height
var bodyDef = createBody( canvas.width/2*bp, GameOffsetY*bp + thickness);//x,y
bodyDef.type = b2Body.b2_staticBody;//一般的都是动态物体,只有墙壁是静态物体
//top outer wall
var topWall = world.CreateBody(bodyDef);
gameObj.outerWall.topWall = topWall;
renderShape(topWall,fixDef).setFillStyle(sysFill).setStrokeStyle(sysStrok);
//bottom outer wall
bodyDef.position.Set(canvas.width/2*bp, (GameHeight+GameOffsetY)*bp-thickness);
var bottomWall = world.CreateBody(bodyDef);
gameObj.outerWall.bottomWall = bottomWall;
renderShape(bottomWall,fixDef).setFillStyle(sysFill).setStrokeStyle(sysStrok);
//left outer wall
fixDef.shape.SetAsBox( thickness, GameHeight*bp/2);
bodyDef.position.Set( GameOffsetX*bp+thickness, canvas.height/2*bp);
var leftWall = world.CreateBody(bodyDef);
gameObj.outerWall.leftWall = leftWall;
renderShape(leftWall,fixDef).setFillStyle(sysFill).setStrokeStyle(sysStrok);
//right outer wall
bodyDef.position.Set( (GameWidth+GameOffsetX)*bp -thickness , canvas.height/2*bp);
var rightWall = world.CreateBody(bodyDef);
gameObj.outerWall.rightWall = rightWall;
renderShape(rightWall,fixDef).setFillStyle(sysFill).setStrokeStyle(sysStrok);
}; initOuterWall();
var createSomeObject = function(){
//create some objects
var shape,width,height;
var bodyDef = createBody(0,0);
var fixDef;
for(var i = 0; i < 20; ++i) {
width = Math.random() + 0.2 //half width
height = Math.random() + 0.2 //half height
if(Math.random() > 0.5) {
fixDef = createFixture(new b2PolygonShape);
fixDef.shape.SetAsBox(width, height);
} else {
fixDef = createFixture(new b2CircleShape(width));
}
bodyDef.position.x = Math.random() * 10+10;
bodyDef.position.y = Math.random() * 10+5;
shape=io.addObj(world.CreateBody(bodyDef),-1).CreateFixture(fixDef).GetShape();
if (shape instanceof b2CircleShape)
shape.prepGraphics(io.b2Scale)
.setFillStyle(Default.fill)
.setStrokeStyle(Default.stroke)
.drawReferenceLine();
else
shape.prepGraphics(io.b2Scale)
.setFillStyle(Default.fill)
.setStrokeStyle(Default.stroke);
}
}; createSomeObject();
function Robot(data,x,y,size){
this.pos = {x: x, y: y};
this.bones = {};
this.bonesArray = [];
this.constraint = [];
this.size = size || 1;
this.body = {}; //robot的iio主体
this.b2body; //robot的box2d主体
this.isBackward = false; //表示robot是否向后(屏幕的左方)
this.preX; //水平翻转的所需x
this.preY; //身体晃动运动所需
this.waitingY;
//先暂定这两个玩意先
this.width = 100;
this.height = 200;
/***************** init iio Obj ********************/
var item,bone,jointName,boneName;
for(var i in data){
item = data[i];
boneName = i;
if(item.isMain){
bone = new Bone(x,y,item.size[0]*this.size,item.size[1]*this.size).addImage(item.imgsrc);
this.body = bone;
this.waitingY = y;//初始化waitingY
}else{
bone = new Bone(item.size[0]*this.size,item.size[1]*this.size).addImage(item.imgsrc);
}
if(typeof item.imgOffset != 'undefined')
bone.setImgOffset(item.imgOffset[0]*this.size,item.imgOffset[1]*this.size);
bone.angle = item.angle;
for(var j in item.joint){
jointName = j;
bone.setJoint(jointName,item.joint[jointName][0]*this.size,item.joint[jointName][1]*this.size);
}
if(item.constraint){
item.constraint.unshift(boneName)
this.constraint.push(item.constraint);
}
bone.zIndex = item.zIndex; //新增个人属性
this.bones[boneName] = bone;
//注意最后才addObj。因为iio每次addObj之后都会调用它的draw方法,而如果没有设置joint的话draw方法报错
io.addObj(bone,item.zIndex);
}
//自己画要对zIndex排序了,苦逼啊
var min = {zIndex:0},tempArray = [];
for(var i in this.bones)
tempArray.push(this.bones[i]);
this.bonesArray = tempArray.sort(function(a,b){
var aZIndex = a.zIndex || 0;
var bZIndex = b.zIndex || 0;
return aZIndex - bZIndex;
});
/************* init box2d Obj **************/
var fixDef = createFixture(new b2PolygonShape);
fixDef.shape.SetAsBox( 100*bp/2, 100*bp/2);//width,height
var bodyDef = createBody( canvas.width*bp/2, canvas.height*bp/2);//x,y
this.b2body = world.CreateBody(bodyDef);
this.b2body.CreateFixture(fixDef).GetShape().prepGraphics(io.b2Scale).setStrokeStyle('red');
renderShape(this.b2body,fixDef) //robot不交给iio管理,就不用renderShape
//注意这里新增了一个额外属性
this.b2body.isJumping = false;
var deg = Math.PI/180;
var robotBones = this.bones;
var _this = this.body; //robot的主体代替整个robot
var b2body = this.b2body;
var goSpeed = 6, backSpeed = 4.5;
this.update = function(cycle){
/**
* position change
*/
this.preX = _this.pos.x;
this.preY = _this.pos.y;
var playerPos = b2body.GetPosition();
if(input[UP] && !player.isJumping){
var force = new b2Vec2(0,-20);
b2body.isJumping = true;
b2body.ApplyImpulse(force, b2body.GetPosition());
}
if(input[RIGHT]){
var force = new b2Vec2(10,0);
b2body.ApplyImpulse(force, {x:playerPos.x,y:playerPos.y+0.5});
}
if(input[LEFT]){
var force = new b2Vec2(-10,0);
b2body.ApplyImpulse(force, {x:playerPos.x,y:playerPos.y+0.5});
}
/**
* animation change
*/
if (input[LEFT] && !input[RIGHT])
this.walk(cycle, this.isBackward ? 'go' : 'back');
else if (input[RIGHT] && !input[LEFT])
this.walk(cycle, this.isBackward ? 'back' : 'go');
else this.wait(cycle);
_this.pos.x = b2body.GetPosition().x*px;
_this.pos.y = b2body.GetPosition().y*px - 50;
/*********** 手臂和头跟着鼠标位置而改变 **************/
//如果使用手的关节会发生非常帅的震动动画
var handPos = robotBones.body.getJointAbsPos('head');
var angleD = Math.atan2(mousePos.y-handPos.y,mousePos.x-handPos.x);
//angleD 的正常范围是-1.57~1.57之间(3.14/2= 1.57 )
if(angleD >1.57 || angleD < -1.57 ){
this.isBackward = true;
this.preX = _this.pos.x; //update
_this.pos.x = canvas.width - _this.width -_this.pos.x;
b2body.SetPosition({x:_this.pos.x*bp,y:b2body.GetPosition().y});
angleD = Math.PI- angleD;
}else{
this.isBackward = false;
}
robotBones.armR.angle = angleD;
robotBones.weapon.angle = angleD;
robotBones.chest.angle = angleD;
robotBones.head.angle = angleD;
robotBones.armL.angle = robotBones.armR.angle-70*deg;
//手臂和手的角度是固定的
robotBones.handR.angle = angleD-90*deg;
robotBones.handL.angle = robotBones.armL.angle;
/************ 集合所有动作都要更新关节之间的位置 ***************/
for(var i = 0; i< this.constraint.length; i++){
var constraint = this.constraint[i];
var childBone = robotBones[constraint[0]];
var parentBone = robotBones[constraint[1]];
childBone.setPosByJoint(parentBone,constraint[2]);
}
//轮循碰撞发生列表来检测汽车是否接触地面
for(var cn = world.GetContactList(); cn != null; cn = cn.GetNext()){
var body1 = cn.GetFixtureA().GetBody();
var body2 = cn.GetFixtureB().GetBody();
if((body1 == player && body2 == gameObj.outerWall.bottomWall) ||
(body2 == player && body1 == gameObj.outerWall.bottomWall)){
b2body.isJumping = false;
}
}
};
this.wait = function(cycle){
this.preY = _this.pos.y;
//要身体下降的距离跟大腿弯身的距离要一致才能保证脚尖水平保持一致
robotBones.body.pos.y = Math.sin(cycle+Math.PI*-.6) * .3+ this.preY;
this.waitingY = _this.pos.y;
var waitLeg = function(boneA,boneB,c,thighB,isRight){
var thigh = 9*deg,
thighB = thighB*deg,
calf = 45*deg,
calfO = -90*deg;
var angleA = Math.sin(c)* thigh + thighB;
var angleB = Math.sin(c + calfO) * calf + calf;
boneA.angle = angleA;
boneB.angle = isRight ? 45*deg: 5*deg;
}
waitLeg(robotBones.legL, robotBones.calfL, cycle,-53,false);
waitLeg(robotBones.legR, robotBones.calfR, cycle ,0,true);
}
this.swing = function(parentBone,childBone,c,settings,isLeg){
var thigh = settings[0]*deg,
thighB = settings[1]*deg,
calf = settings[2]*deg,
calfO = settings[3]*deg;
var angleA = Math.sin(c)* thigh + thighB;
if(isLeg){
var angleB = Math.sin(c + calfO) * calf + calf;
}else{
var angleB = Math.sin(c - calfO) * calf + calf;
}
parentBone.angle = angleA;
childBone.angle = isLeg ? parentBone.angle+angleB : parentBone.angle-angleB;
}
//thigh thighB calf calfO
var settings_go_leg = [65, -20, 45, -105];
var settings_go_arm = [45, 0, 30, 180];
var settings_back_leg = [45, -13, 45, 140];
var settings_back_arm = [20, -11, 27, 180];
this.walk = function(cycle,goOrBack){
//身体晃动运动
this.preY = robotBones.body.pos.y;
robotBones.body.pos.y = Math.sin(cycle*2+Math.PI*3/2) * .8+ this.preY;
if(goOrBack == 'go') cycle+=0.08;
robotBones.body.angle = Math.sin(cycle) * 6*deg;
//head这里在没有武器的时候的动作,有武器的时候动作会被覆盖
robotBones.head.angle = Math.sin(cycle) * 10*deg;
var settings_leg = goOrBack == 'back' ? settings_back_leg : settings_go_leg;
var settings_arm = goOrBack == 'back' ? settings_back_arm : settings_go_arm;
this.swing(robotBones.legL, robotBones.calfL, cycle ,settings_leg,true);
this.swing(robotBones.legR, robotBones.calfR, cycle + Math.PI,settings_leg,true);
//this.swing(robotBones.armL, robotBones.handL,cycle + Math.PI,settings_arm,false);
//this.swing(robotBones.armR, robotBones.handR, cycle,settings_arm,false);
}
}
robot = new Robot(robotData,200,200,0.5);
var cycle=0;
io.setFramerate(60,function(){
//这里没有使用iio的setB2Framerate,因为有bug回事robot转身有问题。使用setFramerate没问题。
//理解内部代码后就可以我们只是简单的在iio更新的代码上,加上box2d物理世界的迭代就可以解决bug
//物理世界的迭代更新
world.Step(1/60, 10, 10);
cycle += 0.08;
robot.update(cycle);
io.draw(); //所有交给iio管理的obj,draw。包括物理世界的对象
/**
* 为什么我不把robot的骨碌都交给iio,因为robot有个水平翻转效果。
* robot的骨骼动画是渲染层,应该跟物理引擎层分开,水平旋转也不会影响物理引擎的渲染
**/
//水平翻转整套效果
if(robot.isBackward){
io.context.save();
io.context.translate(io.canvas.width, 0);
io.context.scale(-1, 1);
}
//遍历所有的骨头draw
for(var i = 0 ; i< robot.bonesArray.length; i++)
robot.bonesArray[i].draw(ctx);
if(robot.isBackward){
io.context.restore();
//注意这里需要恢复context没有旋转的时候的位置
//严格上水平翻转修改的pos.x应该在drawImage层面但是,我这里是整套的骨骼,骨碌上有运动
//简单的翻转不是我想要的效果。
robot.body.pos.x = robot.preX;
robot.b2body.SetPosition({x:robot.preX*bp,y:robot.b2body.GetPosition().y});
}
});
/********* input & contorler **********/
//必须给mousePos给默认值,不然刷新后没有发生mousemove,mousePos为空而报错
var mousePos = {x:io.canvas.width,y:0};
io.canvas.addEventListener('mousemove', function(event){
mousePos = io.getEventPosition(event);
});
var input = [];
var LEFT = 0;
var RIGHT = 1;
var UP = 2;
var DOWN = 3;
var SPACE = 4;
var updateInput = function(event, boolValue){
if(iio.keyCodeIs('left arrow', event) || iio.keyCodeIs('a', event)){
input[LEFT] = boolValue;
}
if(iio.keyCodeIs('right arrow', event) || iio.keyCodeIs('d', event)){
input[RIGHT] = boolValue;
}
if(iio.keyCodeIs('up arrow', event) || iio.keyCodeIs('w', event)){
input[UP] = boolValue;
}
if(iio.keyCodeIs('down arrow', event) || iio.keyCodeIs('s', event)){
input[DOWN] = boolValue;
}
if(iio.keyCodeIs('space',event)){
input[SPACE] = boolValue;
}
}
window.addEventListener('keydown',function(event){
updateInput(event,true);
});
window.addEventListener('keyup',function(event){
updateInput(event,false);
});
io.setBGColor('#555');
}; iio.start(gameApp);
</script>
</html>