craters.js
Advanced tools
Comparing version 1.0.5 to 1.0.6
@@ -6,14 +6,14 @@ // Craters.js micro game framework | ||
import { game } from './game.js' | ||
import { entity, sprite } from './entity.js' | ||
import { loader } from './loader.js' | ||
import { sound } from './sound.js' | ||
import { Game } from './game.js' | ||
import { Entity, Sprite } from './entity.js' | ||
import { Loader } from './loader.js' | ||
import { Sound } from './sound.js' | ||
const Boundary = function numberBoundary (min, max) { | ||
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 { | ||
class Craters { | ||
static version () { | ||
@@ -24,2 +24,2 @@ return '0.0.0.3' | ||
export { craters, loader, game, entity, sprite, sound } | ||
export { Craters, Loader, Game, Entity, Sprite, Sound } |
@@ -1,2 +0,2 @@ | ||
class entity { | ||
class Entity { | ||
constructor () { | ||
@@ -32,3 +32,3 @@ // Setup the defaults if no parameters are given | ||
this.entities = [] | ||
this.entities = []; | ||
} | ||
@@ -66,3 +66,3 @@ | ||
class sprite extends entity { | ||
class Sprite extends Entity { | ||
constructor (scope, args) { | ||
@@ -96,3 +96,3 @@ super() | ||
if (this.state.tick <= 0) { | ||
if (this.orientation == 'vertical') { | ||
if (this.orientation === 'vertical') { | ||
this.state.pos.y = this.state.frames.shift() | ||
@@ -127,2 +127,2 @@ this.state.frames.push(this.state.pos.y) | ||
export { entity, sprite } | ||
export { Entity, Sprite } |
@@ -1,4 +0,4 @@ | ||
class game { | ||
constructor (game, width, height, frames, debug) { | ||
this.game = game || 'body' | ||
class Game { | ||
constructor (container, width, height, frames, debug) { | ||
this.container = container || 'body' | ||
this.constants = { | ||
@@ -31,3 +31,3 @@ gravity: { | ||
var backingRatio = backingStores.reduce(function (prev, curr) { | ||
return (context.hasOwnProperty(curr) ? context[curr] : 1) | ||
return (Object.prototype.hasOwnProperty.call(context, curr) ? context[curr] : 1) | ||
}) | ||
@@ -52,7 +52,7 @@ // Return the proper pixel ratio by dividing the device ratio by the backing ratio | ||
// Append viewport into our game within the dom | ||
this.game = document.querySelector(this.game) | ||
this.game.insertBefore(this.viewport, this.game.firstChild) | ||
this.container = document.querySelector(this.container) | ||
this.container.insertBefore(this.viewport, this.container.firstChild) | ||
// Initiate core modules with the current scope | ||
this.loop = new loop(this) | ||
this.loop = new Loop(this) | ||
this.intitiate() | ||
@@ -104,3 +104,3 @@ } | ||
class loop { | ||
class Loop { | ||
constructor (scope) { | ||
@@ -196,2 +196,2 @@ return this.loop(scope) | ||
export { game } | ||
export { Game } |
@@ -1,16 +0,15 @@ | ||
class input() { | ||
constructor { | ||
this.isPressed = {} | ||
// Set up `onkeydown` event handler. | ||
document.onkeydown = function(ev) { | ||
this.isPressed[ev.keyCode] = true | ||
} | ||
// Set up `onkeyup` event handler. | ||
document.onkeyup = function(ev) { | ||
this.isPressed[ev.keyCode] = false | ||
} | ||
class Input { | ||
constructor () { | ||
this.isPressed = {} | ||
// Set up `onkeydown` event handler. | ||
document.onkeydown = function (ev) { | ||
this.isPressed[ev.keyCode] = true | ||
} | ||
// Set up `onkeyup` event handler. | ||
document.onkeyup = function (ev) { | ||
this.isPressed[ev.keyCode] = false | ||
} | ||
} | ||
} | ||
export { input } | ||
export { Input } |
@@ -1,5 +0,5 @@ | ||
class loader { | ||
class Loader { | ||
constructor () { | ||
this.rescache = {} | ||
} | ||
}; | ||
@@ -9,8 +9,8 @@ load (res, cbk) { | ||
if (res instanceof Array) { | ||
res.forEach(function (url) { | ||
that.rescache[url] = false | ||
that.fetch(url, cbk) | ||
res.forEach(function (i) { | ||
that.rescache[i] = false | ||
that.fetch(i, cbk) | ||
}) | ||
} else { | ||
that.rescache[url] = false | ||
that.rescache[res] = false | ||
that.fetch(res, cbk) | ||
@@ -40,3 +40,3 @@ } | ||
for (var item in that.rescache) { | ||
if (that.rescache.hasOwnProperty(item) && !that.rescache[item]) { | ||
if (Object.prototype.hasOwnProperty.call(that.rescache, item) && !that.rescache[item]) { | ||
ready = false | ||
@@ -51,2 +51,2 @@ } | ||
export { loader } | ||
export { Loader } |
// modified soundbox.js lib | ||
class sound { | ||
class Sound { | ||
constructor () { | ||
this.sounds = {} // The loaded sounds and their instances | ||
this.instances = [] // Sounds that are currently playing | ||
this.default_volume = 1 | ||
} | ||
this.defaultVolume = 1 | ||
}; | ||
load (sound_name, path, callback) { | ||
this.sounds[sound_name] = new Audio(path) | ||
load (name, path, callback) { | ||
this.sounds[name] = new Audio(path) | ||
if (typeof callback === 'function') { | ||
this.sounds[sound_name].addEventListener('canplaythrough', callback) | ||
this.sounds[name].addEventListener('canplaythrough', callback) | ||
} else { | ||
return new Promise((resolve, reject) => { | ||
this.sounds[sound_name].addEventListener('canplaythrough', resolve) | ||
this.sounds[sound_name].addEventListener('error', reject) | ||
this.sounds[name].addEventListener('canplaythrough', resolve) | ||
this.sounds[name].addEventListener('error', reject) | ||
}) | ||
@@ -21,9 +21,9 @@ } | ||
remove (sound_name) { | ||
remove (name) { | ||
if (typeof this.sounds !== 'undefined') { | ||
delete this.sounds[sound_name] | ||
delete this.sounds[name] | ||
} | ||
}; | ||
unlock (sound_name, callback, volume, loop) { | ||
unlock (name, callback, volume, loop) { | ||
var that = this | ||
@@ -35,3 +35,3 @@ var events = ['touchstart', 'touchend', 'mousedown', 'keydown'] | ||
}) | ||
that.play(sound_name, callback, volume, loop) | ||
that.play(name, callback, volume, loop) | ||
} | ||
@@ -42,14 +42,14 @@ | ||
}) | ||
} | ||
}; | ||
play (sound_name, callback, volume, loop) { | ||
play (name, callback, volume, loop) { | ||
loop = loop || false | ||
if (typeof this.sounds[sound_name] === 'undefined') { | ||
console.error("Can't find sound called '" + sound_name + "'.") | ||
if (typeof this.sounds[name] === 'undefined') { | ||
console.error("Can't find sound called '" + name + "'.") | ||
return false | ||
} | ||
}; | ||
var soundInstance = this.sounds[sound_name].cloneNode(true) | ||
soundInstance.volume = typeof volume === 'number' ? volume : this.default_volume | ||
var soundInstance = this.sounds[name].cloneNode(true) | ||
soundInstance.volume = typeof volume === 'number' ? volume : this.defaultVolume | ||
soundInstance.loop = loop | ||
@@ -62,3 +62,3 @@ soundInstance.play() | ||
var index = this.instances.indexOf(soundInstance) | ||
if (index != -1) this.instances.splice(index, 1) | ||
if (index !== -1) this.instances.splice(index, 1) | ||
}) | ||
@@ -70,3 +70,3 @@ | ||
return true | ||
} | ||
}; | ||
@@ -76,8 +76,8 @@ return new Promise((resolve, reject) => soundInstance.addEventListener('ended', resolve)) | ||
stop_all () { | ||
stopAll () { | ||
// Pause all currently playing sounds | ||
// Shallow clone the array to avoid issues with instances auto-removing themselves | ||
var instances_to_stop = this.instances.slice() | ||
for (var instance of instances_to_stop) { | ||
var instancesToStop = this.instances.slice() | ||
for (var instance of instancesToStop) { | ||
instance.pause() | ||
@@ -87,4 +87,4 @@ instance.dispatchEvent(new Event('ended')) | ||
} | ||
} | ||
}; | ||
export { sound } | ||
export { Sound } |
@@ -1,9 +0,7 @@ | ||
'use strict' | ||
// load craters.js script tag works too | ||
import { game } from './craters/craters.js' | ||
'use strict'; | ||
import { Game } from './craters/craters.js' | ||
class mygame extends game { | ||
class mygame extends Game { | ||
intitiate() { | ||
super.intitiate() | ||
// now intitiate my game | ||
super.intitiate(); | ||
} | ||
@@ -17,3 +15,2 @@ | ||
} | ||
window.game = new mygame('#container', window.innerWidth, window.innerHeight, 60, true) |
@@ -1,1 +0,1 @@ | ||
!function(t){var e={};function n(i){if(e[i])return e[i].exports;var o=e[i]={i:i,l:!1,exports:{}};return t[i].call(o.exports,o,o.exports,n),o.l=!0,o.exports}n.m=t,n.c=e,n.d=function(t,e,i){n.o(t,e)||Object.defineProperty(t,e,{enumerable:!0,get:i})},n.r=function(t){"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(t,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(t,"__esModule",{value:!0})},n.t=function(t,e){if(1&e&&(t=n(t)),8&e)return t;if(4&e&&"object"==typeof t&&t&&t.__esModule)return t;var i=Object.create(null);if(n.r(i),Object.defineProperty(i,"default",{enumerable:!0,value:t}),2&e&&"string"!=typeof t)for(var o in t)n.d(i,o,function(e){return t[e]}.bind(null,o));return i},n.n=function(t){var e=t&&t.__esModule?function(){return t.default}:function(){return t};return n.d(e,"a",e),e},n.o=function(t,e){return Object.prototype.hasOwnProperty.call(t,e)},n.p="",n(n.s=1)}([,function(t,e,n){"use strict";n.r(e);class i{constructor(t,e,n,i,r){this.game=t||"body",this.constants={gravity:{x:0,y:100},width:e,height:n,frames:i,debug:r,bgcolor:"rgba(0,0,0,0)",color:"#ff0",font:"1em Arial"},this.state={entities:[]};var a=document.createElement("canvas"),s=a.getContext("2d"),c=window.devicePixelRatio/["webkitBackingStorePixelRatio","mozBackingStorePixelRatio","msBackingStorePixelRatio","oBackingStorePixelRatio","backingStorePixelRatio"].reduce(function(t,e){return s.hasOwnProperty(e)?s[e]:1});a.width=Math.round(this.constants.width*c),a.height=Math.round(this.constants.height*c),a.style.width=this.constants.width+"px",a.style.height=this.constants.height+"px",s.setTransform(c,0,0,c,0,0),this.viewport=a,this.viewport.id="gameViewport",this.context=this.viewport.getContext("2d"),this.game=document.querySelector(this.game),this.game.insertBefore(this.viewport,this.game.firstChild),this.loop=new o(this),this.intitiate()}intitiate(){}update(t,e){for(var n=(t.state||[]).entities,i=0;i<n.length;i++)n[i].update()}render(t,e){var n=t.constants.width,i=t.constants.height;t.context.font=t.constants.font,t.context.save(),t.context.clearRect(0,0,n,i),t.context.fillStyle=t.constants.bgcolor,t.context.fillRect(0,0,n,i),t.context.fill(),t.context.restore(),t.context.fillStyle=t.constants.color,t.constants.debug&&t.context.fillText("fps : "+t.loop.fps,n-100,50);for(var o=(t.state||[]).entities,r=0;r<o.length;r++)o[r].render()}}class o{constructor(t){return this.loop(t)}loop(t){var e={},n=t.constants.frames,i=1e3/n,o=window.performance.now(),r={new:{frameCount:0,startTime:o,sinceStart:0},old:{frameCount:0,startTime:o,sineStart:0}},a="new";return e.fps=0,e.main=function(s){e.stopLoop=window.requestAnimationFrame(e.main);var c,u,l=s,f=l-o;if(f>i){for(var h in o=l-f%i,r)++r[h].frameCount,r[h].sinceStart=l-r[h].startTime;c=r[a],e.fps=Math.round(1e3/(c.sinceStart/c.frameCount)*100)/100,u=r.new.frameCount===r.old.frameCount?5*n:10*n,c.frameCount>u&&(r[a].frameCount=0,r[a].startTime=l,r[a].sinceStart=0,a="new"===a?"old":"new"),t.update(t,l),t.render(t,l)}},e.main(),e}}Number.prototype.boundary=function(t,e){return Math.min(Math.max(this,t),e)};window.game=new class extends i{intitiate(){super.intitiate()}render(){super.render(this),this.context.font="2em Arial",this.context.fillText("It's working.️",65,this.constants.height/2,this.constants.width)}}("#container",window.innerWidth,window.innerHeight,60,!0)}]); | ||
!function(t){var e={};function n(o){if(e[o])return e[o].exports;var i=e[o]={i:o,l:!1,exports:{}};return t[o].call(i.exports,i,i.exports,n),i.l=!0,i.exports}n.m=t,n.c=e,n.d=function(t,e,o){n.o(t,e)||Object.defineProperty(t,e,{enumerable:!0,get:o})},n.r=function(t){"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(t,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(t,"__esModule",{value:!0})},n.t=function(t,e){if(1&e&&(t=n(t)),8&e)return t;if(4&e&&"object"==typeof t&&t&&t.__esModule)return t;var o=Object.create(null);if(n.r(o),Object.defineProperty(o,"default",{enumerable:!0,value:t}),2&e&&"string"!=typeof t)for(var i in t)n.d(o,i,function(e){return t[e]}.bind(null,i));return o},n.n=function(t){var e=t&&t.__esModule?function(){return t.default}:function(){return t};return n.d(e,"a",e),e},n.o=function(t,e){return Object.prototype.hasOwnProperty.call(t,e)},n.p="",n(n.s=1)}([,function(t,e,n){"use strict";n.r(e);class o{constructor(t,e,n,o,r){this.container=t||"body",this.constants={gravity:{x:0,y:100},width:e,height:n,frames:o,debug:r,bgcolor:"rgba(0,0,0,0)",color:"#ff0",font:"1em Arial"},this.state={entities:[]};var a=document.createElement("canvas"),s=a.getContext("2d"),c=window.devicePixelRatio/["webkitBackingStorePixelRatio","mozBackingStorePixelRatio","msBackingStorePixelRatio","oBackingStorePixelRatio","backingStorePixelRatio"].reduce(function(t,e){return Object.prototype.hasOwnProperty.call(s,e)?s[e]:1});a.width=Math.round(this.constants.width*c),a.height=Math.round(this.constants.height*c),a.style.width=this.constants.width+"px",a.style.height=this.constants.height+"px",s.setTransform(c,0,0,c,0,0),this.viewport=a,this.viewport.id="gameViewport",this.context=this.viewport.getContext("2d"),this.container=document.querySelector(this.container),this.container.insertBefore(this.viewport,this.container.firstChild),this.loop=new i(this),this.intitiate()}intitiate(){}update(t,e){for(var n=(t.state||[]).entities,o=0;o<n.length;o++)n[o].update()}render(t,e){var n=t.constants.width,o=t.constants.height;t.context.font=t.constants.font,t.context.save(),t.context.clearRect(0,0,n,o),t.context.fillStyle=t.constants.bgcolor,t.context.fillRect(0,0,n,o),t.context.fill(),t.context.restore(),t.context.fillStyle=t.constants.color,t.constants.debug&&t.context.fillText("fps : "+t.loop.fps,n-100,50);for(var i=(t.state||[]).entities,r=0;r<i.length;r++)i[r].render()}}class i{constructor(t){return this.loop(t)}loop(t){var e={},n=t.constants.frames,o=1e3/n,i=window.performance.now(),r={new:{frameCount:0,startTime:i,sinceStart:0},old:{frameCount:0,startTime:i,sineStart:0}},a="new";return e.fps=0,e.main=function(s){e.stopLoop=window.requestAnimationFrame(e.main);var c,l,u=s,f=u-i;if(f>o){for(var h in i=u-f%o,r)++r[h].frameCount,r[h].sinceStart=u-r[h].startTime;c=r[a],e.fps=Math.round(1e3/(c.sinceStart/c.frameCount)*100)/100,l=r.new.frameCount===r.old.frameCount?5*n:10*n,c.frameCount>l&&(r[a].frameCount=0,r[a].startTime=u,r[a].sinceStart=0,a="new"===a?"old":"new"),t.update(t,u),t.render(t,u)}},e.main(),e}}Number.prototype.boundary=function(t,e){return Math.min(Math.max(this,t),e)};window.game=new class extends o{intitiate(){super.intitiate()}render(){super.render(this),this.context.font="2em Arial",this.context.fillText("It's working.️",65,this.constants.height/2,this.constants.width)}}("#container",window.innerWidth,window.innerHeight,60,!0)}]); |
@@ -1,4 +0,4 @@ | ||
class game { | ||
constructor (game, width, height, frames, debug) { | ||
this.game = game || 'body'; | ||
class Game { | ||
constructor (container, width, height, frames, debug) { | ||
this.container = container || 'body'; | ||
this.constants = { | ||
@@ -31,3 +31,3 @@ gravity: { | ||
var backingRatio = backingStores.reduce(function (prev, curr) { | ||
return (context.hasOwnProperty(curr) ? context[curr] : 1) | ||
return (Object.prototype.hasOwnProperty.call(context, curr) ? context[curr] : 1) | ||
}); | ||
@@ -52,7 +52,7 @@ // Return the proper pixel ratio by dividing the device ratio by the backing ratio | ||
// Append viewport into our game within the dom | ||
this.game = document.querySelector(this.game); | ||
this.game.insertBefore(this.viewport, this.game.firstChild); | ||
this.container = document.querySelector(this.container); | ||
this.container.insertBefore(this.viewport, this.container.firstChild); | ||
// Initiate core modules with the current scope | ||
this.loop = new loop(this); | ||
this.loop = new Loop(this); | ||
this.intitiate(); | ||
@@ -103,3 +103,3 @@ } | ||
} | ||
class loop { | ||
class Loop { | ||
constructor (scope) { | ||
@@ -195,3 +195,3 @@ return this.loop(scope) | ||
class entity { | ||
class Entity { | ||
constructor () { | ||
@@ -260,3 +260,3 @@ // Setup the defaults if no parameters are given | ||
class sprite extends entity { | ||
class Sprite extends Entity { | ||
constructor (scope, args) { | ||
@@ -290,3 +290,3 @@ super(); | ||
if (this.state.tick <= 0) { | ||
if (this.orientation == 'vertical') { | ||
if (this.orientation === 'vertical') { | ||
this.state.pos.y = this.state.frames.shift(); | ||
@@ -321,6 +321,6 @@ this.state.frames.push(this.state.pos.y); | ||
class loader { | ||
class Loader { | ||
constructor () { | ||
this.rescache = {}; | ||
} | ||
}; | ||
@@ -330,8 +330,8 @@ load (res, cbk) { | ||
if (res instanceof Array) { | ||
res.forEach(function (url) { | ||
that.rescache[url] = false; | ||
that.fetch(url, cbk); | ||
res.forEach(function (i) { | ||
that.rescache[i] = false; | ||
that.fetch(i, cbk); | ||
}); | ||
} else { | ||
that.rescache[url] = false; | ||
that.rescache[res] = false; | ||
that.fetch(res, cbk); | ||
@@ -361,3 +361,3 @@ } | ||
for (var item in that.rescache) { | ||
if (that.rescache.hasOwnProperty(item) && !that.rescache[item]) { | ||
if (Object.prototype.hasOwnProperty.call(that.rescache, item) && !that.rescache[item]) { | ||
ready = false; | ||
@@ -373,17 +373,17 @@ } | ||
// modified soundbox.js lib | ||
class sound { | ||
class Sound { | ||
constructor () { | ||
this.sounds = {}; // The loaded sounds and their instances | ||
this.instances = []; // Sounds that are currently playing | ||
this.default_volume = 1; | ||
} | ||
this.defaultVolume = 1; | ||
}; | ||
load (sound_name, path, callback) { | ||
this.sounds[sound_name] = new Audio(path); | ||
load (name, path, callback) { | ||
this.sounds[name] = new Audio(path); | ||
if (typeof callback === 'function') { | ||
this.sounds[sound_name].addEventListener('canplaythrough', callback); | ||
this.sounds[name].addEventListener('canplaythrough', callback); | ||
} else { | ||
return new Promise((resolve, reject) => { | ||
this.sounds[sound_name].addEventListener('canplaythrough', resolve); | ||
this.sounds[sound_name].addEventListener('error', reject); | ||
this.sounds[name].addEventListener('canplaythrough', resolve); | ||
this.sounds[name].addEventListener('error', reject); | ||
}) | ||
@@ -393,9 +393,9 @@ } | ||
remove (sound_name) { | ||
remove (name) { | ||
if (typeof this.sounds !== 'undefined') { | ||
delete this.sounds[sound_name]; | ||
delete this.sounds[name]; | ||
} | ||
}; | ||
unlock (sound_name, callback, volume, loop) { | ||
unlock (name, callback, volume, loop) { | ||
var that = this; | ||
@@ -407,3 +407,3 @@ var events = ['touchstart', 'touchend', 'mousedown', 'keydown']; | ||
}); | ||
that.play(sound_name, callback, volume, loop); | ||
that.play(name, callback, volume, loop); | ||
}; | ||
@@ -414,14 +414,13 @@ | ||
}); | ||
} | ||
}; | ||
play (sound_name, callback, volume, loop) { | ||
play (name, callback, volume, loop) { | ||
loop = loop || false; | ||
if (typeof this.sounds[sound_name] === 'undefined') { | ||
console.error("Can't find sound called '" + sound_name + "'."); | ||
if (typeof this.sounds[name] === 'undefined') { | ||
console.error("Can't find sound called '" + name + "'."); | ||
return false | ||
} | ||
var soundInstance = this.sounds[sound_name].cloneNode(true); | ||
soundInstance.volume = typeof volume === 'number' ? volume : this.default_volume; | ||
var soundInstance = this.sounds[name].cloneNode(true); | ||
soundInstance.volume = typeof volume === 'number' ? volume : this.defaultVolume; | ||
soundInstance.loop = loop; | ||
@@ -434,3 +433,3 @@ soundInstance.play(); | ||
var index = this.instances.indexOf(soundInstance); | ||
if (index != -1) this.instances.splice(index, 1); | ||
if (index !== -1) this.instances.splice(index, 1); | ||
}); | ||
@@ -443,12 +442,11 @@ | ||
} | ||
return new Promise((resolve, reject) => soundInstance.addEventListener('ended', resolve)) | ||
}; | ||
stop_all () { | ||
stopAll () { | ||
// Pause all currently playing sounds | ||
// Shallow clone the array to avoid issues with instances auto-removing themselves | ||
var instances_to_stop = this.instances.slice(); | ||
for (var instance of instances_to_stop) { | ||
var instancesToStop = this.instances.slice(); | ||
for (var instance of instancesToStop) { | ||
instance.pause(); | ||
@@ -462,9 +460,9 @@ instance.dispatchEvent(new Event('ended')); | ||
const Boundary = function numberBoundary (min, max) { | ||
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 { | ||
class Craters { | ||
static version () { | ||
@@ -475,2 +473,2 @@ return '0.0.0.3' | ||
export { craters, entity, game, loader, sound, sprite }; | ||
export { Craters, Entity, Game, Loader, Sound, Sprite }; |
@@ -1,1 +0,1 @@ | ||
!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,s){"use strict";s.r(e),s.d(e,"craters",function(){return h}),s.d(e,"entity",function(){return o}),s.d(e,"game",function(){return i}),s.d(e,"loader",function(){return a}),s.d(e,"sound",function(){return c}),s.d(e,"sprite",function(){return r});class i{constructor(t,e,s,i,o){this.game=t||"body",this.constants={gravity:{x:0,y:100},width:e,height:s,frames:i,debug:o,bgcolor:"rgba(0,0,0,0)",color:"#ff0",font:"1em Arial"},this.state={entities:[]};var r=document.createElement("canvas"),a=r.getContext("2d"),c=window.devicePixelRatio/["webkitBackingStorePixelRatio","mozBackingStorePixelRatio","msBackingStorePixelRatio","oBackingStorePixelRatio","backingStorePixelRatio"].reduce(function(t,e){return a.hasOwnProperty(e)?a[e]:1});r.width=Math.round(this.constants.width*c),r.height=Math.round(this.constants.height*c),r.style.width=this.constants.width+"px",r.style.height=this.constants.height+"px",a.setTransform(c,0,0,c,0,0),this.viewport=r,this.viewport.id="gameViewport",this.context=this.viewport.getContext("2d"),this.game=document.querySelector(this.game),this.game.insertBefore(this.viewport,this.game.firstChild),this.loop=new n(this),this.intitiate()}intitiate(){}update(t,e){for(var s=(t.state||[]).entities,i=0;i<s.length;i++)s[i].update()}render(t,e){var s=t.constants.width,i=t.constants.height;t.context.font=t.constants.font,t.context.save(),t.context.clearRect(0,0,s,i),t.context.fillStyle=t.constants.bgcolor,t.context.fillRect(0,0,s,i),t.context.fill(),t.context.restore(),t.context.fillStyle=t.constants.color,t.constants.debug&&t.context.fillText("fps : "+t.loop.fps,s-100,50);for(var n=(t.state||[]).entities,o=0;o<n.length;o++)n[o].render()}}class n{constructor(t){return this.loop(t)}loop(t){var e={},s=t.constants.frames,i=1e3/s,n=window.performance.now(),o={new:{frameCount:0,startTime:n,sinceStart:0},old:{frameCount:0,startTime:n,sineStart:0}},r="new";return e.fps=0,e.main=function(a){e.stopLoop=window.requestAnimationFrame(e.main);var c,h,u=a,d=u-n;if(d>i){for(var l in n=u-d%i,o)++o[l].frameCount,o[l].sinceStart=u-o[l].startTime;c=o[r],e.fps=Math.round(1e3/(c.sinceStart/c.frameCount)*100)/100,h=o.new.frameCount===o.old.frameCount?5*s:10*s,c.frameCount>h&&(o[r].frameCount=0,o[r].startTime=u,o[r].sinceStart=0,r="new"===r?"old":"new"),t.update(t,u),t.render(t,u)}},e.main(),e}}class o{constructor(){this.type=this.type||"dynamic",this.collision=this.collision||"elastic",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.entities=[]}update(){for(var t=0;t<this.entities.length;t++)this.entities[t].update();switch(this.type){case"dynamic":case"kinematic":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}}render(){for(var t=0;t<this.entities.length;t++)this.entities[t].render()}}class r extends o{constructor(t,e){super(),this.scope=t,this.state={cord:e.pos||{x:0,y:0},pos:{x:0,y:0},size:e.size||{x:0,y:0},frames:e.frames||[],angle:e.angle||0,image:e.image||new Image,delay:e.delay||5,tick:e.tick||0,orientation:e.orientation||"horizontal"}}update(){this.state.tick<=0&&("vertical"==this.orientation?(this.state.pos.y=this.state.frames.shift(),this.state.frames.push(this.state.pos.y)):(this.state.pos.x=this.state.frames.shift(),this.state.frames.push(this.state.pos.x)),this.state.tick=this.state.delay),this.state.tick--}render(){super.render(this),this.scope.context.save(),this.scope.context.translate(this.state.pos.x+this.state.size.x/2,this.state.pos.y+this.state.size.y/2),this.scope.context.rotate(this.state.angle*(Math.PI/180)),this.scope.context.translate(-(this.state.pos.x+this.state.size.x/2),-(this.state.pos.y+this.state.size.y/2)),this.scope.context.drawImage(this.state.image,this.state.pos.x*this.state.size.x,this.state.pos.y*this.state.size.y,this.state.size.x,this.state.size.y,this.state.cord.x,this.state.cord.y,this.state.size.x,this.state.size.y),this.scope.context.restore()}}class a{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[url]=!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)this.rescache.hasOwnProperty(s)&&!this.rescache[s]&&(e=!1);e&&t()}}}class c{constructor(){this.sounds={},this.instances=[],this.default_volume=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,o=["touchstart","touchend","mousedown","keydown"],r=function r(){o.forEach(function(t){document.body.removeEventListener(t,r)}),n.play(t,e,s,i)};o.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.default_volume,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))}stop_all(){var t=this.instances.slice();for(var e of t)e.pause(),e.dispatchEvent(new Event("ended"))}}Number.prototype.boundary=function(t,e){return Math.min(Math.max(this,t),e)};class h{static version(){return"0.0.0.3"}}}]); | ||
!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,s){"use strict";s.r(e),s.d(e,"Craters",function(){return h}),s.d(e,"Entity",function(){return o}),s.d(e,"Game",function(){return i}),s.d(e,"Loader",function(){return a}),s.d(e,"Sound",function(){return c}),s.d(e,"Sprite",function(){return r});class i{constructor(t,e,s,i,o){this.container=t||"body",this.constants={gravity:{x:0,y:100},width:e,height:s,frames:i,debug:o,bgcolor:"rgba(0,0,0,0)",color:"#ff0",font:"1em Arial"},this.state={entities:[]};var r=document.createElement("canvas"),a=r.getContext("2d"),c=window.devicePixelRatio/["webkitBackingStorePixelRatio","mozBackingStorePixelRatio","msBackingStorePixelRatio","oBackingStorePixelRatio","backingStorePixelRatio"].reduce(function(t,e){return Object.prototype.hasOwnProperty.call(a,e)?a[e]:1});r.width=Math.round(this.constants.width*c),r.height=Math.round(this.constants.height*c),r.style.width=this.constants.width+"px",r.style.height=this.constants.height+"px",a.setTransform(c,0,0,c,0,0),this.viewport=r,this.viewport.id="gameViewport",this.context=this.viewport.getContext("2d"),this.container=document.querySelector(this.container),this.container.insertBefore(this.viewport,this.container.firstChild),this.loop=new n(this),this.intitiate()}intitiate(){}update(t,e){for(var s=(t.state||[]).entities,i=0;i<s.length;i++)s[i].update()}render(t,e){var s=t.constants.width,i=t.constants.height;t.context.font=t.constants.font,t.context.save(),t.context.clearRect(0,0,s,i),t.context.fillStyle=t.constants.bgcolor,t.context.fillRect(0,0,s,i),t.context.fill(),t.context.restore(),t.context.fillStyle=t.constants.color,t.constants.debug&&t.context.fillText("fps : "+t.loop.fps,s-100,50);for(var n=(t.state||[]).entities,o=0;o<n.length;o++)n[o].render()}}class n{constructor(t){return this.loop(t)}loop(t){var e={},s=t.constants.frames,i=1e3/s,n=window.performance.now(),o={new:{frameCount:0,startTime:n,sinceStart:0},old:{frameCount:0,startTime:n,sineStart:0}},r="new";return e.fps=0,e.main=function(a){e.stopLoop=window.requestAnimationFrame(e.main);var c,h,u=a,d=u-n;if(d>i){for(var l in n=u-d%i,o)++o[l].frameCount,o[l].sinceStart=u-o[l].startTime;c=o[r],e.fps=Math.round(1e3/(c.sinceStart/c.frameCount)*100)/100,h=o.new.frameCount===o.old.frameCount?5*s:10*s,c.frameCount>h&&(o[r].frameCount=0,o[r].startTime=u,o[r].sinceStart=0,r="new"===r?"old":"new"),t.update(t,u),t.render(t,u)}},e.main(),e}}class o{constructor(){this.type=this.type||"dynamic",this.collision=this.collision||"elastic",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.entities=[]}update(){for(var t=0;t<this.entities.length;t++)this.entities[t].update();switch(this.type){case"dynamic":case"kinematic":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}}render(){for(var t=0;t<this.entities.length;t++)this.entities[t].render()}}class r extends o{constructor(t,e){super(),this.scope=t,this.state={cord:e.pos||{x:0,y:0},pos:{x:0,y:0},size:e.size||{x:0,y:0},frames:e.frames||[],angle:e.angle||0,image:e.image||new Image,delay:e.delay||5,tick:e.tick||0,orientation:e.orientation||"horizontal"}}update(){this.state.tick<=0&&("vertical"===this.orientation?(this.state.pos.y=this.state.frames.shift(),this.state.frames.push(this.state.pos.y)):(this.state.pos.x=this.state.frames.shift(),this.state.frames.push(this.state.pos.x)),this.state.tick=this.state.delay),this.state.tick--}render(){super.render(this),this.scope.context.save(),this.scope.context.translate(this.state.pos.x+this.state.size.x/2,this.state.pos.y+this.state.size.y/2),this.scope.context.rotate(this.state.angle*(Math.PI/180)),this.scope.context.translate(-(this.state.pos.x+this.state.size.x/2),-(this.state.pos.y+this.state.size.y/2)),this.scope.context.drawImage(this.state.image,this.state.pos.x*this.state.size.x,this.state.pos.y*this.state.size.y,this.state.size.x,this.state.size.y,this.state.cord.x,this.state.cord.y,this.state.size.x,this.state.size.y),this.scope.context.restore()}}class a{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()}}}class c{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,o=["touchstart","touchend","mousedown","keydown"],r=function r(){o.forEach(function(t){document.body.removeEventListener(t,r)}),n.play(t,e,s,i)};o.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"))}}Number.prototype.boundary=function(t,e){return Math.min(Math.max(this,t),e)};class h{static version(){return"0.0.0.3"}}}]); |
@@ -1,58 +0,55 @@ | ||
import { game, entity, sprite, loader } from 'craters.js'; | ||
import { Game, Entity, Sprite, Loader } from './../../../app/craters/craters.js' | ||
"use strict"; | ||
class mygame extends game { | ||
intitiate () { | ||
super.intitiate(); | ||
this.score = '0000'; | ||
this.constants.color = 'rgba(255,255,255,1)'; | ||
this.constants.bgcolor = 'rgba(0,0,0,0.001)'; | ||
this.constants.font = '1.5em Arial'; | ||
for (var i = 0; i < 5; i++){ | ||
this.state.entities.push( new boltbug(this, {pos: {x: (Math.random() * this.constants.width), y: (Math.random() * this.constants.height)}})); | ||
} | ||
this.state.entities.push( new ladybug(this, 'f18') ); | ||
} | ||
render () { | ||
super.render(this) | ||
this.context.fillText('score: ' + this.score, (16), (50)); | ||
} | ||
'use strict' | ||
class Buggame extends Game { | ||
intitiate () { | ||
super.intitiate() | ||
this.score = '0000' | ||
this.constants.color = 'rgba(255,255,255,1)' | ||
this.constants.bgcolor = 'rgba(0,0,0,0.001)' | ||
this.constants.font = '1.5em Arial' | ||
for (var i = 0; i < 5; i++) { | ||
this.state.entities.push(new boltbug(this, { pos: { x: (Math.random() * this.constants.width), y: (Math.random() * this.constants.height) } })) | ||
} | ||
this.state.entities.push(new ladybug(this, 'f18')) | ||
} | ||
render () { | ||
super.render(this) | ||
this.context.fillText('score: ' + this.score, (16), (50)) | ||
} | ||
} | ||
class ladybug extends entity { | ||
constructor (scope, name) { | ||
super(); | ||
this.state.size = {x:196, y:218}; | ||
this.state.pos = {x: (scope.constants.width / 2) - (this.state.size.x / 2), y: (scope.constants.height - this.state.size.y)} | ||
scope.state.entities.push(new sprite(scope, {pos: this.state.pos, size: this.state.size, frames: [0, 1, 2], image: media.fetch('./src/media/bug.png')})) | ||
} | ||
class ladybug extends Entity { | ||
constructor (scope, name) { | ||
super() | ||
this.state.size = { x: 196, y: 218 } | ||
this.state.pos = { x: (scope.constants.width / 2) - (this.state.size.x / 2), y: (scope.constants.height - this.state.size.y) } | ||
scope.state.entities.push(new Sprite(scope, { pos: this.state.pos, size: this.state.size, frames: [0, 1, 2], image: media.fetch('./src/media/bug.png') })) | ||
} | ||
} | ||
class boltbug extends entity { | ||
constructor (scope, args) { | ||
super(); | ||
this.state.pos = args.pos; | ||
this.state.angle = (Math.random() * 360); | ||
scope.state.entities.push(new sprite(scope, {size: {x: 214, y: 282}, pos: this.state.pos , frames: [0, 1, 2], image: media.fetch('./src/media/bolt.png'), angle: this.state.angle})) | ||
} | ||
class boltbug extends Entity { | ||
constructor (scope, args) { | ||
super() | ||
this.state.pos = args.pos | ||
this.state.angle = (Math.random() * 360) | ||
scope.state.entities.push(new Sprite(scope, { size: { x: 214, y: 282 }, pos: this.state.pos, frames: [0, 1, 2], image: media.fetch('./src/media/bolt.png'), angle: this.state.angle })) | ||
} | ||
} | ||
// what this does is , it loads all resources | ||
// what this does is , it loads all resources | ||
// and later , it starts the game if all files were loaded | ||
var media = new loader; | ||
media.load([ | ||
'./src/media/bug.png', | ||
'./src/media/bolt.png' | ||
], function() { window.game = new mygame('#container', window.innerWidth, window.innerHeight, 60, false)}); | ||
var media = new Loader() | ||
media.load([ | ||
'./src/media/bug.png', | ||
'./src/media/bolt.png' | ||
], function () { window.game = new Buggame('#container', window.innerWidth, window.innerHeight, 60, false) }) |
@@ -1,1 +0,1 @@ | ||
!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=1)}([function(t,e,s){t.exports=s(2)},function(t,e,s){"use strict";s.r(e);var i=s(0);class n extends i.game{intitiate(){super.intitiate(),this.score="0000",this.constants.color="rgba(255,255,255,1)",this.constants.bgcolor="rgba(0,0,0,0.001)",this.constants.font="1.5em Arial";for(var t=0;t<5;t++)this.state.entities.push(new a(this,{pos:{x:Math.random()*this.constants.width,y:Math.random()*this.constants.height}}));this.state.entities.push(new o(this,"f18"))}render(){super.render(this),this.context.fillText("score: "+this.score,16,50)}}class o extends i.entity{constructor(t,e){super(),this.state.size={x:196,y:218},this.state.pos={x:t.constants.width/2-this.state.size.x/2,y:t.constants.height-this.state.size.y},t.state.entities.push(new i.sprite(t,{pos:this.state.pos,size:this.state.size,frames:[0,1,2],image:r.fetch("./src/media/bug.png")}))}}class a extends i.entity{constructor(t,e){super(),this.state.pos=e.pos,this.state.angle=360*Math.random(),t.state.entities.push(new i.sprite(t,{size:{x:214,y:282},pos:this.state.pos,frames:[0,1,2],image:r.fetch("./src/media/bolt.png"),angle:this.state.angle}))}}var r=new i.loader;r.load(["./src/media/bug.png","./src/media/bolt.png"],function(){window.game=new n("#container",window.innerWidth,window.innerHeight,60,!1)})},function(t,e,s){"use strict";s.r(e);class i{constructor(t,e,s,i,o){this.game=t||"body",this.constants={gravity:{x:0,y:100},width:e,height:s,frames:i,debug:o,bgcolor:"rgba(0,0,0,0)",color:"#ff0",font:"1em Arial"},this.state={entities:[]};var a=document.createElement("canvas"),r=a.getContext("2d"),c=window.devicePixelRatio/["webkitBackingStorePixelRatio","mozBackingStorePixelRatio","msBackingStorePixelRatio","oBackingStorePixelRatio","backingStorePixelRatio"].reduce(function(t,e){return r.hasOwnProperty(e)?r[e]:1});a.width=Math.round(this.constants.width*c),a.height=Math.round(this.constants.height*c),a.style.width=this.constants.width+"px",a.style.height=this.constants.height+"px",r.setTransform(c,0,0,c,0,0),this.viewport=a,this.viewport.id="gameViewport",this.context=this.viewport.getContext("2d"),this.game=document.querySelector(this.game),this.game.insertBefore(this.viewport,this.game.firstChild),this.loop=new n(this),this.intitiate()}intitiate(){}update(t,e){for(var s=(t.state||[]).entities,i=0;i<s.length;i++)s[i].update()}render(t,e){var s=t.constants.width,i=t.constants.height;t.context.font=t.constants.font,t.context.save(),t.context.clearRect(0,0,s,i),t.context.fillStyle=t.constants.bgcolor,t.context.fillRect(0,0,s,i),t.context.fill(),t.context.restore(),t.context.fillStyle=t.constants.color,t.constants.debug&&t.context.fillText("fps : "+t.loop.fps,s-100,50);for(var n=(t.state||[]).entities,o=0;o<n.length;o++)n[o].render()}}class n{constructor(t){return this.loop(t)}loop(t){var e={},s=t.constants.frames,i=1e3/s,n=window.performance.now(),o={new:{frameCount:0,startTime:n,sinceStart:0},old:{frameCount:0,startTime:n,sineStart:0}},a="new";return e.fps=0,e.main=function(r){e.stopLoop=window.requestAnimationFrame(e.main);var c,h,d=r,u=d-n;if(u>i){for(var l in n=d-u%i,o)++o[l].frameCount,o[l].sinceStart=d-o[l].startTime;c=o[a],e.fps=Math.round(1e3/(c.sinceStart/c.frameCount)*100)/100,h=o.new.frameCount===o.old.frameCount?5*s:10*s,c.frameCount>h&&(o[a].frameCount=0,o[a].startTime=d,o[a].sinceStart=0,a="new"===a?"old":"new"),t.update(t,d),t.render(t,d)}},e.main(),e}}class o{constructor(){this.type=this.type||"dynamic",this.collision=this.collision||"elastic",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.entities=[]}update(){for(var t=0;t<this.entities.length;t++)this.entities[t].update();switch(this.type){case"dynamic":case"kinematic":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}}render(){for(var t=0;t<this.entities.length;t++)this.entities[t].render()}}class a extends o{constructor(t,e){super(),this.scope=t,this.state={cord:e.pos||{x:0,y:0},pos:{x:0,y:0},size:e.size||{x:0,y:0},frames:e.frames||[],angle:e.angle||0,image:e.image||new Image,delay:e.delay||5,tick:e.tick||0,orientation:e.orientation||"horizontal"}}update(){this.state.tick<=0&&("vertical"==this.orientation?(this.state.pos.y=this.state.frames.shift(),this.state.frames.push(this.state.pos.y)):(this.state.pos.x=this.state.frames.shift(),this.state.frames.push(this.state.pos.x)),this.state.tick=this.state.delay),this.state.tick--}render(){super.render(this),this.scope.context.save(),this.scope.context.translate(this.state.pos.x+this.state.size.x/2,this.state.pos.y+this.state.size.y/2),this.scope.context.rotate(this.state.angle*(Math.PI/180)),this.scope.context.translate(-(this.state.pos.x+this.state.size.x/2),-(this.state.pos.y+this.state.size.y/2)),this.scope.context.drawImage(this.state.image,this.state.pos.x*this.state.size.x,this.state.pos.y*this.state.size.y,this.state.size.x,this.state.size.y,this.state.cord.x,this.state.cord.y,this.state.size.x,this.state.size.y),this.scope.context.restore()}}class r{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[url]=!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)this.rescache.hasOwnProperty(s)&&!this.rescache[s]&&(e=!1);e&&t()}}}class c{constructor(){this.sounds={},this.instances=[],this.default_volume=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,o=["touchstart","touchend","mousedown","keydown"],a=function a(){o.forEach(function(t){document.body.removeEventListener(t,a)}),n.play(t,e,s,i)};o.forEach(function(t){document.body.addEventListener(t,a,!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.default_volume,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))}stop_all(){var t=this.instances.slice();for(var e of t)e.pause(),e.dispatchEvent(new Event("ended"))}}s.d(e,"craters",function(){return h}),s.d(e,"loader",function(){return r}),s.d(e,"game",function(){return i}),s.d(e,"entity",function(){return o}),s.d(e,"sprite",function(){return a}),s.d(e,"sound",function(){return c});Number.prototype.boundary=function(t,e){return Math.min(Math.max(this,t),e)};class h{static version(){return"0.0.0.3"}}}]); | ||
!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,s){"use strict";s.r(e);class i{constructor(t,e,s,i,a){this.container=t||"body",this.constants={gravity:{x:0,y:100},width:e,height:s,frames:i,debug:a,bgcolor:"rgba(0,0,0,0)",color:"#ff0",font:"1em Arial"},this.state={entities:[]};var o=document.createElement("canvas"),r=o.getContext("2d"),c=window.devicePixelRatio/["webkitBackingStorePixelRatio","mozBackingStorePixelRatio","msBackingStorePixelRatio","oBackingStorePixelRatio","backingStorePixelRatio"].reduce(function(t,e){return Object.prototype.hasOwnProperty.call(r,e)?r[e]:1});o.width=Math.round(this.constants.width*c),o.height=Math.round(this.constants.height*c),o.style.width=this.constants.width+"px",o.style.height=this.constants.height+"px",r.setTransform(c,0,0,c,0,0),this.viewport=o,this.viewport.id="gameViewport",this.context=this.viewport.getContext("2d"),this.container=document.querySelector(this.container),this.container.insertBefore(this.viewport,this.container.firstChild),this.loop=new n(this),this.intitiate()}intitiate(){}update(t,e){for(var s=(t.state||[]).entities,i=0;i<s.length;i++)s[i].update()}render(t,e){var s=t.constants.width,i=t.constants.height;t.context.font=t.constants.font,t.context.save(),t.context.clearRect(0,0,s,i),t.context.fillStyle=t.constants.bgcolor,t.context.fillRect(0,0,s,i),t.context.fill(),t.context.restore(),t.context.fillStyle=t.constants.color,t.constants.debug&&t.context.fillText("fps : "+t.loop.fps,s-100,50);for(var n=(t.state||[]).entities,a=0;a<n.length;a++)n[a].render()}}class n{constructor(t){return this.loop(t)}loop(t){var e={},s=t.constants.frames,i=1e3/s,n=window.performance.now(),a={new:{frameCount:0,startTime:n,sinceStart:0},old:{frameCount:0,startTime:n,sineStart:0}},o="new";return e.fps=0,e.main=function(r){e.stopLoop=window.requestAnimationFrame(e.main);var c,h,l=r,p=l-n;if(p>i){for(var u in n=l-p%i,a)++a[u].frameCount,a[u].sinceStart=l-a[u].startTime;c=a[o],e.fps=Math.round(1e3/(c.sinceStart/c.frameCount)*100)/100,h=a.new.frameCount===a.old.frameCount?5*s:10*s,c.frameCount>h&&(a[o].frameCount=0,a[o].startTime=l,a[o].sinceStart=0,o="new"===o?"old":"new"),t.update(t,l),t.render(t,l)}},e.main(),e}}class a{constructor(){this.type=this.type||"dynamic",this.collision=this.collision||"elastic",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.entities=[]}update(){for(var t=0;t<this.entities.length;t++)this.entities[t].update();switch(this.type){case"dynamic":case"kinematic":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}}render(){for(var t=0;t<this.entities.length;t++)this.entities[t].render()}}class o extends a{constructor(t,e){super(),this.scope=t,this.state={cord:e.pos||{x:0,y:0},pos:{x:0,y:0},size:e.size||{x:0,y:0},frames:e.frames||[],angle:e.angle||0,image:e.image||new Image,delay:e.delay||5,tick:e.tick||0,orientation:e.orientation||"horizontal"}}update(){this.state.tick<=0&&("vertical"===this.orientation?(this.state.pos.y=this.state.frames.shift(),this.state.frames.push(this.state.pos.y)):(this.state.pos.x=this.state.frames.shift(),this.state.frames.push(this.state.pos.x)),this.state.tick=this.state.delay),this.state.tick--}render(){super.render(this),this.scope.context.save(),this.scope.context.translate(this.state.pos.x+this.state.size.x/2,this.state.pos.y+this.state.size.y/2),this.scope.context.rotate(this.state.angle*(Math.PI/180)),this.scope.context.translate(-(this.state.pos.x+this.state.size.x/2),-(this.state.pos.y+this.state.size.y/2)),this.scope.context.drawImage(this.state.image,this.state.pos.x*this.state.size.x,this.state.pos.y*this.state.size.y,this.state.size.x,this.state.size.y,this.state.cord.x,this.state.cord.y,this.state.size.x,this.state.size.y),this.scope.context.restore()}}Number.prototype.boundary=function(t,e){return Math.min(Math.max(this,t),e)};class r extends i{intitiate(){super.intitiate(),this.score="0000",this.constants.color="rgba(255,255,255,1)",this.constants.bgcolor="rgba(0,0,0,0.001)",this.constants.font="1.5em Arial";for(var t=0;t<5;t++)this.state.entities.push(new h(this,{pos:{x:Math.random()*this.constants.width,y:Math.random()*this.constants.height}}));this.state.entities.push(new c(this,"f18"))}render(){super.render(this),this.context.fillText("score: "+this.score,16,50)}}class c extends a{constructor(t,e){super(),this.state.size={x:196,y:218},this.state.pos={x:t.constants.width/2-this.state.size.x/2,y:t.constants.height-this.state.size.y},t.state.entities.push(new o(t,{pos:this.state.pos,size:this.state.size,frames:[0,1,2],image:l.fetch("./src/media/bug.png")}))}}class h extends a{constructor(t,e){super(),this.state.pos=e.pos,this.state.angle=360*Math.random(),t.state.entities.push(new o(t,{size:{x:214,y:282},pos:this.state.pos,frames:[0,1,2],image:l.fetch("./src/media/bolt.png"),angle:this.state.angle}))}}var l=new 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()}}};l.load(["./src/media/bug.png","./src/media/bolt.png"],function(){window.game=new r("#container",window.innerWidth,window.innerHeight,60,!1)})}]); |
@@ -1,5 +0,5 @@ | ||
const path = require('path'); | ||
const path = require('path') | ||
module.exports = { | ||
entry: {'./src/game': './src/game.js'}, | ||
entry: { './src/game': './src/game.js' }, | ||
output: { | ||
@@ -12,2 +12,2 @@ path: path.resolve(__dirname, './'), | ||
} | ||
}; | ||
} |
@@ -1,1 +0,1 @@ | ||
module.exports = require("./app/craters/craters.js") | ||
module.exports = require('./app/craters/craters.js') |
{ | ||
"name": "craters.js", | ||
"version": "1.0.5", | ||
"version": "1.0.6", | ||
"description": "A Compact Game Engine that helps you build fast, modern HTML5 Games", | ||
@@ -14,2 +14,7 @@ "main": "./index.js", | ||
}, | ||
"standard": { | ||
"globals": [ "Audio", "Image", "Event" ] | ||
}, | ||
"keywords": [ | ||
@@ -30,2 +35,2 @@ "node", | ||
} | ||
} | ||
} |
@@ -1,7 +0,6 @@ | ||
# Craters.js ☄️ | ||
# craters.js ☄️ | ||
![npm bundle size](https://img.shields.io/bundlephobia/minzip/craters.js) | ||
![es modules](https://img.shields.io/badge/es-modules-lightblue) | ||
![es modules](https://img.shields.io/badge/es-modules-green) | ||
![](craters.gif) | ||
![craters.js logo](craters.gif) | ||
[craters.js documentation](https://swashvirus.github.io/documentation-craters.js/) | ||
@@ -14,4 +13,7 @@ | ||
[Demo sample game](https://swashvirus.github.io/craters.js/examples/sprites-demo/index.html) | ||
#### features ✨ | ||
- Changelog | ||
[Read changelog](CHANGELOG.md) | ||
- ES modules | ||
@@ -44,3 +46,4 @@ reduces bundle size | ||
```bash | ||
git clone https://github.com/swashvirus/craters.js.git | ||
git clone https://github.com/swashvirus/craters.js.git | ||
# import app/craters/craters.js | ||
``` | ||
@@ -50,3 +53,3 @@ npm install | ||
```bash | ||
npm install craters.js | ||
npm install craters.js | ||
``` | ||
@@ -57,10 +60,7 @@ | ||
'use strict'; | ||
// bundled versions can be found in the dist | ||
// import { game } from 'craters.js' // npm package | ||
import { game } from './craters/craters.js' | ||
import { Game } from 'craters.js' | ||
class mygame extends game { | ||
class mygame extends Game { | ||
intitiate() { | ||
super.intitiate() | ||
// now intitiate my game | ||
super.intitiate(); | ||
} | ||
@@ -77,2 +77,2 @@ | ||
``` | ||
Let's make craters a reality contribute even a missing colon | ||
Submit Issues and Contributions |
module.exports = { | ||
input: './app/craters/craters.js', | ||
output: { | ||
file: './dist/craters.js', | ||
format: 'esm' | ||
} | ||
}; | ||
input: './app/craters/craters.js', | ||
output: { | ||
file: './dist/craters.js', | ||
format: 'esm' | ||
} | ||
} |
@@ -1,12 +0,12 @@ | ||
const path = require('path'); | ||
const path = require('path') | ||
module.exports = { | ||
entry: { | ||
'./build/game': './app/game.js', | ||
'./dist/craters': './dist/craters' | ||
}, | ||
output: { | ||
path: path.resolve(__dirname, './'), | ||
filename: '[name].min.js' | ||
} | ||
}; | ||
entry: { | ||
'./build/game': './app/game.js', | ||
'./dist/craters': './dist/craters' | ||
}, | ||
output: { | ||
path: path.resolve(__dirname, './'), | ||
filename: '[name].min.js' | ||
} | ||
} |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
1320
0
1109716
37