refining js version

This commit is contained in:
logsol 2012-06-03 13:31:37 +02:00
parent 3c8fa410ba
commit 8d0ff1a929
15 changed files with 562 additions and 355 deletions

17
css/style.css Executable file
View file

@ -0,0 +1,17 @@
html, body {
margin: 0;
padding: 0;
display: table;
width: 100%;
height: 100%;
background: #000;
}
#container {
text-align: center;
display: table-cell;
vertical-align: middle;
height: 100%;
}
canvas {
background: #333;
}

View file

@ -1,15 +1,23 @@
<html>
<head>
<title>Box2dWeb Demo</title>
</head>
<body onload="Game.Main();">
<canvas id="canvas" width="600" height="400" style="background-color:#333333;"></canvas>
</body>
<link rel="stylesheet" href="css/style.css" />
<script type="text/javascript" src="lib/JQuery/jquery.1.7.2.min.js"></script>
<script type="text/javascript" src="lib/Box2d/Box2dWeb-2.1.a.3.js"></script>
<script type="text/javascript" src="lib/Config/Setup.js"></script>
<script type="text/javascript" src="lib/Game/Physics/Engine.js"></script>
<script type="text/javascript" src="lib/Game/Processor.js"></script>
<script type="text/javascript" src="lib/Game/Physics/Doll.js"></script>
<script type="text/javascript" src="lib/Game/Player.js"></script>
<script type="text/javascript" src="lib/Game/Main.js"></script>
<script type="text/javascript" src="lib/Chuck/Config/Setup.js"></script>
<script type="text/javascript" src="lib/Chuck/Processor.js"></script>
<script type="text/javascript" src="lib/Chuck/Physics/Engine.js"></script>
<script type="text/javascript" src="lib/Chuck/Player.js"></script>
<script type="text/javascript" src="lib/Chuck/Physics/Doll.js"></script>
<script type="text/javascript" src="lib/Chuck/Control/InputControlUnit.js"></script>
<script type="text/javascript" src="lib/Chuck/Loader/Level.js"></script>
<script type="text/javascript" src="lib/Chuck/Main.js"></script>
</head>
<body onload="Chuck.Main();">
<div id="container">
<canvas id="canvas" width="600" height="400"></canvas>
</div>
</body>
</html>

View file

@ -1,5 +1,7 @@
var Game = {
var Chuck = {
Physics: {},
Loader: {},
Control: {},
b2Vec2 : Box2D.Common.Math.b2Vec2,
b2AABB : Box2D.Collision.b2AABB,
@ -15,7 +17,7 @@ var Game = {
b2MouseJointDef : Box2D.Dynamics.Joints.b2MouseJointDef
};
Game.Settings = {
Chuck.Settings = {
STAGE_WIDTH : 600,
STAGE_HEIGHT : 400,

View 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
View 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
View 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
View 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());
}

View file

@ -1,47 +1,44 @@
Game.Physics.Engine = function () {
Chuck.Physics.Engine = function () {
this._world;
this.init();
}
Game.Physics.Engine.prototype.init = function() {
Chuck.Physics.Engine.prototype.init = function() {
this._world = this._initBox2d();
this.setupDebugDraw();
var doll = new Game.Physics.Doll(this._world);
doll.spawn(100,100);
}
Game.Physics.Engine.prototype._initBox2d = function() {
return new Game.b2World(new Game.b2Vec2(0,Game.Settings.BOX2D_GRAVITY), Game.Settings.BOX2D_ALLOW_SLEEP);
Chuck.Physics.Engine.prototype._initBox2d = function() {
return new Chuck.b2World(new Chuck.b2Vec2(0,Chuck.Settings.BOX2D_GRAVITY), Chuck.Settings.BOX2D_ALLOW_SLEEP);
}
Game.Physics.Engine.prototype.getWorld = function() {
Chuck.Physics.Engine.prototype.getWorld = function() {
return this._world;
}
Game.Physics.Engine.prototype.setCollisionDetector = function() {
Chuck.Physics.Engine.prototype.setCollisionDetector = function() {
var cd = new CollisionDetector();
this._world.SetContactListener(cd);
}
Game.Physics.Engine.prototype.setupDebugDraw = function() {
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 Game.b2DebugDraw();
var dbgDraw = new Chuck.b2DebugDraw();
dbgDraw.SetSprite(debugSprite);
dbgDraw.SetDrawScale(Game.Settings.RATIO);
dbgDraw.SetDrawScale(Chuck.Settings.RATIO);
dbgDraw.SetAlpha(0.5);
dbgDraw.SetFillAlpha(0.1);
dbgDraw.SetLineThickness(0);
dbgDraw.SetFlags(null
| Game.b2DebugDraw.e_shapeBit
| Chuck.b2DebugDraw.e_shapeBit
//| b2DebugDraw.e_jointBit
//| b2DebugDraw.e_coreShapeBit
//| b2DebugDraw.e_aabbBit
@ -55,12 +52,12 @@ Game.Physics.Engine.prototype.setupDebugDraw = function() {
this._world.SetWarmStarting(true);
}
Game.Physics.Engine.prototype.createBody = function(bodyDef) {
Chuck.Physics.Engine.prototype.createBody = function(bodyDef) {
return this._world.CreateBody(bodyDef);
}
Game.Physics.Engine.prototype.update = function() {
this._world.Step(Game.Settings.BOX2D_TIME_STEP, Game.Settings.BOX2D_VELOCITY_ITERATIONS, Game.Settings.BOX2D_POSITION_ITERATIONS);
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();
}

View file

@ -1,34 +1,38 @@
var Player = function() {
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();
}
function Player() {
this._doll = new Doll();
this._mc = EmbedHandler.load(EmbedHandler.CHUCK);
this._mc.stop();
var mclp = new MovieClipLabelParser();
mclp.parse(this._mc);
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);
}
function spawn(x, y) {
Repository.getInstance().createModel(this._mc, this._doll.getBody());
Chuck.Player.prototype.spawn = function(x, y) {
//this._repository.createModel(this._mc, this._doll.getBody());
this._doll.spawn(x, y);
}
function getDoll() {
Chuck.Player.prototype.getDoll = function() {
return this._doll;
}
function getBody() {
Chuck.Player.prototype.getBody = function() {
return this._doll.getBody();
}
function setStanding(isStanding) {
Chuck.Player.prototype.setStanding = function(isStanding) {
var resetStates = ['jump', 'jumploop'];
if (resetStates.indexOf(this._currentAnimationState)>=0 && !this._standing && isStanding)
{
@ -37,23 +41,23 @@ function setStanding(isStanding) {
this._standing = isStanding;
}
function isStanding() {
Chuck.Player.prototype.isStanding = function() {
return this._standing;
}
function move(direction) {
Chuck.Player.prototype.move = function(direction) {
this._moveDirection = direction;
switch(true) {
case direction == this._lookDirection && this.isStanding()
case direction == this._lookDirection && this.isStanding():
this._doll.move(direction, Settings.RUN_SPEED);
break;
case !this.isStanding()
case !this.isStanding():
this._doll.move(direction, Settings.FLY_SPEED);
break;
default
default:
this._doll.move(direction, Settings.WALK_SPEED);
break;
}
@ -63,7 +67,7 @@ function move(direction) {
}
}
function stop() {
Chuck.Player.prototype.stop = function() {
this._moveDirection = 0;
this._doll.stop();
if (this._isWalking() || this._standing) {
@ -71,7 +75,7 @@ function stop() {
}
}
function jump() {
Chuck.Player.prototype.jump = function() {
if (this.isStanding())
{
this._doll.jump();
@ -80,29 +84,26 @@ function jump() {
}
}
function jumping() {
Chuck.Player.prototype.jumping = function() {
if (!this.isStanding()) {
this._doll.jumping();
}
}
function duck() {
if (this._standing && !this._isWalking())
{
Chuck.Player.prototype.duck = function() {
if (this._standing && !this._isWalking()) {
this._animate('duck');
}
}
function standUp() {
if (this._standing)
{
Chuck.Player.prototype.standUp = function() {
if (this._standing) {
this._animate('standup');
}
}
function _animate(type) {
if (type == this._currentAnimationState)
{
Chuck.Player.prototype._animate = function(type) {
if (type == this._currentAnimationState) {
return;
}
@ -111,15 +112,14 @@ function _animate(type) {
this._currentAnimationState = type;
}
function _calculateWalkAnimation() {
if (this._moveDirection == this._lookDirection)
{
Chuck.Player.prototype._calculateWalkAnimation = function() {
if (this._moveDirection == this._lookDirection) {
return 'run';
}
return 'walkback';
}
function look(x, y) {
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;
@ -140,7 +140,7 @@ function look(x, y) {
}
}
function _isWalking() {
Chuck.Player.prototype._isWalking = function() {
var states = ['walk', 'walkback', 'run'];
if (states.indexOf(this._currentAnimationState) >= 0) {
@ -150,7 +150,7 @@ function _isWalking() {
}
// called by CollisionDetection
function onFootSensorDetection(isColliding) {
Chuck.Player.prototype.onFootSensorDetection = function(isColliding) {
if(isColliding) {
if(this._doll.getBody().GetLinearVelocity().y < -Settings.JUMP_SPEED && !this.isStanding()) {
return;
@ -166,7 +166,7 @@ function onFootSensorDetection(isColliding) {
}
}
function update() {
Chuck.Player.prototype.update = function() {
this._mc.head.y = this._mc.head_posmask.y;
if (this._doll.getBody().GetLinearVelocity().x == 0 && this._isWalking()) {

59
lib/Chuck/Processor.js Executable file
View 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();
*/
}

View file

@ -1,114 +0,0 @@
Game.Main = function () {
var processor = new Game.Processor();
processor.init();
//mouse
/*
var mouseX, mouseY, mousePVec, isMouseDown, selectedBody, mouseJoint;
var canvasPosition = getElementPosition(document.getElementById("canvas"));
document.addEventListener("mousedown", function(e) {
isMouseDown = true;
handleMouseMove(e);
document.addEventListener("mousemove", handleMouseMove, true);
}, true);
document.addEventListener("mouseup", function() {
document.removeEventListener("mousemove", handleMouseMove, true);
isMouseDown = false;
mouseX = undefined;
mouseY = undefined;
}, true);
function handleMouseMove(e) {
mouseX = (e.clientX - canvasPosition.x) / 30;
mouseY = (e.clientY - canvasPosition.y) / 30;
};
function getBodyAtMouse() {
mousePVec = new b2Vec2(mouseX, mouseY);
var aabb = new b2AABB();
aabb.lowerBound.Set(mouseX - 0.001, mouseY - 0.001);
aabb.upperBound.Set(mouseX + 0.001, mouseY + 0.001);
// Query the world for overlapping shapes.
selectedBody = null;
world.QueryAABB(getBodyCB, aabb);
return selectedBody;
}
function getBodyCB(fixture) {
if(fixture.GetBody().GetType() != b2Body.b2_staticBody) {
if(fixture.GetShape().TestPoint(fixture.GetBody().GetTransform(), mousePVec)) {
selectedBody = fixture.GetBody();
return false;
}
}
return true;
}
//update
function update() {
if(isMouseDown && (!mouseJoint)) {
var body = getBodyAtMouse();
if(body) {
var md = new b2MouseJointDef();
md.bodyA = world.GetGroundBody();
md.bodyB = body;
md.target.Set(mouseX, mouseY);
md.collideConnected = true;
md.maxForce = 300.0 * body.GetMass();
mouseJoint = world.CreateJoint(md);
body.SetAwake(true);
}
}
if(mouseJoint) {
if(isMouseDown) {
mouseJoint.SetTarget(new b2Vec2(mouseX, mouseY));
} else {
world.DestroyJoint(mouseJoint);
mouseJoint = null;
}
}
world.Step(1 / 60, 10, 10);
world.DrawDebugData();
world.ClearForces();
};
//helpers
//http://js-tut.aardon.de/js-tut/tutorial/position.html
function getElementPosition(element) {
var elem=element, tagname="", x=0, y=0;
while((typeof(elem) == "object") && (typeof(elem.tagName) != "undefined")) {
y += elem.offsetTop;
x += elem.offsetLeft;
tagname = elem.tagName.toUpperCase();
if(tagname == "BODY")
elem=0;
if(typeof(elem) == "object") {
if(typeof(elem.offsetParent) == "object")
elem = elem.offsetParent;
}
}
return {
x: x,
y: y
};
}
*/
};

View file

@ -1,103 +0,0 @@
Game.Physics.Doll = function(world){
this._body;
this._legs;
this._contactPoint;
this.init(world);
}
Game.Physics.Doll.prototype.init = function (world) {
var bodyDef = new Game.b2BodyDef();
bodyDef.position.x = 220 / Game.Settings.RATIO;
bodyDef.position.y = 0 / Game.Settings.RATIO;
bodyDef.fixedRotation = true;
bodyDef.linearDamping = Game.Settings.PLAYER_LINEAR_DAMPING;
bodyDef.type = Game.b2Body.b2_dynamicBody;
this._body = world.CreateBody(bodyDef);
var fixtureDef = new Game.b2FixtureDef();
fixtureDef.density = Game.Settings.PLAYER_DENSITY;
fixtureDef.friction = 0;
fixtureDef.restitution = Game.Settings.PLAYER_RESTITUTION;
var headShape = new Game.b2CircleShape();
headShape.SetRadius(5 / Game.Settings.RATIO);
headShape.SetLocalPosition(new Game.b2Vec2(0 / Game.Settings.RATIO, -37 / Game.Settings.RATIO));
fixtureDef.shape = headShape;
fixtureDef.isSensor = false;
fixtureDef.userData = 'myHead';
this._body.CreateFixture(fixtureDef);
var bodyShape = new Game.b2PolygonShape();
bodyShape.SetAsOrientedBox(5 / Game.Settings.RATIO, 16 / Game.Settings.RATIO, new Game.b2Vec2(0 / Game.Settings.RATIO, -21 / Game.Settings.RATIO));
fixtureDef.shape = bodyShape;
fixtureDef.isSensor = false;
fixtureDef.userData = 'myBody';
this._body.CreateFixture(fixtureDef);
var legsShape = new Game.b2CircleShape();
legsShape.SetRadius(5 / Game.Settings.RATIO);
legsShape.SetLocalPosition(new Game.b2Vec2(0 / Game.Settings.RATIO, -5 / Game.Settings.RATIO));
fixtureDef.shape = legsShape;
fixtureDef.friction = Game.Settings.PLAYER_FRICTION;
fixtureDef.isSensor = false;
fixtureDef.userData = 'myLegs';
this._legs = this._body.CreateFixture(fixtureDef);
var feetShape = new Game.b2CircleShape();
feetShape.SetRadius(4 / Game.Settings.RATIO);
feetShape.SetLocalPosition(new Game.b2Vec2(0 / Game.Settings.RATIO, 0 / Game.Settings.RATIO));
fixtureDef.shape = feetShape;
fixtureDef.isSensor = true;
fixtureDef.userData = 'myFeet';
this._body.CreateFixture(fixtureDef);
this._body.SetActive(false);
}
Game.Physics.Doll.prototype.spawn = function (x, y) {
this._body.SetPosition(new Game.b2Vec2(x / Game.Settings.RATIO, y / Game.Settings.RATIO));
this._body.SetActive(true);
}
Game.Physics.Doll.prototype.getBody = function () {
return this._body;
}
Game.Physics.Doll.prototype._setFriction = function (friction) {
if(!friction) friction = -1;
if (this._legs.GetFriction() != friction)
{
this._legs.SetFriction(friction);
}
}
Game.Physics.Doll.prototype.move = function (direction, speed) {
this._setFriction(Game.Settings.PLAYER_MOTION_FRICTION);
this._body.SetAwake(true);
var vector = new Game.b2Vec2(speed * direction, this._body.GetLinearVelocity().y);
this._body.SetLinearVelocity(vector);
}
Game.Physics.Doll.prototype.stop = function () {
this._setFriction(Game.Settings.PLAYER_FRICTION);
}
Game.Physics.Doll.prototype.jump = function () {
this._body.SetAwake(true);
var vector = new Game.b2Vec2(0, -Game.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.
}
Game.Physics.Doll.prototype.jumping = function () {
var vector = new Game.b2Vec2(0, -0.1);
this._body.ApplyImpulse(vector, this._body.GetPosition());
}

View file

@ -1,66 +0,0 @@
Game.Processor = function() {
var self = this;
this.count = 0;
this._me;
this._engine;
this._camera;
this._repository;
this._inputControlUnit;
this._keyboardInput;
}
Game.Processor.prototype.init = function() {
this._engine = new Game.Physics.Engine();
/*
this._me = new Player();
this._camera = Camera.getInstance()
this._repository = Repository.getInstance();
this._engine.setCollisionDetector();
this._inputControlUnit = InputControlUnit.getInstance();
this._keyboardInput = KeyboardInput.getInstance();
new Level();
new Items();
*/
//Out.put("Players spawn after 3 seconds.");
//Out.put("Notice, that Player2 seems to be afk xD");
window.setInterval(this._update, 1000/60, this);
//View.getInstance().getSprite().addEventListener(Event.ENTER_FRAME, this._update)
}
Game.Processor.prototype.getMe = function() {
return this._me;
}
Game.Processor.prototype.getEngine = function() {
return this._engine;
}
Game.Processor.prototype._spawnPlayers = function(p1, p2) {
p1.spawn(50, 250);
this._camera.follow(p1);
}
Game.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();
*/
}

4
lib/JQuery/jquery.1.7.2.min.js vendored Executable file

File diff suppressed because one or more lines are too long

76
xml/level.xml Executable file
View file

@ -0,0 +1,76 @@
<level><tiles><tile shape="1" x="3" y="18"/>
<tile shape="1" x="37" y="27"/>
<tile shape="1" x="20" y="24"/>
<tile shape="1" x="24" y="27"/>
<tile shape="1" x="37" y="26"/>
<tile shape="1" x="9" y="18"/>
<tile shape="2" x="32" y="25" rotation="1"/>
<tile shape="1" x="23" y="27"/>
<tile shape="3" x="34" y="24" rotation="1"/>
<tile shape="1" x="35" y="28"/>
<tile shape="4" x="17" y="21"/>
<tile shape="2" x="21" y="24"/>
<tile shape="2" x="42" y="23" rotation="3"/>
<tile shape="3" x="30" y="24" rotation="3"/>
<tile shape="2" x="22" y="25"/>
<tile shape="1" x="40" y="25"/>
<tile shape="1" x="38" y="26"/>
<tile shape="1" x="8" y="18"/>
<tile shape="1" x="38" y="25"/>
<tile shape="1" x="28" y="28"/>
<tile shape="1" x="36" y="27"/>
<tile shape="1" x="7" y="18"/>
<tile shape="2" x="20" y="23"/>
<tile shape="2" x="43" y="23" rotation="1"/>
<tile shape="6" x="31" y="24"/>
<tile shape="1" x="16" y="21"/>
<tile shape="1" x="1" y="18"/>
<tile shape="1" x="31" y="29"/>
<tile shape="2" x="30" y="25" rotation="2"/>
<tile shape="4" x="11" y="18"/>
<tile shape="1" x="28" y="27"/>
<tile shape="1" x="28" y="26"/>
<tile shape="1" x="28" y="29"/>
<tile shape="1" x="19" y="23"/>
<tile shape="5" x="12" y="18" rotation="1"/>
<tile shape="1" x="42" y="24"/>
<tile shape="6" x="33" y="24" rotation="2"/>
<tile shape="1" x="39" y="25"/>
<tile shape="1" x="33" y="29"/>
<tile shape="1" x="29" y="29"/>
<tile shape="1" x="21" y="25"/>
<tile shape="1" x="27" y="27"/>
<tile shape="5" x="16" y="20" rotation="1"/>
<tile shape="1" x="5" y="18"/>
<tile shape="5" x="18" y="21" rotation="1"/>
<tile shape="4" x="13" y="19"/>
<tile shape="1" x="14" y="20"/>
<tile shape="1" x="30" y="29"/>
<tile shape="1" x="4" y="18"/>
<tile shape="1" x="6" y="18"/>
<tile shape="1" x="2" y="18"/>
<tile shape="1" x="32" y="24"/>
<tile shape="1" x="34" y="29"/>
<tile shape="1" x="32" y="29"/>
<tile shape="2" x="1" y="16"/>
<tile shape="1" x="10" y="18"/>
<tile shape="1" x="42" y="25"/>
<tile shape="2" x="28" y="25" rotation="3"/>
<tile shape="2" x="0" y="16" rotation="2"/>
<tile shape="1" x="22" y="27"/>
<tile shape="1" x="25" y="27"/>
<tile shape="1" x="31" y="25"/>
<tile shape="5" x="14" y="19" rotation="1"/>
<tile shape="1" x="41" y="25"/>
<tile shape="1" x="36" y="28"/>
<tile shape="4" x="15" y="20"/>
<tile shape="2" x="19" y="22"/>
<tile shape="3" x="26" y="26" rotation="3"/>
<tile shape="1" x="26" y="27"/>
<tile shape="1" x="18" y="22"/>
<tile shape="6" x="27" y="26"/>
<tile shape="1" x="22" y="26"/>
<tile shape="1" x="1" y="17"/>
<tile shape="1" x="35" y="29"/>
<tile shape="1" x="12" y="19"/>
</tiles></level>