-
Notifications
You must be signed in to change notification settings - Fork 0
/
rockmanL_v0.1.html
211 lines (184 loc) · 6.19 KB
/
rockmanL_v0.1.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
<!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>
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 PI = Math.PI,
deg = PI/180,//角度换弧度
px = 30, //物理世界度量单位换像素单位 15*px
bp = 1/30; //像素单位换物理世界度量单位 400*bp
//封装了io和box2d create body直接的关系
function createB2dAndIO(bodyDef,fixDef,getb2Body){
var obj,body;
body = world.CreateBody(bodyDef);
obj = io.addObj(body).CreateFixture(fixDef)
.GetShape()
.prepGraphics(io.b2Scale);
return getb2Body ? body : obj;
}
var world = io.addB2World(new b2World(
new b2Vec2(0, 20) //gravity
, true //allow sleep
));
var createFixture = function(shape) {
var fixture = new b2FixtureDef();
fixture.density = 3;
fixture.friction = 0.6;
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);//不能链式编程
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
createB2dAndIO(bodyDef,fixDef).setFillStyle(sysFill).setStrokeStyle(sysStrok);
//bottom outer wall
bodyDef.position.Set(canvas.width/2*bp, (GameHeight+GameOffsetY)*bp-thickness);
createB2dAndIO(bodyDef,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);
createB2dAndIO(bodyDef,fixDef).setFillStyle(sysFill).setStrokeStyle(sysStrok);
//right outer wall
bodyDef.position.Set( (GameWidth+GameOffsetX)*bp -thickness , canvas.height/2*bp);
createB2dAndIO(bodyDef,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)).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();
var player;
var initPlayer = function(){
var fixDef = createFixture(new b2PolygonShape);
fixDef.shape.SetAsBox( 50*bp/2, 100*bp/2);//width,height
var bodyDef = createBody( canvas.width*bp/2, canvas.height*bp/2);//x,y
player = world.CreateBody(bodyDef);
//for style
io.addObj(player).CreateFixture(fixDef).GetShape().prepGraphics(io.b2Scale)
.setFillStyle(Default.fill).setStrokeStyle(Default.stroke);
}; initPlayer();
//Set the update loop
var mouseJoint;
io.setB2Framerate(60, function(){
if(input[UP]){
var body = player;
var md = new b2MouseJointDef();
md.bodyA = world.GetGroundBody();
md.bodyB = body;
md.target.Set(body.GetPosition().x, body.GetPosition().y);
md.collideConnected = true;
md.maxForce = 300.0 * body.GetMass();
//joint类不需要像body类和fixture类麻烦
mouseJoint = io.addObj(world.CreateJoint(md).prepGraphics().setStrokeStyle('white').setLineWidth(1));
body.SetAwake(true);
}
if(mouseJoint) {
if(input[UP]) {
mouseJoint.SetTarget(new b2Vec2(body.GetPosition().x, (body.GetPosition().y-100*bp)));
} else {
world.DestroyJoint(mouseJoint);
io.rmvObj(mouseJoint);
mouseJoint = null;
}
}
console.log(mouseJoint);
});
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>