mirror of
https://github.com/logsol/chuck.js.git
synced 2026-05-11 10:37:34 +00:00
103 lines
No EOL
3.1 KiB
JavaScript
103 lines
No EOL
3.1 KiB
JavaScript
define([
|
|
"Game/" + GLOBALS.context + "/GameObjects/GameObject",
|
|
"Lib/Vendor/Box2D",
|
|
"Game/Config/Settings"
|
|
],
|
|
|
|
function (Parent, Box2D, Settings) {
|
|
|
|
function Item(physicsEngine, uid, options) {
|
|
this.options = {
|
|
category: options.category,
|
|
image: options.image,
|
|
name: options.name,
|
|
type: options.type,
|
|
grabAngle: parseFloat(options.grabAngle),
|
|
weight: parseFloat(options.weight),
|
|
width: parseFloat(options.width),
|
|
height: parseFloat(options.height),
|
|
rotation: parseFloat(options.rotation),
|
|
bounce: parseFloat(options.bounce),
|
|
x: parseFloat(options.x),
|
|
y: parseFloat(options.y)
|
|
};
|
|
Parent.call(this, physicsEngine, uid);
|
|
this.createFixture();
|
|
this.body.ResetMassData();
|
|
this.flipDirection = 1;
|
|
}
|
|
|
|
Item.prototype = Object.create(Parent.prototype);
|
|
|
|
Item.prototype.getBodyDef = function() {
|
|
|
|
var bodyDef = new Box2D.Dynamics.b2BodyDef();
|
|
bodyDef.type = Box2D.Dynamics.b2Body.b2_dynamicBody;
|
|
bodyDef.position.x = this.options.x / Settings.RATIO;
|
|
bodyDef.position.y = this.options.y / Settings.RATIO;
|
|
bodyDef.angle = 0;
|
|
|
|
return bodyDef;
|
|
}
|
|
|
|
Item.prototype.createFixture = function () {
|
|
var self = this;
|
|
|
|
var itemShape;
|
|
var w = this.options.width / Settings.RATIO;
|
|
var h = this.options.height / Settings.RATIO;
|
|
|
|
if(this.options.type == 'circle'){
|
|
var r = (w + h) / 4 ;
|
|
itemShape = new Box2D.Collision.Shapes.b2CircleShape();
|
|
itemShape.SetRadius(r);
|
|
itemShape.SetLocalPosition(new Box2D.Common.Math.b2Vec2(0, -r));
|
|
} else {
|
|
itemShape = new Box2D.Collision.Shapes.b2PolygonShape();
|
|
itemShape.SetAsOrientedBox(w / 2, h / 2, new Box2D.Common.Math.b2Vec2(0, -(h/2)));
|
|
}
|
|
|
|
|
|
var fixtureDef = new Box2D.Dynamics.b2FixtureDef();
|
|
fixtureDef.shape = itemShape;
|
|
|
|
var offset = 4,
|
|
factor = 80;
|
|
var density = ((this.options.weight + offset) / this.options.width / this.options.height) * factor;
|
|
fixtureDef.density = density;
|
|
fixtureDef.friction = Settings.ITEM_FRICTION;
|
|
|
|
fixtureDef.restitution = this.options.bounce
|
|
? this.options.bounce / 10
|
|
: Settings.ITEM_RESTITUTION;
|
|
|
|
fixtureDef.isSensor = false;
|
|
|
|
fixtureDef.userData = {
|
|
onCollisionChange: this.onCollisionChange.bind(this)
|
|
}
|
|
|
|
this.body.CreateFixture(fixtureDef);
|
|
}
|
|
|
|
Item.prototype.flip = function(direction) {
|
|
this.flipDirection = direction;
|
|
|
|
// FIXME: implement body flip if necessary
|
|
};
|
|
|
|
Item.prototype.beingGrabbed = function(player) {
|
|
// overwrite if necessary
|
|
};
|
|
|
|
Item.prototype.beingReleased = function(player) {
|
|
// overwrite if necessary
|
|
};
|
|
|
|
Item.prototype.onCollisionChange = function(isColliding, fixture, info) {
|
|
// overwrite if necessary
|
|
};
|
|
|
|
return Item;
|
|
|
|
}); |