moved files towards new structure. fixes #22

This commit is contained in:
logsol 2012-07-21 21:20:29 +02:00
parent 8cf8f057bb
commit 9de3147406
40 changed files with 0 additions and 110 deletions

View file

View file

@ -0,0 +1,59 @@
define(function(){
function InputController(player) {
this.player = player;
this._shift;
this._isJumping;
}
InputController.prototype.moveLeft = function() {
this.player.move(-1);
}
InputController.prototype.moveRight = function() {
this.player.move(1);
}
InputController.prototype.stop = function() {
this.player.stop();
}
InputController.prototype.jump = function() {
this._isJumping = true;
this.player.jump();
}
InputController.prototype.jumped = function() {
this._isJumping = false;
}
InputController.prototype.jumping = function() {
if (this._isJumping) {
this.player.jumping();
}
}
InputController.prototype.duck = function() {
this.player.duck();
}
InputController.prototype.standUp = function() {
this.player.standUp();
}
InputController.prototype.activateShift = function() {
this._shift = true;
}
InputController.prototype.deactivateShift = function() {
this._shift = false;
}
InputController.prototype.update = function() {
}
return InputController;
});

View file

View file

@ -0,0 +1,263 @@
define(["Chuck/Settings", "Chuck/Constants", "Vendor/Box2D"], function(Settings, Constants, Box2D) {
// Public
function Level(path, engine) {
this.path = path;
this.engine = engine;
this.levelObject = null;
}
Level.prototype.loadLevelInToEngine = function() {
this.loadLevelObjectFromPath(this.path);
this.createPhysicTiles();
}
Level.prototype.unload = function() {
// TODO unload level from engine if necessary
// Perhaps just remove all bodies?
}
// Private
Level.prototype.createPhysicTiles = function () {
if (!this.levelObject || !this.levelObject.tiles || this.levelObject.tiles.length < 1) {
throw "Level: Can't create physic tiles, no tiles found";
}
var tiles = this.levelObject.tiles;
for (var i = tiles.length - 1; i >= 0; i--) {
this.createPhysicTile(tiles[i]);
}
}
Level.prototype.createPhysicTile = function(tile) {
tile.r = tile.r || 0;
var vertices = this.createVertices(tile);
var bodyDef = new Box2D.Dynamics.b2BodyDef();
bodyDef.type = Box2D.Dynamics.b2Body.b2_staticBody;
bodyDef.position.x = tile.x * Settings.TILE_SIZE / Settings.RATIO;
bodyDef.position.y = tile.y * Settings.TILE_SIZE / Settings.RATIO;
bodyDef.angle = tile.r * 90 * Math.PI / 180;
var tileShape = new Box2D.Collision.Shapes.b2PolygonShape();
tileShape.SetAsArray(vertices, vertices.length);
var fixtureDef = new Box2D.Dynamics.b2FixtureDef();
fixtureDef.shape = tileShape;
fixtureDef.density = 0;
fixtureDef.friction = Settings.TILE_FRICTION;
fixtureDef.restitution = Settings.TILE_RESTITUTION;
fixtureDef.isSensor = false;
fixtureDef.userData = Constants.COLLISION_IDENTIFIER_TILE;
this.engine.createBody(bodyDef).CreateFixture(fixtureDef);
}
Level.prototype.createVertices = function(tile) {
var vs = [];
switch(tile.s) {
case 1:
this.addVec(vs, -1, -1); // o o o
this.addVec(vs, 1, -1); // o o o
this.addVec(vs, 1, 1); // o o o
this.addVec(vs, -1, 1);
break;
case 2:
this.addVec(vs, -1, -1); // o
this.addVec(vs, 1, 1); // o o
this.addVec(vs, -1, 1); // o o o
break;
case 3:
this.addVec(vs, -1, -1); // o
this.addVec(vs, 0, 1); // o
this.addVec(vs, -1, 1); // o o
break;
case 4:
this.addVec(vs, -1, -1); // o
this.addVec(vs, 1, 0); // o o o
this.addVec(vs, 1, 1); // o o o
this.addVec(vs, -1, 1);
break;
case 5:
this.addVec(vs, 1, -1); // o
this.addVec(vs, 1, 1); // o
this.addVec(vs, 0, 1); // o o
break;
case 6:
this.addVec(vs, 1, -1); // o
this.addVec(vs, 1, 1); // o o o
this.addVec(vs, -1, 1); // o o o
this.addVec(vs, -1, 0);
break;
case 7:
this.addVec(vs, -1, 0); //
this.addVec(vs, 0, 1); // o
this.addVec(vs, -1, 1); // o o
break;
case 8:
this.addVec(vs, -1, -1); // o o
this.addVec(vs, 0, -1); // o o o
this.addVec(vs, 1, 0); // o o o
this.addVec(vs, 1, 1);
this.addVec(vs, -1, 1);
break;
default:
break;
}
return vs;
}
Level.prototype.mkArg = function(multiplier) {
return Settings.TILE_SIZE / 2 / Settings.RATIO * multiplier;
}
Level.prototype.addVec = function(vs, m1, m2) {
return vs.push(new Box2D.Common.Math.b2Vec2(this.mkArg(m1), this.mkArg(m2)));
}
Level.prototype.loadLevelObjectFromPath = function(path) {
// TODO: load JSON levelObject from path
// s: shape
// x: x-position
// y: y-position
// r: rotation (optional)
// Shapes:
// 1
// o o o
// o o o
// o o o
// 2
// o
// o o
// o o o
// 3
// o
// o
// o o
// 4
// o
// o o o
// o o o
// 5
// o
// o
// o o
// 6
// o
// o o o
// o o o
// 7
//
// o
// o o
// 8
// o o
// o o o
// o o o
this.levelObject = {
tiles: [
{s:1, x:3, y:18},
{s:1, x:37, y:27},
{s:1, x:20, y:24},
{s:1, x:24, y:27},
{s:1, x:37, y:26},
{s:1, x:9, y:18},
{s:2, x:32, y:25, r:1},
{s:1, x:23, y:27},
{s:3, x:34, y:24, r:1},
{s:1, x:35, y:28},
{s:4, x:17, y:21},
{s:2, x:21, y:24},
{s:2, x:42, y:23, r:3},
{s:3, x:30, y:24, r:3},
{s:2, x:22, y:25},
{s:1, x:40, y:25},
{s:1, x:38, y:26},
{s:1, x:8, y:18},
{s:1, x:38, y:25},
{s:1, x:28, y:28},
{s:1, x:36, y:27},
{s:1, x:7, y:18},
{s:2, x:20, y:23},
{s:2, x:43, y:23, r:1},
{s:6, x:31, y:24},
{s:1, x:16, y:21},
{s:1, x:1, y:18},
{s:1, x:31, y:29},
{s:2, x:30, y:25, r:2},
{s:4, x:11, y:18},
{s:1, x:28, y:27},
{s:1, x:28, y:26},
{s:1, x:28, y:29},
{s:1, x:19, y:23},
{s:5, x:12, y:18, r:1},
{s:1, x:42, y:24},
{s:6, x:33, y:24, r:2},
{s:1, x:39, y:25},
{s:1, x:33, y:29},
{s:1, x:29, y:29},
{s:1, x:21, y:25},
{s:1, x:27, y:27},
{s:5, x:16, y:20, r:1},
{s:1, x:5, y:18},
{s:5, x:18, y:21, r:1},
{s:4, x:13, y:19},
{s:1, x:14, y:20},
{s:1, x:30, y:29},
{s:1, x:4, y:18},
{s:1, x:6, y:18},
{s:1, x:2, y:18},
{s:1, x:32, y:24},
{s:1, x:34, y:29},
{s:1, x:32, y:29},
{s:2, x:1, y:16},
{s:1, x:10, y:18},
{s:1, x:42, y:25},
{s:2, x:28, y:25, r:3},
{s:2, x:0, y:16, r:2},
{s:1, x:22, y:27},
{s:1, x:25, y:27},
{s:1, x:31, y:25},
{s:5, x:14, y:19, r:1},
{s:1, x:41, y:25},
{s:1, x:36, y:28},
{s:4, x:15, y:20},
{s:2, x:19, y:22},
{s:3, x:26, y:26, r:3},
{s:1, x:26, y:27},
{s:1, x:18, y:22},
{s:6, x:27, y:26},
{s:1, x:22, y:26},
{s:1, x:1, y:17},
{s:1, x:35, y:29},
{s:1, x:12, y:19}
]
}
}
return Level;
})

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

