control features implemented

This commit is contained in:
logsol 2012-06-03 18:22:27 +02:00
parent 8d0ff1a929
commit c2b0baaea2
10 changed files with 213 additions and 63 deletions

View file

@ -11,7 +11,10 @@
<script type="text/javascript" src="lib/Chuck/Physics/Engine.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/Player.js"></script>
<script type="text/javascript" src="lib/Chuck/Physics/Doll.js"></script> <script type="text/javascript" src="lib/Chuck/Physics/Doll.js"></script>
<script type="text/javascript" src="lib/Chuck/Collision/Detector.js"></script>
<script type="text/javascript" src="lib/Chuck/Control/InputControlUnit.js"></script> <script type="text/javascript" src="lib/Chuck/Control/InputControlUnit.js"></script>
<script type="text/javascript" src="lib/Chuck/Control/KeyboardInput.js"></script>
<script type="text/javascript" src="lib/Chuck/Control/Key.js"></script>
<script type="text/javascript" src="lib/Chuck/Loader/Level.js"></script> <script type="text/javascript" src="lib/Chuck/Loader/Level.js"></script>
<script type="text/javascript" src="lib/Chuck/Main.js"></script> <script type="text/javascript" src="lib/Chuck/Main.js"></script>
</head> </head>

33
lib/Chuck/Collision/Detector.js Executable file
View file

@ -0,0 +1,33 @@
Chuck.Collision.Detector = function(me) { //extends b2ContactListener {
this._me = me;
this._listener = new Chuck.b2ContactListener();
this._listener._chuckDetector = this;
this._listener.BeginContact = this.BeginContact;
this._listener.PostSolve = this.PostSolve;
this._listener.EndContact = this.EndContact;
}
Chuck.Collision.Detector.prototype.getListener = function() {
return this._listener;
}
Chuck.Collision.Detector.prototype.handleStand = function(point, isColliding) {
if (point.GetFixtureA().GetUserData() == 'myFeet' || point.GetFixtureB().GetUserData() == 'myFeet') {
this._me.onFootSensorDetection(isColliding);
}
}
/** Extension **/
Chuck.Collision.Detector.prototype.BeginContact = function(point) {
this._chuckDetector.handleStand(point, true);
}
Chuck.Collision.Detector.prototype.PostSolve = function(point, impulse) {
this._chuckDetector.handleStand(point, true);
}
Chuck.Collision.Detector.prototype.EndContact = function(point) {
this._chuckDetector.handleStand(point, false);
}

View file

