2018-03-27 17:39:08 +00:00
|
|
|
function Connection(config){
|
|
|
|
|
|
|
|
var self = this;
|
|
|
|
|
|
|
|
// Properties
|
|
|
|
self.from = config.from;
|
|
|
|
self.to = config.to;
|
|
|
|
self.uncuttable = config.uncuttable || false;
|
|
|
|
self.sim = config.sim;
|
|
|
|
|
2018-04-15 21:24:22 +00:00
|
|
|
// Line Sprite
|
2018-04-01 11:50:29 +00:00
|
|
|
self.sprite = new Sprite({
|
|
|
|
src: "sprites/line.png",
|
|
|
|
frames:1, sw:300, sh:20,
|
|
|
|
});
|
|
|
|
self.sprite.pivotX = 2.8;
|
|
|
|
self.sprite.pivotY = 10;
|
|
|
|
|
2018-04-15 21:24:22 +00:00
|
|
|
// Dot Sprite
|
|
|
|
self.dotSprite = new Sprite({
|
|
|
|
src: "sprites/peeps.png",
|
|
|
|
frames:6, sw:200, sh:200,
|
|
|
|
});
|
|
|
|
self.dotSprite.pivotX = 100;
|
|
|
|
self.dotSprite.pivotY = 100;
|
|
|
|
self.dotSprite.scale = 0.1;
|
|
|
|
|
2018-03-27 17:39:08 +00:00
|
|
|
// Update
|
|
|
|
self.update = function(){};
|
|
|
|
|
|
|
|
// Draw
|
|
|
|
self.draw = function(ctx){
|
2018-04-01 16:34:52 +00:00
|
|
|
|
|
|
|
var s = self.sim.options.scale || 1;
|
|
|
|
|
2018-04-01 11:50:29 +00:00
|
|
|
ctx.save();
|
|
|
|
ctx.translate(self.from.x, self.from.y);
|
|
|
|
var dx = self.to.x - self.from.x;
|
|
|
|
var dy = self.to.y - self.from.y;
|
|
|
|
var a = Math.atan2(dy,dx);
|
|
|
|
var dist = Math.sqrt(dx*dx + dy*dy);
|
2018-04-20 18:47:58 +00:00
|
|
|
ctx.rotate(a);
|
|
|
|
|
|
|
|
// SHAKE
|
|
|
|
if(self.shaking>=0 && self.shaking<1){
|
|
|
|
self.shaking+=0.05;
|
|
|
|
var amplitude = (1-self.shaking)*3;
|
|
|
|
ctx.translate(0, Math.sin(self.shaking*Math.TAU*3)*amplitude);
|
|
|
|
}
|
|
|
|
|
2018-04-01 11:50:29 +00:00
|
|
|
self.sprite.scaleX = dist/300;
|
|
|
|
self.sprite.scaleY = self.uncuttable ? 1 : 0.5; // thick=uncuttable
|
2018-04-01 16:34:52 +00:00
|
|
|
//self.sprite.scaleY *= s;
|
2018-04-20 18:47:58 +00:00
|
|
|
//self.sprite.rotation = a;
|
2018-04-01 11:50:29 +00:00
|
|
|
self.sprite.draw(ctx);
|
|
|
|
ctx.restore();
|
2018-04-01 16:34:52 +00:00
|
|
|
|
2018-04-15 21:24:22 +00:00
|
|
|
// DRAW CONTAGION DOT
|
|
|
|
if(self.contagionDot){
|
|
|
|
var infectedFrame = self.sim.options.infectedFrame || 1;
|
|
|
|
self.dotSprite.x = self.contagionDot.x;
|
|
|
|
self.dotSprite.y = self.contagionDot.y;
|
|
|
|
self.dotSprite.gotoFrame(infectedFrame);
|
|
|
|
self.dotSprite.draw(ctx);
|
|
|
|
}
|
|
|
|
|
2018-03-27 17:39:08 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// Hit Test with a LINE SEGMENT
|
|
|
|
// code adapted from https://gist.github.com/Joncom/e8e8d18ebe7fe55c3894
|
|
|
|
self.hitTest = function(line){
|
|
|
|
|
|
|
|
var p0_x, p0_y, p1_x, p1_y, p2_x, p2_y, p3_x, p3_y;
|
|
|
|
p0_x = line[0];
|
|
|
|
p0_y = line[1];
|
|
|
|
p1_x = line[2];
|
|
|
|
p1_y = line[3];
|
|
|
|
p2_x = self.from.x;
|
|
|
|
p2_y = self.from.y;
|
|
|
|
p3_x = self.to.x;
|
|
|
|
p3_y = self.to.y;
|
|
|
|
|
|
|
|
var s1_x, s1_y, s2_x, s2_y;
|
|
|
|
s1_x = p1_x - p0_x;
|
|
|
|
s1_y = p1_y - p0_y;
|
|
|
|
s2_x = p3_x - p2_x;
|
|
|
|
s2_y = p3_y - p2_y;
|
|
|
|
var s, t;
|
|
|
|
s = (-s1_y * (p0_x - p2_x) + s1_x * (p0_y - p2_y)) / (-s2_x * s1_y + s1_x * s2_y);
|
|
|
|
t = ( s2_x * (p0_y - p2_y) - s2_y * (p0_x - p2_x)) / (-s2_x * s1_y + s1_x * s2_y);
|
|
|
|
|
|
|
|
return (s >= 0 && s <= 1 && t >= 0 && t <= 1);
|
|
|
|
|
|
|
|
};
|
|
|
|
|
2018-04-15 21:24:22 +00:00
|
|
|
// Animate
|
|
|
|
self.contagionDot = null;
|
|
|
|
self.animate = function(){
|
|
|
|
|
|
|
|
// Infection?
|
|
|
|
var cFrom, cTo;
|
|
|
|
if(self.from.infected && (!self.to.infected && self.to.isPastThreshold)){
|
|
|
|
cFrom = self.from;
|
|
|
|
cTo = self.to;
|
|
|
|
}
|
|
|
|
if(self.to.infected && (!self.from.infected && self.from.isPastThreshold)){
|
|
|
|
cFrom = self.to;
|
|
|
|
cTo = self.from;
|
|
|
|
}
|
|
|
|
|
|
|
|
// boop!
|
|
|
|
if(cFrom && cTo){
|
|
|
|
|
|
|
|
// ANIMATE IT
|
|
|
|
cFrom = { x:cFrom.x, y:cFrom.y };
|
|
|
|
cTo = { x:cTo.x, y:cTo.y };
|
|
|
|
tweenPosition(cFrom, cTo, function(point){
|
|
|
|
self.contagionDot = point;
|
|
|
|
}, easeLinear);
|
|
|
|
|
|
|
|
// Then, goodbye later
|
|
|
|
setTimeout(function(){
|
|
|
|
self.contagionDot = null;
|
|
|
|
},333);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
};
|
2018-04-20 18:47:58 +00:00
|
|
|
self.shaking = -1;
|
|
|
|
self.shake = function(){
|
|
|
|
self.shaking = 0;
|
|
|
|
};
|
2018-04-15 21:24:22 +00:00
|
|
|
|
2018-03-28 14:57:19 +00:00
|
|
|
}
|