188
app/game/Core/Player.js Normal file
View file

@ -0,0 +1,188 @@
define(["Chuck/Physics/Doll", "Chuck/Settings"], function(Doll, Settings){
function Player (physicsEngine, id, repository) {
this.physicsEngine = physicsEngine;
this.id = id;
this.repository = repository;
this.standing = false;
this.doll;
this.mc;
this.currentAnimationState = 'stand';
this.lookDirection = 1;
this.moveDirection = 0;
this.init(id);
}
Player.prototype.init = function(id) {
this.doll = new Doll(this.physicsEngine, id);
//this.mc = EmbedHandler.load(EmbedHandler.CHUCK);
//this.mc.stop();
//var mclp = new MovieClipLabelParser();
//mclp.parse(this.mc);
}
Player.prototype.spawn = function(x, y) {
//this.repository.createModel(this.mc, this.doll.getBody());
this.doll.spawn(x, y);
}
Player.prototype.getDoll = function() {
return this.doll;
}
Player.prototype.getBody = function() {
return this.doll.getBody();
}
Player.prototype.setStanding = function(isStanding) {
var resetStates = ['jump', 'jumploop'];
if (resetStates.indexOf(this.currentAnimationState)>=0 && !this.standing && isStanding) {
this.animate('stand');
}
this.standing = isStanding;
}
Player.prototype.isStanding = function() {
return this.standing;
}
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());
}
}
Player.prototype.stop = function() {
this.moveDirection = 0;
this.doll.stop();
if (this.isWalking() || this.standing) {
this.animate('stand');
}
}
Player.prototype.jump = function() {
if (this.isStanding()) {
this.doll.jump();
this.animate('jump');
this.setStanding(false);
}
}
Player.prototype.jumping = function() {
if (!this.isStanding()) {
this.doll.jumping();
}
}
Player.prototype.duck = function() {
if (this.standing && !this.isWalking()) {
this.animate('duck');
}
}
Player.prototype.standUp = function() {
if (this.standing) {
this.animate('standup');
}
}
Player.prototype.animate = function(type) {
if (type == this.currentAnimationState) {
return;
}
//this.mc.gotoAndPlay(type);
this.currentAnimationState = type;
}
Player.prototype.calculateWalkAnimation = function() {
if (this.moveDirection == this.lookDirection) {
return 'run';
}
return 'walkback';
}
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());
}*/
}
Player.prototype.isWalking = function() {
var states = ['walk', 'walkback', 'run'];
if (states.indexOf(this.currentAnimationState) >= 0) {
return true;
}
return false;
}
// called by CollisionDetection
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');
}
}
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);
}
}
Player.prototype.destroy = function() {
this.doll.destroy();
}
return Player;
});

View file

@ -0,0 +1,25 @@
define(["Protocol/Parser"], function(Parser) {
var Helper = {}
Helper.encodeCommand = function(command, options){
return Parser.encode(Helper.assemble(command, options));
}
Helper.assemble = function(command, options){
var commands = {};
commands[command] = options || null;
return commands;
}
Helper.runCommands = function(message, callback){
var commands = Parser.decode(message);
for(var command in commands) {
callback(command, commands[command]);
}
}
return Helper;
});

View file

@ -0,0 +1,14 @@
define(function() {
var Parser = {};
Parser.encode = function(message){
return JSON.stringify(message);
}
Parser.decode = function(message){
return JSON.parse(message);
}
return Parser;
});