mirror of
https://github.com/logsol/chuck.js.git
synced 2026-05-11 18:47:35 +00:00
added player movement on user input
This commit is contained in:
parent
56caf8b1b3
commit
5a7234ade4
5 changed files with 281 additions and 67 deletions
|
|
@ -2,6 +2,7 @@
|
|||
<html>
|
||||
<head>
|
||||
<title>Chuck</title>
|
||||
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js" type="text/javascript"></script>
|
||||
<script data-main="client" src="require.js"></script>
|
||||
<style>
|
||||
html, body {
|
||||
|
|
|
|||
75
lib/Chuck/Control/InputControlUnit.js
Executable file
75
lib/Chuck/Control/InputControlUnit.js
Executable file
|
|
@ -0,0 +1,75 @@
|
|||
define(["Chuck/Control/KeyboardInput"], function(KeyboardInput){
|
||||
|
||||
function InputControlUnit(me) {
|
||||
|
||||
this._keyboardInput = new KeyboardInput(this);
|
||||
this._me = me;
|
||||
|
||||
this._shift;
|
||||
this._isJumping;
|
||||
|
||||
this.KEY_LEFT = 65;
|
||||
this.KEY_RIGHT = 68;
|
||||
this.KEY_UP = 87;
|
||||
this.KEY_DOWN = 83;
|
||||
|
||||
this.init();
|
||||
}
|
||||
|
||||
InputControlUnit.prototype.init = function() {
|
||||
this._keyboardInput.registerKey(this.KEY_LEFT, 'moveLeft', 'stop', 'moveLeft');
|
||||
this._keyboardInput.registerKey(this.KEY_RIGHT, 'moveRight', 'stop', 'moveRight');
|
||||
this._keyboardInput.registerKey(this.KEY_UP, 'jump', 'jumped', 'jumping');
|
||||
this._keyboardInput.registerKey(this.KEY_DOWN, 'duck', 'standUp', 'duck');
|
||||
this._keyboardInput.registerKey(this.KEY_DOWN, 'activateShift', 'activateShift', 'deactivateShift');
|
||||
}
|
||||
|
||||
InputControlUnit.prototype.moveLeft = function() {
|
||||
this._me.move(-1);
|
||||
}
|
||||
|
||||
InputControlUnit.prototype.moveRight = function() {
|
||||
this._me.move(1);
|
||||
}
|
||||
|
||||
InputControlUnit.prototype.stop = function() {
|
||||
this._me.stop();
|
||||
}
|
||||
|
||||
InputControlUnit.prototype.jump = function() {
|
||||
this._isJumping = true;
|
||||
this._me.jump();
|
||||
}
|
||||
|
||||
InputControlUnit.prototype.jumped = function() {
|
||||
this._isJumping = false;
|
||||
}
|
||||
|
||||
InputControlUnit.prototype.jumping = function() {
|
||||
if (this._isJumping) {
|
||||
this._me.jumping();
|
||||
}
|
||||
}
|
||||
|
||||
InputControlUnit.prototype.duck = function() {
|
||||
this._me.duck();
|
||||
}
|
||||
|
||||
InputControlUnit.prototype.standUp = function() {
|
||||
this._me.standUp();
|
||||
}
|
||||
|
||||
InputControlUnit.prototype.activateShift = function() {
|
||||
this._shift = true;
|
||||
}
|
||||
|
||||
InputControlUnit.prototype.deactivateShift = function() {
|
||||
this._shift = false;
|
||||
}
|
||||
|
||||
InputControlUnit.prototype.update = function() {
|
||||
this._keyboardInput.update();
|
||||
}
|
||||
|
||||
return InputControlUnit;
|
||||
});
|
||||
61
lib/Chuck/Control/Key.js
Executable file
61
lib/Chuck/Control/Key.js
Executable file
|
|
@ -0,0 +1,61 @@
|
|||
define(function(){
|
||||
|
||||
function Key () {
|
||||
this._active = false;
|
||||
this._activityUpdateStatus = false;
|
||||
this._activityUpdateNeeded = false;
|
||||
this._keyDownFunction = null;
|
||||
this._keyUpFunction = null;
|
||||
this._keyFrameFunction = null;
|
||||
}
|
||||
|
||||
Key.prototype.setActivityUpdateStatus = function(active) {
|
||||
this._activityUpdateStatus = active;
|
||||
}
|
||||
|
||||
Key.prototype.getActivityUpdateStatus = function() {
|
||||
return this._activityUpdateStatus;
|
||||
}
|
||||
|
||||
Key.prototype.setActivityUpdateNeeded = function(need) {
|
||||
this._activityUpdateNeeded = need;
|
||||
}
|
||||
|
||||
Key.prototype.getActivityUpdateNeeded = function() {
|
||||
return this._activityUpdateNeeded;
|
||||
}
|
||||
|
||||
Key.prototype.setActive = function(active) {
|
||||
this._active = active;
|
||||
}
|
||||
|
||||
Key.prototype.getActive = function() {
|
||||
return this._active;
|
||||
}
|
||||
|
||||
Key.prototype.setKeyDownFunction = function(f) {
|
||||
this._keyDownFunction = f;
|
||||
}
|
||||
|
||||
Key.prototype.getKeyDownFunction = function() {
|
||||
return this._keyDownFunction;
|
||||
}
|
||||
|
||||
Key.prototype.setKeyUpFunction = function(f) {
|
||||
this._keyUpFunction = f;
|
||||
}
|
||||
|
||||
Key.prototype.getKeyUpFunction = function() {
|
||||
return this._keyUpFunction;
|
||||
}
|
||||
|
||||
Key.prototype.setKeyFrameFunction = function(f) {
|
||||
this._keyFrameFunction = f;
|
||||
}
|
||||
|
||||
Key.prototype.getKeyFrameFunction = function() {
|
||||
return this._keyFrameFunction;
|
||||
}
|
||||
|
||||
return Key;
|
||||
});
|
||||
74
lib/Chuck/Control/KeyboardInput.js
Executable file
74
lib/Chuck/Control/KeyboardInput.js
Executable file
|
|
@ -0,0 +1,74 @@
|
|||
define(["Chuck/Control/Key"], function(Key){
|
||||
|
||||
function KeyboardInput (inputControlUnit) {
|
||||
|
||||
this._registry = {};
|
||||
this._inputControlUnit = inputControlUnit;
|
||||
|
||||
this.init();
|
||||
}
|
||||
|
||||
KeyboardInput.prototype.init = function() {
|
||||
$(window).keydown($.proxy(this._onKeyDown, this));
|
||||
$(window).keyup($.proxy(this._onKeyUp, this));
|
||||
}
|
||||
|
||||
KeyboardInput.prototype.registerKey = function(keyCode, onKeyDown, onKeyUp, onKeyFrame) {
|
||||
var key = new Key();
|
||||
key.setKeyDownFunction(onKeyDown);
|
||||
key.setKeyUpFunction(onKeyUp);
|
||||
key.setKeyFrameFunction(onKeyFrame);
|
||||
this._registry[keyCode] = key;
|
||||
}
|
||||
|
||||
KeyboardInput.prototype._getKeyByKeyCode = function(keyCode) {
|
||||
return this._registry[keyCode];
|
||||
}
|
||||
|
||||
KeyboardInput.prototype._onKeyDown = function(e) {
|
||||
var key = this._getKeyByKeyCode(e.keyCode);
|
||||
if (key && key.getActive() == false) {
|
||||
key.setActivityUpdateStatus(true);
|
||||
key.setActivityUpdateNeeded(true);
|
||||
}
|
||||
}
|
||||
|
||||
KeyboardInput.prototype._onKeyUp = function(e) {
|
||||
var key = this._getKeyByKeyCode(e.keyCode);
|
||||
if (key != null) {
|
||||
key.setActivityUpdateStatus(false);
|
||||
key.setActivityUpdateNeeded(true);
|
||||
}
|
||||
}
|
||||
|
||||
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;
|
||||
});
|
||||
}
|
||||
|
||||
return KeyboardInput;
|
||||
});
|
||||
|
|
@ -1,4 +1,12 @@
|
|||
define(["Chuck/Physics/Engine", "Chuck/Player", "Box2D/Box2D"], function(PhysicsEngine, Player, Box2D){
|
||||
var requires = [
|
||||
"Chuck/Physics/Engine",
|
||||
"Chuck/Player",
|
||||
"Chuck/Control/InputControlUnit",
|
||||
"Chuck/Settings",
|
||||
"Box2D/Box2D"
|
||||
];
|
||||
|
||||
define(requires, function(PhysicsEngine, Player, InputControlUnit, Settings, Box2D){
|
||||
|
||||
function Processor () {
|
||||
this._me;
|
||||
|
|
@ -16,67 +24,62 @@ define(["Chuck/Physics/Engine", "Chuck/Player", "Box2D/Box2D"], function(Physics
|
|||
Processor.prototype.init = function() {
|
||||
|
||||
this._physicsEngine = new PhysicsEngine();
|
||||
|
||||
|
||||
|
||||
|
||||
{
|
||||
var world = this._physicsEngine.getWorld();
|
||||
|
||||
var fixDef = new Box2D.Dynamics.b2FixtureDef;
|
||||
fixDef.density = 1.0;
|
||||
fixDef.friction = 0.99;
|
||||
fixDef.restitution = .51;
|
||||
var bodyDef = new Box2D.Dynamics.b2BodyDef;
|
||||
|
||||
// create ground
|
||||
bodyDef.type = Box2D.Dynamics.b2Body.b2_staticBody;
|
||||
fixDef.shape = new Box2D.Collision.Shapes.b2PolygonShape;
|
||||
fixDef.shape.SetAsBox(20, 2);
|
||||
bodyDef.position.Set(10, 400 / 30 + 1.8);
|
||||
world.CreateBody(bodyDef).CreateFixture(fixDef);
|
||||
bodyDef.position.Set(10, -1.8);
|
||||
world.CreateBody(bodyDef).CreateFixture(fixDef);
|
||||
fixDef.shape.SetAsBox(2, 14);
|
||||
bodyDef.position.Set(-1.8, 13);
|
||||
world.CreateBody(bodyDef).CreateFixture(fixDef);
|
||||
bodyDef.position.Set(21.8, 13);
|
||||
world.CreateBody(bodyDef).CreateFixture(fixDef);
|
||||
|
||||
// create object
|
||||
bodyDef.type = Box2D.Dynamics.b2Body.b2_dynamicBody;
|
||||
fixDef.shape = new Box2D.Collision.Shapes.b2PolygonShape;
|
||||
fixDef.shape.SetAsBox(0.4, 0.4);
|
||||
bodyDef.position.x = 10;
|
||||
bodyDef.position.y = 2;
|
||||
bodyDef.userData = {
|
||||
'bodyId': 1 + ''
|
||||
};
|
||||
|
||||
world.CreateBody(bodyDef).CreateFixture(fixDef);
|
||||
this._bodyDef = bodyDef;
|
||||
}
|
||||
|
||||
|
||||
this._makeBox();
|
||||
|
||||
this._me = new Player(this._physicsEngine, null);
|
||||
this._me.spawn(100, 0);
|
||||
|
||||
/*
|
||||
//this._camera = Camera.getInstance()
|
||||
//this._repository = Repository.getInstance();
|
||||
this._physicsEngine.setCollisionDetector(this._me);
|
||||
this._keyboardInput = new Chuck.Control.KeyboardInput();
|
||||
this._inputControlUnit = new Chuck.Control.InputControlUnit(this._keyboardInput, this._me);
|
||||
if(Settings.IS_BROWSER_ENVIRONMENT) {
|
||||
|
||||
new Chuck.Loader.Level(this._physicsEngine);
|
||||
this._me = new Player(this._physicsEngine, null);
|
||||
this._me.spawn(100, 0);
|
||||
|
||||
this._inputControlUnit = new InputControlUnit(this._me);
|
||||
//this._camera = Camera.getInstance()
|
||||
//this._camera.follow(this._me);
|
||||
//this._repository = Repository.getInstance();
|
||||
//this._physicsEngine.setCollisionDetector(this._me);
|
||||
}
|
||||
|
||||
//new Chuck.Loader.Level(this._physicsEngine);
|
||||
//new Items();
|
||||
|
||||
|
||||
//this._camera.follow(this._me);
|
||||
*/
|
||||
setInterval(this._update, 1000/60, this);
|
||||
//View.getInstance().getSprite().addEventListener(Event.ENTER_FRAME, this._update)
|
||||
}
|
||||
|
||||
|
||||
Processor.prototype._makeBox = function() {
|
||||
var world = this._physicsEngine.getWorld();
|
||||
|
||||
var fixDef = new Box2D.Dynamics.b2FixtureDef;
|
||||
fixDef.density = 1.0;
|
||||
fixDef.friction = 0.99;
|
||||
fixDef.restitution = .51;
|
||||
var bodyDef = new Box2D.Dynamics.b2BodyDef;
|
||||
|
||||
// create ground
|
||||
bodyDef.type = Box2D.Dynamics.b2Body.b2_staticBody;
|
||||
fixDef.shape = new Box2D.Collision.Shapes.b2PolygonShape;
|
||||
fixDef.shape.SetAsBox(20, 2);
|
||||
bodyDef.position.Set(10, 400 / 30 + 1.8);
|
||||
world.CreateBody(bodyDef).CreateFixture(fixDef);
|
||||
bodyDef.position.Set(10, -1.8);
|
||||
world.CreateBody(bodyDef).CreateFixture(fixDef);
|
||||
fixDef.shape.SetAsBox(2, 14);
|
||||
bodyDef.position.Set(-1.8, 13);
|
||||
world.CreateBody(bodyDef).CreateFixture(fixDef);
|
||||
bodyDef.position.Set(21.8, 13);
|
||||
world.CreateBody(bodyDef).CreateFixture(fixDef);
|
||||
|
||||
// create object
|
||||
bodyDef.type = Box2D.Dynamics.b2Body.b2_dynamicBody;
|
||||
fixDef.shape = new Box2D.Collision.Shapes.b2PolygonShape;
|
||||
fixDef.shape.SetAsBox(0.4, 0.4);
|
||||
bodyDef.position.x = 10;
|
||||
bodyDef.position.y = 2;
|
||||
bodyDef.userData = {
|
||||
'bodyId': 1 + ''
|
||||
};
|
||||
|
||||
world.CreateBody(bodyDef).CreateFixture(fixDef);
|
||||
this._bodyDef = bodyDef;
|
||||
}
|
||||
|
||||
Processor.prototype.getPhysicsEngine = function() {
|
||||
|
|
@ -88,18 +91,18 @@ define(["Chuck/Physics/Engine", "Chuck/Player", "Box2D/Box2D"], function(Physics
|
|||
}
|
||||
|
||||
Processor.prototype._update = function(self) {
|
||||
|
||||
//console.log(self._physicsEngine.getWorld().GetBodyList().GetPosition());
|
||||
|
||||
self._physicsEngine.update();
|
||||
|
||||
//console.log(self._physicsEngine.getWorld().GetBodyList().GetPosition());
|
||||
|
||||
//self._repository.update();
|
||||
//self._keyboardInput.update();
|
||||
//self._me.update();
|
||||
//self._camera.update();
|
||||
|
||||
if(Settings.IS_BROWSER_ENVIRONMENT) {
|
||||
self._inputControlUnit.update();
|
||||
self._me.update();
|
||||
//self._camera.update();
|
||||
//self._repository.update();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
return Processor;
|
||||
});
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue