Skip to content

Commit

Permalink
wrap plugins in IIFE
Browse files Browse the repository at this point in the history
  • Loading branch information
straker committed Feb 23, 2019
1 parent e840257 commit 5e7f75c
Show file tree
Hide file tree
Showing 2 changed files with 103 additions and 94 deletions.
133 changes: 70 additions & 63 deletions examples/plugins/advancedCollisionPlugin.html
Expand Up @@ -8,70 +8,76 @@
<canvas id="game" width="600" height="400" style="background: #333331"></canvas>
<script id="code">
// plugin: upgrade kontra.sprite with advanced collision handling
let RECTANGLE = 0;
let CIRCLE = 1;

let accountForAnchor = function(object) {
let x = object.x;
let y = object.y;
let width = object.shape === RECTANGLE ? object.width: object.radius;
let height = object.shape === RECTANGLE ? object.height: object.radius;

if (object.anchor) {
x -= width * object.anchor.x;
y -= height * object.anchor.y;
(function() {
let RECTANGLE = 0;
let CIRCLE = 1;

let accountForAnchor = function(object) {
let x = object.x;
let y = object.y;
let width = object.shape === RECTANGLE ? object.width: object.radius;
let height = object.shape === RECTANGLE ? object.height: object.radius;

if (object.anchor) {
x -= width * object.anchor.x;
y -= height * object.anchor.y;
}

return {
x,
y,
width: object.width,
height: object.height,
radius: object.radius,
shape: object.shape
};
}

let recVsRec = function(rec1, rec2) {
return rec1.x < rec2.x + rec2.width &&
rec1.x + rec1.width > rec2.x &&
rec1.y < rec2.y + rec2.height &&
rec1.y + rec1.height > rec2.y;
}

return {
x,
y,
width: object.width,
height: object.height,
radius: object.radius,
shape: object.shape
};
}

let recVsRec = function(rec1, rec2) {
return rec1.x < rec2.x + rec2.width &&
rec1.x + rec1.width > rec2.x &&
rec1.y < rec2.y + rec2.height &&
rec1.y + rec1.height > rec2.y;
}

let recVsCircle = function(rec, circle) {
let dx = circle.x - Math.max(rec.x, Math.min(circle.x, rec.x + rec.width));
let dy = circle.y - Math.max(rec.y, Math.min(circle.y, rec.y + rec.height));
return dx * dx + dy * dy < circle.radius * circle.radius;
}

let circleVsCircle = function(circle1, circle2) {
let dx = circle1.x - circle2.x;
let dy = circle1.y - circle2.y;
return Math.sqrt(dx * dx + dy * dy) <= circle1.radius + circle2.radius;
}

advacedCollisionPlugin = {
afterCollidesWith(sprite, result, object) {
let obj1 = accountForAnchor(sprite);
let obj2 = accountForAnchor(object);

if (obj1.shape === RECTANGLE) {
if (obj2.shape === RECTANGLE) {
return recVsRec(obj1, obj2);
let recVsCircle = function(rec, circle) {
let dx = circle.x - Math.max(rec.x, Math.min(circle.x, rec.x + rec.width));
let dy = circle.y - Math.max(rec.y, Math.min(circle.y, rec.y + rec.height));
return dx * dx + dy * dy < circle.radius * circle.radius;
}

let circleVsCircle = function(circle1, circle2) {
let dx = circle1.x - circle2.x;
let dy = circle1.y - circle2.y;
return Math.sqrt(dx * dx + dy * dy) <= circle1.radius + circle2.radius;
}

window.advacedCollisionPlugin = {
shapes: {
RECTANGLE,
CIRCLE
},
afterCollidesWith(sprite, result, object) {
let obj1 = accountForAnchor(sprite);
let obj2 = accountForAnchor(object);

if (obj1.shape === RECTANGLE) {
if (obj2.shape === RECTANGLE) {
return recVsRec(obj1, obj2);
}
else {
return recVsCircle(obj1, obj2);
}
}
else if (obj2.shape === RECTANGLE) {
return recVsCircle(obj2, obj1);
}
else {
return recVsCircle(obj1, obj2);
return circleVsCircle(obj1, obj2);
}
}
else if (obj2.shape === RECTANGLE) {
return recVsCircle(obj2, obj1);
}
else {
return circleVsCircle(obj1, obj2);
}
}
}
})();



Expand All @@ -86,6 +92,7 @@
kontra.plugin.register('sprite', advacedCollisionPlugin);

let sprites = [];
let shapes = advacedCollisionPlugin.shapes;

// rec sprite
sprites.push(kontra.sprite({
Expand All @@ -95,7 +102,7 @@
dy: 0,
width: 40,
height: 40,
shape: RECTANGLE,
shape: shapes.RECTANGLE,
_color: 'red',
update: function() {
this.advance();
Expand All @@ -111,7 +118,7 @@
dy: 0,
width: 40,
height: 40,
shape: RECTANGLE,
shape: shapes.RECTANGLE,
_color: 'blue',
update: function() {
this.advance();
Expand All @@ -126,7 +133,7 @@
dx: 3,
dy: 0,
radius: 20,
shape: CIRCLE,
shape: shapes.CIRCLE,
_color: 'yellow',
update: function() {
this.advance();
Expand All @@ -147,7 +154,7 @@
dx: -3,
dy: 0,
radius: 20,
shape: CIRCLE,
shape: shapes.CIRCLE,
_color: 'orange',
update: function() {
this.advance();
Expand All @@ -169,7 +176,7 @@
dy: 0,
width: 40,
height: 40,
shape: RECTANGLE,
shape: shapes.RECTANGLE,
_color: 'green',
update: function() {
this.advance();
Expand All @@ -184,7 +191,7 @@
dx: -3,
dy: 0,
radius: 20,
shape: CIRCLE,
shape: shapes.CIRCLE,
_color: 'purple',
update: function() {
this.advance();
Expand Down
64 changes: 33 additions & 31 deletions examples/plugins/extendingVecotr.html
Expand Up @@ -8,39 +8,41 @@
<canvas id="game" width="600" height="400" style="background: #333331"></canvas>
<script id="code">
// plugin: upgrade kontra.vector with advanced vector functions
let subtract = function(vec) {
return kontra.vector(this.x - vec.x, this.y - vec.y);
}
let dot = function(vec) {
return this.x * vec.x + this.y * vec.y;
}
let cross = function(vec) {
return this.x * vec.y - this.y * vec.x;
}
let length = function() {
return this.dot(this, this) ** 0.5;
}
let scale = function(percent) {
return kontra.vector(this.x * percent, this.y * percent);
}
let normalize = function() {
this.scale(this, 1 / (this.length(this) || 1));
}
let distance = function(vec) {
return this.length(this.subtract(this, vec));
}
(function() {
let subtract = function(vec) {
return kontra.vector(this.x - vec.x, this.y - vec.y);
}
let dot = function(vec) {
return this.x * vec.x + this.y * vec.y;
}
let cross = function(vec) {
return this.x * vec.y - this.y * vec.x;
}
let length = function() {
return this.dot(this, this) ** 0.5;
}
let scale = function(percent) {
return kontra.vector(this.x * percent, this.y * percent);
}
let normalize = function() {
this.scale(this, 1 / (this.length(this) || 1));
}
let distance = function(vec) {
return this.length(this.subtract(this, vec));
}

let advancedVectorPlugin = {}
let advancedVectorPlugin = {}

kontra.plugin.extend('vector', {
subtract,
dot,
cross,
length,
scale,
normalize,
distance
});
kontra.plugin.extend('vector', {
subtract,
dot,
cross,
length,
scale,
normalize,
distance
});
})();



Expand Down

0 comments on commit 5e7f75c

Please sign in to comment.