Added Player

This commit is contained in:
logsol 2012-07-01 16:03:31 +02:00
parent 2ce46e1f43
commit 7560fd634a
3 changed files with 93 additions and 98 deletions

View file

@ -1,56 +1,57 @@
define(['Doll'], function(){ define(["Box2D/Box2D", "Chuck/Settings"], function(Box2D, Settings){
Chuck.Physics.Doll = function(engine){
this._engine = engine; function Doll (physicsEngine){
this._physicsEngine = physicsEngine;
this._body; this._body;
this._legs; this._legs;
this._contactPoint; this._contactPoint;
this.init(this._engine.getWorld()); this.init(this._physicsEngine.getWorld());
} }
Chuck.Physics.Doll.prototype.init = function (world) { Doll.prototype.init = function (world) {
var bodyDef = new Chuck.b2BodyDef(); var bodyDef = new Box2D.Dynamics.b2BodyDef();
bodyDef.position.x = 220 / Chuck.Settings.RATIO; bodyDef.position.x = 220 / Settings.RATIO;
bodyDef.position.y = 0 / Chuck.Settings.RATIO; bodyDef.position.y = 0 / Settings.RATIO;
bodyDef.fixedRotation = true; bodyDef.fixedRotation = true;
bodyDef.linearDamping = Chuck.Settings.PLAYER_LINEAR_DAMPING; bodyDef.linearDamping = Settings.PLAYER_LINEAR_DAMPING;
bodyDef.type = Chuck.b2Body.b2_dynamicBody; bodyDef.type = Box2D.Dynamics.b2Body.b2_dynamicBody;
this._body = world.CreateBody(bodyDef); this._body = world.CreateBody(bodyDef);
var fixtureDef = new Chuck.b2FixtureDef(); var fixtureDef = new Box2D.Dynamics.b2FixtureDef();
fixtureDef.density = Chuck.Settings.PLAYER_DENSITY; fixtureDef.density = Settings.PLAYER_DENSITY;
fixtureDef.friction = 0; fixtureDef.friction = 0;
fixtureDef.restitution = Chuck.Settings.PLAYER_RESTITUTION; fixtureDef.restitution = Settings.PLAYER_RESTITUTION;
var headShape = new Chuck.b2CircleShape(); var headShape = new Box2D.Collision.Shapes.b2CircleShape();
headShape.SetRadius(5 / Chuck.Settings.RATIO); headShape.SetRadius(5 / Settings.RATIO);
headShape.SetLocalPosition(new Chuck.b2Vec2(0 / Chuck.Settings.RATIO, -37 / Chuck.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 = 'myHead'; fixtureDef.userData = 'myHead';
this._body.CreateFixture(fixtureDef); this._body.CreateFixture(fixtureDef);
var bodyShape = new Chuck.b2PolygonShape(); var bodyShape = new Box2D.Collision.Shapes.b2PolygonShape();
bodyShape.SetAsOrientedBox(5 / Chuck.Settings.RATIO, 16 / Chuck.Settings.RATIO, new Chuck.b2Vec2(0 / Chuck.Settings.RATIO, -21 / Chuck.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 = 'myBody'; fixtureDef.userData = 'myBody';
this._body.CreateFixture(fixtureDef); this._body.CreateFixture(fixtureDef);
var legsShape = new Chuck.b2CircleShape(); var legsShape = new Box2D.Collision.Shapes.b2CircleShape();
legsShape.SetRadius(5 / Chuck.Settings.RATIO); legsShape.SetRadius(5 / Settings.RATIO);
legsShape.SetLocalPosition(new Chuck.b2Vec2(0 / Chuck.Settings.RATIO, -5 / Chuck.Settings.RATIO)); legsShape.SetLocalPosition(new Box2D.Common.Math.b2Vec2(0 / Settings.RATIO, -5 / Settings.RATIO));
fixtureDef.shape = legsShape; fixtureDef.shape = legsShape;
fixtureDef.friction = Chuck.Settings.PLAYER_FRICTION; fixtureDef.friction = Settings.PLAYER_FRICTION;
fixtureDef.isSensor = false; fixtureDef.isSensor = false;
fixtureDef.userData = 'myLegs'; fixtureDef.userData = 'myLegs';
this._legs = this._body.CreateFixture(fixtureDef); this._legs = this._body.CreateFixture(fixtureDef);
var feetShape = new Chuck.b2CircleShape(); var feetShape = new Box2D.Collision.Shapes.b2CircleShape();
feetShape.SetRadius(4 / Chuck.Settings.RATIO); feetShape.SetRadius(4 / Settings.RATIO);
feetShape.SetLocalPosition(new Chuck.b2Vec2(0 / Chuck.Settings.RATIO, 0 / Chuck.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 = 'myFeet'; fixtureDef.userData = 'myFeet';
@ -59,16 +60,16 @@ define(['Doll'], function(){
this._body.SetActive(false); this._body.SetActive(false);
} }
Chuck.Physics.Doll.prototype.spawn = function (x, y) { Doll.prototype.spawn = function (x, y) {
this._body.SetPosition(new Chuck.b2Vec2(x / Chuck.Settings.RATIO, y / Chuck.Settings.RATIO)); this._body.SetPosition(new Box2D.Common.Math.b2Vec2(x / Settings.RATIO, y / Settings.RATIO));
this._body.SetActive(true); this._body.SetActive(true);
} }
Chuck.Physics.Doll.prototype.getBody = function () { Doll.prototype.getBody = function () {
return this._body; return this._body;
} }
Chuck.Physics.Doll.prototype._setFriction = function (friction) { Doll.prototype._setFriction = function (friction) {
if(!friction) friction = -1; if(!friction) friction = -1;
if (this._legs.GetFriction() != friction) if (this._legs.GetFriction() != friction)
@ -77,29 +78,31 @@ define(['Doll'], function(){
} }
} }
Chuck.Physics.Doll.prototype.move = function (direction, speed) { Doll.prototype.move = function (direction, speed) {
this._setFriction(Chuck.Settings.PLAYER_MOTION_FRICTION); this._setFriction(Settings.PLAYER_MOTION_FRICTION);
this._body.SetAwake(true); this._body.SetAwake(true);
var vector = new Chuck.b2Vec2(speed * direction, this._body.GetLinearVelocity().y); var vector = new Box2D.Common.Math.b2Vec2(speed * direction, this._body.GetLinearVelocity().y);
this._body.SetLinearVelocity(vector); this._body.SetLinearVelocity(vector);
} }
Chuck.Physics.Doll.prototype.stop = function () { Doll.prototype.stop = function () {
this._setFriction(Chuck.Settings.PLAYER_FRICTION); this._setFriction(Settings.PLAYER_FRICTION);
} }
Chuck.Physics.Doll.prototype.jump = function () { Doll.prototype.jump = function () {
this._body.SetAwake(true); this._body.SetAwake(true);
var vector = new Chuck.b2Vec2(0, -Chuck.Settings.JUMP_SPEED); var vector = new Box2D.Common.Math.b2Vec2(0, -Settings.JUMP_SPEED);
this._body.ApplyImpulse(vector, this._body.GetPosition()); this._body.ApplyImpulse(vector, this._body.GetPosition());
// maybe change to a constant force instead of applying of force? // maybe change to a constant force instead of applying of force?
// to prevent higher jumping running uphill, etc. // to prevent higher jumping running uphill, etc.
} }
Chuck.Physics.Doll.prototype.jumping = function () { Doll.prototype.jumping = function () {
var vector = new Chuck.b2Vec2(0, -0.1); var vector = new Box2D.Common.Math.b2Vec2(0, -0.1);
this._body.ApplyImpulse(vector, this._body.GetPosition()); this._body.ApplyImpulse(vector, this._body.GetPosition());
} }
}
return Doll;
});

View file

@ -1,4 +1,4 @@
define(['Doll'], function(){ define(["Chuck/Physics/Doll", "Chuck/Settings"], function(Doll, Settings){
function Player (physicsEngine, repository) { function Player (physicsEngine, repository) {
this._physicsEngine = physicsEngine; this._physicsEngine = physicsEngine;
@ -36,8 +36,7 @@ define(['Doll'], function(){
Player.prototype.setStanding = function(isStanding) { Player.prototype.setStanding = function(isStanding) {
var resetStates = ['jump', 'jumploop']; var resetStates = ['jump', 'jumploop'];
if (resetStates.indexOf(this._currentAnimationState)>=0 && !this._standing && isStanding) if (resetStates.indexOf(this._currentAnimationState)>=0 && !this._standing && isStanding) {
{
this._animate('stand'); this._animate('stand');
} }
this._standing = isStanding; this._standing = isStanding;
@ -52,15 +51,15 @@ define(['Doll'], function(){
switch(true) { switch(true) {
case direction == this._lookDirection && this.isStanding(): case direction == this._lookDirection && this.isStanding():
this._doll.move(direction, Chuck.Settings.RUN_SPEED); this._doll.move(direction, Settings.RUN_SPEED);
break; break;
case !this.isStanding(): case !this.isStanding():
this._doll.move(direction, Chuck.Settings.FLY_SPEED); this._doll.move(direction, Settings.FLY_SPEED);
break; break;
default: default:
this._doll.move(direction, Chuck.Settings.WALK_SPEED); this._doll.move(direction, Settings.WALK_SPEED);
break; break;
} }
@ -78,8 +77,7 @@ define(['Doll'], function(){
} }
Player.prototype.jump = function() { Player.prototype.jump = function() {
if (this.isStanding()) if (this.isStanding()) {
{
this._doll.jump(); this._doll.jump();
this._animate('jump'); this._animate('jump');
this.setStanding(false); this.setStanding(false);
@ -123,15 +121,15 @@ define(['Doll'], function(){
Player.prototype.look = function(x, y) { Player.prototype.look = function(x, y) {
/* /*
var degree = Math.atan2(Chuck.Settings.STAGE_WIDTH / 2 - x, Chuck.Settings.STAGE_HEIGHT / 2 - 25 - y) / (Math.PI / 180); var degree = Math.atan2(Settings.STAGE_WIDTH / 2 - x, Settings.STAGE_HEIGHT / 2 - 25 - y) / (Math.PI / 180);
var lastLookDirection = this._lookDirection; var lastLookDirection = this._lookDirection;
if (x < Chuck.Settings.STAGE_WIDTH / 2) { if (x < Settings.STAGE_WIDTH / 2) {
this._mc.scaleX = -1; this._mc.scaleX = -1;
this._lookDirection = -1; this._lookDirection = -1;
degree = (-45 + degree / 2); degree = (-45 + degree / 2);
this._mc.head.rotation = degree; this._mc.head.rotation = degree;
} else if (x >= Chuck.Settings.STAGE_WIDTH / 2) { } else if (x >= Settings.STAGE_WIDTH / 2) {
this._mc.scaleX = 1; this._mc.scaleX = 1;
this._lookDirection = 1; this._lookDirection = 1;
degree = (45 + -degree / 2) - 90; degree = (45 + -degree / 2) - 90;
@ -155,7 +153,7 @@ define(['Doll'], function(){
// called by CollisionDetection // called by CollisionDetection
Player.prototype.onFootSensorDetection = function(isColliding) { Player.prototype.onFootSensorDetection = function(isColliding) {
if(isColliding) { if(isColliding) {
if(this._doll.getBody().GetLinearVelocity().y < -Chuck.Settings.JUMP_SPEED && !this.isStanding()) { if(this._doll.getBody().GetLinearVelocity().y < -Settings.JUMP_SPEED && !this.isStanding()) {
return; return;
} }
this.setStanding(true); this.setStanding(true);
@ -180,4 +178,6 @@ define(['Doll'], function(){
this.setStanding(true); this.setStanding(true);
} }
} }
}
return Player;
});

View file

@ -1,4 +1,4 @@
define(["Chuck/Physics/Engine", "Box2D/Box2D"], function(PhysicsEngine, Box2D){ define(["Chuck/Physics/Engine", "Chuck/Player", "Box2D/Box2D"], function(PhysicsEngine, Player, Box2D){
function Processor () { function Processor () {
this._me; this._me;
@ -20,55 +20,47 @@ define(["Chuck/Physics/Engine", "Box2D/Box2D"], function(PhysicsEngine, Box2D){
{
var world = this._physicsEngine.getWorld(); var world = this._physicsEngine.getWorld();
var fixDef = new Box2D.Dynamics.b2FixtureDef; var fixDef = new Box2D.Dynamics.b2FixtureDef;
fixDef.density = 1.0; fixDef.density = 1.0;
fixDef.friction = 0.99; fixDef.friction = 0.99;
fixDef.restitution = .51; fixDef.restitution = .51;
var bodyDef = new Box2D.Dynamics.b2BodyDef; var bodyDef = new Box2D.Dynamics.b2BodyDef;
// create ground // create ground
bodyDef.type = Box2D.Dynamics.b2Body.b2_staticBody; bodyDef.type = Box2D.Dynamics.b2Body.b2_staticBody;
fixDef.shape = new Box2D.Collision.Shapes.b2PolygonShape; fixDef.shape = new Box2D.Collision.Shapes.b2PolygonShape;
fixDef.shape.SetAsBox(20, 2); fixDef.shape.SetAsBox(20, 2);
bodyDef.position.Set(10, 400 / 30 + 1.8); bodyDef.position.Set(10, 400 / 30 + 1.8);
world.CreateBody(bodyDef).CreateFixture(fixDef); world.CreateBody(bodyDef).CreateFixture(fixDef);
bodyDef.position.Set(10, -1.8); bodyDef.position.Set(10, -1.8);
world.CreateBody(bodyDef).CreateFixture(fixDef); world.CreateBody(bodyDef).CreateFixture(fixDef);
fixDef.shape.SetAsBox(2, 14); fixDef.shape.SetAsBox(2, 14);
bodyDef.position.Set(-1.8, 13); bodyDef.position.Set(-1.8, 13);
world.CreateBody(bodyDef).CreateFixture(fixDef); world.CreateBody(bodyDef).CreateFixture(fixDef);
bodyDef.position.Set(21.8, 13); bodyDef.position.Set(21.8, 13);
world.CreateBody(bodyDef).CreateFixture(fixDef); world.CreateBody(bodyDef).CreateFixture(fixDef);
// create object // create object
bodyDef.type = Box2D.Dynamics.b2Body.b2_dynamicBody; bodyDef.type = Box2D.Dynamics.b2Body.b2_dynamicBody;
fixDef.shape = new Box2D.Collision.Shapes.b2PolygonShape; fixDef.shape = new Box2D.Collision.Shapes.b2PolygonShape;
fixDef.shape.SetAsBox(0.4, 0.4); fixDef.shape.SetAsBox(0.4, 0.4);
bodyDef.position.x = 10; bodyDef.position.x = 10;
bodyDef.position.y = 2; bodyDef.position.y = 2;
bodyDef.userData = { bodyDef.userData = {
'bodyId': 1 + '' 'bodyId': 1 + ''
}; };
world.CreateBody(bodyDef).CreateFixture(fixDef); world.CreateBody(bodyDef).CreateFixture(fixDef);
this._bodyDef = bodyDef; this._bodyDef = bodyDef;
}
//this._me = new Chuck.Player(this._physicsEngine, this._repository);
this._me = new Player(this._physicsEngine, null);
this._me.spawn(100, 0);
/* /*
//this._camera = Camera.getInstance() //this._camera = Camera.getInstance()
@ -80,7 +72,7 @@ define(["Chuck/Physics/Engine", "Box2D/Box2D"], function(PhysicsEngine, Box2D){
new Chuck.Loader.Level(this._physicsEngine); new Chuck.Loader.Level(this._physicsEngine);
//new Items(); //new Items();
this._me.spawn(100, 0);
//this._camera.follow(this._me); //this._camera.follow(this._me);
*/ */
setInterval(this._update, 1000/60, this); setInterval(this._update, 1000/60, this);