UNPKG

artistic-engine

Version:

General purpose html5 canvas rendering game engine

1 lines 13.8 kB
var t={539:(t,e,s)=>{s.d(e,{Z:()=>n});var i=s(391);class r{constructor(){this.children=[],this.parent=null}get Root(){let t=this;for(;null!=t.Parent;)t=t.Parent;return t}get Parent(){return this.parent}get Children(){return this.children.slice()}attachChildren(t,e=1/0){let s=-1;if(Array.isArray(t)){const i=Math.max(0,Math.min(e,this.children.length));for(let e=0;e<t.length;e++)s=this.attachChildren(t[e],i+e)}else{for(let e=this;null!=e;e=e.parent)if(e===t)throw new Error("Loop of parent-child relationships detected.");null!=t.parent&&(t.parent.detachChildren(t),t.onDetach(t.parent)),this.children.push(t),t.parent=this,s=this.setChildIndex(t,e),t.onAttach(this)}return s}detachChildren(t){if(Array.isArray(t))for(const e of t)this.detachChildren(e);else{const e=this.children.indexOf(t);e>-1&&(this.children.splice(e,1),t.parent=null)}}getChildIndex(t){return this.children.indexOf(t)}setChildIndex(t,e){const s=this.getChildIndex(t);if(-1===s)throw new Error("I AM NOT YOUR FATHER");this.children.splice(s,1);const i=Math.max(0,Math.min(e,this.children.length));return this.children.splice(i,0,t),i}setParent(t=null){null==t?this.parent?.detachChildren(this):t.attachChildren(this)}onAttach(t){}onDetach(t){}}class n extends r{constructor(t){super(),this.dimension=new i.Z,this.position=new i.Z,t&&(this.dimension.X=t.W??0,this.dimension.Y=t.H??0,this.position.X=t.X??0,this.position.Y=t.Y??0)}get Position(){return this.position}get X(){return this.position.X}get Y(){return this.position.Y}get Dimension(){return this.dimension}get W(){return this.dimension.X}get H(){return this.dimension.Y}get Width(){return this.dimension.X}get Height(){return this.dimension.Y}get AbsoluteX(){return(this.parent?.AbsoluteX??0)+this.position.X}get AbsoluteY(){return(this.parent?.AbsoluteY??0)+this.position.Y}set Position(t){this.position=t}set X(t){this.position.X=t}set Y(t){this.position.Y=t}set Dimension(t){this.dimension=t}set W(t){this.dimension.X=t}set H(t){this.dimension.Y=t}set Width(t){this.dimension.X=t}set Height(t){this.dimension.Y=t}setPosition(t,e){this.X=t,this.Y=e}setSize(t,e){this.W=t,this.H=e}}},841:(t,e,s)=>{s.d(e,{qu:()=>o,jy:()=>i.Z});var i=s(727),r=s(283);class n extends r.Z{onDrawShape(t){t.beginPath();const e=this.Width/2,s=this.Height/2;t.ellipse(e,s,e,s,0,0,n.PI2),t.closePath()}onFillShape(t){t.fillStyle=this.fillStyle,t.fill()}}n.PI2=2*Math.PI;class h extends i.Z{onDraw(t,e){}onAttachEngine(t,e){}onDetachEngine(t,e){}}class o extends h{onDraw(t,e){}onAttachEngine(t,e){}onDetachEngine(t,e){}}},283:(t,e,s)=>{s.d(e,{Z:()=>r});var i=s(727);class r extends i.Z{constructor(){super(...arguments),this.fillStyle="#000"}get FillStyle(){return this.fillStyle}set FillStyle(t){this.fillStyle=t}onDraw(t,e){this.onDrawShape(t,e),this.onFillShape(t,e)}}},727:(t,e,s)=>{s.d(e,{Z:()=>n});var i=s(539),r=s(333);class n extends i.Z{constructor(){super(...arguments),this.contextMutator=!1,this.opacity=1,this.draw=(t,e)=>{t.translate(this.X,this.Y);const s=this.needsTransform,i=this.hasRegion,r=this.hasOpacity;if(this.contextMutator||(this.contextMutator=s||i||r),this.contextMutator){if(t.save(),s&&t.transform(this.transform.m11,this.transform.m21,this.transform.m12,this.transform.m22,this.transform.ox,this.transform.oy),i){this.beforeClip(t,e);const s=this.region();t.clip(s)}r&&(t.globalAlpha=this.opacity)}this.onDraw(t,e),this.contextMutator&&(t.restore(),this.afterRestore(t,e));for(const s of this.Children)s instanceof n&&s.draw(t,e);this.afterChildren(t,e),t.translate(-this.X,-this.Y)}}get Transform(){return this.transform||(this.transform=new r.Z),this.transform}get Region(){return this.region}get Opacity(){return this.opacity}get needsTransform(){return null!=this.transform&&!this.transform.isIdentity}get hasRegion(){return null!=this.region}get hasOpacity(){return 1!==this.opacity}set Transform(t){this.transform=t}set Region(t){this.region=t}set Opacity(t){this.opacity=Math.max(0,Math.min(t,1))}beforeClip(t,e){}afterRestore(t,e){}afterChildren(t,e){}}},333:(t,e,s)=>{s.d(e,{Z:()=>r});var i=s(391);class r{constructor(t=1,e=0,s=0,i=1,r=0,n=0){this.values=[t,e,s,i,r,n]}get m11(){return this.values[0]}get m21(){return this.values[1]}get m12(){return this.values[2]}get m22(){return this.values[3]}get ox(){return this.values[4]}get oy(){return this.values[5]}get Determinant(){return this.m11*this.m22-this.m12*this.m21}get isInvertible(){return 0!==this.Determinant}get isIdentity(){return 1===this.m11&&1===this.m22&&0===this.m12&&0===this.m21&&0===this.ox&&0===this.oy}set m11(t){this.values[0]=t}set m21(t){this.values[1]=t}set m12(t){this.values[2]=t}set m22(t){this.values[3]=t}set ox(t){this.values[4]=t}set oy(t){this.values[5]=t}static fromDOM(t){return new this(t.a,t.b,t.c,t.d,t.e,t.f)}translate(t,e){return this.linear(1,0,0,1,t,e)}scale(t,e){return void 0===e?this.scale(t,t):this.linear(t,0,0,e,0,0)}linear(t,e,s,i,r,n){return this.values=[t*this.m11+s*this.m21,e*this.m11+i*this.m21,t*this.m12+s*this.m22,e*this.m12+i*this.m22,this.m11*r+this.m12*n+this.ox,this.m21*r+this.m22*n+this.oy],this}rotate(t){const e=Math.sin(t),s=Math.cos(t);return this.linear(s,e,-e,s,0,0)}apply(t,e){let s;if(t instanceof i.Z)s=t,e=t.Y,t=t.X,s.X=this.m11*t+this.m12*e+this.ox,s.Y=this.m21*t+this.m22*e+this.oy;else{if(void 0===e)throw new Error("Transform#apply method requires two number parameters or one Vector2D.");s=new i.Z(this.m11*t+this.m12*e+this.ox,this.m21*t+this.m22*e+this.oy)}return s}multiply(t){return this.linear(...t.values)}invert(){const t=this.Determinant;if(0===t)throw new Error("This transform is not invertible");const e=this.m22/t,s=-1*this.m12/t,i=(this.m12*this.oy-this.m22*this.ox)/t,r=-1*this.m21/t,n=this.m11/t,h=-1*(this.m11*this.oy-this.m21*this.ox)/t;return this.m11=e,this.m12=s,this.ox=i,this.m21=r,this.m22=n,this.oy=h,this}skew(t=0,e=0){return this.linear(1,t,e,1,0,0),this}reset(){return this.m11=1,this.m22=1,this.m12=0,this.m21=0,this.ox=0,this.oy=0,this}copy(){return new r(...this.values)}copyTo(t){t.m11=this.m11,t.m21=this.m21,t.m12=this.m12,t.m22=this.m22,t.ox=this.ox,t.oy=this.oy}toDOM(){return new DOMMatrix([this.m11,this.m21,this.m12,this.m22,this.ox,this.oy])}}},70:(t,e,s)=>{s.d(e,{Z:()=>i});class i{constructor(...t){this.values=[...t],this.size=t.length}get Size(){return this.size}copy(t,e=!1){if(this.size>t.size&&!e)throw new Error(`Copying vector of size ${this.size} to ${t.size} will cause data loss.\nInclude force option if intended.`);return t.overwrite(this.values),t}isequal(t,e){if(this.size!==t.size)return!1;const s=this.values.slice(),i=t.values.slice();return s.every(((t,s)=>null==e?t==i[s]:t.toFixed(e)==i[s].toFixed(e)))}shift(t=1){t%=this.size;const e=this.values.slice(0,-t);return e.unshift(...this.values.slice(-t)),this.values=e,this}overwrite(t){let e=0;this.values=this.values.map((function(){return t[e++]||0}))}}},391:(t,e,s)=>{s.d(e,{Z:()=>r});var i=s(70);class r extends i.Z{constructor(t=0,e=0){super(t,e)}get X(){return this.values[0]}get Y(){return this.values[1]}set X(t){this.values[0]=t}set Y(t){this.values[1]=t}}}},e={};function s(i){var r=e[i];if(void 0!==r)return r.exports;var n=e[i]={exports:{}};return t[i](n,n.exports,s),n.exports}s.d=(t,e)=>{for(var i in e)s.o(e,i)&&!s.o(t,i)&&Object.defineProperty(t,i,{enumerable:!0,get:e[i]})},s.o=(t,e)=>Object.prototype.hasOwnProperty.call(t,e),s.r=t=>{"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(t,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(t,"__esModule",{value:!0})};var i={};(()=>{s.d(i,{gO:()=>h,eY:()=>m,D4:()=>o,JH:()=>a.Z,av:()=>d,wx:()=>n.Z,OW:()=>t});var t={};s.r(t),s.d(t,{Vector2D:()=>e.Z,Vector3D:()=>u,Vector4D:()=>c});var e=s(391),r=s(841),n=s(333);class h{constructor(){this.fonts=[],this.images=new Map,this.audios=new Map,this.samples=new Map,this.onLoad=()=>{}}get AudioContext(){return this.audioContext}set AudioContext(t){this.audioContext=t}addFont(t,e){return this.fonts.push(new FontFace(t,e)),this}addImage(t,e){return this.images.set(t,e),this}addAudio(t,e){return this.audios.set(t,e),this}addSample(t,e){return this.samples.set(t,e),this}async load(){const t=[],e=this.fonts.map((t=>t.load().then((()=>document.fonts.add(t))).catch((e=>{throw new Error("Failed to load font resource: "+t.family+"\nReason: "+e)}))));return this.fonts=[],t.push(...e),this.images.forEach(((e,s)=>{e instanceof Blob||"string"==typeof e&&t.push(fetch(e).then((async t=>{const e=await t.blob();this.images.set(s,e)})).catch((t=>{throw new Error("Failed to load image resource: "+s+"\nReason: "+t)})))})),this.audios.size+this.samples.size>0&&(this.audioContext instanceof AudioContext||(this.audioContext=new AudioContext),this.audios.forEach(((e,s)=>{e instanceof HTMLAudioElement||"string"==typeof e&&t.push(new Promise((t=>{const i=new Audio(e),r=()=>{i.removeEventListener("canplaythrough",r),this.audios.set(s,i),t(i)};i.addEventListener("canplaythrough",r)})).catch((t=>{throw new Error("Failed to load audio resource: "+s+"\nReason: "+t)})))})),this.samples.forEach(((e,s)=>{e instanceof AudioBuffer||"string"==typeof e&&t.push(fetch(e).then((async t=>{const e=await t.arrayBuffer(),i=await this.AudioContext.decodeAudioData(e);this.samples.set(s,i)})).catch((t=>{throw new Error("Failed to load audio resource: "+s+"\nReason: "+t)})))}))),Promise.all(t).then((()=>this.onLoad(this.images,this.audios)))}getImage(t){const e=this.images.get(t);if(e instanceof Blob)return e;throw new Error("Specified image is not loaded. Please check whether the name is correct or the load method has been called.")}getAudio(t){const e=this.audios.get(t);if(e instanceof HTMLAudioElement)return e;throw new Error("Specified audio is not loaded. Please check whether the name is correct or the load method has been called.")}getSample(t){const e=this.samples.get(t);if(e instanceof AudioBuffer)return e;throw new Error("Specified audio is not loaded. Please check whether the name is correct or the load method has been called.")}}class o{constructor(t,e){let s;if(this.previousTimestamp=0,this.animationId=-1,this.scene=new r.qu,this.camera=new n.Z,this.modifiers=[],this.assetLoader=new h,s="string"==typeof t?document.querySelector(t):t,null===s)throw new Error("Unable to identify canvas.");this.canvas=s,this.subReset=()=>{};const i=this.canvas.getContext("2d",e);if(null==i)throw new Error("Unable to initialize Canvas Context.");this.context=i}get Canvas(){return this.canvas}get Context(){return this.context}get Scene(){return this.scene}get Camera(){return this.camera}get AssetLoader(){return this.assetLoader}set Scene(t){const e=this.scene;t.setParent(null),e.onDetachEngine(this,t),this.scene=t,this.scene.onAttachEngine(this,e)}set Camera(t){this.camera=t}set AssetLoader(t){this.assetLoader=t}setSubResetFunction(t){this.subReset=t}resizeCanvas(t){t instanceof e.Z?(this.Canvas.width=t.X,this.Canvas.height=t.Y):(this.Canvas.width=t?.W??window.innerWidth,this.Canvas.height=t?.H??window.innerHeight)}start(){this.render(this.previousTimestamp)}stop(){cancelAnimationFrame(this.animationId)}registerModifier(t){t.register(),this.modifiers.push(t)}unregisterModifier(t){const e=this.modifiers.indexOf(t);this.modifiers.splice(e,1)}render(t){const e=t-this.previousTimestamp;this.previousTimestamp=t;const s=[];for(let t=0;t<this.modifiers.length;t++){const e=this.modifiers[t];e.tick(),e.Progress>=1&&s.push(t)}for(let t=s.length-1;t>=0;t--)this.modifiers.splice(s[t],1);this.context.reset(),this.subReset(this.context),this.camera.isIdentity||this.context.transform(this.camera.m11,this.camera.m21,this.camera.m12,this.camera.m22,this.camera.ox,this.camera.oy),this.scene?.draw(this.context,e),this.animationId=requestAnimationFrame(this.render.bind(this))}}var a=s(539),l=s(70);class u extends l.Z{constructor(t=0,e=0,s=0){super(t,e,s)}get X(){return this.values[0]}get Y(){return this.values[1]}get Z(){return this.values[2]}set X(t){this.values[0]=t}set Y(t){this.values[1]=t}set Z(t){this.values[2]=t}}class c extends l.Z{constructor(t=0,e=0,s=0,i=0){super(t,e,s,i)}get X(){return this.values[0]}get Y(){return this.values[1]}get Z(){return this.values[2]}get W(){return this.values[3]}set X(t){this.values[0]=t}set Y(t){this.values[1]=t}set Z(t){this.values[2]=t}set W(t){this.values[3]=t}}class m{constructor(t){this.image=t}get Image(){return this.image}set Options(t){this.options=t}async getImageBitmap(t,e=0,s=0,i=0){return Array.isArray(t)?this.getImageBitmap(...t):"object"==typeof t?this.getImageBitmap(t.X??0,t.Y??0,t.W??0,t.H??0):void 0===t?createImageBitmap(this.image,this.options):createImageBitmap(this.image,t,e,s,i,this.options)}async createImageBitmapList(t){const e=[];for(const s of t)e.push(this.getImageBitmap(s));return Promise.all(e)}}class d{constructor(t="sans-serif"){this.style="",this.variant="",this.weight="",this.size="20px",this.lineHeight="",this.family=t}get Family(){return this.family}get Style(){return this.style}get Variant(){return this.variant}get Weight(){return this.weight}get Size(){return this.size}get LineHeight(){return this.lineHeight}setStyle(t){return this.style=t,this}setVariant(t){return this.variant=t,this}setWeight(t){return this.weight=t,this}setSize(t){return this.size=t,this}setLineHeight(t){return this.lineHeight=t,this}toString(){if(this.size.length<1||this.family.length<1)throw new Error("Failed to generate font style with empty size/family.");let t="";return this.style.length>0&&(t+=this.style+" "),this.variant.length>0&&(t+=this.variant+" "),this.weight.length>0&&(t+=this.weight+" "),t+=this.size,this.lineHeight.length>0&&(t+="/"+this.lineHeight),t+=" "+this.family,t}}})();var r=i.gO,n=i.eY,h=i.D4,o=i.JH,a=i.av,l=i.wx,u=i.OW;export{r as AssetLoader,n as Bitmap,h as Engine,o as Entity,a as FontBuilder,l as Transform,u as Vector};