initial commit

This commit is contained in:
logsol 2012-06-02 18:23:43 +02:00 committed by karl
parent c9d9ee2a70
commit 753b173fbc
13 changed files with 44688 additions and 0 deletions

114
lib/Game/Main.js Executable file
View file

@ -0,0 +1,114 @@
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
};
}
*/
};

103
lib/Game/Physics/Doll.js Executable file
View file

@ -0,0 +1,103 @@
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());
}

66
lib/Game/Physics/Engine.js Executable file
View file

@ -0,0 +1,66 @@
Game.Physics.Engine = function () {
this._world;
this.init();
}
Game.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);
}
Game.Physics.Engine.prototype.getWorld = function() {
return this._world;
}
Game.Physics.Engine.prototype.setCollisionDetector = function() {
var cd = new CollisionDetector();
this._world.SetContactListener(cd);
}
Game.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();
dbgDraw.SetSprite(debugSprite);
dbgDraw.SetDrawScale(Game.Settings.RATIO);
dbgDraw.SetAlpha(0.5);
dbgDraw.SetFillAlpha(0.1);
dbgDraw.SetLineThickness(0);
dbgDraw.SetFlags(null
| Game.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);
}
Game.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);
this._world.ClearForces();
this._world.DrawDebugData();
}

179
lib/Game/Player.js Executable file
View file

@ -0,0 +1,179 @@
var Player = function() {
this._standing;
this._doll;
this._mc;
this._currentAnimationState = 'stand';
this._lookDirection = 1;
this._moveDirection = 0;
}
function Player() {
this._doll = new Doll();
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());
this._doll.spawn(x, y);
}
function getDoll() {
return this._doll;
}
function getBody() {
return this._doll.getBody();
}
function setStanding(isStanding) {
var resetStates = ['jump', 'jumploop'];
if (resetStates.indexOf(this._currentAnimationState)>=0 && !this._standing && isStanding)
{
this._animate('stand');
}
this._standing = isStanding;
}
function isStanding() {
return this._standing;
}
function move(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());
}
}
function stop() {
this._moveDirection = 0;
this._doll.stop();
if (this._isWalking() || this._standing) {
this._animate('stand');
}
}
function jump() {
if (this.isStanding())
{
this._doll.jump();
this._animate('jump');
this.setStanding(false);
}
}
function jumping() {
if (!this.isStanding()) {
this._doll.jumping();
}
}
function duck() {
if (this._standing && !this._isWalking())
{
this._animate('duck');
}
}
function standUp() {
if (this._standing)
{
this._animate('standup');
}
}
function _animate(type) {
if (type == this._currentAnimationState)
{
return;
}
this._mc.gotoAndPlay(type);
this._currentAnimationState = type;
}
function _calculateWalkAnimation() {
if (this._moveDirection == this._lookDirection)
{
return 'run';
}
return 'walkback';
}
function look(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());
}
}
function _isWalking() {
var states = ['walk', 'walkback', 'run'];
if (states.indexOf(this._currentAnimationState) >= 0) {
return true;
}
return false;
}
// called by CollisionDetection
function onFootSensorDetection(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');
}
}
function update() {
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);
}
}

66
lib/Game/Processor.js Executable file
View file

@ -0,0 +1,66 @@
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();
*/
}