mirror of
https://github.com/logsol/chuck.js.git
synced 2026-05-11 10:37:34 +00:00
fixes #98 revisit throwing
This commit is contained in:
parent
21f948e2b4
commit
b6d67a0581
3 changed files with 166 additions and 116 deletions
|
|
@ -13,6 +13,7 @@ function (Parent, KeyboardInput, DomController, Settings, Swiper) {
|
||||||
|
|
||||||
this.x = 0;
|
this.x = 0;
|
||||||
this.y = 0;
|
this.y = 0;
|
||||||
|
this.mousePosition = {x:0,y:0};
|
||||||
this.modifier = false;
|
this.modifier = false;
|
||||||
this.swiper = null;
|
this.swiper = null;
|
||||||
this.lastLookDirection = 1;
|
this.lastLookDirection = 1;
|
||||||
|
|
@ -104,6 +105,8 @@ function (Parent, KeyboardInput, DomController, Settings, Swiper) {
|
||||||
var self = this;
|
var self = this;
|
||||||
|
|
||||||
canvas.onmousedown = function(e) {
|
canvas.onmousedown = function(e) {
|
||||||
|
self.mousePosition = {x:0,y:0};
|
||||||
|
|
||||||
if(!self.playerController.player.isHoldingSomething()) {
|
if(!self.playerController.player.isHoldingSomething()) {
|
||||||
var options = {
|
var options = {
|
||||||
x: self.x,
|
x: self.x,
|
||||||
|
|
@ -112,6 +115,7 @@ function (Parent, KeyboardInput, DomController, Settings, Swiper) {
|
||||||
self.playerController.handActionRequest(options);
|
self.playerController.handActionRequest(options);
|
||||||
} else {
|
} else {
|
||||||
self.swiper = new Swiper();
|
self.swiper = new Swiper();
|
||||||
|
self.swiper.draw(e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -178,7 +182,9 @@ function (Parent, KeyboardInput, DomController, Settings, Swiper) {
|
||||||
e.webkitMovementY ||
|
e.webkitMovementY ||
|
||||||
0;
|
0;
|
||||||
|
|
||||||
this.swiper.swipe(movementX, -movementY);
|
this.mousePosition.x += movementX;
|
||||||
|
this.mousePosition.y += movementY;
|
||||||
|
this.swiper.swipe(this.mousePosition.x, -this.mousePosition.y);
|
||||||
};
|
};
|
||||||
|
|
||||||
KeyboardAndMouse.prototype.activateModifier = function() {
|
KeyboardAndMouse.prototype.activateModifier = function() {
|
||||||
|
|
|
||||||
|
|
@ -4,127 +4,172 @@ define([
|
||||||
|
|
||||||
function (Nc) {
|
function (Nc) {
|
||||||
|
|
||||||
var MAX_LENGTH = 150;
|
var MAX_LENGTH = 200;
|
||||||
|
var MIN_LENGTH = 5;
|
||||||
|
|
||||||
function Swiper() {
|
function Swiper() {
|
||||||
this.points = [];
|
this.points = [];
|
||||||
this.angleSum = 0;
|
this.angleSum = 0;
|
||||||
this.lengthSum = 0;
|
this.lengthSum = 0;
|
||||||
}
|
this.finished = false;
|
||||||
|
}
|
||||||
|
|
||||||
Swiper.prototype.swipe = function(x, y) {
|
Swiper.prototype.swipe = function(x, y) {
|
||||||
|
|
||||||
|
if(this.finished) return;
|
||||||
if(this.lengthSum > MAX_LENGTH) return;
|
|
||||||
|
|
||||||
this.points.push({x:x, y:y});
|
|
||||||
|
|
||||||
Nc.trigger(Nc.ns.client.view.swiper.swipe, x, y);
|
var points = this.points;
|
||||||
|
|
||||||
var points = this.points;
|
// Check if swipe is long enough to count
|
||||||
|
if(points.length >= 1) {
|
||||||
if(points.length >= 3) {
|
var lastPoint = points[points.length-1];
|
||||||
|
var a = Math.abs(x) - Math.abs(lastPoint.x);
|
||||||
var i = points.length - 1;
|
var b = Math.abs(y) - Math.abs(lastPoint.y);
|
||||||
|
var currentLength = Math.sqrt(a * a + b * b); // Pythagoras -> hypotenuse
|
||||||
var vectors = [
|
|
||||||
{
|
if(currentLength < MIN_LENGTH) return;
|
||||||
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(
|
points.push({x:x, y:y});
|
||||||
Math.pow(vectors[0].x, 2)
|
|
||||||
+ Math.pow(vectors[0].y, 2)
|
if(points.length >= 2) {
|
||||||
);
|
|
||||||
var lastLength = Math.sqrt(
|
this.updateLengthSum(currentLength);
|
||||||
Math.pow(vectors[1].x, 2)
|
|
||||||
+ Math.pow(vectors[1].y, 2)
|
|
||||||
);
|
|
||||||
|
|
||||||
var angle = 180 - (Math.acos((dotProduct / (currentLength * lastLength)) % 1) * 180 / Math.PI);
|
if(this.points.length >= 3) {
|
||||||
angle *= direction;
|
this.updateAngleSum(currentLength);
|
||||||
|
}
|
||||||
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 i = points.length - 1;
|
||||||
var angularVelocity = this.angleSum;
|
Nc.trigger(Nc.ns.client.view.swiper.swipe, points[i].x, points[i].y);
|
||||||
var length = this.lengthSum;
|
}
|
||||||
|
|
||||||
if (this.points.length < 1) {
|
Swiper.prototype.updateLengthSum = function(currentLength) {
|
||||||
return {
|
var points = this.points;
|
||||||
x: 0,
|
var i = points.length - 1;
|
||||||
y: 0,
|
|
||||||
av: 0
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
var p0x = this.points[0].x;
|
var lengthSum = this.lengthSum + Math.abs(currentLength);
|
||||||
var p0y = this.points[0].y;
|
if(lengthSum > MAX_LENGTH) {
|
||||||
var sumx = 0;
|
|
||||||
var sumy = 0;
|
var diff = lengthSum - MAX_LENGTH;
|
||||||
|
var ratio = (currentLength - diff) / currentLength;
|
||||||
|
|
||||||
Nc.trigger(Nc.ns.client.view.swiper.end);
|
// Offset triangle to origin
|
||||||
|
var x0 = points[i].x - points[i-1].x;
|
||||||
for(var i=0, count = this.points.length; i < count; i++) {
|
var y0 = points[i].y - points[i-1].y;
|
||||||
var p = this.points[i];
|
|
||||||
sumx += p.x - p0x;
|
// Multiply with ratio and offset back
|
||||||
sumy += p.y - p0y;
|
var clippedX = x0 * ratio + points[i-1].x;
|
||||||
}
|
var clippedY = y0 * ratio + points[i-1].y;
|
||||||
|
|
||||||
var direction = {
|
points[i].x = clippedX;
|
||||||
x: sumx / count,
|
points[i].y = clippedY;
|
||||||
y: sumy / count
|
|
||||||
};
|
this.finished = true;
|
||||||
|
|
||||||
|
lengthSum = MAX_LENGTH;
|
||||||
|
}
|
||||||
|
|
||||||
|
this.lengthSum = lengthSum;
|
||||||
|
};
|
||||||
|
|
||||||
|
Swiper.prototype.updateAngleSum = function(currentLength) {
|
||||||
|
|
||||||
|
var points = this.points;
|
||||||
|
var i = points.length - 1;
|
||||||
|
|
||||||
|
// last two lines
|
||||||
|
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 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;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
Swiper.prototype.swipeEnd = function(x, y) {
|
||||||
|
var angularVelocity = this.angleSum;
|
||||||
|
var length = this.lengthSum;
|
||||||
|
|
||||||
|
if (this.points.length < 1) {
|
||||||
|
return {
|
||||||
|
x: 0,
|
||||||
|
y: 0,
|
||||||
|
av: 0
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
var p0x = this.points[0].x;
|
||||||
|
var p0y = this.points[0].y;
|
||||||
|
var sumx = 0;
|
||||||
|
var sumy = 0;
|
||||||
|
|
||||||
|
Nc.trigger(Nc.ns.client.view.swiper.end);
|
||||||
|
|
||||||
|
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;
|
var larger = Math.abs(direction.x) > Math.abs(direction.y) ? direction.x : direction.y;
|
||||||
direction.x /= Math.abs(larger);
|
direction.x /= Math.abs(larger);
|
||||||
direction.y /= Math.abs(larger);
|
direction.y /= Math.abs(larger);
|
||||||
|
|
||||||
this.angleSum = 0;
|
this.angleSum = 0;
|
||||||
this.lengthSum = 0;
|
this.lengthSum = 0;
|
||||||
this.points = [];
|
this.points = [];
|
||||||
|
this.finished = false;
|
||||||
return {
|
|
||||||
x: direction.x * length / 100,
|
return {
|
||||||
y: direction.y * length / 100,
|
x: direction.x * length / 100,
|
||||||
av: angularVelocity / 100
|
y: direction.y * length / 100,
|
||||||
}
|
av: angularVelocity / 100
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Swiper.prototype.destroy = function() {
|
Swiper.prototype.destroy = function() {
|
||||||
for (var i = 0; i < this.ncTokens.length; i++) {
|
for (var i = 0; i < this.ncTokens.length; i++) {
|
||||||
Nc.off(this.ncTokens[i]);
|
Nc.off(this.ncTokens[i]);
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
return Swiper;
|
return Swiper;
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
@ -17,11 +17,6 @@ function (Parent, PIXI, Nc, Settings) {
|
||||||
Nc.on(Nc.ns.client.view.swiper.end, this.end, this)
|
Nc.on(Nc.ns.client.view.swiper.end, this.end, this)
|
||||||
];
|
];
|
||||||
|
|
||||||
this.offset = {
|
|
||||||
x: Settings.STAGE_WIDTH / 2,
|
|
||||||
y: Settings.STAGE_HEIGHT / 2
|
|
||||||
}
|
|
||||||
|
|
||||||
this.sprite = new PIXI.Graphics();
|
this.sprite = new PIXI.Graphics();
|
||||||
this.container.addChild(this.sprite);
|
this.container.addChild(this.sprite);
|
||||||
|
|
||||||
|
|
@ -31,13 +26,17 @@ function (Parent, PIXI, Nc, Settings) {
|
||||||
Swiper.prototype = Object.create(Parent.prototype);
|
Swiper.prototype = Object.create(Parent.prototype);
|
||||||
|
|
||||||
Swiper.prototype.swipe = function(x, y) {
|
Swiper.prototype.swipe = function(x, y) {
|
||||||
|
var offset = {
|
||||||
|
x: Settings.STAGE_WIDTH / 2,
|
||||||
|
y: Settings.STAGE_HEIGHT / 2
|
||||||
|
}
|
||||||
|
|
||||||
this.sprite.moveTo(this.offset.x + this.last.x, this.offset.y + this.last.y);
|
this.sprite.moveTo(offset.x + this.last.x, offset.y + this.last.y);
|
||||||
|
|
||||||
this.last.x += x;
|
this.last.x = x;
|
||||||
this.last.y -= y;
|
this.last.y = -y;
|
||||||
|
|
||||||
this.sprite.lineTo(this.offset.x + this.last.x, this.offset.y + this.last.y);
|
this.sprite.lineTo(offset.x + this.last.x, offset.y + this.last.y);
|
||||||
};
|
};
|
||||||
|
|
||||||
Swiper.prototype.end = function(x, y) {
|
Swiper.prototype.end = function(x, y) {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue