mirror of
https://github.com/logsol/chuck.js.git
synced 2026-05-11 10:37:34 +00:00
some more renaming
This commit is contained in:
parent
9de3147406
commit
bd45f538c7
34 changed files with 0 additions and 0 deletions
114
app/Game/Core/Physics/Doll.js
Normal file
114
app/Game/Core/Physics/Doll.js
Normal file
|
|
@ -0,0 +1,114 @@
|
|||
define(["Vendor/Box2D", "Chuck/Constants", "Chuck/Settings"], function(Box2D, Constants, Settings){
|
||||
|
||||
function Doll (physicsEngine, id){
|
||||
this.id = id;
|
||||
this.physicsEngine = physicsEngine;
|
||||
this.body;
|
||||
this.legs;
|
||||
this.contactPoint;
|
||||
|
||||
this.init(this.physicsEngine.getWorld());
|
||||
}
|
||||
|
||||
Doll.prototype.init = function (world) {
|
||||
|
||||
var bodyDef = new Box2D.Dynamics.b2BodyDef();
|
||||
bodyDef.position.x = 220 / Settings.RATIO;
|
||||
bodyDef.position.y = 0 / Settings.RATIO;
|
||||
bodyDef.fixedRotation = true;
|
||||
bodyDef.linearDamping = Settings.PLAYER_LINEAR_DAMPING;
|
||||
bodyDef.type = Box2D.Dynamics.b2Body.b2_dynamicBody;
|
||||
bodyDef.userData = Constants.COLLISION_IDENTIFIER_PLAYER + '-' + this.id;
|
||||
|
||||
this.body = world.CreateBody(bodyDef);
|
||||
|
||||
var fixtureDef = new Box2D.Dynamics.b2FixtureDef();
|
||||
fixtureDef.density = Settings.PLAYER_DENSITY;
|
||||
fixtureDef.friction = 0;
|
||||
fixtureDef.restitution = Settings.PLAYER_RESTITUTION;
|
||||
|
||||
var headShape = new Box2D.Collision.Shapes.b2CircleShape();
|
||||
headShape.SetRadius(5 / Settings.RATIO);
|
||||
headShape.SetLocalPosition(new Box2D.Common.Math.b2Vec2(0 / Settings.RATIO, -37 / Settings.RATIO));
|
||||
fixtureDef.shape = headShape;
|
||||
fixtureDef.isSensor = false;
|
||||
fixtureDef.userData = Constants.COLLISION_IDENTIFIER_PLAYER_HEAD;
|
||||
this.body.CreateFixture(fixtureDef);
|
||||
|
||||
var bodyShape = new Box2D.Collision.Shapes.b2PolygonShape();
|
||||
bodyShape.SetAsOrientedBox(5 / Settings.RATIO, 16 / Settings.RATIO, new Box2D.Common.Math.b2Vec2(0 / Settings.RATIO, -21 / Settings.RATIO));
|
||||
fixtureDef.shape = bodyShape;
|
||||
fixtureDef.isSensor = false;
|
||||
fixtureDef.userData = Constants.COLLISION_IDENTIFIER_PLAYER_CHEST;
|
||||
this.body.CreateFixture(fixtureDef);
|
||||
|
||||
var legsShape = new Box2D.Collision.Shapes.b2CircleShape();
|
||||
legsShape.SetRadius(5 / Settings.RATIO);
|
||||
legsShape.SetLocalPosition(new Box2D.Common.Math.b2Vec2(0 / Settings.RATIO, -5 / Settings.RATIO));
|
||||
fixtureDef.shape = legsShape;
|
||||
fixtureDef.friction = Settings.PLAYER_FRICTION;
|
||||
fixtureDef.isSensor = false;
|
||||
fixtureDef.userData = Constants.COLLISION_IDENTIFIER_PLAYER_LEGS;
|
||||
|
||||
this.legs = this.body.CreateFixture(fixtureDef);
|
||||
|
||||
var feetShape = new Box2D.Collision.Shapes.b2CircleShape();
|
||||
feetShape.SetRadius(4 / Settings.RATIO);
|
||||
feetShape.SetLocalPosition(new Box2D.Common.Math.b2Vec2(0 / Settings.RATIO, 0 / Settings.RATIO));
|
||||
fixtureDef.shape = feetShape;
|
||||
fixtureDef.isSensor = true;
|
||||
fixtureDef.userData = Constants.COLLISION_IDENTIFIER_FOOTSENSOR;
|
||||
this.body.CreateFixture(fixtureDef);
|
||||
|
||||
this.body.SetActive(false);
|
||||
}
|
||||
|
||||
Doll.prototype.spawn = function (x, y) {
|
||||
this.body.SetPosition(new Box2D.Common.Math.b2Vec2(x / Settings.RATIO, y / Settings.RATIO));
|
||||
this.body.SetActive(true);
|
||||
}
|
||||
|
||||
Doll.prototype.getBody = function () {
|
||||
return this.body;
|
||||
}
|
||||
|
||||
Doll.prototype.setFriction = function (friction) {
|
||||
if(!friction) friction = -1;
|
||||
|
||||
if (this.legs.GetFriction() != friction) {
|
||||
this.legs.SetFriction(friction);
|
||||
}
|
||||
}
|
||||
|
||||
Doll.prototype.move = function (direction, speed) {
|
||||
this.setFriction(Settings.PLAYER_MOTION_FRICTION);
|
||||
this.body.SetAwake(true);
|
||||
var vector = new Box2D.Common.Math.b2Vec2(speed * direction, this.body.GetLinearVelocity().y);
|
||||
this.body.SetLinearVelocity(vector);
|
||||
}
|
||||
|
||||
Doll.prototype.stop = function () {
|
||||
this.setFriction(Settings.PLAYER_FRICTION);
|
||||
}
|
||||
|
||||
Doll.prototype.jump = function () {
|
||||
this.body.SetAwake(true);
|
||||
|
||||
var vector = new Box2D.Common.Math.b2Vec2(0, -Settings.JUMP_SPEED);
|
||||
this.body.ApplyImpulse(vector, this.body.GetPosition());
|
||||
|
||||
// maybe change to a constant force instead of applying of force?
|
||||
// to prevent higher jumping running uphill, etc.
|
||||
}
|
||||
|
||||
Doll.prototype.jumping = function () {
|
||||
var vector = new Box2D.Common.Math.b2Vec2(0, -0.05);
|
||||
this.body.ApplyImpulse(vector, this.body.GetPosition());
|
||||
}
|
||||
|
||||
Doll.prototype.destroy = function() {
|
||||
this.body.GetWorld().DestroyBody(this.body);
|
||||
}
|
||||
|
||||
return Doll;
|
||||
});
|
||||
63
app/Game/Core/Physics/Engine.js
Executable file
63
app/Game/Core/Physics/Engine.js
Executable file
|
|
@ -0,0 +1,63 @@
|
|||
define(["Chuck/Settings", "Client/Dom", "Vendor/Box2D", "Chuck/Collision/Detector"], function(Settings, Dom, Box2D, CollisionDetector){
|
||||
|
||||
function Engine () {
|
||||
this.world;
|
||||
this.init();
|
||||
}
|
||||
|
||||
Engine.prototype.init = function() {
|
||||
this.world = new Box2D.Dynamics.b2World(new Box2D.Common.Math.b2Vec2(0, Settings.BOX2D_GRAVITY), Settings.BOX2D_ALLOW_SLEEP);
|
||||
|
||||
if(Settings.IS_BROWSER_ENVIRONMENT && Settings.DEBUG_MODE) {
|
||||
this.setupDebugDraw();
|
||||
}
|
||||
}
|
||||
|
||||
Engine.prototype.getWorld = function() {
|
||||
return this.world;
|
||||
}
|
||||
|
||||
Engine.prototype.setCollisionDetector = function(me) {
|
||||
|
||||
var detector = new CollisionDetector(me);
|
||||
this.world.SetContactListener(detector.getListener());
|
||||
}
|
||||
|
||||
Engine.prototype.setupDebugDraw = function() {
|
||||
//var debugSprite = Settings.DEBUG_DRAW_CANVAS_SPRITE;
|
||||
var debugSprite = Dom.getDebugCanvas().getContext("2d");
|
||||
|
||||
// set debug draw
|
||||
var debugDraw = new Box2D.Dynamics.b2DebugDraw();
|
||||
|
||||
debugDraw.SetSprite(debugSprite);
|
||||
debugDraw.SetDrawScale(Settings.RATIO);
|
||||
debugDraw.SetFillAlpha(0.5);
|
||||
debugDraw.SetLineThickness(1.0);
|
||||
|
||||
debugDraw.SetFlags(null
|
||||
| Box2D.Dynamics.b2DebugDraw.e_shapeBit
|
||||
| Box2D.Dynamics.b2DebugDraw.e_jointBit
|
||||
//| Box2D.Dynamics.b2DebugDraw.e_coreShapeBit
|
||||
//| Box2D.Dynamics.b2DebugDraw.e_aabbBit
|
||||
//| Box2D.Dynamics.b2DebugDraw.e_centerOfMassBit
|
||||
//| Box2D.Dynamics.b2DebugDraw.e_obbBit
|
||||
//| Box2D.Dynamics.b2DebugDraw.e_pairBit
|
||||
);
|
||||
|
||||
this.world.SetDebugDraw(debugDraw);
|
||||
this.world.SetWarmStarting(true);
|
||||
}
|
||||
|
||||
Engine.prototype.createBody = function(bodyDef) {
|
||||
return this.world.CreateBody(bodyDef);
|
||||
}
|
||||
|
||||
Engine.prototype.update = function() {
|
||||
this.world.Step(Settings.BOX2D_TIME_STEP, Settings.BOX2D_VELOCITY_ITERATIONS, Settings.BOX2D_POSITION_ITERATIONS);
|
||||
this.world.ClearForces();
|
||||
this.world.DrawDebugData();
|
||||
}
|
||||
|
||||
return Engine;
|
||||
})
|
||||
Loading…
Add table
Add a link
Reference in a new issue