-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfurniture.js
46 lines (40 loc) · 1.62 KB
/
furniture.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
function Furniture(img, x, y, rotation=0, isRound=false) {
x = x+offset;
y = y+offset;
this.img = img;
// this.img = new createjs.Shape();
// this.img.graphics.beginFill("red").rect(0, 0, img.image.width, img.image.height);
this.img.regX = img.image.width/2;
this.img.regY = img.image.height/2;
this.img.x = x;
this.img.y = y;
this.img.rotation = rotation;
stage.addChild(this.img);
var boxFD = {
density: 1.0,
friction: 0.5,
restitution: 0.5,
};
this.box = world.createDynamicBody(Vec2(x/scale, y/scale));
if(!isRound)
this.box.createFixture(planck.Box(img.image.width / (2 * scale), img.image.height / (2 * scale)), boxFD);
else
this.box.createFixture(planck.Circle(img.image.width / (2 * scale)), boxFD);
this.box.setTransform(this.box.getPosition(), rotation*(Math.PI/180));
let frictionstr = (img.image.width*img.image.height/2000);
world.createJoint(planck.FrictionJoint({collideConnected : true, maxForce: frictionstr, maxTorque: frictionstr}, this.box, walls));
var self = this;
this.img.on("mousedown", function(evt) {
mouseJoint = planck.MouseJoint({maxForce: 300}, mouseGround, self.box, Vec2(evt.stageX / scale, evt.stageY / scale));
world.createJoint(mouseJoint);
});
this.img.on("pressup", function(evt) {
world.destroyJoint(mouseJoint);
mouseJoint = null;
});
this.update = function(){
this.img.x = this.box.getPosition().x*scale;
this.img.y = this.box.getPosition().y*scale;
this.img.rotation = this.box.getAngle()*(180/Math.PI);
}
}