mirror of
https://github.com/logsol/chuck.js.git
synced 2026-05-11 18:47:35 +00:00
Merge branch 'restructuring' of github.com:logsol/chuck.js into restructuring
This commit is contained in:
commit
38bc5adabd
8 changed files with 85 additions and 74 deletions
|
|
@ -8,20 +8,10 @@ function(Box2D, Parent) {
|
||||||
function Detector(me) {
|
function Detector(me) {
|
||||||
Parent.call(this);
|
Parent.call(this);
|
||||||
this.me = me;
|
this.me = me;
|
||||||
|
|
||||||
this.listener = new Box2D.Dynamics.b2ContactListener();
|
|
||||||
this.listener.chuckDetector = this;
|
|
||||||
this.listener.BeginContact = this.BeginContact;
|
|
||||||
this.listener.PostSolve = this.PostSolve;
|
|
||||||
this.listener.EndContact = this.EndContact;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Detector.prototype = Object.create(Parent);
|
Detector.prototype = Object.create(Parent);
|
||||||
|
|
||||||
Detector.prototype.getListener = function() {
|
|
||||||
return this.listener;
|
|
||||||
}
|
|
||||||
|
|
||||||
Detector.prototype.handleStand = function(point, isColliding) {
|
Detector.prototype.handleStand = function(point, isColliding) {
|
||||||
if (point.GetFixtureA().GetUserData() == Detector.IDENTIFIER.PLAYER_FOOT_SENSOR
|
if (point.GetFixtureA().GetUserData() == Detector.IDENTIFIER.PLAYER_FOOT_SENSOR
|
||||||
|| point.GetFixtureB().GetUserData() == Detector.IDENTIFIER.PLAYER_FOOT_SENSOR) {
|
|| point.GetFixtureB().GetUserData() == Detector.IDENTIFIER.PLAYER_FOOT_SENSOR) {
|
||||||
|
|
@ -30,19 +20,5 @@ function(Box2D, Parent) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Extension **/
|
|
||||||
|
|
||||||
Detector.prototype.BeginContact = function(point) {
|
|
||||||
this.chuckDetector.handleStand(point, true);
|
|
||||||
}
|
|
||||||
|
|
||||||
Detector.prototype.PostSolve = function(point, impulse) {
|
|
||||||
this.chuckDetector.handleStand(point, true);
|
|
||||||
}
|
|
||||||
|
|
||||||
Detector.prototype.EndContact = function(point) {
|
|
||||||
this.chuckDetector.handleStand(point, false);
|
|
||||||
}
|
|
||||||
|
|
||||||
return Detector;
|
return Detector;
|
||||||
});
|
});
|
||||||
|
|
@ -1,6 +1,16 @@
|
||||||
define(function() {
|
define([
|
||||||
|
"Lib/Vendor/Box2D",
|
||||||
|
"Game/Core/Collision/Detector"
|
||||||
|
],
|
||||||
|
|
||||||
|
function(Box2D, Parent) {
|
||||||
|
|
||||||
function Detector() {
|
function Detector() {
|
||||||
|
this.listener = new Box2D.Dynamics.b2ContactListener();
|
||||||
|
this.listener.chuckDetector = this;
|
||||||
|
this.listener.BeginContact = this.BeginContact;
|
||||||
|
this.listener.PostSolve = this.PostSolve;
|
||||||
|
this.listener.EndContact = this.EndContact;
|
||||||
}
|
}
|
||||||
|
|
||||||
Detector.IDENTIFIER = {
|
Detector.IDENTIFIER = {
|
||||||
|
|
@ -12,6 +22,27 @@ define(function() {
|
||||||
PLAYER_FOOT_SENSOR: 'footsensor'
|
PLAYER_FOOT_SENSOR: 'footsensor'
|
||||||
}
|
}
|
||||||
|
|
||||||
return Detector;
|
Detector.prototype.getListener = function() {
|
||||||
|
return this.listener;
|
||||||
|
}
|
||||||
|
|
||||||
|
Detector.prototype.handleStand = function(point, isColliding) {
|
||||||
|
throw "Overwrite this function";
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Extension **/
|
||||||
|
|
||||||
|
Detector.prototype.BeginContact = function(point) {
|
||||||
|
this.chuckDetector.handleStand(point, true);
|
||||||
|
}
|
||||||
|
|
||||||
|
Detector.prototype.PostSolve = function(point, impulse) {
|
||||||
|
this.chuckDetector.handleStand(point, true);
|
||||||
|
}
|
||||||
|
|
||||||
|
Detector.prototype.EndContact = function(point) {
|
||||||
|
this.chuckDetector.handleStand(point, false);
|
||||||
|
}
|
||||||
|
|
||||||
|
return Detector;
|
||||||
});
|
});
|
||||||
|
|
@ -1,4 +1,4 @@
|
||||||
define(["Lib/Vendor/Box2D", "Game/Config/Settings", "Game/Core/Collision/Detector"], function(Box2D, Settings, Detector) {
|
define(["Lib/Vendor/Box2D", "Game/Config/Settings", "Game/Core/Collision/Detector"], function(Box2D, Settings, CollisionDetector) {
|
||||||
|
|
||||||
function Doll (physicsEngine, id){
|
function Doll (physicsEngine, id){
|
||||||
this.id = id;
|
this.id = id;
|
||||||
|
|
@ -18,7 +18,7 @@ define(["Lib/Vendor/Box2D", "Game/Config/Settings", "Game/Core/Collision/Detecto
|
||||||
bodyDef.fixedRotation = true;
|
bodyDef.fixedRotation = true;
|
||||||
bodyDef.linearDamping = Settings.PLAYER_LINEAR_DAMPING;
|
bodyDef.linearDamping = Settings.PLAYER_LINEAR_DAMPING;
|
||||||
bodyDef.type = Box2D.Dynamics.b2Body.b2_dynamicBody;
|
bodyDef.type = Box2D.Dynamics.b2Body.b2_dynamicBody;
|
||||||
bodyDef.userData = Detector.IDENTIFIER.PLAYER + '-' + this.id;
|
bodyDef.userData = CollisionDetector.IDENTIFIER.PLAYER + '-' + this.id;
|
||||||
|
|
||||||
this.body = world.CreateBody(bodyDef);
|
this.body = world.CreateBody(bodyDef);
|
||||||
|
|
||||||
|
|
@ -32,14 +32,14 @@ define(["Lib/Vendor/Box2D", "Game/Config/Settings", "Game/Core/Collision/Detecto
|
||||||
headShape.SetLocalPosition(new Box2D.Common.Math.b2Vec2(0 / Settings.RATIO, -37 / Settings.RATIO));
|
headShape.SetLocalPosition(new Box2D.Common.Math.b2Vec2(0 / Settings.RATIO, -37 / Settings.RATIO));
|
||||||
fixtureDef.shape = headShape;
|
fixtureDef.shape = headShape;
|
||||||
fixtureDef.isSensor = false;
|
fixtureDef.isSensor = false;
|
||||||
fixtureDef.userData = Detector.IDENTIFIER.PLAYER_HEAD;
|
fixtureDef.userData = CollisionDetector.IDENTIFIER.PLAYER_HEAD;
|
||||||
this.body.CreateFixture(fixtureDef);
|
this.body.CreateFixture(fixtureDef);
|
||||||
|
|
||||||
var bodyShape = new Box2D.Collision.Shapes.b2PolygonShape();
|
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));
|
bodyShape.SetAsOrientedBox(5 / Settings.RATIO, 16 / Settings.RATIO, new Box2D.Common.Math.b2Vec2(0 / Settings.RATIO, -21 / Settings.RATIO));
|
||||||
fixtureDef.shape = bodyShape;
|
fixtureDef.shape = bodyShape;
|
||||||
fixtureDef.isSensor = false;
|
fixtureDef.isSensor = false;
|
||||||
fixtureDef.userData = Detector.IDENTIFIER.PLAYER_CHEST;
|
fixtureDef.userData = CollisionDetector.IDENTIFIER.PLAYER_CHEST;
|
||||||
this.body.CreateFixture(fixtureDef);
|
this.body.CreateFixture(fixtureDef);
|
||||||
|
|
||||||
var legsShape = new Box2D.Collision.Shapes.b2CircleShape();
|
var legsShape = new Box2D.Collision.Shapes.b2CircleShape();
|
||||||
|
|
@ -48,7 +48,7 @@ define(["Lib/Vendor/Box2D", "Game/Config/Settings", "Game/Core/Collision/Detecto
|
||||||
fixtureDef.shape = legsShape;
|
fixtureDef.shape = legsShape;
|
||||||
fixtureDef.friction = Settings.PLAYER_FRICTION;
|
fixtureDef.friction = Settings.PLAYER_FRICTION;
|
||||||
fixtureDef.isSensor = false;
|
fixtureDef.isSensor = false;
|
||||||
fixtureDef.userData = Detector.IDENTIFIER.PLAYER_LEGS;
|
fixtureDef.userData = CollisionDetector.IDENTIFIER.PLAYER_LEGS;
|
||||||
|
|
||||||
this.legs = this.body.CreateFixture(fixtureDef);
|
this.legs = this.body.CreateFixture(fixtureDef);
|
||||||
|
|
||||||
|
|
@ -57,7 +57,7 @@ define(["Lib/Vendor/Box2D", "Game/Config/Settings", "Game/Core/Collision/Detecto
|
||||||
feetShape.SetLocalPosition(new Box2D.Common.Math.b2Vec2(0 / Settings.RATIO, 0 / Settings.RATIO));
|
feetShape.SetLocalPosition(new Box2D.Common.Math.b2Vec2(0 / Settings.RATIO, 0 / Settings.RATIO));
|
||||||
fixtureDef.shape = feetShape;
|
fixtureDef.shape = feetShape;
|
||||||
fixtureDef.isSensor = true;
|
fixtureDef.isSensor = true;
|
||||||
fixtureDef.userData = Detector.IDENTIFIER.FOOTSENSOR;
|
fixtureDef.userData = CollisionDetector.IDENTIFIER.FOOTSENSOR;
|
||||||
this.body.CreateFixture(fixtureDef);
|
this.body.CreateFixture(fixtureDef);
|
||||||
|
|
||||||
this.body.SetActive(false);
|
this.body.SetActive(false);
|
||||||
|
|
|
||||||
|
|
@ -1,16 +1,16 @@
|
||||||
define(["Chuck/Settings", "Client/Dom", "Vendor/Box2D", "Chuck/Collision/Detector"], function(Settings, Dom, Box2D, CollisionDetector){
|
define([
|
||||||
|
"Game/Config/Settings",
|
||||||
|
"Lib/Vendor/Box2D",
|
||||||
|
"Game/Core/Collision/Detector"
|
||||||
|
],
|
||||||
|
|
||||||
|
function(Settings, Box2D, CollisionDetector) {
|
||||||
|
|
||||||
function Engine () {
|
function Engine () {
|
||||||
this.world;
|
this.world = new Box2D.Dynamics.b2World(
|
||||||
this.init();
|
new Box2D.Common.Math.b2Vec2(0, Settings.BOX2D_GRAVITY),
|
||||||
}
|
Settings.BOX2D_ALLOW_SLEEP
|
||||||
|
);
|
||||||
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() {
|
Engine.prototype.getWorld = function() {
|
||||||
|
|
@ -19,36 +19,10 @@ define(["Chuck/Settings", "Client/Dom", "Vendor/Box2D", "Chuck/Collision/Detecto
|
||||||
|
|
||||||
Engine.prototype.setCollisionDetector = function(me) {
|
Engine.prototype.setCollisionDetector = function(me) {
|
||||||
|
|
||||||
var detector = new CollisionDetector(me);
|
var detector = new CollisionDetector(me); // FIXME: check if core collision detector works
|
||||||
this.world.SetContactListener(detector.getListener());
|
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) {
|
Engine.prototype.createBody = function(bodyDef) {
|
||||||
return this.world.CreateBody(bodyDef);
|
return this.world.CreateBody(bodyDef);
|
||||||
}
|
}
|
||||||
|
|
@ -56,7 +30,6 @@ define(["Chuck/Settings", "Client/Dom", "Vendor/Box2D", "Chuck/Collision/Detecto
|
||||||
Engine.prototype.update = function() {
|
Engine.prototype.update = function() {
|
||||||
this.world.Step(Settings.BOX2D_TIME_STEP, Settings.BOX2D_VELOCITY_ITERATIONS, Settings.BOX2D_POSITION_ITERATIONS);
|
this.world.Step(Settings.BOX2D_TIME_STEP, Settings.BOX2D_VELOCITY_ITERATIONS, Settings.BOX2D_POSITION_ITERATIONS);
|
||||||
this.world.ClearForces();
|
this.world.ClearForces();
|
||||||
this.world.DrawDebugData();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return Engine;
|
return Engine;
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,9 @@
|
||||||
define(["Chuck/Physics/Doll", "Chuck/Settings"], function(Doll, Settings){
|
define([
|
||||||
|
"Game/Core/Physics/Doll",
|
||||||
|
"Game/Config/Settings"
|
||||||
|
],
|
||||||
|
|
||||||
|
function(Doll, Settings) {
|
||||||
|
|
||||||
function Player (physicsEngine, id, repository) {
|
function Player (physicsEngine, id, repository) {
|
||||||
this.physicsEngine = physicsEngine;
|
this.physicsEngine = physicsEngine;
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,8 @@
|
||||||
define(["Protocol/Parser"], function(Parser) {
|
define([
|
||||||
|
"Game/Core/Protocol/Parser"
|
||||||
|
],
|
||||||
|
|
||||||
|
function(Parser) {
|
||||||
|
|
||||||
var Helper = {}
|
var Helper = {}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,7 @@
|
||||||
define(function() {
|
define([
|
||||||
|
],
|
||||||
|
|
||||||
|
function() {
|
||||||
|
|
||||||
var Parser = {};
|
var Parser = {};
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,19 @@
|
||||||
|
define([
|
||||||
|
"Lib/Vendor/Box2D",
|
||||||
|
"Game/Core/Collision/Detector"
|
||||||
|
],
|
||||||
|
|
||||||
|
function(Box2D, Parent) {
|
||||||
|
|
||||||
|
function Detector() {
|
||||||
|
Parent.call(this);
|
||||||
|
}
|
||||||
|
|
||||||
|
Detector.prototype = Object.create(Parent);
|
||||||
|
|
||||||
|
Detector.prototype.handleStand = function(point, isColliding) {
|
||||||
|
throw "Implement this function";
|
||||||
|
}
|
||||||
|
|
||||||
|
return Detector;
|
||||||
|
});
|
||||||
Loading…
Add table
Add a link
Reference in a new issue