Skip to content

Commit

Permalink
refactor: context in one variable, expose context (#481)
Browse files Browse the repository at this point in the history
  • Loading branch information
lajbel authored Oct 30, 2024
1 parent 3672710 commit 8f0d864
Show file tree
Hide file tree
Showing 54 changed files with 454 additions and 401 deletions.
1 change: 1 addition & 0 deletions src/app/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1225,6 +1225,7 @@ export const initApp = (opt: {
resizeObserver.observe(state.canvas);

return {
state,
dt,
fixedDt,
restDt,
Expand Down
8 changes: 4 additions & 4 deletions src/app/frame.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { app, debug } from "../kaplay";
import { _k } from "../kaplay";

export function dt() {
return app.dt() * debug.timeScale;
return _k.app.dt();
}

export function fixedDt() {
return app.fixedDt() * debug.timeScale;
return _k.app.fixedDt();
}

export function restDt() {
return app.restDt() * debug.timeScale;
return _k.app.restDt();
}
4 changes: 2 additions & 2 deletions src/assets/aseprite.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { assets } from "../kaplay";
import { _k } from "../kaplay";
import { Quad } from "../math";
import { getFileName } from "../utils";
import { type Asset, fetchJSON } from "./asset";
Expand Down Expand Up @@ -41,7 +41,7 @@ export function loadAseprite(
? fetchJSON(jsonSrc)
: Promise.resolve(jsonSrc);

return assets.sprites.add(
return _k.assets.sprites.add(
name,
resolveJSON.then((data: AsepriteData) => {
const size = data.meta.size;
Expand Down
24 changes: 12 additions & 12 deletions src/assets/asset.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { SPRITE_ATLAS_HEIGHT, SPRITE_ATLAS_WIDTH } from "../constants";
import type { GfxCtx } from "../gfx/gfx";
import TexPacker from "../gfx/texPacker";
import { assets } from "../kaplay";
import { _k } from "../kaplay";
import { KEvent } from "../utils";
import type { BitmapFontData } from "./bitmapFont";
import type { FontData } from "./font";
Expand Down Expand Up @@ -139,13 +139,13 @@ export function fetchArrayBuffer(path: string) {
// global load path prefix
export function loadRoot(path?: string): string {
if (path !== undefined) {
assets.urlPrefix = path;
_k.assets.urlPrefix = path;
}
return assets.urlPrefix;
return _k.assets.urlPrefix;
}

export function loadJSON(name: string, url: string) {
return assets.custom.add(name, fetchJSON(fixURL(url)));
return _k.assets.custom.add(name, fetchJSON(fixURL(url)));
}

// wrapper around image loader to get a Promise
Expand All @@ -162,24 +162,24 @@ export function loadImg(src: string): Promise<HTMLImageElement> {

export function loadProgress(): number {
const buckets = [
assets.sprites,
assets.sounds,
assets.shaders,
assets.fonts,
assets.bitmapFonts,
assets.custom,
_k.assets.sprites,
_k.assets.sounds,
_k.assets.shaders,
_k.assets.fonts,
_k.assets.bitmapFonts,
_k.assets.custom,
];
return buckets.reduce((n, bucket) => n + bucket.progress(), 0)
/ buckets.length;
}

export function getAsset(name: string): Asset<any> | null {
return assets.custom.get(name) ?? null;
return _k.assets.custom.get(name) ?? null;
}

// wrap individual loaders with global loader counter, for stuff like progress bar
export function load<T>(prom: Promise<T>): Asset<T> {
return assets.custom.add(null, prom);
return _k.assets.custom.add(null, prom);
}

// create assets
Expand Down
8 changes: 4 additions & 4 deletions src/assets/bitmapFont.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { ASCII_CHARS } from "../constants";
import { Texture } from "../gfx";
import { assets, gfx } from "../kaplay";
import { _k } from "../kaplay";
import type { Quad } from "../math/math";
import type { TexFilter } from "../types";
import { type Asset, loadImg } from "./asset";
Expand All @@ -16,7 +16,7 @@ export interface GfxFont {
export type BitmapFontData = GfxFont;

export function getBitmapFont(name: string): Asset<BitmapFontData> | null {
return assets.bitmapFonts.get(name) ?? null;
return _k.assets.bitmapFonts.get(name) ?? null;
}

export interface LoadBitmapFontOpt {
Expand All @@ -36,12 +36,12 @@ export function loadBitmapFont(
): Asset<BitmapFontData> {
const fontSrc = fixURL(src);

return assets.bitmapFonts.add(
return _k.assets.bitmapFonts.add(
name,
loadImg(fontSrc)
.then((img) => {
return makeFont(
Texture.fromImage(gfx.ggl, img, opt),
Texture.fromImage(_k.gfx.ggl, img, opt),
gw,
gh,
opt.chars ?? ASCII_CHARS,
Expand Down
8 changes: 4 additions & 4 deletions src/assets/font.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
} from "../constants";
import type { Texture } from "../gfx";
import type { DrawTextOpt } from "../gfx/draw/drawText";
import { assets, globalOpt } from "../kaplay";
import { _k } from "../kaplay";
import { rgb } from "../math/color";
import { Quad } from "../math/math";
import type { LoadFontOpt, Outline, TexFilter } from "../types";
Expand Down Expand Up @@ -58,7 +58,7 @@ export function resolveFont(
| void
{
if (!src) {
return resolveFont(globalOpt.font ?? DEF_FONT);
return resolveFont(_k.globalOpt.font ?? DEF_FONT);
}
if (typeof src === "string") {
const bfont = getBitmapFont(src);
Expand Down Expand Up @@ -89,7 +89,7 @@ export function resolveFont(
}

export function getFont(name: string): Asset<FontData> | null {
return assets.fonts.get(name) ?? null;
return _k.assets.fonts.get(name) ?? null;
}

// TODO: pass in null src to store opt for default fonts like "monospace"
Expand All @@ -105,7 +105,7 @@ export function loadFont(
);
document.fonts.add(font);

return assets.fonts.add(
return _k.assets.fonts.add(
name,
font.load().catch((err) => {
throw new Error(`Failed to load font from "${fontSrc}": ${err}`);
Expand Down
4 changes: 2 additions & 2 deletions src/assets/pedit.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { assets } from "../kaplay";
import { _k } from "../kaplay";
import { type Asset, fetchJSON, loadImg } from "./asset";
import { loadSprite, type SpriteAnims, type SpriteData } from "./sprite";
import { fixURL } from "./utils";
Expand All @@ -16,7 +16,7 @@ export function loadPedit(
): Asset<SpriteData> {
src = fixURL(src);

return assets.sprites.add(
return _k.assets.sprites.add(
name,
new Promise(async (resolve) => {
const data = typeof src === "string"
Expand Down
12 changes: 6 additions & 6 deletions src/assets/shader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
VERTEX_FORMAT,
} from "../constants";
import { type GfxCtx } from "../gfx";
import { assets, gfx } from "../kaplay";
import { _k } from "../kaplay";
import { Color } from "../math/color";
import { Mat4, Vec2 } from "../math/math";
import type { RenderProps } from "../types";
Expand Down Expand Up @@ -170,7 +170,7 @@ export function resolveShader(
src: RenderProps["shader"],
): ShaderData | Asset<ShaderData> | null {
if (!src) {
return gfx.defShader;
return _k.gfx.defShader;
}
if (typeof src === "string") {
const shader = getShader(src);
Expand All @@ -192,15 +192,15 @@ export function resolveShader(
}

export function getShader(name: string): Asset<ShaderData> | null {
return assets.shaders.get(name) ?? null;
return _k.assets.shaders.get(name) ?? null;
}

export function loadShader(
name: string | null,
vert?: string,
frag?: string,
) {
return assets.shaders.addLoaded(name, makeShader(gfx.ggl, vert, frag));
return _k.assets.shaders.addLoaded(name, makeShader(_k.gfx.ggl, vert, frag));
}

export function loadShaderURL(
Expand All @@ -216,7 +216,7 @@ export function loadShaderURL(
: Promise.resolve(null);
const load = Promise.all([resolveUrl(vert), resolveUrl(frag)])
.then(([vcode, fcode]: [string | null, string | null]) => {
return makeShader(gfx.ggl, vcode, fcode);
return makeShader(_k.gfx.ggl, vcode, fcode);
});
return assets.shaders.add(name, load);
return _k.assets.shaders.add(name, load);
}
10 changes: 5 additions & 5 deletions src/assets/sound.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { assets, audio } from "../kaplay";
import { _k } from "../kaplay";
import { dataURLToArrayBuffer, isDataURL } from "../utils";
import { Asset, fetchArrayBuffer, loadProgress } from "./asset";
import { fixURL } from "./utils";
Expand All @@ -12,7 +12,7 @@ export class SoundData {

static fromArrayBuffer(buf: ArrayBuffer): Promise<SoundData> {
return new Promise((resolve, reject) =>
audio.ctx.decodeAudioData(buf, resolve, reject)
_k.audio.ctx.decodeAudioData(buf, resolve, reject)
).then((buf) => new SoundData(buf as AudioBuffer));
}

Expand Down Expand Up @@ -55,7 +55,7 @@ export function resolveSound(
}

export function getSound(name: string): Asset<SoundData> | null {
return assets.sounds.get(name) ?? null;
return _k.assets.sounds.get(name) ?? null;
}

// load a sound to asset manager
Expand All @@ -64,7 +64,7 @@ export function loadSound(
src: string | ArrayBuffer,
): Asset<SoundData> {
src = fixURL(src);
return assets.sounds.add(
return _k.assets.sounds.add(
name,
typeof src === "string"
? SoundData.fromURL(src)
Expand All @@ -80,5 +80,5 @@ export function loadMusic(
const a = new Audio(musicUrl);
a.preload = "auto";

return assets.music[name as keyof typeof assets.music] = musicUrl;
return _k.assets.music[name as string] = musicUrl;
}
14 changes: 7 additions & 7 deletions src/assets/sprite.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Asset, loadImg, loadProgress } from "../assets";
import type { DrawSpriteOpt } from "../gfx";
import type { Texture } from "../gfx/gfx";
import { assets } from "../kaplay";
import { _k } from "../kaplay";
import beanSpriteSrc from "../kassets/bean.png";
import { Quad } from "../math/math";
import { type ImageSource } from "../types";
Expand Down Expand Up @@ -132,7 +132,7 @@ export class SpriteData {
data: ImageSource,
opt: LoadSpriteOpt = {},
): SpriteData {
const [tex, quad] = assets.packer.add(data);
const [tex, quad] = _k.assets.packer.add(data);
const frames = opt.frames
? opt.frames.map((f) =>
new Quad(
Expand Down Expand Up @@ -191,7 +191,7 @@ export function resolveSprite(
}

export function getSprite(name: string): Asset<SpriteData> | null {
return assets.sprites.get(name) ?? null;
return _k.assets.sprites.get(name) ?? null;
}

// load a sprite to asset manager
Expand All @@ -207,7 +207,7 @@ export function loadSprite(
src = fixURL(src);
if (Array.isArray(src)) {
if (src.some((s) => typeof s === "string")) {
return assets.sprites.add(
return _k.assets.sprites.add(
name,
Promise.all(src.map((s) => {
return typeof s === "string"
Expand All @@ -217,18 +217,18 @@ export function loadSprite(
);
}
else {
return assets.sprites.addLoaded(
return _k.assets.sprites.addLoaded(
name,
createSpriteSheet(src as ImageSource[], opt),
);
}
}
else {
if (typeof src === "string") {
return assets.sprites.add(name, SpriteData.from(src, opt));
return _k.assets.sprites.add(name, SpriteData.from(src, opt));
}
else {
return assets.sprites.addLoaded(
return _k.assets.sprites.addLoaded(
name,
SpriteData.fromImage(src, opt),
);
Expand Down
4 changes: 2 additions & 2 deletions src/assets/spriteAtlas.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { SPRITE_ATLAS_HEIGHT, SPRITE_ATLAS_WIDTH } from "../constants";
import { assets } from "../kaplay";
import { _k } from "../kaplay";
import { Quad } from "../math";
import { type Asset, fetchJSON, load } from "./asset";
import {
Expand Down Expand Up @@ -75,7 +75,7 @@ export function loadSpriteAtlas(
info.height / h * quad.h,
);
const spr = new SpriteData(atlas.tex, frames, info.anims);
assets.sprites.addLoaded(name, spr);
_k.assets.sprites.addLoaded(name, spr);
map[name] = spr;
}
return map;
Expand Down
4 changes: 2 additions & 2 deletions src/assets/utils.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { assets } from "../kaplay";
import { _k } from "../kaplay";
import { isDataURL } from "../utils";

export function fixURL<D>(url: D): D {
if (typeof url !== "string" || isDataURL(url)) return url;
return assets.urlPrefix + url as D;
return _k.assets.urlPrefix + url as D;
}
4 changes: 2 additions & 2 deletions src/audio/burp.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { audio } from "../kaplay";
import { _k } from "../kaplay";
import { type AudioPlay, type AudioPlayOpt, play } from "./play";

// core KAPLAY logic
export function burp(opt?: AudioPlayOpt): AudioPlay {
return play(audio.burpSnd, opt);
return _k.k.play(_k.audio.burpSnd, opt);
}
10 changes: 5 additions & 5 deletions src/audio/play.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Asset, resolveSound, type SoundData } from "../assets";
import { assets, audio } from "../kaplay";
import { _k } from "../kaplay";
import type { MusicData } from "../types";
import { KEvent, type KEventController } from "../utils";
import { playMusic } from "./playMusic";
Expand Down Expand Up @@ -133,11 +133,11 @@ export function play(
| Asset<MusicData>,
opt: AudioPlayOpt = {},
): AudioPlay {
if (typeof src === "string" && assets.music[src]) {
return playMusic(assets.music[src], opt);
if (typeof src === "string" && _k.assets.music[src]) {
return playMusic(_k.assets.music[src], opt);
}

const ctx = audio.ctx;
const ctx = _k.audio.ctx;
let paused = opt.paused ?? false;
let srcNode = ctx.createBufferSource();
const onEndEvents = new KEvent();
Expand All @@ -162,7 +162,7 @@ export function play(
};
panNode.pan.value = opt.pan ?? 0;
panNode.connect(gainNode);
gainNode.connect(audio.masterNode);
gainNode.connect(_k.audio.masterNode);
gainNode.gain.value = opt.volume ?? 1;

const start = (data: SoundData) => {
Expand Down
Loading

0 comments on commit 8f0d864

Please sign in to comment.