mirror of
https://github.com/logsol/chuck.js.git
synced 2026-05-11 10:37:34 +00:00
Throwing with Swiper, working but needs to be refined. No angulat verlocity yet.
This commit is contained in:
parent
6d9d02615a
commit
cf2182676b
5 changed files with 198 additions and 85 deletions
|
|
@ -3,15 +3,18 @@ define([
|
||||||
"Game/Client/Control/KeyboardInput",
|
"Game/Client/Control/KeyboardInput",
|
||||||
"Game/Client/View/DomController",
|
"Game/Client/View/DomController",
|
||||||
"Game/Config/Settings",
|
"Game/Config/Settings",
|
||||||
|
"Game/Client/Control/Swiper"
|
||||||
],
|
],
|
||||||
|
|
||||||
function (Parent, KeyboardInput, DomController, Settings) {
|
function (Parent, KeyboardInput, DomController, Settings, Swiper) {
|
||||||
|
|
||||||
function KeyboardAndMouse(playerController) {
|
function KeyboardAndMouse(playerController) {
|
||||||
Parent.call(this);
|
Parent.call(this);
|
||||||
|
|
||||||
this.x = 0;
|
this.x = 0;
|
||||||
this.y = 0;
|
this.y = 0;
|
||||||
|
this.modifier = false;
|
||||||
|
this.swiper = null;
|
||||||
|
|
||||||
this.playerController = playerController;
|
this.playerController = playerController;
|
||||||
this.keyboardInit();
|
this.keyboardInit();
|
||||||
|
|
@ -22,7 +25,8 @@ function (Parent, KeyboardInput, DomController, Settings) {
|
||||||
|
|
||||||
KeyboardAndMouse.prototype.keyboardInit = function() {
|
KeyboardAndMouse.prototype.keyboardInit = function() {
|
||||||
|
|
||||||
this.keyboardInput = new KeyboardInput(this.playerController);
|
this.keyboardInput = new KeyboardInput();
|
||||||
|
var self = this;
|
||||||
|
|
||||||
var keys = {
|
var keys = {
|
||||||
w:87,
|
w:87,
|
||||||
|
|
@ -40,8 +44,8 @@ function (Parent, KeyboardInput, DomController, Settings) {
|
||||||
right: 39,
|
right: 39,
|
||||||
|
|
||||||
space: 32,
|
space: 32,
|
||||||
|
|
||||||
tab: 9,
|
tab: 9,
|
||||||
|
shift: 16,
|
||||||
|
|
||||||
plus: 187,
|
plus: 187,
|
||||||
plusfx: 171,
|
plusfx: 171,
|
||||||
|
|
@ -50,37 +54,65 @@ function (Parent, KeyboardInput, DomController, Settings) {
|
||||||
zero: 48
|
zero: 48
|
||||||
}
|
}
|
||||||
|
|
||||||
this.keyboardInput.registerKey(keys.a, 'moveLeft', 'stop');
|
function bind2Pc(methodName) {
|
||||||
this.keyboardInput.registerKey(keys.left, 'moveLeft', 'stop');
|
return self.playerController[methodName].bind(self.playerController);
|
||||||
|
}
|
||||||
|
|
||||||
this.keyboardInput.registerKey(keys.d, 'moveRight', 'stop');
|
this.keyboardInput.registerKey(keys.a, bind2Pc('moveLeft'), bind2Pc('stop'));
|
||||||
this.keyboardInput.registerKey(keys.right, 'moveRight', 'stop');
|
this.keyboardInput.registerKey(keys.left, bind2Pc('moveLeft'), bind2Pc('stop'));
|
||||||
|
this.keyboardInput.registerKey(keys.d, bind2Pc('moveRight'), bind2Pc('stop'));
|
||||||
|
this.keyboardInput.registerKey(keys.right, bind2Pc('moveRight'), bind2Pc('stop'));
|
||||||
|
this.keyboardInput.registerKey(keys.w, bind2Pc('jump'), bind2Pc('jumpStop'));
|
||||||
|
this.keyboardInput.registerKey(keys.up, bind2Pc('jump'), bind2Pc('jumpStop'));
|
||||||
|
this.keyboardInput.registerKey(keys.space, bind2Pc('jump'), bind2Pc('jumpStop'));
|
||||||
|
this.keyboardInput.registerKey(keys.plus, bind2Pc('zoomIn'));
|
||||||
|
this.keyboardInput.registerKey(keys.plusfx, bind2Pc('zoomIn'));
|
||||||
|
this.keyboardInput.registerKey(keys.minus, bind2Pc('zoomOut'));
|
||||||
|
this.keyboardInput.registerKey(keys.minusfx, bind2Pc('zoomOut'));
|
||||||
|
this.keyboardInput.registerKey(keys.zero, bind2Pc('zoomReset'));
|
||||||
|
this.keyboardInput.registerKey(keys.tab, bind2Pc('showInfo'), bind2Pc('hideInfo'));
|
||||||
|
this.keyboardInput.registerKey(keys.f, bind2Pc('handActionLeft'));
|
||||||
|
this.keyboardInput.registerKey(keys.g, bind2Pc('handActionRight'));
|
||||||
|
this.keyboardInput.registerKey(keys.k, bind2Pc('suicide'));
|
||||||
|
|
||||||
this.keyboardInput.registerKey(keys.w, 'jump', 'jumpStop');
|
this.keyboardInput.registerKey(
|
||||||
this.keyboardInput.registerKey(keys.up, 'jump', 'jumpStop');
|
keys.shift,
|
||||||
this.keyboardInput.registerKey(keys.space, 'jump', 'jumpStop');
|
this.activateModifier.bind(this),
|
||||||
|
this.deactivateModifier.bind(this)
|
||||||
this.keyboardInput.registerKey(keys.plus, 'zoomIn');
|
);
|
||||||
this.keyboardInput.registerKey(keys.plusfx, 'zoomIn');
|
|
||||||
this.keyboardInput.registerKey(keys.minus, 'zoomOut');
|
|
||||||
this.keyboardInput.registerKey(keys.minusfx, 'zoomOut');
|
|
||||||
this.keyboardInput.registerKey(keys.zero, 'zoomReset');
|
|
||||||
|
|
||||||
this.keyboardInput.registerKey(keys.tab, 'showInfo', 'hideInfo');
|
|
||||||
|
|
||||||
this.keyboardInput.registerKey(keys.f, 'handActionLeft');
|
|
||||||
this.keyboardInput.registerKey(keys.g, 'handActionRight');
|
|
||||||
|
|
||||||
this.keyboardInput.registerKey(keys.k, 'suicide');
|
|
||||||
};
|
};
|
||||||
|
|
||||||
KeyboardAndMouse.prototype.mouseInit = function() {
|
KeyboardAndMouse.prototype.mouseInit = function() {
|
||||||
|
|
||||||
var canvas = DomController.getCanvas();
|
var canvas = DomController.getCanvas();
|
||||||
var self = this;
|
var self = this;
|
||||||
|
|
||||||
canvas.onmousemove = function(e){
|
canvas.onmousedown = function(e) {
|
||||||
|
if(!self.playerController.player.isHoldingSomething()) {
|
||||||
|
self.playerController.handActionRequest(self.x, self.y);
|
||||||
|
} else {
|
||||||
|
self.swiper = new Swiper();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
canvas.onmousemove = function(e) {
|
||||||
|
if (self.swiper) {
|
||||||
|
self.draw(e);
|
||||||
|
} else if(self.modifier) {
|
||||||
|
self.updateViewport(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
canvas.onmouseup = function(e) {
|
||||||
|
if(self.swiper) {
|
||||||
|
console.log(e)
|
||||||
|
var xya = self.swiper.swipeEnd(e.x, e.y);
|
||||||
|
self.playerController.handActionRequest(xya.x, xya.y);
|
||||||
|
self.swiper = null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
KeyboardAndMouse.prototype.updateViewport = function(e) {
|
||||||
// -1 +1 +1 +1 xy scaling should
|
// -1 +1 +1 +1 xy scaling should
|
||||||
// -1 -1 +1 -1 be like this
|
// -1 -1 +1 -1 be like this
|
||||||
|
|
||||||
|
|
@ -94,36 +126,26 @@ function (Parent, KeyboardInput, DomController, Settings) {
|
||||||
e.webkitMovementY ||
|
e.webkitMovementY ||
|
||||||
0;
|
0;
|
||||||
|
|
||||||
self.x += movementX / Settings.VIEWPORT_SPEED_FACTOR;
|
this.x += movementX / Settings.VIEWPORT_SPEED_FACTOR;
|
||||||
if(self.x > 1) {
|
if(this.x > 1) {
|
||||||
self.x = 1;
|
this.x = 1;
|
||||||
}
|
}
|
||||||
if(self.x < -1) {
|
if(this.x < -1) {
|
||||||
self.x = -1;
|
this.x = -1;
|
||||||
};
|
};
|
||||||
|
|
||||||
self.y -= movementY / Settings.VIEWPORT_SPEED_FACTOR;
|
this.y -= movementY / Settings.VIEWPORT_SPEED_FACTOR;
|
||||||
if(self.y > 1) {
|
if(this.y > 1) {
|
||||||
self.y = 1;
|
this.y = 1;
|
||||||
}
|
}
|
||||||
if(self.y < -1) {
|
if(this.y < -1) {
|
||||||
self.y = -1;
|
this.y = -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
self.onXyChange(self.x, self.y);
|
this.onXyChange(this.x, this.y);
|
||||||
}
|
|
||||||
|
|
||||||
canvas.onmousedown = function(e) {
|
|
||||||
|
|
||||||
var x = (((e.clientX - this.offsetLeft) / Settings.STAGE_WIDTH) * 2) - 1;
|
|
||||||
var y = (((Settings.STAGE_HEIGHT - (e.clientY - this.offsetTop)) / Settings.STAGE_HEIGHT) * 2) -1;
|
|
||||||
|
|
||||||
self.playerController.handActionRequest(x, y);
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
KeyboardAndMouse.prototype.update = function(e) {
|
KeyboardAndMouse.prototype.draw = function(e) {
|
||||||
/*
|
|
||||||
var movementX = e.movementX ||
|
var movementX = e.movementX ||
|
||||||
e.mozMovementX ||
|
e.mozMovementX ||
|
||||||
e.webkitMovementX ||
|
e.webkitMovementX ||
|
||||||
|
|
@ -134,29 +156,19 @@ function (Parent, KeyboardInput, DomController, Settings) {
|
||||||
e.webkitMovementY ||
|
e.webkitMovementY ||
|
||||||
0;
|
0;
|
||||||
|
|
||||||
//console.log(movementX, movementY);
|
this.swiper.swipe(movementX, -movementY);
|
||||||
|
|
||||||
|
|
||||||
this.x += movementX;
|
|
||||||
if(this.x > 1) {
|
|
||||||
this.x = 1;
|
|
||||||
}
|
|
||||||
if(this.x < -1) {
|
|
||||||
this.x = -1;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
this.y += movementY;
|
KeyboardAndMouse.prototype.activateModifier = function() {
|
||||||
if(this.y > 1) {
|
this.modifier = true;
|
||||||
this.y = 1;
|
};
|
||||||
}
|
|
||||||
if(this.y < -1) {
|
|
||||||
this.y = -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
KeyboardAndMouse.prototype.deactivateModifier = function() {
|
||||||
|
this.modifier = false;
|
||||||
|
this.x = 0.3;
|
||||||
|
this.y = 0;
|
||||||
this.onXyChange(this.x, this.y);
|
this.onXyChange(this.x, this.y);
|
||||||
*/
|
|
||||||
};
|
};
|
||||||
|
|
||||||
return KeyboardAndMouse;
|
return KeyboardAndMouse;
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
@ -4,11 +4,8 @@ define([
|
||||||
|
|
||||||
function (Key) {
|
function (Key) {
|
||||||
|
|
||||||
function KeyboardInput (playerController) {
|
function KeyboardInput () {
|
||||||
|
|
||||||
this._registry = {};
|
this._registry = {};
|
||||||
this._playerController = playerController;
|
|
||||||
|
|
||||||
this.init();
|
this.init();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -34,7 +31,7 @@ function (Key) {
|
||||||
|
|
||||||
if (key && !key.getActive()) {
|
if (key && !key.getActive()) {
|
||||||
var callback = key.getKeyDownFunction();
|
var callback = key.getKeyDownFunction();
|
||||||
if(callback) this._playerController[callback]();
|
if(callback) callback();
|
||||||
key.setActive(true);
|
key.setActive(true);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -46,7 +43,7 @@ function (Key) {
|
||||||
var key = this._getKeyByKeyCode(e.keyCode);
|
var key = this._getKeyByKeyCode(e.keyCode);
|
||||||
if (key && key.getActive()) {
|
if (key && key.getActive()) {
|
||||||
var callback = key.getKeyUpFunction();
|
var callback = key.getKeyUpFunction();
|
||||||
if(callback) this._playerController[callback]();
|
if(callback) callback();
|
||||||
key.setActive(false);
|
key.setActive(false);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -13,9 +13,6 @@ function (Parent, Nc, KeyboardAndMouse, Gamepad) {
|
||||||
|
|
||||||
this.keyboardAndMouse = new KeyboardAndMouse(this);
|
this.keyboardAndMouse = new KeyboardAndMouse(this);
|
||||||
this.gamepad = new Gamepad(this);
|
this.gamepad = new Gamepad(this);
|
||||||
|
|
||||||
this.ncTokens = [
|
|
||||||
];
|
|
||||||
}
|
}
|
||||||
|
|
||||||
PlayerController.prototype = Object.create(Parent.prototype);
|
PlayerController.prototype = Object.create(Parent.prototype);
|
||||||
|
|
@ -23,7 +20,6 @@ function (Parent, Nc, KeyboardAndMouse, Gamepad) {
|
||||||
PlayerController.prototype.update = function() {
|
PlayerController.prototype.update = function() {
|
||||||
Parent.prototype.update.call(this);
|
Parent.prototype.update.call(this);
|
||||||
this.gamepad.update();
|
this.gamepad.update();
|
||||||
this.keyboardAndMouse.update();
|
|
||||||
};
|
};
|
||||||
|
|
||||||
PlayerController.prototype.moveLeft = function () {
|
PlayerController.prototype.moveLeft = function () {
|
||||||
|
|
@ -94,11 +90,6 @@ function (Parent, Nc, KeyboardAndMouse, Gamepad) {
|
||||||
Nc.trigger(Nc.ns.client.game.zoomReset, false);
|
Nc.trigger(Nc.ns.client.game.zoomReset, false);
|
||||||
};
|
};
|
||||||
|
|
||||||
PlayerController.prototype.destroy = function() {
|
|
||||||
Nc.offAll(this.ncTokens);
|
|
||||||
Parent.prototype.destroy.call(this);
|
|
||||||
};
|
|
||||||
|
|
||||||
|
|
||||||
return PlayerController;
|
return PlayerController;
|
||||||
});
|
});
|
||||||
109
app/Game/Client/Control/Swiper.js
Normal file
109
app/Game/Client/Control/Swiper.js
Normal file
|
|
@ -0,0 +1,109 @@
|
||||||
|
define([
|
||||||
|
],
|
||||||
|
|
||||||
|
function () {
|
||||||
|
|
||||||
|
var MAX_LENGTH = 300;
|
||||||
|
|
||||||
|
function Swiper() {
|
||||||
|
this.points = [];
|
||||||
|
this.angleSum = 0;
|
||||||
|
this.lengthSum = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
Swiper.prototype.swipe = function(x, y) {
|
||||||
|
console.log("swipe")
|
||||||
|
|
||||||
|
if(this.lengthSum > MAX_LENGTH) return;
|
||||||
|
|
||||||
|
this.points.push({x:x, y:y});
|
||||||
|
var points = this.points;
|
||||||
|
|
||||||
|
if(points.length >= 3) {
|
||||||
|
|
||||||
|
var i = points.length - 1;
|
||||||
|
|
||||||
|
var vectors = [
|
||||||
|
{
|
||||||
|
x: points[i].x - points[i-1].x,
|
||||||
|
y: points[i].y - points[i-1].y
|
||||||
|
},
|
||||||
|
{
|
||||||
|
x: points[i-2].x - points[i-1].x,
|
||||||
|
y: points[i-2].y - points[i-1].y
|
||||||
|
}
|
||||||
|
];
|
||||||
|
|
||||||
|
var yx = vectors[0].y * vectors[1].x;
|
||||||
|
var xy = vectors[0].x * vectors[1].y;
|
||||||
|
var direction = 0;
|
||||||
|
if(yx > xy) {
|
||||||
|
direction = -1;
|
||||||
|
} else if (yx < xy) {
|
||||||
|
direction = 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
var dotProduct = vectors[0].x
|
||||||
|
* vectors[1].x
|
||||||
|
+ vectors[0].y
|
||||||
|
* vectors[1].y;
|
||||||
|
|
||||||
|
var currentLength = Math.sqrt(
|
||||||
|
Math.pow(vectors[0].x, 2)
|
||||||
|
+ Math.pow(vectors[0].y, 2)
|
||||||
|
);
|
||||||
|
var lastLength = Math.sqrt(
|
||||||
|
Math.pow(vectors[1].x, 2)
|
||||||
|
+ Math.pow(vectors[1].y, 2)
|
||||||
|
);
|
||||||
|
|
||||||
|
var angle = 180 - (Math.acos((dotProduct / (currentLength * lastLength)) % 1) * 180 / Math.PI);
|
||||||
|
angle *= direction;
|
||||||
|
|
||||||
|
if(!isNaN(parseFloat(angle)) && direction != 0) {
|
||||||
|
this.angleSum += angle;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(!isNaN(parseFloat(currentLength))) {
|
||||||
|
this.lengthSum += Math.abs(currentLength);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Swiper.prototype.swipeEnd = function(x, y) {
|
||||||
|
var spin = this.angleSum;
|
||||||
|
var length = this.lengthSum;
|
||||||
|
var p0x = this.points[0].x;
|
||||||
|
var p0y = this.points[0].y;
|
||||||
|
var sumx = 0;
|
||||||
|
var sumy = 0;
|
||||||
|
|
||||||
|
for(var i=0, count = this.points.length; i < count; i++) {
|
||||||
|
var p = this.points[i];
|
||||||
|
sumx += p.x - p0x;
|
||||||
|
sumy += p.y - p0y;
|
||||||
|
}
|
||||||
|
|
||||||
|
var direction = {
|
||||||
|
x: sumx / count,
|
||||||
|
y: sumy / count
|
||||||
|
};
|
||||||
|
|
||||||
|
var larger = Math.abs(direction.x) > Math.abs(direction.y) ? direction.x : direction.y;
|
||||||
|
direction.x /= Math.abs(larger);
|
||||||
|
direction.y /= Math.abs(larger);
|
||||||
|
|
||||||
|
this.angleSum = 0;
|
||||||
|
this.lengthSum = 0;
|
||||||
|
this.points = [];
|
||||||
|
|
||||||
|
return {
|
||||||
|
x: direction.x * length / 100,
|
||||||
|
y: direction.y * length / 100,
|
||||||
|
spin: spin
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return Swiper;
|
||||||
|
|
||||||
|
});
|
||||||
|
|
@ -102,6 +102,10 @@ function (Parent, Nc, Settings) {
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
Player.prototype.isHoldingSomething = function() {
|
||||||
|
return !!this.holdingItem;
|
||||||
|
};
|
||||||
|
|
||||||
Player.prototype.destroy = function() {
|
Player.prototype.destroy = function() {
|
||||||
clearTimeout(this.healthBarViewVisibleTimeout);
|
clearTimeout(this.healthBarViewVisibleTimeout);
|
||||||
Nc.trigger(Nc.ns.client.view.healthBar.remove, this.healthBarView);
|
Nc.trigger(Nc.ns.client.view.healthBar.remove, this.healthBarView);
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue