mirror of
https://github.com/logsol/chuck.js.git
synced 2026-05-11 18:47:35 +00:00
refining js version
This commit is contained in:
parent
3c8fa410ba
commit
8d0ff1a929
15 changed files with 562 additions and 355 deletions
60
lib/Chuck/Config/Setup.js
Executable file
60
lib/Chuck/Config/Setup.js
Executable file
|
|
@ -0,0 +1,60 @@
|
|||
var Chuck = {
|
||||
Physics: {},
|
||||
Loader: {},
|
||||
Control: {},
|
||||
|
||||
b2Vec2 : Box2D.Common.Math.b2Vec2,
|
||||
b2AABB : Box2D.Collision.b2AABB,
|
||||
b2BodyDef : Box2D.Dynamics.b2BodyDef,
|
||||
b2Body : Box2D.Dynamics.b2Body,
|
||||
b2FixtureDef : Box2D.Dynamics.b2FixtureDef,
|
||||
b2Fixture : Box2D.Dynamics.b2Fixture,
|
||||
b2World : Box2D.Dynamics.b2World,
|
||||
b2MassData : Box2D.Collision.Shapes.b2MassData,
|
||||
b2PolygonShape : Box2D.Collision.Shapes.b2PolygonShape,
|
||||
b2CircleShape : Box2D.Collision.Shapes.b2CircleShape,
|
||||
b2DebugDraw : Box2D.Dynamics.b2DebugDraw,
|
||||
b2MouseJointDef : Box2D.Dynamics.Joints.b2MouseJointDef
|
||||
};
|
||||
|
||||
Chuck.Settings = {
|
||||
STAGE_WIDTH : 600,
|
||||
STAGE_HEIGHT : 400,
|
||||
|
||||
// BOX2D INITIALATORS
|
||||
RATIO : 35,
|
||||
BOX2D_WORLD_AABB_SIZE : 3000,
|
||||
BOX2D_ALLOW_SLEEP : true,
|
||||
BOX2D_GRAVITY : 16,
|
||||
BOX2D_VELOCITY_ITERATIONS : 5,
|
||||
BOX2D_POSITION_ITERATIONS : 5,
|
||||
BOX2D_TIME_STEP : 1 / 30,
|
||||
|
||||
// GRAPHIC PATHS
|
||||
GRAPHICS_PATH : 'img',
|
||||
GRAPHICS_SUBPATH_ITEMS : 'Items',
|
||||
GRAPHICS_SUBPATH_CHARACTERS : 'Characters',
|
||||
|
||||
TILE_SIZE : 15,
|
||||
|
||||
// GAME PLAY
|
||||
WALK_SPEED : 2.5,
|
||||
RUN_SPEED : 4.0,
|
||||
FLY_SPEED : 3.2,
|
||||
JUMP_SPEED : 3.0,
|
||||
|
||||
// restitution : bouncyness, friction : rubbing, density : mass
|
||||
TILE_FRICTION : 0.99,
|
||||
TILE_RESTITUTION : 0.1,
|
||||
|
||||
PLAYER_DENSITY : 0.96,
|
||||
PLAYER_FRICTION : 5,
|
||||
PLAYER_MOTION_FRICTION : 0,
|
||||
PLAYER_RESTITUTION : 0.0,
|
||||
PLAYER_LINEAR_DAMPING : .5,
|
||||
|
||||
|
||||
ITEM_DENSITY : 0.9,
|
||||
ITEM_FRICTION : 0.99,
|
||||
ITEM_RESTITUTION : 0.02
|
||||
}
|
||||
77
lib/Chuck/Control/InputControlUnit.js
Executable file
77
lib/Chuck/Control/InputControlUnit.js
Executable file
|
|
@ -0,0 +1,77 @@
|
|||
Chuck.Control.InputControlUnit = function() {
|
||||
var KEY_LEFT;
|
||||
var KEY_RIGHT;
|
||||
var KEY_UP;
|
||||
var KEY_DOWN;
|
||||
|
||||
var _instance;
|
||||
var _ki;
|
||||
var _me;
|
||||
var _shift;
|
||||
var _isJumping;
|
||||
|
||||
this.init();
|
||||
}
|
||||
Chuck.Control.InputControlUnit.prototype.InputControlUnit = function() {
|
||||
this._me = Processor.getInstance().getMe();
|
||||
this._ki = KeyboardInput.getInstance();
|
||||
|
||||
this._ki.registerKey(KEY_LEFT, this.moveLeft, this.stop, this.moveLeft);
|
||||
this._ki.registerKey(KEY_RIGHT, this.moveRight, this.stop, this.moveRight);
|
||||
this._ki.registerKey(KEY_UP, this.jump, this.jumped, this.jumping);
|
||||
this._ki.registerKey(KEY_DOWN, this.duck, this.standUp, this.duck);
|
||||
this._ki.registerKey(KEY_DOWN, this.activateShift, this.activateShift, this.deactivateShift);
|
||||
|
||||
this._ki.registerKey(37, this.wasd);
|
||||
this._ki.registerKey(38, this.wasd);
|
||||
this._ki.registerKey(39, this.wasd);
|
||||
this._ki.registerKey(40, this.wasd);
|
||||
}
|
||||
|
||||
|
||||
Chuck.Control.InputControlUnit.prototype.wasd = function() {
|
||||
trace('wasd benutzen alter...');
|
||||
}
|
||||
|
||||
Chuck.Control.InputControlUnit.prototype.moveLeft = function() {
|
||||
this._me.move(-1);
|
||||
}
|
||||
|
||||
Chuck.Control.InputControlUnit.prototype.moveRight = function() {
|
||||
this._me.move(1);
|
||||
}
|
||||
|
||||
Chuck.Control.InputControlUnit.prototype.stop = function() {
|
||||
this._me.stop();
|
||||
}
|
||||
|
||||
Chuck.Control.InputControlUnit.prototype.jump = function() {
|
||||
this._isJumping = true;
|
||||
this._me.jump();
|
||||
}
|
||||
|
||||
Chuck.Control.InputControlUnit.prototype.jumped = function() {
|
||||
this._isJumping = false;
|
||||
}
|
||||
|
||||
Chuck.Control.InputControlUnit.prototype.jumping = function() {
|
||||
if (this._isJumping) {
|
||||
this._me.jumping();
|
||||
}
|
||||
}
|
||||
|
||||
Chuck.Control.InputControlUnit.prototype.duck = function() {
|
||||
this._me.duck();
|
||||
}
|
||||
|
||||
Chuck.Control.InputControlUnit.prototype.standUp = function() {
|
||||
this._me.standUp();
|
||||
}
|
||||
|
||||
Chuck.Control.InputControlUnit.prototype.activateShift = function() {
|
||||
this._shift = true;
|
||||
}
|
||||
|
||||
Chuck.Control.InputControlUnit.prototype.deactivateShift = function() {
|
||||
this._shift = false;
|
||||
}
|
||||
139
lib/Chuck/Loader/Level.js
Executable file
139
lib/Chuck/Loader/Level.js
Executable file
|
|
@ -0,0 +1,139 @@
|
|||
Chuck.Loader.Level = function(engine) {
|
||||
this._engine = engine;
|
||||
this.init();
|
||||
}
|
||||
|
||||
Chuck.Loader.Level.prototype.init = function() {
|
||||
this.load();
|
||||
}
|
||||
|
||||
Chuck.Loader.Level.prototype.load = function() {
|
||||
$.get('xml/level.xml', $.proxy(this.process, this));
|
||||
}
|
||||
|
||||
Chuck.Loader.Level.prototype.process = function(xml) {
|
||||
var objects, tile;
|
||||
|
||||
objects = $(xml).find('tile');
|
||||
for (var i = 0; i < objects.length; i++) {
|
||||
|
||||
var tile = {
|
||||
shape: objects[i].attributes.shape
|
||||
? parseInt(objects[i].attributes.shape.value)
|
||||
: 1,
|
||||
x: objects[i].attributes.x
|
||||
? parseInt(objects[i].attributes.x.value) * Chuck.Settings.TILE_SIZE
|
||||
: 0,
|
||||
y: objects[i].attributes.y
|
||||
? parseInt(objects[i].attributes.y.value) * Chuck.Settings.TILE_SIZE
|
||||
: 0,
|
||||
rotation: objects[i].attributes.rotation
|
||||
? parseInt(objects[i].attributes.rotation.value)
|
||||
: 0
|
||||
}
|
||||
this.createPhysicTile(tile);
|
||||
}
|
||||
}
|
||||
|
||||
Chuck.Loader.Level.prototype.generateAllTiles = function() {
|
||||
// GENERATING ALL POSSIBLE TILE SHAPES
|
||||
var xpos= 185;
|
||||
var ypos = 150;
|
||||
var space = 0;
|
||||
|
||||
for (var i = 0; i < 8; i++)
|
||||
{
|
||||
for (var r = 0; r < 4; r++)
|
||||
{
|
||||
this.createPhysicTile(i+1, xpos + r * (Chuck.Settings.TILE_SIZE + space), ypos + i * (Chuck.Settings.TILE_SIZE + space), r);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Chuck.Loader.Level.prototype.createPhysicTile = function(tile) {
|
||||
if(tile.rotation == undefined){
|
||||
tile.rotation = 0;
|
||||
}
|
||||
|
||||
var tileSize = Chuck.Settings.TILE_SIZE;
|
||||
var vertices = [];
|
||||
switch(tile.shape) {
|
||||
case 1:
|
||||
vertices[0] = new Chuck.b2Vec2(-tileSize / 2 / Chuck.Settings.RATIO, -tileSize / 2 / Chuck.Settings.RATIO); // o o o
|
||||
vertices[1] = new Chuck.b2Vec2( tileSize / 2 / Chuck.Settings.RATIO, -tileSize / 2 / Chuck.Settings.RATIO); // o o o
|
||||
vertices[2] = new Chuck.b2Vec2( tileSize / 2 / Chuck.Settings.RATIO, tileSize / 2 / Chuck.Settings.RATIO); // o o o
|
||||
vertices[3] = new Chuck.b2Vec2(-tileSize / 2 / Chuck.Settings.RATIO, tileSize / 2 / Chuck.Settings.RATIO);
|
||||
break;
|
||||
|
||||
case 2:
|
||||
vertices[0] = new Chuck.b2Vec2(-tileSize / 2 / Chuck.Settings.RATIO, -tileSize / 2 / Chuck.Settings.RATIO); // o
|
||||
vertices[1] = new Chuck.b2Vec2( tileSize / 2 / Chuck.Settings.RATIO, tileSize / 2 / Chuck.Settings.RATIO); // o o
|
||||
vertices[2] = new Chuck.b2Vec2(-tileSize / 2 / Chuck.Settings.RATIO, tileSize / 2 / Chuck.Settings.RATIO); // o o o
|
||||
break;
|
||||
|
||||
case 3:
|
||||
vertices[0] = new Chuck.b2Vec2(-tileSize / 2 / Chuck.Settings.RATIO, -tileSize / 2 / Chuck.Settings.RATIO); // o
|
||||
vertices[1] = new Chuck.b2Vec2( 0, tileSize / 2 / Chuck.Settings.RATIO); // o
|
||||
vertices[2] = new Chuck.b2Vec2(-tileSize / 2 / Chuck.Settings.RATIO, tileSize / 2 / Chuck.Settings.RATIO); // o o
|
||||
break;
|
||||
|
||||
case 4:
|
||||
vertices[0] = new Chuck.b2Vec2(-tileSize / 2 / Chuck.Settings.RATIO, -tileSize / 2 / Chuck.Settings.RATIO); // o
|
||||
vertices[1] = new Chuck.b2Vec2( tileSize / 2 / Chuck.Settings.RATIO, 0 ); // o o o
|
||||
vertices[2] = new Chuck.b2Vec2( tileSize / 2 / Chuck.Settings.RATIO, tileSize / 2 / Chuck.Settings.RATIO); // o o o
|
||||
vertices[3] = new Chuck.b2Vec2(-tileSize / 2 / Chuck.Settings.RATIO, tileSize / 2 / Chuck.Settings.RATIO);
|
||||
break;
|
||||
|
||||
case 5:
|
||||
vertices[0] = new Chuck.b2Vec2( tileSize / 2 / Chuck.Settings.RATIO, -tileSize / 2 / Chuck.Settings.RATIO); // o
|
||||
vertices[1] = new Chuck.b2Vec2( tileSize / 2 / Chuck.Settings.RATIO, tileSize / 2 / Chuck.Settings.RATIO); // o
|
||||
vertices[2] = new Chuck.b2Vec2( 0 , tileSize / 2 / Chuck.Settings.RATIO); // o o
|
||||
break;
|
||||
|
||||
case 6:
|
||||
vertices[0] = new Chuck.b2Vec2( tileSize / 2 / Chuck.Settings.RATIO, -tileSize / 2 / Chuck.Settings.RATIO); // o
|
||||
vertices[1] = new Chuck.b2Vec2( tileSize / 2 / Chuck.Settings.RATIO, tileSize / 2 / Chuck.Settings.RATIO); // o o o
|
||||
vertices[2] = new Chuck.b2Vec2(-tileSize / 2 / Chuck.Settings.RATIO, tileSize / 2 / Chuck.Settings.RATIO); // o o o
|
||||
vertices[3] = new Chuck.b2Vec2(-tileSize / 2 / Chuck.Settings.RATIO, 0 );
|
||||
break;
|
||||
|
||||
case 7:
|
||||
vertices[0] = new Chuck.b2Vec2(-tileSize / 2 / Chuck.Settings.RATIO, 0 ); //
|
||||
vertices[1] = new Chuck.b2Vec2( 0 , tileSize / 2 / Chuck.Settings.RATIO); // o
|
||||
vertices[2] = new Chuck.b2Vec2(-tileSize / 2 / Chuck.Settings.RATIO, tileSize / 2 / Chuck.Settings.RATIO); // o o
|
||||
break;
|
||||
|
||||
case 8:
|
||||
vertices[0] = new Chuck.b2Vec2(-tileSize / 2 / Chuck.Settings.RATIO, -tileSize / 2 / Chuck.Settings.RATIO);// o o
|
||||
vertices[1] = new Chuck.b2Vec2( 0 , -tileSize / 2 / Chuck.Settings.RATIO);// o o o
|
||||
vertices[2] = new Chuck.b2Vec2( tileSize / 2 / Chuck.Settings.RATIO, 0 ); // o o o
|
||||
vertices[3] = new Chuck.b2Vec2( tileSize / 2 / Chuck.Settings.RATIO, tileSize / 2 / Chuck.Settings.RATIO);
|
||||
vertices[4] = new Chuck.b2Vec2(-tileSize / 2 / Chuck.Settings.RATIO, tileSize / 2 / Chuck.Settings.RATIO);
|
||||
break;
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
var bodyDef = new Chuck.b2BodyDef();
|
||||
bodyDef.type = Chuck.b2Body.b2_staticBody;
|
||||
bodyDef.position.x = tile.x / Chuck.Settings.RATIO;
|
||||
bodyDef.position.y = tile.y / Chuck.Settings.RATIO;
|
||||
bodyDef.angle = tile.rotation * 90 * Math.PI / 180;
|
||||
|
||||
var tileShape = new Chuck.b2PolygonShape;
|
||||
|
||||
tileShape.SetAsArray(vertices, vertices.length);
|
||||
|
||||
var fixtureDef = new Chuck.b2FixtureDef();
|
||||
fixtureDef.shape = tileShape;
|
||||
fixtureDef.density = 0;
|
||||
fixtureDef.friction = Chuck.Settings.TILE_FRICTION;
|
||||
fixtureDef.restitution = Chuck.Settings.TILE_RESTITUTION;
|
||||
fixtureDef.isSensor = false;
|
||||
fixtureDef.userData = 'tile';
|
||||
|
||||
var body = this._engine.createBody(bodyDef);
|
||||
body.CreateFixture(fixtureDef);
|
||||
}
|
||||
7
lib/Chuck/Main.js
Executable file
7
lib/Chuck/Main.js
Executable file
|
|
@ -0,0 +1,7 @@
|
|||
Chuck.Main = function () {
|
||||
|
||||
// Settings were loaded automatically
|
||||
|
||||
var processor = new Chuck.Processor();
|
||||
processor.init();
|
||||
};
|
||||
104
lib/Chuck/Physics/Doll.js
Executable file
104
lib/Chuck/Physics/Doll.js
Executable file
|
|
@ -0,0 +1,104 @@
|
|||
Chuck.Physics.Doll = function(engine){
|
||||
this._engine = engine;
|
||||
this._body;
|
||||
this._legs;
|
||||
this._contactPoint;
|
||||
|
||||
this.init(this._engine.getWorld());
|
||||
}
|
||||
|
||||
Chuck.Physics.Doll.prototype.init = function (world) {
|
||||
|
||||
var bodyDef = new Chuck.b2BodyDef();
|
||||
bodyDef.position.x = 220 / Chuck.Settings.RATIO;
|
||||
bodyDef.position.y = 0 / Chuck.Settings.RATIO;
|
||||
bodyDef.fixedRotation = true;
|
||||
bodyDef.linearDamping = Chuck.Settings.PLAYER_LINEAR_DAMPING;
|
||||
bodyDef.type = Chuck.b2Body.b2_dynamicBody;
|
||||
|
||||
this._body = world.CreateBody(bodyDef);
|
||||
|
||||
var fixtureDef = new Chuck.b2FixtureDef();
|
||||
fixtureDef.density = Chuck.Settings.PLAYER_DENSITY;
|
||||
fixtureDef.friction = 0;
|
||||
fixtureDef.restitution = Chuck.Settings.PLAYER_RESTITUTION;
|
||||
|
||||
var headShape = new Chuck.b2CircleShape();
|
||||
headShape.SetRadius(5 / Chuck.Settings.RATIO);
|
||||
headShape.SetLocalPosition(new Chuck.b2Vec2(0 / Chuck.Settings.RATIO, -37 / Chuck.Settings.RATIO));
|
||||
fixtureDef.shape = headShape;
|
||||
fixtureDef.isSensor = false;
|
||||
fixtureDef.userData = 'myHead';
|
||||
this._body.CreateFixture(fixtureDef);
|
||||
|
||||
var bodyShape = new Chuck.b2PolygonShape();
|
||||
bodyShape.SetAsOrientedBox(5 / Chuck.Settings.RATIO, 16 / Chuck.Settings.RATIO, new Chuck.b2Vec2(0 / Chuck.Settings.RATIO, -21 / Chuck.Settings.RATIO));
|
||||
fixtureDef.shape = bodyShape;
|
||||
fixtureDef.isSensor = false;
|
||||
fixtureDef.userData = 'myBody';
|
||||
this._body.CreateFixture(fixtureDef);
|
||||
|
||||
var legsShape = new Chuck.b2CircleShape();
|
||||
legsShape.SetRadius(5 / Chuck.Settings.RATIO);
|
||||
legsShape.SetLocalPosition(new Chuck.b2Vec2(0 / Chuck.Settings.RATIO, -5 / Chuck.Settings.RATIO));
|
||||
fixtureDef.shape = legsShape;
|
||||
fixtureDef.friction = Chuck.Settings.PLAYER_FRICTION;
|
||||
fixtureDef.isSensor = false;
|
||||
fixtureDef.userData = 'myLegs';
|
||||
this._legs = this._body.CreateFixture(fixtureDef);
|
||||
|
||||
var feetShape = new Chuck.b2CircleShape();
|
||||
feetShape.SetRadius(4 / Chuck.Settings.RATIO);
|
||||
feetShape.SetLocalPosition(new Chuck.b2Vec2(0 / Chuck.Settings.RATIO, 0 / Chuck.Settings.RATIO));
|
||||
fixtureDef.shape = feetShape;
|
||||
fixtureDef.isSensor = true;
|
||||
fixtureDef.userData = 'myFeet';
|
||||
this._body.CreateFixture(fixtureDef);
|
||||
|
||||
this._body.SetActive(false);
|
||||
}
|
||||
|
||||
Chuck.Physics.Doll.prototype.spawn = function (x, y) {
|
||||
this._body.SetPosition(new Chuck.b2Vec2(x / Chuck.Settings.RATIO, y / Chuck.Settings.RATIO));
|
||||
this._body.SetActive(true);
|
||||
}
|
||||
|
||||
Chuck.Physics.Doll.prototype.getBody = function () {
|
||||
return this._body;
|
||||
}
|
||||
|
||||
Chuck.Physics.Doll.prototype._setFriction = function (friction) {
|
||||
if(!friction) friction = -1;
|
||||
|
||||
if (this._legs.GetFriction() != friction)
|
||||
{
|
||||
this._legs.SetFriction(friction);
|
||||
}
|
||||
}
|
||||
|
||||
Chuck.Physics.Doll.prototype.move = function (direction, speed) {
|
||||
this._setFriction(Chuck.Settings.PLAYER_MOTION_FRICTION);
|
||||
this._body.SetAwake(true);
|
||||
var vector = new Chuck.b2Vec2(speed * direction, this._body.GetLinearVelocity().y);
|
||||
this._body.SetLinearVelocity(vector);
|
||||
}
|
||||
|
||||
Chuck.Physics.Doll.prototype.stop = function () {
|
||||
this._setFriction(Chuck.Settings.PLAYER_FRICTION);
|
||||
}
|
||||
|
||||
Chuck.Physics.Doll.prototype.jump = function () {
|
||||
this._body.SetAwake(true);
|
||||
|
||||
var vector = new Chuck.b2Vec2(0, -Chuck.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.
|
||||
}
|
||||
|
||||
Chuck.Physics.Doll.prototype.jumping = function () {
|
||||
var vector = new Chuck.b2Vec2(0, -0.1);
|
||||
this._body.ApplyImpulse(vector, this._body.GetPosition());
|
||||
}
|
||||
|
||||
63
lib/Chuck/Physics/Engine.js
Executable file
63
lib/Chuck/Physics/Engine.js
Executable file
|
|
@ -0,0 +1,63 @@
|
|||
Chuck.Physics.Engine = function () {
|
||||
this._world;
|
||||
|
||||
this.init();
|
||||
}
|
||||
|
||||
Chuck.Physics.Engine.prototype.init = function() {
|
||||
this._world = this._initBox2d();
|
||||
this.setupDebugDraw();
|
||||
}
|
||||
|
||||
Chuck.Physics.Engine.prototype._initBox2d = function() {
|
||||
return new Chuck.b2World(new Chuck.b2Vec2(0,Chuck.Settings.BOX2D_GRAVITY), Chuck.Settings.BOX2D_ALLOW_SLEEP);
|
||||
}
|
||||
|
||||
Chuck.Physics.Engine.prototype.getWorld = function() {
|
||||
return this._world;
|
||||
}
|
||||
|
||||
Chuck.Physics.Engine.prototype.setCollisionDetector = function() {
|
||||
var cd = new CollisionDetector();
|
||||
this._world.SetContactListener(cd);
|
||||
}
|
||||
|
||||
Chuck.Physics.Engine.prototype.setupDebugDraw = function() {
|
||||
//var debugSprite = new Sprite();
|
||||
//View.getInstance().add(debugSprite);
|
||||
|
||||
var debugSprite = document.getElementById("canvas").getContext("2d");
|
||||
|
||||
// set debug draw
|
||||
var dbgDraw = new Chuck.b2DebugDraw();
|
||||
|
||||
dbgDraw.SetSprite(debugSprite);
|
||||
dbgDraw.SetDrawScale(Chuck.Settings.RATIO);
|
||||
dbgDraw.SetAlpha(0.5);
|
||||
dbgDraw.SetFillAlpha(0.1);
|
||||
dbgDraw.SetLineThickness(0);
|
||||
|
||||
dbgDraw.SetFlags(null
|
||||
| Chuck.b2DebugDraw.e_shapeBit
|
||||
//| b2DebugDraw.e_jointBit
|
||||
//| b2DebugDraw.e_coreShapeBit
|
||||
//| b2DebugDraw.e_aabbBit
|
||||
//| b2DebugDraw.e_centerOfMassBit
|
||||
//| b2DebugDraw.e_obbBit
|
||||
//| b2DebugDraw.e_pairBit
|
||||
);
|
||||
|
||||
this._world.SetDebugDraw(dbgDraw);
|
||||
|
||||
this._world.SetWarmStarting(true);
|
||||
}
|
||||
|
||||
Chuck.Physics.Engine.prototype.createBody = function(bodyDef) {
|
||||
return this._world.CreateBody(bodyDef);
|
||||
}
|
||||
|
||||
Chuck.Physics.Engine.prototype.update = function() {
|
||||
this._world.Step(Chuck.Settings.BOX2D_TIME_STEP, Chuck.Settings.BOX2D_VELOCITY_ITERATIONS, Chuck.Settings.BOX2D_POSITION_ITERATIONS);
|
||||
this._world.ClearForces();
|
||||
this._world.DrawDebugData();
|
||||
}
|
||||
179
lib/Chuck/Player.js
Executable file
179
lib/Chuck/Player.js
Executable file
|
|
@ -0,0 +1,179 @@
|
|||
Chuck.Player = function(engine, repository) {
|
||||
this._engine = engine;
|
||||
this._repository = repository;
|
||||
this._standing;
|
||||
this._doll;
|
||||
this._mc;
|
||||
this._currentAnimationState = 'stand';
|
||||
this._lookDirection = 1;
|
||||
this._moveDirection = 0;
|
||||
|
||||
this.init();
|
||||
}
|
||||
|
||||
Chuck.Player.prototype.init = function() {
|
||||
this._doll = new Chuck.Physics.Doll(this._engine);
|
||||
//this._mc = EmbedHandler.load(EmbedHandler.CHUCK);
|
||||
//this._mc.stop();
|
||||
//var mclp = new MovieClipLabelParser();
|
||||
//mclp.parse(this._mc);
|
||||
}
|
||||
|
||||
Chuck.Player.prototype.spawn = function(x, y) {
|
||||
//this._repository.createModel(this._mc, this._doll.getBody());
|
||||
this._doll.spawn(x, y);
|
||||
}
|
||||
|
||||
Chuck.Player.prototype.getDoll = function() {
|
||||
return this._doll;
|
||||
}
|
||||
|
||||
Chuck.Player.prototype.getBody = function() {
|
||||
return this._doll.getBody();
|
||||
}
|
||||
|
||||
Chuck.Player.prototype.setStanding = function(isStanding) {
|
||||
var resetStates = ['jump', 'jumploop'];
|
||||
if (resetStates.indexOf(this._currentAnimationState)>=0 && !this._standing && isStanding)
|
||||
{
|
||||
this._animate('stand');
|
||||
}
|
||||
this._standing = isStanding;
|
||||
}
|
||||
|
||||
Chuck.Player.prototype.isStanding = function() {
|
||||
return this._standing;
|
||||
}
|
||||
|
||||
Chuck.Player.prototype.move = function(direction) {
|
||||
this._moveDirection = direction;
|
||||
|
||||
switch(true) {
|
||||
case direction == this._lookDirection && this.isStanding():
|
||||
this._doll.move(direction, Settings.RUN_SPEED);
|
||||
break;
|
||||
|
||||
case !this.isStanding():
|
||||
this._doll.move(direction, Settings.FLY_SPEED);
|
||||
break;
|
||||
|
||||
default:
|
||||
this._doll.move(direction, Settings.WALK_SPEED);
|
||||
break;
|
||||
}
|
||||
|
||||
if (this.isStanding()) {
|
||||
this._animate(this._calculateWalkAnimation());
|
||||
}
|
||||
}
|
||||
|
||||
Chuck.Player.prototype.stop = function() {
|
||||
this._moveDirection = 0;
|
||||
this._doll.stop();
|
||||
if (this._isWalking() || this._standing) {
|
||||
this._animate('stand');
|
||||
}
|
||||
}
|
||||
|
||||
Chuck.Player.prototype.jump = function() {
|
||||
if (this.isStanding())
|
||||
{
|
||||
this._doll.jump();
|
||||
this._animate('jump');
|
||||
this.setStanding(false);
|
||||
}
|
||||
}
|
||||
|
||||
Chuck.Player.prototype.jumping = function() {
|
||||
if (!this.isStanding()) {
|
||||
this._doll.jumping();
|
||||
}
|
||||
}
|
||||
|
||||
Chuck.Player.prototype.duck = function() {
|
||||
if (this._standing && !this._isWalking()) {
|
||||
this._animate('duck');
|
||||
}
|
||||
}
|
||||
|
||||
Chuck.Player.prototype.standUp = function() {
|
||||
if (this._standing) {
|
||||
this._animate('standup');
|
||||
}
|
||||
}
|
||||
|
||||
Chuck.Player.prototype._animate = function(type) {
|
||||
if (type == this._currentAnimationState) {
|
||||
return;
|
||||
}
|
||||
|
||||
this._mc.gotoAndPlay(type);
|
||||
|
||||
this._currentAnimationState = type;
|
||||
}
|
||||
|
||||
Chuck.Player.prototype._calculateWalkAnimation = function() {
|
||||
if (this._moveDirection == this._lookDirection) {
|
||||
return 'run';
|
||||
}
|
||||
return 'walkback';
|
||||
}
|
||||
|
||||
Chuck.Player.prototype.look = function(x, y) {
|
||||
var degree = Math.atan2(Settings.STAGE_WIDTH / 2 - x, Settings.STAGE_HEIGHT / 2 - 25 - y) / (Math.PI / 180);
|
||||
var lastLookDirection = this._lookDirection;
|
||||
|
||||
if (x < Settings.STAGE_WIDTH / 2) {
|
||||
this._mc.scaleX = -1;
|
||||
this._lookDirection = -1;
|
||||
degree = (-45 + degree / 2);
|
||||
this._mc.head.rotation = degree;
|
||||
} else if (x >= Settings.STAGE_WIDTH / 2) {
|
||||
this._mc.scaleX = 1;
|
||||
this._lookDirection = 1;
|
||||
degree = (45 + -degree / 2) - 90;
|
||||
this._mc.head.rotation = degree;
|
||||
}
|
||||
|
||||
if (this._lookDirection != lastLookDirection && this._isWalking()) {
|
||||
this._animate(this._calculateWalkAnimation());
|
||||
}
|
||||
}
|
||||
|
||||
Chuck.Player.prototype._isWalking = function() {
|
||||
var states = ['walk', 'walkback', 'run'];
|
||||
|
||||
if (states.indexOf(this._currentAnimationState) >= 0) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
// called by CollisionDetection
|
||||
Chuck.Player.prototype.onFootSensorDetection = function(isColliding) {
|
||||
if(isColliding) {
|
||||
if(this._doll.getBody().GetLinearVelocity().y < -Settings.JUMP_SPEED && !this.isStanding()) {
|
||||
return;
|
||||
}
|
||||
this.setStanding(true);
|
||||
} else {
|
||||
// TODO This needs some more thought to it.
|
||||
// maybe take a look at collision groups for collision detection,
|
||||
// to group all tiles together
|
||||
|
||||
//this.setStanding(false);
|
||||
//this._animate('jumploop');
|
||||
}
|
||||
}
|
||||
|
||||
Chuck.Player.prototype.update = function() {
|
||||
this._mc.head.y = this._mc.head_posmask.y;
|
||||
|
||||
if (this._doll.getBody().GetLinearVelocity().x == 0 && this._isWalking()) {
|
||||
this.stop();
|
||||
}
|
||||
|
||||
if (!this._doll.getBody().IsAwake()) {
|
||||
this.setStanding(true);
|
||||
}
|
||||
}
|
||||
59
lib/Chuck/Processor.js
Executable file
59
lib/Chuck/Processor.js
Executable file
|
|
@ -0,0 +1,59 @@
|
|||
Chuck.Processor = function() {
|
||||
|
||||
var self = this;
|
||||
this.count = 0;
|
||||
this._me;
|
||||
this._engine;
|
||||
this._camera;
|
||||
this._repository;
|
||||
this._inputControlUnit;
|
||||
this._keyboardInput;
|
||||
}
|
||||
|
||||
Chuck.Processor.prototype.init = function() {
|
||||
this._engine = new Chuck.Physics.Engine();
|
||||
|
||||
this._me = new Chuck.Player(this._engine, this._repository);
|
||||
|
||||
//this._camera = Camera.getInstance()
|
||||
//this._repository = Repository.getInstance();
|
||||
|
||||
//this._engine.setCollisionDetector();
|
||||
|
||||
//this._inputControlUnit = InputControlUnit.getInstance();
|
||||
//this._keyboardInput = KeyboardInput.getInstance();
|
||||
|
||||
|
||||
new Chuck.Loader.Level(this._engine);
|
||||
//new Items();
|
||||
|
||||
this._me.spawn(100, 0);
|
||||
//this._camera.follow(this._me);
|
||||
|
||||
window.setInterval(this._update, 1000/60, this);
|
||||
//View.getInstance().getSprite().addEventListener(Event.ENTER_FRAME, this._update)
|
||||
}
|
||||
|
||||
|
||||
|
||||
Chuck.Processor.prototype.getMe = function() {
|
||||
return this._me;
|
||||
}
|
||||
|
||||
Chuck.Processor.prototype.getEngine = function() {
|
||||
return this._engine;
|
||||
}
|
||||
|
||||
|
||||
Chuck.Processor.prototype._update = function(self) {
|
||||
self.count++;
|
||||
|
||||
self._engine.update();
|
||||
// Order is important
|
||||
/*
|
||||
self._repository.update();
|
||||
self._keyboardInput.update();
|
||||
self._me.update();
|
||||
self._camera.update();
|
||||
*/
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue