Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

craters.js

Package Overview
Dependencies
Maintainers
1
Versions
15
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

craters.js - npm Package Compare versions

Comparing version 1.1.0 to 1.2.0

craters/craters.js

197

dist/craters.js
var Craters = (function (exports) {
'use strict';
// Rect collision tests the edges of each rect to
// test whether the objects are overlapping the other
class Collision {
static detect (collider, collidee) {
// Store the collider and collidee edges
var l1 = collider.state.pos.x;
var t1 = collider.state.pos.y;
var r1 = l1 + collider.state.size.x;
var b1 = t1 + collider.state.size.y;
var l2 = collidee.state.pos.x;
var t2 = collidee.state.pos.y;
var r2 = l2 + collidee.state.size.x;
var b2 = t2 + collidee.state.size.y;
// If the any of the edges are beyond any of the
// others, then we know that the box cannot be
// colliding
if (b1 < t2 || t1 > b2 || r1 < l2 || l1 > r2) {
return false;
}
// If the algorithm made it here, it had to collide
return true;
}
}
// Game Loop Module

@@ -121,29 +94,29 @@ // This module contains the game loop, which handles

class Canvas {
constructor (width, height, container) {
var container = document.querySelector(container || 'body');
// Generate a canvas and store it as our viewport
var canvas = document.createElement('canvas');
var context = canvas.getContext('2d');
// Pass our canvas' context to our getPixelRatio method
var backingStores = ['webkitBackingStorePixelRatio', 'mozBackingStorePixelRatio', 'msBackingStorePixelRatio', 'oBackingStorePixelRatio', 'backingStorePixelRatio'];
var deviceRatio = window.devicePixelRatio;
// Iterate through our backing store props and determine the proper backing ratio.
var backingRatio = backingStores.reduce(function (prev, curr) {
return (Object.prototype.hasOwnProperty.call(context, curr) ? context[curr] : 1)
});
// Return the proper pixel ratio by dividing the device ratio by the backing ratio
var ratio = deviceRatio / backingRatio;
// Set the canvas' width then downscale via CSS
canvas.width = Math.round(width * ratio);
canvas.height = Math.round(height * ratio);
canvas.style.width = width + 'px';
canvas.style.height = height + 'px';
// Scale the context so we get accurate pixel density
context.setTransform(ratio, 0, 0, ratio, 0, 0);
// Append viewport into our game within the dom
container.insertBefore(canvas, container.firstChild);
return canvas // return the canvas
}
constructor (width, height, container) {
container = document.querySelector(container || 'body');
// Generate a canvas and store it as our viewport
var canvas = document.createElement('canvas');
var context = canvas.getContext('2d');
// Pass our canvas' context to our getPixelRatio method
var backingStores = ['webkitBackingStorePixelRatio', 'mozBackingStorePixelRatio', 'msBackingStorePixelRatio', 'oBackingStorePixelRatio', 'backingStorePixelRatio'];
var deviceRatio = window.devicePixelRatio;
// Iterate through our backing store props and determine the proper backing ratio.
var backingRatio = backingStores.reduce(function (prev, curr) {
return (Object.prototype.hasOwnProperty.call(context, curr) ? context[curr] : 1)
});
// Return the proper pixel ratio by dividing the device ratio by the backing ratio
var ratio = deviceRatio / backingRatio;
// Set the canvas' width then downscale via CSS
canvas.width = Math.round(width * ratio);
canvas.height = Math.round(height * ratio);
canvas.style.width = width + 'px';
canvas.style.height = height + 'px';
// Scale the context so we get accurate pixel density
context.setTransform(ratio, 0, 0, ratio, 0, 0);
// Append viewport into our game within the dom
container.insertBefore(canvas, container.firstChild);
return canvas // return the canvas
}
}

@@ -157,27 +130,48 @@

size: {
x: 10,
y: 10
},
bgcolor: 'rgba(0,0,0,0)',
color: '#ff0',
font: '1em Arial'
x: 10,
y: 10
}
};
}
addObject (obj) {
// used for adding entities
this.entities.push(obj);
}
removeObject (index) {
// used to remove entities
this.entities.splice(index, 1);
}
update () {
for (var entity = 0; entity < this.entities.length; entity++) {
// Fire off each active entities `render` method
this.entities[entity].update();
}
// Loop through all bodies and update one at a time
this.entities.forEach(function (body) {
body.update();
switch (body.type) {
case 'dynamic':
case 'kinematic':
case 'default':
body.state.vel.x += body.state.accel.x;
body.state.vel.y += body.state.accel.y;
body.state.pos.x += body.state.vel.x;
body.state.pos.y += body.state.vel.y;
break
default:
// throw new Error('type not valid')
}
});
}
render () {
for (var entity = 0; entity < this.entities.length; entity++) {
// Fire off each active entities `render` method
this.entities[entity].render(); }
// Loop through all bodies and update one at a time
this.entities.forEach(function (body) {
body.render();
});
}
clearContext (context, size) {
context.clearRect(0, 0, size.x, size.y);
context.clearRect(0, 0, size.x, size.y);
}

@@ -209,12 +203,4 @@ }

};
this.type = 'kinematic';
}
update () {
super.update ();
this.state.vel.x += this.state.accel.x;
this.state.vel.y += this.state.accel.y;
this.state.pos.x += this.state.vel.x;
this.state.pos.y += this.state.vel.y;
}
}

@@ -228,25 +214,25 @@

this.state.pos = args.pos || {
x: 0,
y: 0
};
this.state.crop = {
x: 0,
y: 0
};
this.state.size = args.size || {
x: 0,
y: 0
};
this.state.frames = args.frames || [];
this.state.angle = args.angle || 0;
this.state.image = args.image || new Image();
this.state.delay = args.delay || 5;
this.state.tick = args.tick || 0;
this.state.orientation = args.orientation || 'horizontal';
}
x: 0,
y: 0
};
this.state.crop = {
x: 0,
y: 0
};
this.state.size = args.size || {
x: 0,
y: 0
};
this.state.frames = args.frames || [];
this.state.angle = args.angle || 0;
this.state.image = args.image || new Image();
this.state.delay = args.delay || 5;
this.state.tick = args.tick || 0;
this.state.orientation = args.orientation || 'horizontal';
}
update () {
super.update ();
super.update();
if (this.state.tick <= 0) {

@@ -412,9 +398,9 @@ if (this.orientation === 'vertical') {

// Craters.js micro game framework
/*
const boundary = function numberboundary (min, max) {
return Math.min(Math.max(this, min), max)
};
}
// Expose methods
Number.prototype.boundary = boundary;
Number.prototype.boundary = boundary
*/
class Craters {

@@ -427,3 +413,2 @@ static version () {

exports.Canvas = Canvas;
exports.Collision = Collision;
exports.Craters = Craters;

@@ -430,0 +415,0 @@ exports.Entity = Entity;

@@ -1,1 +0,1 @@

!function(t){var e={};function s(i){if(e[i])return e[i].exports;var a=e[i]={i:i,l:!1,exports:{}};return t[i].call(a.exports,a,a.exports,s),a.l=!0,a.exports}s.m=t,s.c=e,s.d=function(t,e,i){s.o(t,e)||Object.defineProperty(t,e,{enumerable:!0,get:i})},s.r=function(t){"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(t,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(t,"__esModule",{value:!0})},s.t=function(t,e){if(1&e&&(t=s(t)),8&e)return t;if(4&e&&"object"==typeof t&&t&&t.__esModule)return t;var i=Object.create(null);if(s.r(i),Object.defineProperty(i,"default",{enumerable:!0,value:t}),2&e&&"string"!=typeof t)for(var a in t)s.d(i,a,function(e){return t[e]}.bind(null,a));return i},s.n=function(t){var e=t&&t.__esModule?function(){return t.default}:function(){return t};return s.d(e,"a",e),e},s.o=function(t,e){return Object.prototype.hasOwnProperty.call(t,e)},s.p="",s(s.s=0)}([function(t,e){!function(t){"use strict";class e{constructor(t,e,s,i,a){this.entities=[],this.state={container:t,size:{x:10,y:10},bgcolor:"rgba(0,0,0,0)",color:"#ff0",font:"1em Arial"}}update(){for(var t=0;t<this.entities.length;t++)this.entities[t].update()}render(){for(var t=0;t<this.entities.length;t++)this.entities[t].render()}clearContext(t,e){t.clearRect(0,0,e.x,e.y)}}class s extends e{constructor(){super(),this.state={size:{x:10,y:10},pos:{x:0,y:0},vel:{x:0,y:0},accel:{x:0,y:0},radius:10,angle:0}}update(){super.update(),this.state.vel.x+=this.state.accel.x,this.state.vel.y+=this.state.accel.y,this.state.pos.x+=this.state.vel.x,this.state.pos.y+=this.state.vel.y}}Number.prototype.boundary=function(t,e){return Math.min(Math.max(this,t),e)};t.Canvas=class{constructor(t,e,s){s=document.querySelector(s||"body");var i=document.createElement("canvas"),a=i.getContext("2d"),r=window.devicePixelRatio/["webkitBackingStorePixelRatio","mozBackingStorePixelRatio","msBackingStorePixelRatio","oBackingStorePixelRatio","backingStorePixelRatio"].reduce(function(t,e){return Object.prototype.hasOwnProperty.call(a,e)?a[e]:1});return i.width=Math.round(t*r),i.height=Math.round(e*r),i.style.width=t+"px",i.style.height=e+"px",a.setTransform(r,0,0,r,0,0),s.insertBefore(i,s.firstChild),i}},t.Collision=class{static detect(t,e){var s=t.state.pos.x,i=t.state.pos.y,a=s+t.state.size.x,r=i+t.state.size.y,n=e.state.pos.x,o=e.state.pos.y,c=n+e.state.size.x,h=o+e.state.size.y;return!(r<o||i>h||a<n||s>c)}},t.Craters=class{static version(){return"0.0.0.5"}},t.Entity=s,t.Game=e,t.Loader=class{constructor(){this.rescache={}}load(t,e){var s=this;t instanceof Array?t.forEach(function(t){s.rescache[t]=!1,s.fetch(t,e)}):(s.rescache[t]=!1,s.fetch(t,e))}fetch(t,e){var s=this;if(s.rescache[t])return s.rescache[t];var i=new Image;i.onload=function(){s.rescache[t]=i,s.ready(e)},i.src=t}ready(t){if("function"==typeof t){var e=!0;for(var s in this.rescache)Object.prototype.hasOwnProperty.call(this.rescache,s)&&!this.rescache[s]&&(e=!1);e&&t()}}},t.Loop=class{constructor(t,e){var s={},i=1e3/e,a=window.performance.now(),r={new:{frameCount:0,startTime:a,sinceStart:0},old:{frameCount:0,startTime:a,sineStart:0}},n="new";return s.fps=0,s.main=function(o){s.stopLoop=window.requestAnimationFrame(s.main);var c,h,u=o,d=u-a;if(d>i){for(var l in a=u-d%i,r)++r[l].frameCount,r[l].sinceStart=u-r[l].startTime;c=r[n],s.fps=Math.round(1e3/(c.sinceStart/c.frameCount)*100)/100,h=r.new.frameCount===r.old.frameCount?5*e:10*e,c.frameCount>h&&(r[n].frameCount=0,r[n].startTime=u,r[n].sinceStart=0,n="new"===n?"old":"new"),t.update(t,u),t.render(t,u)}},s.main(),s}},t.Sound=class{constructor(){this.sounds={},this.instances=[],this.defaultVolume=1}load(t,e,s){if(this.sounds[t]=new Audio(e),"function"!=typeof s)return new Promise((e,s)=>{this.sounds[t].addEventListener("canplaythrough",e),this.sounds[t].addEventListener("error",s)});this.sounds[t].addEventListener("canplaythrough",s)}remove(t){void 0!==this.sounds&&delete this.sounds[t]}unlock(t,e,s,i){var a=this,r=["touchstart","touchend","mousedown","keydown"],n=function n(){r.forEach(function(t){document.body.removeEventListener(t,n)}),a.play(t,e,s,i)};r.forEach(function(t){document.body.addEventListener(t,n,!1)})}play(t,e,s,i){if(i=i||!1,void 0===this.sounds[t])return console.error("Can't find sound called '"+t+"'."),!1;var a=this.sounds[t].cloneNode(!0);return a.volume="number"==typeof s?s:this.defaultVolume,a.loop=i,a.play(),this.instances.push(a),a.addEventListener("ended",()=>{var t=this.instances.indexOf(a);-1!==t&&this.instances.splice(t,1)}),"function"==typeof e?(a.addEventListener("ended",e),!0):new Promise((t,e)=>a.addEventListener("ended",t))}stopAll(){var t=this.instances.slice();for(var e of t)e.pause(),e.dispatchEvent(new Event("ended"))}},t.Sprite=class extends s{constructor(t,e){super(),this.scope=t,this.state.pos=e.pos||{x:0,y:0},this.state.crop={x:0,y:0},this.state.size=e.size||{x:0,y:0},this.state.frames=e.frames||[],this.state.angle=e.angle||0,this.state.image=e.image||new Image,this.state.delay=e.delay||5,this.state.tick=e.tick||0,this.state.orientation=e.orientation||"horizontal"}update(){super.update(),this.state.tick<=0&&("vertical"===this.orientation?(this.state.crop.y=this.state.frames.shift(),this.state.frames.push(this.state.crop.y)):(this.state.crop.x=this.state.frames.shift(),this.state.frames.push(this.state.crop.x)),this.state.tick=this.state.delay),this.state.tick--}render(){super.render(),this.scope.context.save(),this.scope.context.translate(this.state.crop.x+this.state.size.x/2,this.state.crop.y+this.state.size.y/2),this.scope.context.rotate(this.state.angle*(Math.PI/180)),this.scope.context.translate(-(this.state.crop.x+this.state.size.x/2),-(this.state.crop.y+this.state.size.y/2)),this.scope.context.drawImage(this.state.image,this.state.crop.x*this.state.size.x,this.state.crop.y*this.state.size.y,this.state.size.x,this.state.size.y,this.state.pos.x,this.state.pos.y,this.state.size.x,this.state.size.y),this.scope.context.restore()}}}({})}]);
!function(t){var e={};function s(i){if(e[i])return e[i].exports;var n=e[i]={i:i,l:!1,exports:{}};return t[i].call(n.exports,n,n.exports,s),n.l=!0,n.exports}s.m=t,s.c=e,s.d=function(t,e,i){s.o(t,e)||Object.defineProperty(t,e,{enumerable:!0,get:i})},s.r=function(t){"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(t,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(t,"__esModule",{value:!0})},s.t=function(t,e){if(1&e&&(t=s(t)),8&e)return t;if(4&e&&"object"==typeof t&&t&&t.__esModule)return t;var i=Object.create(null);if(s.r(i),Object.defineProperty(i,"default",{enumerable:!0,value:t}),2&e&&"string"!=typeof t)for(var n in t)s.d(i,n,function(e){return t[e]}.bind(null,n));return i},s.n=function(t){var e=t&&t.__esModule?function(){return t.default}:function(){return t};return s.d(e,"a",e),e},s.o=function(t,e){return Object.prototype.hasOwnProperty.call(t,e)},s.p="",s(s.s=0)}([function(t,e){!function(t){"use strict";class e{constructor(t,e,s,i,n){this.entities=[],this.state={container:t,size:{x:10,y:10}}}addObject(t){this.entities.push(t)}removeObject(t){this.entities.splice(t,1)}update(){this.entities.forEach(function(t){switch(t.update(),t.type){case"dynamic":case"kinematic":case"default":t.state.vel.x+=t.state.accel.x,t.state.vel.y+=t.state.accel.y,t.state.pos.x+=t.state.vel.x,t.state.pos.y+=t.state.vel.y}})}render(){this.entities.forEach(function(t){t.render()})}clearContext(t,e){t.clearRect(0,0,e.x,e.y)}}class s extends e{constructor(){super(),this.state={size:{x:10,y:10},pos:{x:0,y:0},vel:{x:0,y:0},accel:{x:0,y:0},radius:10,angle:0},this.type="kinematic"}}t.Canvas=class{constructor(t,e,s){s=document.querySelector(s||"body");var i=document.createElement("canvas"),n=i.getContext("2d"),a=window.devicePixelRatio/["webkitBackingStorePixelRatio","mozBackingStorePixelRatio","msBackingStorePixelRatio","oBackingStorePixelRatio","backingStorePixelRatio"].reduce(function(t,e){return Object.prototype.hasOwnProperty.call(n,e)?n[e]:1});return i.width=Math.round(t*a),i.height=Math.round(e*a),i.style.width=t+"px",i.style.height=e+"px",n.setTransform(a,0,0,a,0,0),s.insertBefore(i,s.firstChild),i}},t.Craters=class{static version(){return"0.0.0.5"}},t.Entity=s,t.Game=e,t.Loader=class{constructor(){this.rescache={}}load(t,e){var s=this;t instanceof Array?t.forEach(function(t){s.rescache[t]=!1,s.fetch(t,e)}):(s.rescache[t]=!1,s.fetch(t,e))}fetch(t,e){var s=this;if(s.rescache[t])return s.rescache[t];var i=new Image;i.onload=function(){s.rescache[t]=i,s.ready(e)},i.src=t}ready(t){if("function"==typeof t){var e=!0;for(var s in this.rescache)Object.prototype.hasOwnProperty.call(this.rescache,s)&&!this.rescache[s]&&(e=!1);e&&t()}}},t.Loop=class{constructor(t,e){var s={},i=1e3/e,n=window.performance.now(),a={new:{frameCount:0,startTime:n,sinceStart:0},old:{frameCount:0,startTime:n,sineStart:0}},r="new";return s.fps=0,s.main=function(o){s.stopLoop=window.requestAnimationFrame(s.main);var c,h,u=o,d=u-n;if(d>i){for(var l in n=u-d%i,a)++a[l].frameCount,a[l].sinceStart=u-a[l].startTime;c=a[r],s.fps=Math.round(1e3/(c.sinceStart/c.frameCount)*100)/100,h=a.new.frameCount===a.old.frameCount?5*e:10*e,c.frameCount>h&&(a[r].frameCount=0,a[r].startTime=u,a[r].sinceStart=0,r="new"===r?"old":"new"),t.update(t,u),t.render(t,u)}},s.main(),s}},t.Sound=class{constructor(){this.sounds={},this.instances=[],this.defaultVolume=1}load(t,e,s){if(this.sounds[t]=new Audio(e),"function"!=typeof s)return new Promise((e,s)=>{this.sounds[t].addEventListener("canplaythrough",e),this.sounds[t].addEventListener("error",s)});this.sounds[t].addEventListener("canplaythrough",s)}remove(t){void 0!==this.sounds&&delete this.sounds[t]}unlock(t,e,s,i){var n=this,a=["touchstart","touchend","mousedown","keydown"],r=function r(){a.forEach(function(t){document.body.removeEventListener(t,r)}),n.play(t,e,s,i)};a.forEach(function(t){document.body.addEventListener(t,r,!1)})}play(t,e,s,i){if(i=i||!1,void 0===this.sounds[t])return console.error("Can't find sound called '"+t+"'."),!1;var n=this.sounds[t].cloneNode(!0);return n.volume="number"==typeof s?s:this.defaultVolume,n.loop=i,n.play(),this.instances.push(n),n.addEventListener("ended",()=>{var t=this.instances.indexOf(n);-1!==t&&this.instances.splice(t,1)}),"function"==typeof e?(n.addEventListener("ended",e),!0):new Promise((t,e)=>n.addEventListener("ended",t))}stopAll(){var t=this.instances.slice();for(var e of t)e.pause(),e.dispatchEvent(new Event("ended"))}},t.Sprite=class extends s{constructor(t,e){super(),this.scope=t,this.state.pos=e.pos||{x:0,y:0},this.state.crop={x:0,y:0},this.state.size=e.size||{x:0,y:0},this.state.frames=e.frames||[],this.state.angle=e.angle||0,this.state.image=e.image||new Image,this.state.delay=e.delay||5,this.state.tick=e.tick||0,this.state.orientation=e.orientation||"horizontal"}update(){super.update(),this.state.tick<=0&&("vertical"===this.orientation?(this.state.crop.y=this.state.frames.shift(),this.state.frames.push(this.state.crop.y)):(this.state.crop.x=this.state.frames.shift(),this.state.frames.push(this.state.crop.x)),this.state.tick=this.state.delay),this.state.tick--}render(){super.render(),this.scope.context.save(),this.scope.context.translate(this.state.crop.x+this.state.size.x/2,this.state.crop.y+this.state.size.y/2),this.scope.context.rotate(this.state.angle*(Math.PI/180)),this.scope.context.translate(-(this.state.crop.x+this.state.size.x/2),-(this.state.crop.y+this.state.size.y/2)),this.scope.context.drawImage(this.state.image,this.state.crop.x*this.state.size.x,this.state.crop.y*this.state.size.y,this.state.size.x,this.state.size.y,this.state.pos.x,this.state.pos.y,this.state.size.x,this.state.size.y),this.scope.context.restore()}}}({})}]);

@@ -1,1 +0,1 @@

module.exports = require('./app/craters/craters.js')
module.exports = require('./craters/craters.js')
{
"name": "craters.js",
"version": "1.1.0",
"version": "1.2.0",
"description": "A Compact Game Engine that helps you build fast, modern HTML5 Games",
"main": "./index.js",
"scripts": {
"test": "",
"build": "rollup --config && webpack"
"test": "node ./__test__/craters.test.js",
"fix": "bash ./.beautify && standard ./craters/*.js --fix #&& standard ./examples/*/*.js --fix",
"build": "npm run fix && rollup --config && webpack"
},

@@ -15,5 +16,8 @@ "repository": {

"standard": {
"globals": [ "Audio", "Image", "Event" ]
"globals": [
"Audio",
"Image",
"Event"
]
},
"keywords": [

@@ -32,4 +36,7 @@ "html5-game-engine",

"homepage": "https://github.com/swashvirus/craters.js#readme",
"dependencies": {
"dependencies": {},
"devDependencies": {
"chai": "^4.2.0",
"esm": "^3.2.25"
}
}
}
module.exports = {
input: './app/craters/craters.js',
input: './craters/craters.js',
output: [{

@@ -4,0 +4,0 @@ file: './dist/craters.js',

@@ -5,3 +5,2 @@ const path = require('path')

entry: {
'./build/game.min.js': './app/game.js',
'./dist/craters.min.js': './dist/craters.js',

@@ -8,0 +7,0 @@ './dist/craters.min.mjs': './dist/craters.mjs'

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc