@phaserjs/phaser
Version:
1,952 lines (1,871 loc) • 506 kB
JavaScript
var __defProp = Object.defineProperty;
var __markAsModule = (target) => __defProp(target, "__esModule", { value: true });
var __export = (target, all) => {
__markAsModule(target);
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
// src/animation/index.ts
var animation_exports = {};
__export(animation_exports, {
AddFrame: () => AddFrame,
AddFrames: () => AddFrames,
Animation: () => Animation,
AnimationFrame: () => AnimationFrame,
CalculateDuration: () => CalculateDuration,
CreateAnimData: () => CreateAnimData,
CreateAnimationFromAtlas: () => CreateAnimationFromAtlas,
LinkFrames: () => LinkFrames,
Play: () => Play,
RemoveFrame: () => RemoveFrame,
RemoveFrames: () => RemoveFrames
});
// src/animation/CalculateDuration.ts
function CalculateDuration(animation, frameRate, duration) {
const totalFrames = animation.frames.size;
if (!Number.isFinite(duration) && !Number.isFinite(frameRate)) {
animation.frameRate = 24;
animation.duration = 24 / totalFrames * 1e3;
} else if (duration && !Number.isFinite(frameRate)) {
animation.duration = duration;
animation.frameRate = totalFrames / (duration / 1e3);
} else {
animation.frameRate = frameRate;
animation.duration = totalFrames / frameRate * 1e3;
}
animation.msPerFrame = 1e3 / animation.frameRate;
return animation;
}
// src/animation/LinkFrames.ts
function LinkFrames(animation) {
const totalFrames = animation.frames.size;
if (totalFrames === 0) {
return animation;
}
let i = 0;
const framePercent = 1 / totalFrames;
let firstFrame;
let prevFrame2;
for (const frame2 of animation.frames.values()) {
if (!prevFrame2) {
frame2.isFirst = true;
animation.firstFrame = frame2;
firstFrame = frame2;
} else {
prevFrame2.nextFrame = frame2;
frame2.prevFrame = prevFrame2;
}
prevFrame2 = frame2;
i++;
frame2.progress = framePercent * i;
if (i === totalFrames) {
frame2.isLast = true;
frame2.nextFrame = firstFrame;
firstFrame.prevFrame = frame2;
}
}
return animation;
}
// src/animation/AddFrame.ts
function AddFrame(animation, frame2) {
animation.frames.add(frame2);
CalculateDuration(animation, animation.frameRate);
return LinkFrames(animation);
}
// src/animation/AddFrames.ts
function AddFrames(animation, ...frames) {
frames.forEach((frame2) => {
animation.frames.add(frame2);
});
CalculateDuration(animation, animation.frameRate);
return LinkFrames(animation);
}
// src/animation/Animation.ts
var Animation = class {
key;
frames;
firstFrame;
msPerFrame;
frameRate;
duration;
skipMissedFrames;
delay;
hold;
repeat;
repeatDelay;
yoyo;
showOnStart;
hideOnComplete;
paused;
constructor(config) {
const {
key,
frames = [],
frameRate = null,
duration = null,
skipMissedFrames = true,
delay = 0,
repeat = 0,
repeatDelay = 0,
yoyo = false,
showOnStart = false,
hideOnComplete = false,
paused = false
} = config;
this.key = key;
this.skipMissedFrames = skipMissedFrames;
this.delay = delay;
this.repeat = repeat;
this.repeatDelay = repeatDelay;
this.yoyo = yoyo;
this.showOnStart = showOnStart;
this.hideOnComplete = hideOnComplete;
this.paused = paused;
this.frames = new Set(frames);
CalculateDuration(this, frameRate, duration);
LinkFrames(this);
}
getTotalFrames() {
return this.frames.size;
}
destroy() {
this.frames.clear();
}
};
// src/animation/AnimationFrame.ts
var AnimationFrame = class {
texture;
frame;
isFirst = false;
isLast = false;
isKeyFrame = false;
nextFrame;
prevFrame;
duration = 0;
progress = 0;
constructor(texture, frame2) {
this.texture = texture;
this.frame = frame2;
}
destroy() {
this.texture = null;
this.frame = null;
this.nextFrame = null;
this.prevFrame = null;
}
};
// src/animation/CreateAnimData.ts
function CreateAnimData(currentAnim = "", frameRate = 0, duration = 0, delay = 0, repeat = 0, repeatDelay = 0, yoyo = false, hold = 0, showOnStart = false, hideOnComplete = false) {
return {
currentAnim,
frameRate,
duration,
delay,
repeat,
repeatDelay,
yoyo,
hold,
showOnStart,
hideOnComplete,
stopAfter: 0,
startFrame: 0,
timeScale: 1,
onStart: null,
onRepeat: null,
onComplete: null,
nextFrameTime: 0,
repeatCount: 0,
isPlaying: false,
forceRestart: false,
pendingStart: false,
playingForward: true
};
}
// src/textures/GetFramesInRange.ts
function GetFramesInRange(texture, config) {
const {
prefix = "",
start = 0,
zeroPad = 0,
suffix = ""
} = config;
let end = config.end;
const output = [];
const diff2 = start < end ? 1 : -1;
end += diff2;
for (let i = start; i !== end; i += diff2) {
const frameKey = prefix + i.toString().padStart(zeroPad, "0") + suffix;
output.push(texture.getFrame(frameKey));
}
return output;
}
// src/textures/TextureManagerInstance.ts
var instance;
var TextureManagerInstance = {
get: () => {
return instance;
},
set: (manager) => {
if (instance) {
throw new Error("Cannot instantiate TextureManager more than once");
}
instance = manager;
}
};
// src/textures/GetTexture.ts
function GetTexture(key) {
return TextureManagerInstance.get().get(key);
}
// src/renderer/BindingQueue.ts
var queue = [];
var BindingQueue = {
add: (texture, glConfig) => {
queue.push({ texture, glConfig });
},
get: () => {
return queue;
},
clear: () => {
queue.length = 0;
}
};
// src/textures/UpdateFrameUVs.ts
function UpdateFrameUVs(frame2) {
const { x, y, width, height } = frame2;
const baseTextureWidth = frame2.texture.width;
const baseTextureHeight = frame2.texture.height;
frame2.u0 = x / baseTextureWidth;
frame2.v0 = y / baseTextureHeight;
frame2.u1 = (x + width) / baseTextureWidth;
frame2.v1 = (y + height) / baseTextureHeight;
return frame2;
}
// src/textures/Frame.ts
var Frame = class {
texture;
key;
x;
y;
width;
height;
trimmed = false;
sourceSizeWidth;
sourceSizeHeight;
spriteSourceSizeX;
spriteSourceSizeY;
spriteSourceSizeWidth;
spriteSourceSizeHeight;
pivot;
u0;
v0;
u1;
v1;
constructor(texture, key, x, y, width, height) {
this.texture = texture;
this.key = key;
this.x = x;
this.y = y;
this.width = width;
this.height = height;
this.sourceSizeWidth = width;
this.sourceSizeHeight = height;
UpdateFrameUVs(this);
}
destroy() {
this.texture = null;
}
};
// src/textures/SetFrameSize.ts
function SetFrameSize(frame2, width, height) {
frame2.width = width;
frame2.height = height;
frame2.sourceSizeWidth = width;
frame2.sourceSizeHeight = height;
return UpdateFrameUVs(frame2);
}
// src/textures/Texture.ts
var Texture = class {
key = "";
locked = true;
width;
height;
image;
binding;
firstFrame;
frames;
data;
constructor(image, width, height, glConfig) {
if (image) {
width = image.width;
height = image.height;
}
this.image = image;
this.width = width;
this.height = height;
this.frames = new Map();
this.data = {};
this.addFrame("__BASE", 0, 0, width, height);
BindingQueue.add(this, glConfig);
}
addFrame(key, x, y, width, height) {
if (this.frames.has(key)) {
return null;
}
const frame2 = new Frame(this, key, x, y, width, height);
this.frames.set(key, frame2);
if (!this.firstFrame || this.firstFrame.key === "__BASE") {
this.firstFrame = frame2;
}
return frame2;
}
getFrame(key) {
if (!key) {
return this.firstFrame;
}
if (key instanceof Frame) {
key = key.key;
}
let frame2 = this.frames.get(key);
if (!frame2) {
console.warn(`Frame missing: ${key}`);
frame2 = this.firstFrame;
}
return frame2;
}
setSize(width, height) {
this.width = width;
this.height = height;
const frame2 = this.frames.get("__BASE");
SetFrameSize(frame2, width, height);
}
update(image, glConfig) {
this.image = image;
this.setSize(image.width, image.height);
BindingQueue.add(this, glConfig);
}
destroy() {
if (this.binding) {
this.binding.destroy();
}
this.frames.clear();
this.binding = null;
this.data = null;
this.image = null;
this.firstFrame = null;
}
};
// src/animation/CreateAnimationFromAtlas.ts
function CreateAnimationFromAtlas(config) {
const texture = config.texture instanceof Texture ? config.texture : GetTexture(config.texture);
const frames = [];
GetFramesInRange(texture, config).forEach((frame2) => {
frames.push(new AnimationFrame(texture, frame2));
});
return new Animation({ frames, ...config });
}
// src/animation/Play.ts
function Play(animation, config = {}, ...sprites) {
const data = CreateAnimData(animation.key, animation.frameRate, animation.duration, animation.delay, animation.repeat, animation.repeatDelay, animation.yoyo, animation.hold, animation.showOnStart, animation.hideOnComplete);
Object.assign(data, config);
data.nextFrameTime = animation.msPerFrame + data.delay;
sprites.forEach((sprite) => {
if (!sprite || !sprite.animData) {
return;
}
const spriteAnimData = sprite.animData;
if (spriteAnimData.isPlaying) {
if (sprite.currentAnimation !== animation) {
spriteAnimData.isPlaying = false;
if (spriteAnimData.onComplete) {
spriteAnimData.onComplete(sprite, sprite.currentAnimation);
}
} else if (!data.forceRestart) {
return;
}
}
Object.assign(spriteAnimData, data);
sprite.currentAnimation = animation;
sprite.currentFrame = animation.firstFrame;
sprite.play();
});
return sprites;
}
// src/animation/RemoveFrame.ts
function RemoveFrame(animation, frame2) {
animation.frames.delete(frame2);
CalculateDuration(animation, animation.frameRate);
return LinkFrames(animation);
}
// src/animation/RemoveFrames.ts
function RemoveFrames(animation, ...frames) {
frames.forEach((frame2) => {
animation.frames.delete(frame2);
});
CalculateDuration(animation, animation.frameRate);
return LinkFrames(animation);
}
// src/camera/index.ts
var camera_exports = {};
__export(camera_exports, {
StaticCamera: () => StaticCamera,
WorldCamera: () => WorldCamera
});
// node_modules/bitecs/dist/index.mjs
var TYPES_ENUM = {
i8: "i8",
ui8: "ui8",
ui8c: "ui8c",
i16: "i16",
ui16: "ui16",
i32: "i32",
ui32: "ui32",
f32: "f32",
f64: "f64",
eid: "eid"
};
var TYPES_NAMES = {
i8: "Int8",
ui8: "Uint8",
ui8c: "Uint8Clamped",
i16: "Int16",
ui16: "Uint16",
i32: "Int32",
ui32: "Uint32",
eid: "Uint32",
f32: "Float32",
f64: "Float64"
};
var TYPES = {
i8: Int8Array,
ui8: Uint8Array,
ui8c: Uint8ClampedArray,
i16: Int16Array,
ui16: Uint16Array,
i32: Int32Array,
ui32: Uint32Array,
f32: Float32Array,
f64: Float64Array,
eid: Uint32Array
};
var UNSIGNED_MAX = {
uint8: 2 ** 8,
uint16: 2 ** 16,
uint32: 2 ** 32
};
var roundToMultiple = (mul) => (x) => Math.ceil(x / mul) * mul;
var roundToMultiple4 = roundToMultiple(4);
var $storeRef = Symbol("storeRef");
var $storeSize = Symbol("storeSize");
var $storeMaps = Symbol("storeMaps");
var $storeFlattened = Symbol("storeFlattened");
var $storeBase = Symbol("storeBase");
var $storeType = Symbol("storeType");
var $storeArrayCounts = Symbol("storeArrayCount");
var $storeSubarrays = Symbol("storeSubarrays");
var $subarrayCursors = Symbol("subarrayCursors");
var $subarray = Symbol("subarray");
var $subarrayFrom = Symbol("subarrayFrom");
var $subarrayTo = Symbol("subarrayTo");
var $parentArray = Symbol("subStore");
var $tagStore = Symbol("tagStore");
var $queryShadow = Symbol("queryShadow");
var $serializeShadow = Symbol("serializeShadow");
var $indexType = Symbol("indexType");
var $indexBytes = Symbol("indexBytes");
var $isEidType = Symbol("isEidType");
var stores = {};
var resize = (ta, size) => {
const newBuffer = new ArrayBuffer(size * ta.BYTES_PER_ELEMENT);
const newTa = new ta.constructor(newBuffer);
newTa.set(ta, 0);
return newTa;
};
var createShadow = (store, key) => {
if (!ArrayBuffer.isView(store)) {
const shadowStore = store[$parentArray].slice(0).fill(0);
store[key] = store.map((_, eid) => {
const from = store[eid][$subarrayFrom];
const to = store[eid][$subarrayTo];
return shadowStore.subarray(from, to);
});
} else {
store[key] = store.slice(0).fill(0);
}
};
var resizeSubarray = (metadata, store, size) => {
const cursors = metadata[$subarrayCursors];
let type = store[$storeType];
const length = store[0].length;
const indexType = length <= UNSIGNED_MAX.uint8 ? "ui8" : length <= UNSIGNED_MAX.uint16 ? "ui16" : "ui32";
const arrayCount = metadata[$storeArrayCounts][type];
const summedLength = Array(arrayCount).fill(0).reduce((a, p) => a + length, 0);
const array = new TYPES[type](roundToMultiple4(summedLength * size));
array.set(metadata[$storeSubarrays][type]);
metadata[$storeSubarrays][type] = array;
array[$indexType] = TYPES_NAMES[indexType];
array[$indexBytes] = TYPES[indexType].BYTES_PER_ELEMENT;
const start = cursors[type];
let end = 0;
for (let eid = 0; eid < size; eid++) {
const from = cursors[type] + eid * length;
const to = from + length;
store[eid] = metadata[$storeSubarrays][type].subarray(from, to);
store[eid][$subarrayFrom] = from;
store[eid][$subarrayTo] = to;
store[eid][$subarray] = true;
store[eid][$indexType] = TYPES_NAMES[indexType];
store[eid][$indexBytes] = TYPES[indexType].BYTES_PER_ELEMENT;
end = to;
}
cursors[type] = end;
store[$parentArray] = metadata[$storeSubarrays][type].subarray(start, end);
};
var resizeRecursive = (metadata, store, size) => {
Object.keys(store).forEach((key) => {
const ta = store[key];
if (Array.isArray(ta)) {
resizeSubarray(metadata, ta, size);
store[$storeFlattened].push(ta);
} else if (ArrayBuffer.isView(ta)) {
store[key] = resize(ta, size);
store[$storeFlattened].push(store[key]);
} else if (typeof ta === "object") {
resizeRecursive(metadata, store[key], size);
}
});
};
var resizeStore = (store, size) => {
if (store[$tagStore])
return;
store[$storeSize] = size;
store[$storeFlattened].length = 0;
Object.keys(store[$subarrayCursors]).forEach((k) => {
store[$subarrayCursors][k] = 0;
});
resizeRecursive(store, store, size);
};
var resetStoreFor = (store, eid) => {
if (store[$storeFlattened]) {
store[$storeFlattened].forEach((ta) => {
if (ArrayBuffer.isView(ta))
ta[eid] = 0;
else
ta[eid].fill(0);
});
}
};
var createTypeStore = (type, length) => {
const totalBytes = length * TYPES[type].BYTES_PER_ELEMENT;
const buffer = new ArrayBuffer(totalBytes);
const store = new TYPES[type](buffer);
store[$isEidType] = type === TYPES_ENUM.eid;
return store;
};
var createArrayStore = (metadata, type, length) => {
const size = metadata[$storeSize];
const store = Array(size).fill(0);
store[$storeType] = type;
store[$isEidType] = type === TYPES_ENUM.eid;
const cursors = metadata[$subarrayCursors];
const indexType = length < UNSIGNED_MAX.uint8 ? "ui8" : length < UNSIGNED_MAX.uint16 ? "ui16" : "ui32";
if (!length)
throw new Error("bitECS - Must define component array length");
if (!TYPES[type])
throw new Error(`bitECS - Invalid component array property type ${type}`);
if (!metadata[$storeSubarrays][type]) {
const arrayCount = metadata[$storeArrayCounts][type];
const summedLength = Array(arrayCount).fill(0).reduce((a, p) => a + length, 0);
const array = new TYPES[type](roundToMultiple4(summedLength * size));
metadata[$storeSubarrays][type] = array;
array[$indexType] = TYPES_NAMES[indexType];
array[$indexBytes] = TYPES[indexType].BYTES_PER_ELEMENT;
}
const start = cursors[type];
let end = 0;
for (let eid = 0; eid < size; eid++) {
const from = cursors[type] + eid * length;
const to = from + length;
store[eid] = metadata[$storeSubarrays][type].subarray(from, to);
store[eid][$subarrayFrom] = from;
store[eid][$subarrayTo] = to;
store[eid][$subarray] = true;
store[eid][$indexType] = TYPES_NAMES[indexType];
store[eid][$indexBytes] = TYPES[indexType].BYTES_PER_ELEMENT;
end = to;
}
cursors[type] = end;
store[$parentArray] = metadata[$storeSubarrays][type].subarray(start, end);
return store;
};
var isArrayType = (x) => Array.isArray(x) && typeof x[0] === "string" && typeof x[1] === "number";
var createStore = (schema, size) => {
const $store = Symbol("store");
if (!schema || !Object.keys(schema).length) {
stores[$store] = {
[$storeSize]: size,
[$tagStore]: true,
[$storeBase]: () => stores[$store]
};
return stores[$store];
}
schema = JSON.parse(JSON.stringify(schema));
const arrayCounts = {};
const collectArrayCounts = (s) => {
const keys = Object.keys(s);
for (const k of keys) {
if (isArrayType(s[k])) {
if (!arrayCounts[s[k][0]])
arrayCounts[s[k][0]] = 0;
arrayCounts[s[k][0]]++;
} else if (s[k] instanceof Object) {
collectArrayCounts(s[k]);
}
}
};
collectArrayCounts(schema);
const metadata = {
[$storeSize]: size,
[$storeMaps]: {},
[$storeSubarrays]: {},
[$storeRef]: $store,
[$subarrayCursors]: Object.keys(TYPES).reduce((a, type) => ({ ...a, [type]: 0 }), {}),
[$storeFlattened]: [],
[$storeArrayCounts]: arrayCounts
};
if (schema instanceof Object && Object.keys(schema).length) {
const recursiveTransform = (a, k) => {
if (typeof a[k] === "string") {
a[k] = createTypeStore(a[k], size);
a[k][$storeBase] = () => stores[$store];
metadata[$storeFlattened].push(a[k]);
} else if (isArrayType(a[k])) {
const [type, length] = a[k];
a[k] = createArrayStore(metadata, type, length);
a[k][$storeBase] = () => stores[$store];
metadata[$storeFlattened].push(a[k]);
} else if (a[k] instanceof Object) {
a[k] = Object.keys(a[k]).reduce(recursiveTransform, a[k]);
}
return a;
};
stores[$store] = Object.assign(Object.keys(schema).reduce(recursiveTransform, schema), metadata);
stores[$store][$storeBase] = () => stores[$store];
return stores[$store];
}
};
var SparseSet = () => {
const dense = [];
const sparse = [];
dense.sort = function(comparator) {
const result = Array.prototype.sort.call(this, comparator);
for (let i = 0; i < dense.length; i++) {
sparse[dense[i]] = i;
}
return result;
};
const has = (val) => dense[sparse[val]] === val;
const add = (val) => {
if (has(val))
return;
sparse[val] = dense.push(val) - 1;
};
const remove = (val) => {
if (!has(val))
return;
const index = sparse[val];
const swapped = dense.pop();
if (swapped !== val) {
dense[index] = swapped;
sparse[swapped] = index;
}
};
return {
add,
remove,
has,
sparse,
dense
};
};
var newEntities = new Map();
var $entityMasks = Symbol("entityMasks");
var $entityComponents = Symbol("entityComponents");
var $entitySparseSet = Symbol("entitySparseSet");
var $entityArray = Symbol("entityArray");
var $entityIndices = Symbol("entityIndices");
var $removedEntities = Symbol("removedEntities");
var defaultSize = 1e5;
var globalEntityCursor = 0;
var globalSize = defaultSize;
var getGlobalSize = () => globalSize;
var removed = [];
var getEntityCursor = () => globalEntityCursor;
var eidToWorld = new Map();
var addEntity = (world2) => {
if (globalEntityCursor + 1 >= defaultSize) {
console.error(`bitECS - max entities of ${defaultSize} reached, increase with setDefaultSize function.`);
return;
}
const eid = removed.length > 0 ? removed.shift() : globalEntityCursor++;
world2[$entitySparseSet].add(eid);
eidToWorld.set(eid, world2);
world2[$notQueries].forEach((q) => {
const match = queryCheckEntity(world2, q, eid);
if (match)
queryAddEntity(q, eid);
});
world2[$entityComponents].set(eid, new Set());
return eid;
};
var removeEntity = (world2, eid) => {
if (!world2[$entitySparseSet].has(eid))
return;
world2[$queries].forEach((q) => {
queryRemoveEntity(world2, q, eid);
});
removed.push(eid);
world2[$entitySparseSet].remove(eid);
world2[$entityComponents].delete(eid);
world2[$localEntities].delete(world2[$localEntityLookup].get(eid));
world2[$localEntityLookup].delete(eid);
for (let i = 0; i < world2[$entityMasks].length; i++)
world2[$entityMasks][i][eid] = 0;
};
function Any(...comps) {
return function QueryAny() {
return comps;
};
}
function All(...comps) {
return function QueryAll() {
return comps;
};
}
function None(...comps) {
return function QueryNone() {
return comps;
};
}
var $queries = Symbol("queries");
var $notQueries = Symbol("notQueries");
var $queryAny = Symbol("queryAny");
var $queryAll = Symbol("queryAll");
var $queryNone = Symbol("queryNone");
var $queryMap = Symbol("queryMap");
var $dirtyQueries = Symbol("$dirtyQueries");
var $queryComponents = Symbol("queryComponents");
var $enterQuery = Symbol("enterQuery");
var $exitQuery = Symbol("exitQuery");
var registerQuery = (world2, query) => {
const components2 = [];
const notComponents = [];
const changedComponents = [];
query[$queryComponents].forEach((c) => {
if (typeof c === "function") {
const [comp, mod] = c();
if (!world2[$componentMap].has(comp))
registerComponent(world2, comp);
if (mod === "not") {
notComponents.push(comp);
}
if (mod === "changed") {
changedComponents.push(comp);
components2.push(comp);
}
} else {
if (!world2[$componentMap].has(c))
registerComponent(world2, c);
components2.push(c);
}
});
const mapComponents = (c) => world2[$componentMap].get(c);
const allComponents = components2.concat(notComponents).map(mapComponents);
const sparseSet = SparseSet();
const archetypes = [];
const changed = [];
const toRemove = SparseSet();
const entered = [];
const exited = [];
const generations = allComponents.map((c) => c.generationId).reduce((a, v) => {
if (a.includes(v))
return a;
a.push(v);
return a;
}, []);
const reduceBitflags = (a, c) => {
if (!a[c.generationId])
a[c.generationId] = 0;
a[c.generationId] |= c.bitflag;
return a;
};
const masks = components2.map(mapComponents).reduce(reduceBitflags, {});
const notMasks = notComponents.map(mapComponents).reduce(reduceBitflags, {});
const hasMasks = allComponents.reduce(reduceBitflags, {});
const flatProps = components2.filter((c) => !c[$tagStore]).map((c) => Object.getOwnPropertySymbols(c).includes($storeFlattened) ? c[$storeFlattened] : [c]).reduce((a, v) => a.concat(v), []);
const shadows = flatProps.map((prop) => {
const $ = Symbol();
createShadow(prop, $);
return prop[$];
}, []);
const q = Object.assign(sparseSet, {
archetypes,
changed,
components: components2,
notComponents,
changedComponents,
allComponents,
masks,
notMasks,
hasMasks,
generations,
flatProps,
toRemove,
entered,
exited,
shadows
});
world2[$queryMap].set(query, q);
world2[$queries].add(q);
allComponents.forEach((c) => {
c.queries.add(q);
});
if (notComponents.length)
world2[$notQueries].add(q);
for (let eid = 0; eid < getEntityCursor(); eid++) {
if (!world2[$entitySparseSet].has(eid))
continue;
const match = queryCheckEntity(world2, q, eid);
if (match)
queryAddEntity(q, eid);
}
};
var diff = (q, clearDiff) => {
if (clearDiff)
q.changed = [];
const { flatProps, shadows } = q;
for (let i = 0; i < q.dense.length; i++) {
const eid = q.dense[i];
let dirty = false;
for (let pid = 0; pid < flatProps.length; pid++) {
const prop = flatProps[pid];
const shadow = shadows[pid];
if (ArrayBuffer.isView(prop[eid])) {
for (let i2 = 0; i2 < prop[eid].length; i2++) {
if (prop[eid][i2] !== shadow[eid][i2]) {
dirty = true;
shadow[eid][i2] = prop[eid][i2];
break;
}
}
} else {
if (prop[eid] !== shadow[eid]) {
dirty = true;
shadow[eid] = prop[eid];
}
}
}
if (dirty)
q.changed.push(eid);
}
return q.changed;
};
var flatten = (a, v) => a.concat(v);
var aggregateComponentsFor = (mod) => (x) => x.filter((f) => f.name === mod().constructor.name).reduce(flatten);
var getAnyComponents = aggregateComponentsFor(Any);
var getAllComponents = aggregateComponentsFor(All);
var getNoneComponents = aggregateComponentsFor(None);
var defineQuery = (...args) => {
let components2;
let any, all, none;
if (Array.isArray(args[0])) {
components2 = args[0];
} else {
any = getAnyComponents(args);
all = getAllComponents(args);
none = getNoneComponents(args);
}
if (components2 === void 0 || components2[$componentMap] !== void 0) {
return (world2) => world2 ? world2[$entityArray] : components2[$entityArray];
}
const query = function(world2, clearDiff = true) {
if (!world2[$queryMap].has(query))
registerQuery(world2, query);
const q = world2[$queryMap].get(query);
commitRemovals(world2);
if (q.changedComponents.length)
return diff(q, clearDiff);
return q.dense;
};
query[$queryComponents] = components2;
query[$queryAny] = any;
query[$queryAll] = all;
query[$queryNone] = none;
return query;
};
var queryCheckEntity = (world2, q, eid) => {
const { masks, notMasks, generations } = q;
let or = 0;
for (let i = 0; i < generations.length; i++) {
const generationId = generations[i];
const qMask = masks[generationId];
const qNotMask = notMasks[generationId];
const eMask = world2[$entityMasks][generationId][eid];
if (qNotMask && (eMask & qNotMask) !== 0) {
return false;
}
if (qMask && (eMask & qMask) !== qMask) {
return false;
}
}
return true;
};
var queryAddEntity = (q, eid) => {
if (q.has(eid))
return;
q.add(eid);
q.entered.push(eid);
};
var queryCommitRemovals = (q) => {
for (let i = q.toRemove.dense.length - 1; i >= 0; i--) {
const eid = q.toRemove.dense[i];
q.toRemove.remove(eid);
q.remove(eid);
}
};
var commitRemovals = (world2) => {
if (!world2[$dirtyQueries].size)
return;
world2[$dirtyQueries].forEach(queryCommitRemovals);
world2[$dirtyQueries].clear();
};
var queryRemoveEntity = (world2, q, eid) => {
if (!q.has(eid) || q.toRemove.has(eid))
return;
q.toRemove.add(eid);
world2[$dirtyQueries].add(q);
q.exited.push(eid);
};
var $componentMap = Symbol("componentMap");
var components = [];
var defineComponent = (schema) => {
const component = createStore(schema, getGlobalSize());
if (schema && Object.keys(schema).length)
components.push(component);
return component;
};
var incrementBitflag = (world2) => {
world2[$bitflag] *= 2;
if (world2[$bitflag] >= 2 ** 31) {
world2[$bitflag] = 1;
world2[$entityMasks].push(new Uint32Array(world2[$size]));
}
};
var registerComponent = (world2, component) => {
if (!component)
throw new Error(`bitECS - Cannot register null or undefined component`);
const queries = new Set();
const notQueries = new Set();
const changedQueries = new Set();
world2[$queries].forEach((q) => {
if (q.allComponents.includes(component)) {
queries.add(q);
}
});
world2[$componentMap].set(component, {
generationId: world2[$entityMasks].length - 1,
bitflag: world2[$bitflag],
store: component,
queries,
notQueries,
changedQueries
});
if (component[$storeSize] < getGlobalSize()) {
resizeStore(component, getGlobalSize());
}
incrementBitflag(world2);
};
var hasComponent = (world2, component, eid) => {
const registeredComponent = world2[$componentMap].get(component);
if (!registeredComponent)
return;
const { generationId, bitflag } = registeredComponent;
const mask = world2[$entityMasks][generationId][eid];
return (mask & bitflag) === bitflag;
};
var addComponent = (world2, component, eid, reset = true) => {
if (eid === void 0)
throw new Error("bitECS - entity is undefined.");
if (!world2[$entitySparseSet].has(eid))
throw new Error("bitECS - entity does not exist in the world.");
if (!world2[$componentMap].has(component))
registerComponent(world2, component);
if (hasComponent(world2, component, eid))
return;
const c = world2[$componentMap].get(component);
const { generationId, bitflag, queries, notQueries } = c;
world2[$entityMasks][generationId][eid] |= bitflag;
queries.forEach((q) => {
if (q.toRemove.has(eid))
q.toRemove.remove(eid);
const match = queryCheckEntity(world2, q, eid);
if (match)
queryAddEntity(q, eid);
if (!match)
queryRemoveEntity(world2, q, eid);
});
world2[$entityComponents].get(eid).add(component);
if (reset)
resetStoreFor(component, eid);
};
var removeComponent = (world2, component, eid, reset = false) => {
const c = world2[$componentMap].get(component);
const { generationId, bitflag, queries, notQueries } = c;
if (!(world2[$entityMasks][generationId][eid] & bitflag))
return;
world2[$entityMasks][generationId][eid] &= ~bitflag;
queries.forEach((q) => {
if (q.toRemove.has(eid))
q.toRemove.remove(eid);
const match = queryCheckEntity(world2, q, eid);
if (match)
queryAddEntity(q, eid);
if (!match)
queryRemoveEntity(world2, q, eid);
});
world2[$entityComponents].get(eid).delete(component);
if (reset)
resetStoreFor(component, eid);
};
var $size = Symbol("size");
var $resizeThreshold = Symbol("resizeThreshold");
var $bitflag = Symbol("bitflag");
var $archetypes = Symbol("archetypes");
var $localEntities = Symbol("localEntities");
var $localEntityLookup = Symbol("localEntityLookp");
var worlds = [];
var createWorld = (obj = {}) => {
const world2 = obj;
resetWorld(world2);
worlds.push(world2);
return world2;
};
var resetWorld = (world2) => {
const size = getGlobalSize();
world2[$size] = size;
if (world2[$entityArray])
world2[$entityArray].forEach((eid) => removeEntity(world2, eid));
world2[$entityMasks] = [new Uint32Array(size)];
world2[$entityComponents] = new Map();
world2[$archetypes] = [];
world2[$entitySparseSet] = SparseSet();
world2[$entityArray] = world2[$entitySparseSet].dense;
world2[$bitflag] = 1;
world2[$componentMap] = new Map();
world2[$queryMap] = new Map();
world2[$queries] = new Set();
world2[$notQueries] = new Set();
world2[$dirtyQueries] = new Set();
world2[$localEntities] = new Map();
world2[$localEntityLookup] = new Map();
return world2;
};
var Types = TYPES_ENUM;
// src/components/transform/Transform2DComponent.ts
var TRANSFORM = {
IS_ROOT: 0,
X: 1,
Y: 2,
ROTATION: 3,
SCALE_X: 4,
SCALE_Y: 5,
SKEW_X: 6,
SKEW_Y: 7,
AXIS_ALIGNED: 8,
FRAME_X1: 9,
FRAME_Y1: 10,
FRAME_X2: 11,
FRAME_Y2: 12,
LOCAL_A: 13,
LOCAL_B: 14,
LOCAL_C: 15,
LOCAL_D: 16,
LOCAL_TX: 17,
LOCAL_TY: 18,
BOUNDS_X1: 19,
BOUNDS_Y1: 20,
BOUNDS_X2: 21,
BOUNDS_Y2: 22,
ORIGIN_X: 23,
ORIGIN_Y: 24,
WORLD_A: 25,
WORLD_B: 26,
WORLD_C: 27,
WORLD_D: 28,
WORLD_TX: 29,
WORLD_TY: 30,
FRAME_WIDTH: 31,
FRAME_HEIGHT: 32,
IN_VIEW: 33,
UPDATED: 34
};
var Transform2DComponent = defineComponent({
data: [Types.f32, 35]
});
// src/GameObjectWorld.ts
var world = createWorld();
var GameObjectWorld = world;
// src/components/transform/AddTransform2DComponent.ts
function AddTransform2DComponent(id) {
addComponent(GameObjectWorld, Transform2DComponent, id);
const data = Transform2DComponent.data[id];
data[TRANSFORM.SCALE_X] = 1;
data[TRANSFORM.SCALE_Y] = 1;
data[TRANSFORM.AXIS_ALIGNED] = 1;
}
// src/utils/NOOP.ts
function NOOP() {
}
// src/math/mat4/Matrix4.ts
var Matrix4 = class {
data;
onChange;
constructor(src) {
const data = new Float32Array(16);
this.data = data;
this.onChange = NOOP;
if (src) {
if (Array.isArray(src)) {
this.fromArray(src);
} else {
this.fromArray(src.data);
}
} else {
data[0] = 1;
data[5] = 1;
data[10] = 1;
data[15] = 1;
}
}
set(m00, m01, m02, m03, m10, m11, m12, m13, m20, m21, m22, m23, m30, m31, m32, m33) {
this.data.set([
m00,
m01,
m02,
m03,
m10,
m11,
m12,
m13,
m20,
m21,
m22,
m23,
m30,
m31,
m32,
m33
]);
this.onChange(this);
return this;
}
toArray(dst = [], index = 0) {
const data = this.data;
for (let i = 0; i < 16; i++) {
dst[index + i] = data[i];
}
return dst;
}
fromArray(src, index = 0) {
const data = this.data;
for (let i = 0; i < 16; i++) {
data[i] = src[index + i];
}
this.onChange(this);
return this;
}
toString() {
return "[ mat4=" + this.data.join(", ") + " ]";
}
destroy() {
this.onChange = NOOP;
this.data = null;
}
};
// src/components/transform/SetBounds.ts
function SetBounds(id, x, y, right, bottom) {
const data = Transform2DComponent.data[id];
data[TRANSFORM.BOUNDS_X1] = x;
data[TRANSFORM.BOUNDS_Y1] = y;
data[TRANSFORM.BOUNDS_X2] = right;
data[TRANSFORM.BOUNDS_Y2] = bottom;
}
// src/components/dirty/DirtyComponent.ts
var DIRTY = {
TRANSFORM: 0,
CHILD_TRANSFORM: 1,
COLOR: 2,
CHILD_COLOR: 3,
CHILD_CACHE: 4,
WORLD_TRANSFORM: 5,
DISPLAY_LIST: 6,
SELF: 7
};
var DirtyComponent = defineComponent({
data: [Types.ui8, 8]
});
// src/GameInstance.ts
var instance2;
var frame = 0;
var elapsed = 0;
var GameInstance = {
get: () => {
return instance2;
},
set: (game) => {
instance2 = game;
},
getFrame: () => {
return frame;
},
setFrame: (current) => {
frame = current;
},
getElapsed: () => {
return elapsed;
},
setElapsed: (current) => {
elapsed = current;
}
};
// src/components/hierarchy/HierarchyComponent.ts
var HIERARCHY = {
WORLD: 0,
PARENT: 1,
NEXT: 2,
PREV: 3,
FIRST: 4,
LAST: 5,
NUM_CHILDREN: 6,
DEPTH: 7
};
var HierarchyComponent = defineComponent({
data: [Types.ui32, 8]
});
// src/components/hierarchy/GetParentID.ts
function GetParentID(id) {
return HierarchyComponent.data[id][HIERARCHY.PARENT];
}
// src/components/dirty/SetDirtyChildCache.ts
function SetDirtyChildCache(id) {
DirtyComponent.data[id][DIRTY.CHILD_CACHE] = 1;
}
// src/components/dirty/SetDirtyChildTransform.ts
function SetDirtyChildTransform(id) {
DirtyComponent.data[id][DIRTY.CHILD_TRANSFORM] = 1;
}
// src/components/permissions/PermissionsComponent.ts
var PERMISSION = {
VISIBLE: 0,
VISIBLE_CHILDREN: 1,
WILL_UPDATE: 2,
WILL_UPDATE_CHILDREN: 3,
WILL_RENDER: 4,
WILL_RENDER_CHILDREN: 5,
WILL_CACHE_CHILDREN: 6,
WILL_TRANSFORM_CHILDREN: 7,
WILL_COLOR_CHILDREN: 8,
CUSTOM_DISPLAY_LIST: 9
};
var PermissionsComponent = defineComponent({
data: [Types.ui8, 10]
});
// src/components/permissions/WillCacheChildren.ts
function WillCacheChildren(id) {
return !!PermissionsComponent.data[id][PERMISSION.WILL_CACHE_CHILDREN];
}
// src/components/dirty/SetDirtyParents.ts
var prevParentID;
var prevFrame;
function SetDirtyParents(childID) {
let id = GetParentID(childID);
const frame2 = GameInstance.getFrame();
if (id === prevParentID && frame2 === prevFrame) {
return;
}
prevParentID = id;
prevFrame = frame2;
while (id) {
SetDirtyChildTransform(id);
if (WillCacheChildren(id)) {
SetDirtyChildCache(id);
}
id = GetParentID(id);
}
}
// src/components/dirty/SetDirtyTransform.ts
function SetDirtyTransform(id) {
DirtyComponent.data[id][DIRTY.TRANSFORM] = 1;
SetDirtyParents(id);
}
// src/components/transform/UpdateExtent.ts
function UpdateExtent(id, width, height) {
const data = Transform2DComponent.data[id];
const x = -data[TRANSFORM.ORIGIN_X] * width;
const y = -data[TRANSFORM.ORIGIN_Y] * height;
data[TRANSFORM.FRAME_X1] = x;
data[TRANSFORM.FRAME_Y1] = y;
data[TRANSFORM.FRAME_X2] = x + width;
data[TRANSFORM.FRAME_Y2] = y + height;
data[TRANSFORM.FRAME_WIDTH] = width;
data[TRANSFORM.FRAME_HEIGHT] = height;
SetDirtyTransform(id);
}
// src/components/transform/Size.ts
var Size = class {
id;
_data;
constructor(id, width = 0, height = 0) {
this.id = id;
this._data = Transform2DComponent.data[id];
this.set(width, height);
}
set(width, height = width) {
this.width = width;
this.height = height;
return this;
}
set width(value) {
UpdateExtent(this.id, value, this.height);
}
get width() {
return this._data[TRANSFORM.FRAME_WIDTH];
}
set height(value) {
UpdateExtent(this.id, this.width, value);
}
get height() {
return this._data[TRANSFORM.FRAME_HEIGHT];
}
set x(value) {
this.width = value;
}
get x() {
return this.width;
}
set y(value) {
this.height = value;
}
get y() {
return this.height;
}
destroy() {
this._data = null;
}
};
// src/camera/BaseCamera.ts
var BaseCamera = class {
id = addEntity(GameObjectWorld);
type = "BaseCamera";
name = "";
size;
matrix;
isDirty;
_data;
constructor(width, height) {
const id = this.id;
AddTransform2DComponent(id);
this.matrix = new Matrix4();
this.size = new Size(id, width, height);
this._data = Transform2DComponent.data[id];
this.reset(width, height);
}
preRender() {
return this.isDirty;
}
postRender() {
this.isDirty = false;
}
getBoundsX() {
return this._data[TRANSFORM.BOUNDS_X1];
}
getBoundsY() {
return this._data[TRANSFORM.BOUNDS_Y1];
}
getBoundsRight() {
return this._data[TRANSFORM.BOUNDS_X2];
}
getBoundsBottom() {
return this._data[TRANSFORM.BOUNDS_Y2];
}
getMatrix() {
return this.matrix.data;
}
reset(width, height) {
this.size.set(width, height);
this.isDirty = true;
SetBounds(this.id, 0, 0, width, height);
}
destroy() {
const id = this.id;
removeComponent(GameObjectWorld, Transform2DComponent, id);
removeEntity(GameObjectWorld, id);
}
};
// src/components/dirty/ClearDirtyTransform.ts
function ClearDirtyTransform(id) {
DirtyComponent.data[id][DIRTY.TRANSFORM] = 0;
}
// src/components/dirty/HasDirtyTransform.ts
function HasDirtyTransform(id) {
return !!DirtyComponent.data[id][DIRTY.TRANSFORM];
}
// src/camera/StaticCamera.ts
var StaticCamera = class extends BaseCamera {
type = "StaticCamera";
constructor(width, height) {
super(width, height);
}
preRender() {
const id = this.id;
if (HasDirtyTransform(id)) {
this.isDirty = true;
ClearDirtyTransform(id);
return true;
}
return false;
}
};
// src/components/transform/Position.ts
var Position = class {
id;
_x;
_y;
_data;
constructor(id, x = 0, y = 0) {
this.id = id;
this._data = Transform2DComponent.data[id];
this.set(x, y);
}
set(x, y = x) {
this.x = x;
this.y = y;
return this;
}
set x(value) {
this._x = value;
this._data[TRANSFORM.X] = value;
SetDirtyTransform(this.id);
}
get x() {
return this._x;
}
set y(value) {
this._y = value;
this._data[TRANSFORM.Y] = value;
SetDirtyTransform(this.id);
}
get y() {
return this._y;
}
destroy() {
this._data = null;
}
};
// src/camera/WorldCamera.ts
var WorldCamera = class extends BaseCamera {
type = "WorldCamera";
position;
constructor(width, height) {
super(width, height);
this.position = new Position(this.id, 0, 0);
}
set x(value) {
this.position.x = value;
}
get x() {
return this.position.x;
}
set y(value) {
this.position.y = value;
}
get y() {
return this.position.y;
}
setPosition(x, y) {
this.position.set(x, y);
return this;
}
preRender() {
const id = this.id;
if (HasDirtyTransform(id)) {
const x = this.x;
const y = this.y;
const w = this.size.width;
const h = this.size.height;
const ox = -x + w / 2;
const oy = -y + h / 2;
const bx = ox - w / 2;
const by = oy - h / 2;
SetBounds(id, bx, by, bx + w, by + h);
const data = this.matrix.data;
data[12] = this.x;
data[13] = this.y;
ClearDirtyTransform(id);
this.isDirty = true;
return true;
}
return false;
}
};
// src/color/index.ts
var color_exports = {};
__export(color_exports, {
CloneColor: () => CloneColor,
Color: () => Color,
FromHSV: () => FromHSV,
GetColorFromRGB: () => GetColorFromRGB,
GetColorSpectrum: () => GetColorSpectrum,
RGBAToFloat: () => RGBAToFloat,
RGBToFloat: () => RGBToFloat,
SetGray: () => SetGray,
SetHSV: () => SetHSV
});
// src/color/Color.ts
var Color = class {
rgba;
constructor(red = 0, green = 0, blue = 0, alpha = 255) {
this.rgba = new Uint8ClampedArray([red, green, blue, alpha]);
}
set(red, green, blue, alpha = this.alpha) {
this.rgba.set([red, green, blue, alpha]);
return this;
}
setColor(color) {
const rgba = this.rgba;
const alpha = color > 16777215 ? color >>> 24 : 255;
rgba.set([
color >> 16 & 255,
color >> 8 & 255,
color & 255,
alpha
]);
return this;
}
getColor(includeAlpha = false) {
const [r, g, b, a] = this.rgba;
if (includeAlpha) {
return a << 24 | r << 16 | g << 8 | b;
} else {
return r << 16 | g << 8 | b;
}
}
get red() {
return this.rgba[0];
}
set red(value) {
this.rgba[0] = value;
}
get green() {
return this.rgba[1];
}
set green(value) {
this.rgba[1] = value;
}
get blue() {
return this.rgba[2];
}
set blue(value) {
this.rgba[2] = value;
}
get alpha() {
return this.rgba[3];
}
set alpha(value) {
this.rgba[3] = value;
}
get r() {
return this.rgba[0] / 255;
}
get g() {
return this.rgba[1] / 255;
}
get b() {
return this.rgba[2] / 255;
}
get a() {
return this.rgba[3] / 255;
}
};
// src/color/CloneColor.ts
function CloneColor(color) {
return new Color(color.red, color.green, color.blue, color.alpha);
}
// src/color/SetHSV.ts
function ConvertValue(n, h, s, v) {
const k = (n + h * 6) % 6;
const min = Math.min(k, 4 - k, 1);
return Math.round(255 * (v - v * s * Math.max(0, min)));
}
function SetHSV(color, h, s = 1, v = 1) {
const r = ConvertValue(5, h, s, v);
const g = ConvertValue(3, h, s, v);
const b = ConvertValue(1, h, s, v);
return color.set(r, g, b);
}
// src/color/FromHSV.ts
function FromHSV(h, s = 1, v = 1) {
return SetHSV(new Color(), h, s, v);
}
// src/color/GetColorFromRGB.ts
function GetColorFromRGB(red, green, blue) {
return red << 16 | green << 8 | blue;
}
// src/color/GetColorSpectrum.ts
function GetColorSpectrum(limit = 1024) {
const colors = [];
const range = 255;
let i;
let r = 255;
let g = 0;
let b = 0;
for (i = 0; i <= range; i++) {
colors.push(new Color(r, i, b));
}
g = 255;
for (i = range; i >= 0; i--) {
colors.push(new Color(i, g, b));
}
r = 0;
for (i = 0; i <= range; i++, g--) {
colors.push(new Color(r, g, i));
}
g = 0;
b = 255;
for (i = 0; i <= range; i++, b--, r++) {
colors.push(new Color(r, g, b));
}
if (limit === 1024) {
return colors;
} else {
const out = [];
let t = 0;
const inc = 1024 / limit;
for (i = 0; i < limit; i++) {
out.push(colors[Math.floor(t)]);
t += inc;
}
return out;
}
}
// src/color/RGBAToFloat.ts
function RGBAToFloat(red, green, blue, alpha) {
return alpha << 24 | red << 16 | green << 8 | blue;
}
// src/color/RGBToFloat.ts
function RGBToFloat(red, green, blue) {
return red << 16 | green << 8 | blue;
}
// src/color/SetGray.ts
function SetGray(color, amount) {
return color.set(amount, amount, amount);
}
// src/config/index.ts
var config_exports = {};
__export(config_exports, {
BackgroundColor: () => BackgroundColor,
Banner: () => Banner,
BatchSize: () => BatchSize,
Canvas: () => Canvas,
CanvasContext: () => CanvasContext,
DefaultOrigin: () => DefaultOrigin,
GlobalVar: () => GlobalVar,
MaxTextures: () => MaxTextures,
Parent: () => Parent,
Scenes: () => Scenes,
Size: () => Size2,
WebGL: () => WebGL,
WebGLContext: () => WebGLContext
});
// src/config/const.ts
var CONFIG_DEFAULTS = {
AUTO: "Auto",
BACKGROUND_COLOR: "BackgroundColor",
BANNER: "Banner",
BATCH_SIZE: "BatchSize",
CANVAS_CONTEXT: "CanvasContext",
CANVAS: "Canvas",
DEFAULT_ORIGIN: "DefaultOrigin",
GLOBAL_VAR: "GlobalVar",
MAX_TEXTURES: "MaxTextures",
PARENT: "Parent",
RENDERER: "Renderer",
SCENES: "Scenes",
SIZE: "Size",
WEBGL_CONTEXT: "WebGLContext",
WEBGL: "WebGL",
WORLD_SIZE: "WorldSize",
WORLD_WIDTH: "WorldWidth",
WORLD_HEIGHT: "WorldHeight"
};
// src/config/ConfigStore.ts
var ConfigStore = new Map();
// src/config/backgroundcolor/SetBackgroundColor.ts
function SetBackgroundColor(color) {
ConfigStore.set(CONFIG_DEFAULTS.BACKGROUND_COLOR, color);
}
// src/config/backgroundcolor/BackgroundColor.ts
function BackgroundColor(color) {
return () => {
SetBackgroundColor(color);
};
}
// src/config/banner/SetBanner.ts
function SetBanner(title = "", version = "", url = "", color = "#fff", background = "linear-gradient(#3e0081 40%, #00bcc3)") {
ConfigStore.set(CONFIG_DEFAULTS.BANNER, { title, version, url, color, background });
}
// src/config/banner/Banner.ts
function Banner(title, version, url, color, background) {
return () => {
SetBanner(title, version, url, color, background);
};
}
// src/config/batchsize/SetBatchSize.ts
function SetBatchSize(size) {
ConfigStore.set(CONFIG_DEFAULTS.BATCH_SIZE, size);
}
// src/config/batchsize/BatchSize.ts
function BatchSize(size) {
return () => {
SetBatchSize(size);
};
}
// src/config/backgroundcolor/GetBackgroundColor.ts
function GetBackgroundColor() {
return ConfigStore.get(CONFIG_DEFAULTS.BACKGROUND_COLOR);
}
// src/config/canvascontext/GetCanvasContext.ts
function GetCanvasContext() {
return ConfigStore.get(CONFIG_DEFAULTS.CANVAS_CONTEXT);
}
// src/config/size/GetHeight.ts
function GetHeight() {
return ConfigStore.get(CONFIG_DEFAULTS.SIZE).height;
}
// src/config/size/GetResolution.ts
function GetResolution() {
return ConfigStore.get(CONFIG_DEFAULTS.SIZE).resolution;
}
// src/config/size/GetWidth.ts
function GetWidth() {
return ConfigStore.get(CONFIG_DEFAULTS.SIZE).width;
}
// src/renderer/canvas/CanvasRenderer.ts
var CanvasRenderer = class {
canvas;
ctx;
clearColor;
width;
height;
resolution;
textureIndex;
flushTotal;
clearBeforeRender = true;
optimizeRedraw = true;
autoResize = true;
constructor() {
this.width = GetWidth();
this.height = GetHeight();
this.resolution = GetResolution();
this.setBackgroundColor(GetBackgroundColor());
const canvas = document.createElement("canvas");
this.canvas = canvas;
this.initContext();
}
initContext() {
const ctx = this.canvas.getContext("2d", GetCanvasContext());
this.ctx = ctx;
this.resize(this.width, this.height, this.resolution);
}
resize(width, height, resolution = 1) {
this.width = width * resolution;
this.height = height * resolution;
this.resolution = resolution;
const canvas = this.canvas;
canvas.width = this.width;
canvas.height = this.height;
if (this.autoResize) {
canvas.style.width = (this.width / resolution).toString() + "px";
canvas.style.height = (this.height / resolution).toString() + "px";
}
}
setBackgroundColor(color) {
const r = color >> 16 & 255;
const g = color >> 8 & 255;
const b = color & 255;
const a = color > 16777215 ? color >>> 24 : 255;
this.clearColor = `rgba(${r}, ${g}, ${b}, ${a})`;
return this;
}
reset() {
const ctx = this.ctx;
ctx.globalAlpha = 1;
ctx.globalCompositeOperation = "source-over";
ctx.setTransform(1, 0, 0, 1, 0, 0);
}
begin(willRedraw) {
}
end() {
}
render() {
}
destroy() {
}
};
// src/config/renderer/SetRenderer.ts
function SetRenderer(renderer) {
ConfigStore.set(CONFIG_DEFAULTS.RENDERER, renderer);
}
// src/config/canvas/Canvas.ts
function Canvas() {
return () => {
SetRenderer(CanvasRenderer);
};
}
// src/config/canvascontext/SetCanvasContext.ts
function SetCanvasContext(contextAttributes) {
ConfigStore.set(CONFIG_DEFAULTS.CANVAS_CONTEXT, contextAttributes);
}
// src/config/canvascontext/CanvasContext.ts
function CanvasContext(contextAttributes) {
return () => {
SetCanvasContext(contextAttributes);
};
}
// src/config/defaultorigin/SetDefaultOrigin.ts
function SetDefaultOrigin(x = 0.5, y = x) {
ConfigStore.set(CONFIG_DEFAULTS.DEFAULT_ORIGIN, { x, y });
}
// src/config/defaultorigin/DefaultOrigin.ts
function DefaultOrigin(x = 0.5, y = x) {
return () => {
SetDefaultOrigin(x, y);
};
}
// src/config/globalvar/SetGlobalVar.ts
function SetGlobalVar(name) {
ConfigStore.set(CONFIG_DEFAULTS.GLOBAL_VAR, name);
}
// src/config/globalvar/GlobalVar.ts
function GlobalVar(name) {
return () => {
SetGlobalVar(name);
};
}
// src/config/maxtextures/SetMaxTextures.ts
function SetMaxTextures(max) {
ConfigStore.set(CONFIG_DEFAULTS.MAX_TEXTURES, max);
}
// src/config/maxtextures/MaxTextures.ts
function MaxTextures(max = 0) {
return () => {
SetMaxTextures(ma