@ -2,6 +2,7 @@ var Chuck = {
Physics: {}, Physics: {},
Loader: {}, Loader: {},
Control: {}, Control: {},
Collision: {},
b2Vec2 : Box2D.Common.Math.b2Vec2, b2Vec2 : Box2D.Common.Math.b2Vec2,
b2AABB : Box2D.Collision.b2AABB, b2AABB : Box2D.Collision.b2AABB,
@ -14,7 +15,8 @@ var Chuck = {
b2PolygonShape : Box2D.Collision.Shapes.b2PolygonShape, b2PolygonShape : Box2D.Collision.Shapes.b2PolygonShape,
b2CircleShape : Box2D.Collision.Shapes.b2CircleShape, b2CircleShape : Box2D.Collision.Shapes.b2CircleShape,
b2DebugDraw : Box2D.Dynamics.b2DebugDraw, b2DebugDraw : Box2D.Dynamics.b2DebugDraw,
b2MouseJointDef : Box2D.Dynamics.Joints.b2MouseJointDef b2MouseJointDef : Box2D.Dynamics.Joints.b2MouseJointDef,
b2ContactListener : Box2D.Dynamics.b2ContactListener
}; };
Chuck.Settings = { Chuck.Settings = {
@ -49,7 +51,7 @@ Chuck.Settings = {
PLAYER_DENSITY : 0.96, PLAYER_DENSITY : 0.96,
PLAYER_FRICTION : 5, PLAYER_FRICTION : 5,
PLAYER_MOTION_FRICTION : 0, PLAYER_MOTION_FRICTION : 0.1,
PLAYER_RESTITUTION : 0.0, PLAYER_RESTITUTION : 0.0,
PLAYER_LINEAR_DAMPING : .5, PLAYER_LINEAR_DAMPING : .5,

View file

@ -1,36 +1,27 @@
Chuck.Control.InputControlUnit = function() { Chuck.Control.InputControlUnit = function(ki, me) {
var KEY_LEFT; this._ki = ki;
var KEY_RIGHT; this._me = me;
var KEY_UP;
var KEY_DOWN;
var _instance; this._shift;
var _ki; this._isJumping;
var _me;
var _shift; this.KEY_LEFT = 65;
var _isJumping; this.KEY_RIGHT = 68;
this.KEY_UP = 87;
this.KEY_DOWN = 83;
this.init(); 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); Chuck.Control.InputControlUnit.prototype.init = function() {
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.setInputControlUnit(this);
this._ki.registerKey(38, this.wasd);
this._ki.registerKey(39, this.wasd);
this._ki.registerKey(40, this.wasd);
}
this._ki.registerKey(this.KEY_LEFT, 'moveLeft', 'stop', 'moveLeft');
Chuck.Control.InputControlUnit.prototype.wasd = function() { this._ki.registerKey(this.KEY_RIGHT, 'moveRight', 'stop', 'moveRight');
trace('wasd benutzen alter...'); this._ki.registerKey(this.KEY_UP, 'jump', 'jumped', 'jumping');
this._ki.registerKey(this.KEY_DOWN, 'duck', 'standUp', 'duck');
this._ki.registerKey(this.KEY_DOWN, 'activateShift', 'activateShift', 'deactivateShift');
} }
Chuck.Control.InputControlUnit.prototype.moveLeft = function() { Chuck.Control.InputControlUnit.prototype.moveLeft = function() {

56
lib/Chuck/Control/Key.js Executable file
View file

@ -0,0 +1,56 @@
Chuck.Control.Key = function() {
this._active = false;
this._activityUpdateStatus = false;
this._activityUpdateNeeded = false;
this._keyDown = null;
this._keyUp = null;
this._keyFrame = null;
}
Chuck.Control.Key.prototype.setActivityUpdateStatus = function(active) {
this._activityUpdateStatus = active;
}
Chuck.Control.Key.prototype.getActivityUpdateStatus = function() {
return this._activityUpdateStatus;
}
Chuck.Control.Key.prototype.setActivityUpdateNeeded = function(need) {
this._activityUpdateNeeded = need;
}
Chuck.Control.Key.prototype.getActivityUpdateNeeded = function() {
return this._activityUpdateNeeded;
}
Chuck.Control.Key.prototype.setActive = function(active) {
this._active = active;
}
Chuck.Control.Key.prototype.getActive = function() {
return this._active;
}
Chuck.Control.Key.prototype.setKeyDownFunction = function(f) {
this._keyDown = f;
}
Chuck.Control.Key.prototype.getKeyDownFunction = function() {
return this._keyDown;
}
Chuck.Control.Key.prototype.setKeyUpFunction = function(f) {
this._keyUp = f;
}
Chuck.Control.Key.prototype.getKeyUpFunction = function() {
return this._keyUp;
}
Chuck.Control.Key.prototype.setKeyFrameFunction = function(f) {
this._keyFrame = f;
}
Chuck.Control.Key.prototype.getKeyFrameFunction = function() {
return this._keyFrame;
}

View file

@ -0,0 +1,72 @@
Chuck.Control.KeyboardInput = function() {
this._registry = {};
this._inputControlUnit = null;
this.init();
}
Chuck.Control.KeyboardInput.prototype.init = function() {
$(window).keydown($.proxy(this._onKeyDown, this));
$(window).keyup($.proxy(this._onKeyUp, this));
}
Chuck.Control.KeyboardInput.prototype.setInputControlUnit = function(inputControlUnit) {
this._inputControlUnit = inputControlUnit;
}
Chuck.Control.KeyboardInput.prototype.registerKey = function(keyCode, onKeyDown, onKeyUp, onKeyFrame) {
var key = new Chuck.Control.Key();
key.setKeyDownFunction(onKeyDown);
key.setKeyUpFunction(onKeyUp);
key.setKeyFrameFunction(onKeyFrame);
this._registry[keyCode] = key;
}
Chuck.Control.KeyboardInput.prototype._getKeyByKeyCode = function(keyCode) {
return this._registry[keyCode];
}
Chuck.Control.KeyboardInput.prototype._onKeyDown = function(e) {
var key = this._getKeyByKeyCode(e.keyCode);
if (key && key.getActive() == false) {
key.setActivityUpdateStatus(true);
key.setActivityUpdateNeeded(true);
}
}
Chuck.Control.KeyboardInput.prototype._onKeyUp = function(e) {
var key = this._getKeyByKeyCode(e.keyCode);
if (key != null) {
key.setActivityUpdateStatus(false);
key.setActivityUpdateNeeded(true);
}
}
Chuck.Control.KeyboardInput.prototype.update = function() {
var callback = null;
var self = this;
$.each(this._registry, function(keyCode, key) {
if (key.getActivityUpdateNeeded()) {
if (key.getActivityUpdateStatus() == true) {
callback = key.getKeyDownFunction();
key.setActive(true);
} else {
callback = key.getKeyUpFunction();
key.setActive(false);
}
key.setActivityUpdateNeeded(false);
}
if (callback) {
self._inputControlUnit[callback]();
} else {
if (key.getActive()) {
callback = key.getKeyFrameFunction();
if (callback) {
self._inputControlUnit[callback]();
}
}
}
callback = null;
});
}

View file

@ -1,7 +1,5 @@
Chuck.Main = function () { Chuck.Main = function () {
// Settings were loaded automatically // Settings were loaded automatically
var processor = new Chuck.Processor(); var processor = new Chuck.Processor();
processor.init(); processor.init();
}; };

View file

@ -1,6 +1,5 @@
Chuck.Physics.Engine = function () { Chuck.Physics.Engine = function () {
this._world; this._world;
this.init(); this.init();
} }
@ -17,9 +16,10 @@ Chuck.Physics.Engine.prototype.getWorld = function() {
return this._world; return this._world;
} }
Chuck.Physics.Engine.prototype.setCollisionDetector = function() { Chuck.Physics.Engine.prototype.setCollisionDetector = function(me) {
var cd = new CollisionDetector(); var cd = new Chuck.Collision.Detector(me);
this._world.SetContactListener(cd); var listener = cd.getListener();
this._world.SetContactListener(listener);
} }
Chuck.Physics.Engine.prototype.setupDebugDraw = function() { Chuck.Physics.Engine.prototype.setupDebugDraw = function() {

View file

@ -1,7 +1,7 @@
Chuck.Player = function(engine, repository) { Chuck.Player = function(engine, repository) {
this._engine = engine; this._engine = engine;
this._repository = repository; this._repository = repository;
this._standing; this._standing = false;
this._doll; this._doll;
this._mc; this._mc;
this._currentAnimationState = 'stand'; this._currentAnimationState = 'stand';
@ -50,15 +50,15 @@ Chuck.Player.prototype.move = function(direction) {
switch(true) { switch(true) {
case direction == this._lookDirection && this.isStanding(): case direction == this._lookDirection && this.isStanding():
this._doll.move(direction, Settings.RUN_SPEED); this._doll.move(direction, Chuck.Settings.RUN_SPEED);
break; break;
case !this.isStanding(): case !this.isStanding():
this._doll.move(direction, Settings.FLY_SPEED); this._doll.move(direction, Chuck.Settings.FLY_SPEED);
break; break;
default: default:
this._doll.move(direction, Settings.WALK_SPEED); this._doll.move(direction, Chuck.Settings.WALK_SPEED);
break; break;
} }
@ -107,7 +107,7 @@ Chuck.Player.prototype._animate = function(type) {
return; return;
} }
this._mc.gotoAndPlay(type); //this._mc.gotoAndPlay(type);
this._currentAnimationState = type; this._currentAnimationState = type;
} }
@ -120,15 +120,15 @@ Chuck.Player.prototype._calculateWalkAnimation = function() {
} }
Chuck.Player.prototype.look = function(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 degree = Math.atan2(Chuck.Settings.STAGE_WIDTH / 2 - x, Chuck.Settings.STAGE_HEIGHT / 2 - 25 - y) / (Math.PI / 180);
var lastLookDirection = this._lookDirection; var lastLookDirection = this._lookDirection;
if (x < Settings.STAGE_WIDTH / 2) { if (x < Chuck.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 >= Settings.STAGE_WIDTH / 2) { } else if (x >= Chuck.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;
@ -152,7 +152,7 @@ Chuck.Player.prototype._isWalking = function() {
// called by CollisionDetection // called by CollisionDetection
Chuck.Player.prototype.onFootSensorDetection = function(isColliding) { Chuck.Player.prototype.onFootSensorDetection = function(isColliding) {
if(isColliding) { if(isColliding) {
if(this._doll.getBody().GetLinearVelocity().y < -Settings.JUMP_SPEED && !this.isStanding()) { if(this._doll.getBody().GetLinearVelocity().y < -Chuck.Settings.JUMP_SPEED && !this.isStanding()) {
return; return;
} }
this.setStanding(true); this.setStanding(true);
@ -167,7 +167,7 @@ Chuck.Player.prototype.onFootSensorDetection = function(isColliding) {
} }
Chuck.Player.prototype.update = function() { Chuck.Player.prototype.update = function() {
this._mc.head.y = this._mc.head_posmask.y; //this._mc.head.y = this._mc.head_posmask.y;
if (this._doll.getBody().GetLinearVelocity().x == 0 && this._isWalking()) { if (this._doll.getBody().GetLinearVelocity().x == 0 && this._isWalking()) {
this.stop(); this.stop();

View file

@ -11,18 +11,14 @@ Chuck.Processor = function() {
} }
Chuck.Processor.prototype.init = function() { Chuck.Processor.prototype.init = function() {
this._engine = new Chuck.Physics.Engine(); this._engine = new Chuck.Physics.Engine();
this._me = new Chuck.Player(this._engine, this._repository);
this._me = new Chuck.Player(this._engine, this._repository); //this._camera = Camera.getInstance()
//this._camera = Camera.getInstance()
//this._repository = Repository.getInstance(); //this._repository = Repository.getInstance();
this._engine.setCollisionDetector(this._me);
//this._engine.setCollisionDetector(); this._keyboardInput = new Chuck.Control.KeyboardInput();
this._inputControlUnit = new Chuck.Control.InputControlUnit(this._keyboardInput, this._me);
//this._inputControlUnit = InputControlUnit.getInstance();
//this._keyboardInput = KeyboardInput.getInstance();
new Chuck.Loader.Level(this._engine); new Chuck.Loader.Level(this._engine);
//new Items(); //new Items();
@ -50,10 +46,9 @@ Chuck.Processor.prototype._update = function(self) {
self._engine.update(); self._engine.update();
// Order is important // Order is important
/*
self._repository.update(); //self._repository.update();
self._keyboardInput.update(); self._keyboardInput.update();
self._me.update(); self._me.update();
self._camera.update(); //self._camera.update();
*/
} }