From 9b3d2b914797b0420de1f2e6d6965dd3fdc62466 Mon Sep 17 00:00:00 2001 From: lajbel Date: Wed, 23 Oct 2024 19:19:30 -0300 Subject: [PATCH] chore: format many files --- examples/animation.js | 2 +- examples/binding.js | 78 +++--- examples/button.js | 6 +- examples/camera.js | 2 +- examples/collision.js | 2 +- examples/collisionshapes.js | 4 +- examples/component.js | 164 ++++++------- examples/doublejump.js | 366 ++++++++++++++-------------- examples/eatlove.js | 242 +++++++++--------- examples/platformBox.js | 2 +- examples/prettyDebug.js | 5 +- examples/tsconfig.json | 2 +- examples/weirdTextTags.js | 2 +- src/audio/play.ts | 2 +- src/components/draw/sprite.ts | 12 +- src/components/physics/effectors.ts | 31 ++- src/gfx/draw/drawDebug.ts | 34 ++- src/gfx/formatText.ts | 41 ++-- src/math/math.ts | 8 +- tsconfig.dts.json | 34 +-- tsconfig.json | 2 +- 21 files changed, 537 insertions(+), 504 deletions(-) diff --git a/examples/animation.js b/examples/animation.js index 7abdfe61..20d3aa4c 100644 --- a/examples/animation.js +++ b/examples/animation.js @@ -137,7 +137,7 @@ timedSquaringBean.animate( }, ); -// We'll move this bean in a curve +// We'll move this bean in a curve // Using spline interpolation to move according to a smoothened path const curvingBean = add([ sprite("bean"), diff --git a/examples/binding.js b/examples/binding.js index 6a49d9b4..8a346289 100644 --- a/examples/binding.js +++ b/examples/binding.js @@ -2,68 +2,68 @@ // You can set the input bindings for your game! kaplay({ - buttons: { - // Buttons for jumping - "jump": { - // When using a gamepad the button for jumping will be south - gamepad: ["south"], - // When using a keyboard the button will be "up" or "w" - keyboard: ["up", "w"], - // When using a mouse the button will be "left" - mouse: "left", - }, - // Buttons for inspecting - "inspect": { - gamepad: "east", - keyboard: "f", - mouse: "right", - }, - }, + buttons: { + // Buttons for jumping + "jump": { + // When using a gamepad the button for jumping will be south + gamepad: ["south"], + // When using a keyboard the button will be "up" or "w" + keyboard: ["up", "w"], + // When using a mouse the button will be "left" + mouse: "left", + }, + // Buttons for inspecting + "inspect": { + gamepad: "east", + keyboard: "f", + mouse: "right", + }, + }, }); -loadBean() +loadBean(); // Set the gravity acceleration (pixels per second) setGravity(1600); // Add player game object const player = add([ - sprite("bean"), - pos(center()), - area(), - // body() component gives the ability to respond to gravity - body(), + sprite("bean"), + pos(center()), + area(), + // body() component gives the ability to respond to gravity + body(), ]); // Add a platform to hold the player add([ - rect(width(), 48), - outline(4), - area(), - pos(0, height() - 48), - // Give objects a body() component if you don't want other solid objects pass through - body({ isStatic: true }), + rect(width(), 48), + outline(4), + area(), + pos(0, height() - 48), + // Give objects a body() component if you don't want other solid objects pass through + body({ isStatic: true }), ]); // Adds an object with a text add([ - text("Press jump button", { width: width() / 2 }), - pos(12, 12), + text("Press jump button", { width: width() / 2 }), + pos(12, 12), ]); // This runs when the button for "jump" is pressed (will be on any input device) onButtonPress("jump", () => { - // You can get the type of device that the last input was inputted in! - debug.log(getLastInputDeviceType()); + // You can get the type of device that the last input was inputted in! + debug.log(getLastInputDeviceType()); - // Now we'll check if the player is on the ground to make it jump - if (player.isGrounded()) { - // .jump() is provided by body() - player.jump(); - } + // Now we'll check if the player is on the ground to make it jump + if (player.isGrounded()) { + // .jump() is provided by body() + player.jump(); + } }); // When the button for inspecting is pressed we will log in the debug console for our game the text "inspecting" onButtonDown("inspect", () => { - debug.log("inspecting"); + debug.log("inspecting"); }); diff --git a/examples/button.js b/examples/button.js index c06a0cec..b686405c 100644 --- a/examples/button.js +++ b/examples/button.js @@ -10,7 +10,11 @@ kaplay({ onUpdate(() => setCursor("default")); // Function that adds a button to the game with a given text, position and function -function addButton(txt = "start game", p = vec2(200, 100), f = () => debug.log("hello")) { +function addButton( + txt = "start game", + p = vec2(200, 100), + f = () => debug.log("hello"), +) { // add a parent background object const btn = add([ rect(240, 80, { radius: 8 }), diff --git a/examples/camera.js b/examples/camera.js index 63e43efd..0b44e8aa 100644 --- a/examples/camera.js +++ b/examples/camera.js @@ -103,4 +103,4 @@ onClick(() => { // Use toWorld() to transform a screen-space coordinate (like mousePos()) to // the world-space coordinate, which has the camera transform applied addKaboom(toWorld(mousePos())); -}); \ No newline at end of file +}); diff --git a/examples/collision.js b/examples/collision.js index cd83c473..895386b2 100644 --- a/examples/collision.js +++ b/examples/collision.js @@ -121,4 +121,4 @@ player.onUpdate(() => { // Can also be toggled by pressing F1 debug.inspect = true; -// Check out https://kaplayjs.com/doc/AreaComp/ for everything area() provides \ No newline at end of file +// Check out https://kaplayjs.com/doc/AreaComp/ for everything area() provides diff --git a/examples/collisionshapes.js b/examples/collisionshapes.js index 6cf75101..17d4e2f9 100644 --- a/examples/collisionshapes.js +++ b/examples/collisionshapes.js @@ -30,9 +30,9 @@ loop(1, () => { body(), offscreen({ destroy: true, distance: 10 }), ]); - + // getTreeRoot() gets the root of the game, the object that holds every other object - // This line basically means that if there are more than 20 objects, we destroy the last one + // This line basically means that if there are more than 20 objects, we destroy the last one if (getTreeRoot().children.length > 20) { destroy(getTreeRoot().children[1]); } diff --git a/examples/component.js b/examples/component.js index f6caf6e3..47d8b24f 100644 --- a/examples/component.js +++ b/examples/component.js @@ -8,103 +8,103 @@ loadBean(); // Components are just function that returns a js object that follows a certain format // This object contains certain properties which then become available in your object to use function funky() { - // Can use local closed variables to store component state - let isFunky = false; - - return { - // ------------------ - // Special properties that controls the behavior of the component (all optional) - - // These properties (id and require specially id) are handled by kaplay, id is the name of the component - // If you want to get all objects with this component you can do get("funky") - // Be careful to tag objects with what might be the id of a component - - id: "funky", // The name of the component - require: ["scale", "color"], // If this component depend on any other components - // If the you put components in require and attach this component to an object that doesn't have these components - // The game will throw an error - - // Runs when the host object is added to the game - add() { - // E.g. Register some events from other components, do some bookkeeping, etc. - }, - - // Runs every frame as long as the host object exists - update() { - if (!isFunky) return; - - // "this" in all component methods refers to the the game object this component is attached to - // Here we're updating some properties provided by other components - this.color = rgb(rand(0, 255), rand(0, 255), rand(0, 255)); - this.scale = vec2(rand(1, 2)); - }, - - // Runs every frame (after update) as long as the host object exists - draw() { - // E.g. Custom drawXXX() operations. - }, - - // Runs when the host object is destroyed - destroy() { - // E.g. Clean up event handlers, etc. - }, - - // When you press F1 you can get a list of inspect properties a component might provide for an object - // Here you can provide custom ones - inspect() { - return "funky: " + isFunky; - }, - - // ------------------ - // All other properties and methods are directly assigned to the host object - - // This means that the object is getting funky, not that you're getting the property funky lol! - getFunky() { - isFunky = true; - }, - }; + // Can use local closed variables to store component state + let isFunky = false; + + return { + // ------------------ + // Special properties that controls the behavior of the component (all optional) + + // These properties (id and require specially id) are handled by kaplay, id is the name of the component + // If you want to get all objects with this component you can do get("funky") + // Be careful to tag objects with what might be the id of a component + + id: "funky", // The name of the component + require: ["scale", "color"], // If this component depend on any other components + // If the you put components in require and attach this component to an object that doesn't have these components + // The game will throw an error + + // Runs when the host object is added to the game + add() { + // E.g. Register some events from other components, do some bookkeeping, etc. + }, + + // Runs every frame as long as the host object exists + update() { + if (!isFunky) return; + + // "this" in all component methods refers to the the game object this component is attached to + // Here we're updating some properties provided by other components + this.color = rgb(rand(0, 255), rand(0, 255), rand(0, 255)); + this.scale = vec2(rand(1, 2)); + }, + + // Runs every frame (after update) as long as the host object exists + draw() { + // E.g. Custom drawXXX() operations. + }, + + // Runs when the host object is destroyed + destroy() { + // E.g. Clean up event handlers, etc. + }, + + // When you press F1 you can get a list of inspect properties a component might provide for an object + // Here you can provide custom ones + inspect() { + return "funky: " + isFunky; + }, + + // ------------------ + // All other properties and methods are directly assigned to the host object + + // This means that the object is getting funky, not that you're getting the property funky lol! + getFunky() { + isFunky = true; + }, + }; } // Adds an object with the funky component const bean = add([ - sprite("bean"), - pos(center()), - anchor("center"), - scale(1), - color(), - area(), - // Use our component here - funky(), - // Tags are empty components, it's equivalent to a { id: "friend" } - "friend", - // Plain objects here are components too and work the same way, except unnamed - { - coolness: 100, - friends: [], - }, + sprite("bean"), + pos(center()), + anchor("center"), + scale(1), + color(), + area(), + // Use our component here + funky(), + // Tags are empty components, it's equivalent to a { id: "friend" } + "friend", + // Plain objects here are components too and work the same way, except unnamed + { + coolness: 100, + friends: [], + }, ]); onKeyPress("space", () => { - // .coolness is from our plain object 'unnamed component' - if (bean.coolness >= 100) { - // We can use .getFunky() provided by the funky() component now - bean.getFunky(); - } + // .coolness is from our plain object 'unnamed component' + if (bean.coolness >= 100) { + // We can use .getFunky() provided by the funky() component now + bean.getFunky(); + } }); onKeyPress("r", () => { - // .use() is on every game object, it adds a component at runtime - bean.use(rotate(rand(0, 360))); + // .use() is on every game object, it adds a component at runtime + bean.use(rotate(rand(0, 360))); }); onKeyPress("escape", () => { - // .unuse() removes a component from the game object - // The tag is the one that appears on the id - bean.unuse("funky"); + // .unuse() removes a component from the game object + // The tag is the one that appears on the id + bean.unuse("funky"); }); // Adds a text object add([ - text("Press space to get funky", { width: width() }), - pos(12, 12), + text("Press space to get funky", { width: width() }), + pos(12, 12), ]); diff --git a/examples/doublejump.js b/examples/doublejump.js index dd34b8d9..2815242a 100644 --- a/examples/doublejump.js +++ b/examples/doublejump.js @@ -2,7 +2,7 @@ // How to use the doubleJump component in this little game kaplay({ - background: [141, 183, 255], + background: [141, 183, 255], }); // Loads sprites @@ -21,200 +21,200 @@ const NUM_PLATFORMS = 5; // a spinning component for fun, for more info check the 'component' example function spin(speed = 1200) { - let spinning = false; - return { - require: ["rotate"], - update() { - if (!spinning) { - return; - } - this.angle -= speed * dt(); - if (this.angle <= -360) { - spinning = false; - this.angle = 0; - } - }, - spin() { - spinning = true; - }, - }; + let spinning = false; + return { + require: ["rotate"], + update() { + if (!spinning) { + return; + } + this.angle -= speed * dt(); + if (this.angle <= -360) { + spinning = false; + this.angle = 0; + } + }, + spin() { + spinning = true; + }, + }; } // Setsup the game scene scene("game", () => { - // This score textObject holds a value property in a plain object - const score = add([ - text("0", { size: 24 }), - pos(24, 24), - { value: 0 }, - ]); - - const bean = add([ - sprite("bean"), - area(), - anchor("center"), - pos(0, 0), - body({ jumpForce: JUMP_FORCE }), - // Adds the double jump component - doubleJump(), - rotate(0), - spin(), - ]); - - // Adds a num of platforms that go from left to right - for (let i = 1; i < NUM_PLATFORMS; i++) { - add([ - sprite("grass"), - area(), - pos(rand(0, width()), i * height() / NUM_PLATFORMS), - body({ isStatic: true }), - anchor("center"), - "platform", - { - speed: rand(120, 320), - dir: choose([-1, 1]), - }, - ]); - } - - // go to the first platform - bean.pos = get("platform")[0].pos.sub(0, 64); - - // Generates coins on those platforms - function genCoin(avoid) { - const plats = get("platform"); - let idx = randi(0, plats.length); - // avoid the spawning on the same platforms - if (avoid != null) { - idx = choose([...plats.keys()].filter((i) => i !== avoid)); - } - const plat = plats[idx]; - add([ - pos(), - anchor("center"), - sprite("coin"), - area(), - follow(plat, vec2(0, -60)), - "coin", - { idx: idx }, - ]); - } - - genCoin(0); - - for (let i = 0; i < width() / 64; i++) { - add([ - pos(i * 64, height()), - sprite("spike"), - area(), - anchor("bot"), - scale(), - "danger", - ]); - } - - bean.onCollide("danger", () => { - go("lose"); - }); - - bean.onCollide("coin", (c) => { - destroy(c); - play("coin"); - score.value += 1; - score.text = score.value.toString(); - genCoin(c.idx); - }); - - // The double jupm component provides us this function that runs when we double jump - bean.onDoubleJump(() => { - // So we can call the spin() method provided by the spin() component to spin - bean.spin(); - }); - - onUpdate("platform", (p) => { - p.move(p.dir * p.speed, 0); - if (p.pos.x < 0 || p.pos.x > width()) { - p.dir = -p.dir; - } - }); - - onKeyPress("space", () => { - bean.doubleJump(); - }); - - // Will move the bean left and right - function move(x) { - bean.move(x, 0); - if (bean.pos.x < 0) { - bean.pos.x = width(); - } - else if (bean.pos.x > width()) { - bean.pos.x = 0; - } - } - - // both keys will trigger - onKeyDown("left", () => { - move(-PLAYER_SPEED); - }); - - onKeyDown("right", () => { - move(PLAYER_SPEED); - }); - - // The south button will call the doubleJump, for more info on gamepads check the 'gamepad' example - onGamepadButtonPress("south", () => bean.doubleJump()); - - onGamepadStick("left", (v) => { - move(v.x * PLAYER_SPEED); - }); - - let timeLeft = 30; - - const timer = add([ - anchor("topright"), - pos(width() - 24, 24), - text(timeLeft.toString()), - ]); - - onUpdate(() => { - timeLeft -= dt(); - if (timeLeft <= 0) { - go("win", score.value); - } - timer.text = timeLeft.toFixed(2); - }); + // This score textObject holds a value property in a plain object + const score = add([ + text("0", { size: 24 }), + pos(24, 24), + { value: 0 }, + ]); + + const bean = add([ + sprite("bean"), + area(), + anchor("center"), + pos(0, 0), + body({ jumpForce: JUMP_FORCE }), + // Adds the double jump component + doubleJump(), + rotate(0), + spin(), + ]); + + // Adds a num of platforms that go from left to right + for (let i = 1; i < NUM_PLATFORMS; i++) { + add([ + sprite("grass"), + area(), + pos(rand(0, width()), i * height() / NUM_PLATFORMS), + body({ isStatic: true }), + anchor("center"), + "platform", + { + speed: rand(120, 320), + dir: choose([-1, 1]), + }, + ]); + } + + // go to the first platform + bean.pos = get("platform")[0].pos.sub(0, 64); + + // Generates coins on those platforms + function genCoin(avoid) { + const plats = get("platform"); + let idx = randi(0, plats.length); + // avoid the spawning on the same platforms + if (avoid != null) { + idx = choose([...plats.keys()].filter((i) => i !== avoid)); + } + const plat = plats[idx]; + add([ + pos(), + anchor("center"), + sprite("coin"), + area(), + follow(plat, vec2(0, -60)), + "coin", + { idx: idx }, + ]); + } + + genCoin(0); + + for (let i = 0; i < width() / 64; i++) { + add([ + pos(i * 64, height()), + sprite("spike"), + area(), + anchor("bot"), + scale(), + "danger", + ]); + } + + bean.onCollide("danger", () => { + go("lose"); + }); + + bean.onCollide("coin", (c) => { + destroy(c); + play("coin"); + score.value += 1; + score.text = score.value.toString(); + genCoin(c.idx); + }); + + // The double jupm component provides us this function that runs when we double jump + bean.onDoubleJump(() => { + // So we can call the spin() method provided by the spin() component to spin + bean.spin(); + }); + + onUpdate("platform", (p) => { + p.move(p.dir * p.speed, 0); + if (p.pos.x < 0 || p.pos.x > width()) { + p.dir = -p.dir; + } + }); + + onKeyPress("space", () => { + bean.doubleJump(); + }); + + // Will move the bean left and right + function move(x) { + bean.move(x, 0); + if (bean.pos.x < 0) { + bean.pos.x = width(); + } + else if (bean.pos.x > width()) { + bean.pos.x = 0; + } + } + + // both keys will trigger + onKeyDown("left", () => { + move(-PLAYER_SPEED); + }); + + onKeyDown("right", () => { + move(PLAYER_SPEED); + }); + + // The south button will call the doubleJump, for more info on gamepads check the 'gamepad' example + onGamepadButtonPress("south", () => bean.doubleJump()); + + onGamepadStick("left", (v) => { + move(v.x * PLAYER_SPEED); + }); + + let timeLeft = 30; + + const timer = add([ + anchor("topright"), + pos(width() - 24, 24), + text(timeLeft.toString()), + ]); + + onUpdate(() => { + timeLeft -= dt(); + if (timeLeft <= 0) { + go("win", score.value); + } + timer.text = timeLeft.toFixed(2); + }); }); // Sets up the scene where we win scene("win", (score) => { - add([ - sprite("bean"), - pos(width() / 2, height() / 2 - 80), - scale(2), - anchor("center"), - ]); - - // display score - add([ - text(score), - pos(width() / 2, height() / 2 + 80), - scale(2), - anchor("center"), - ]); - - // go back to game with space is pressed - onKeyPress("space", () => go("game")); - onGamepadButtonPress("south", () => go("game")); + add([ + sprite("bean"), + pos(width() / 2, height() / 2 - 80), + scale(2), + anchor("center"), + ]); + + // display score + add([ + text(score), + pos(width() / 2, height() / 2 + 80), + scale(2), + anchor("center"), + ]); + + // go back to game with space is pressed + onKeyPress("space", () => go("game")); + onGamepadButtonPress("south", () => go("game")); }); // Sets up the scene where we lose :( scene("lose", () => { - add([ - text("You Lose"), - ]); - onKeyPress("space", () => go("game")); - onGamepadButtonPress("south", () => go("game")); + add([ + text("You Lose"), + ]); + onKeyPress("space", () => go("game")); + onGamepadButtonPress("south", () => go("game")); }); // Starts the game by entering the game scene diff --git a/examples/eatlove.js b/examples/eatlove.js index 424fb0f0..4c0a5513 100644 --- a/examples/eatlove.js +++ b/examples/eatlove.js @@ -4,14 +4,14 @@ kaplay(); // A lttle game about eating fruit! const fruits = [ - "apple", - "pineapple", - "grape", - "watermelon", + "apple", + "pineapple", + "grape", + "watermelon", ]; for (const fruit of fruits) { - loadSprite(fruit, `/sprites/${fruit}.png`); + loadSprite(fruit, `/sprites/${fruit}.png`); } loadSprite("bean", "/sprites/bean.png"); @@ -20,130 +20,130 @@ loadSound("hit", "/examples/sounds/hit.mp3"); loadSound("wooosh", "/examples/sounds/wooosh.mp3"); scene("start", () => { - // Plays the wooosh sound - play("wooosh"); - - add([ - text("Eat All"), - pos(center().sub(0, 100)), - scale(2), - anchor("center"), - ]); - - add([ - sprite("heart"), - pos(center().add(0, 100)), - scale(2), - anchor("center"), - ]); - - wait(1.5, () => go("game")); + // Plays the wooosh sound + play("wooosh"); + + add([ + text("Eat All"), + pos(center().sub(0, 100)), + scale(2), + anchor("center"), + ]); + + add([ + sprite("heart"), + pos(center().add(0, 100)), + scale(2), + anchor("center"), + ]); + + wait(1.5, () => go("game")); }); // main game scene content scene("game", () => { - const SPEED_MIN = 120; - const SPEED_MAX = 640; - - // add the player game object - const player = add([ - sprite("bean"), - pos(40, 20), - area({ scale: 0.5 }), - anchor("center"), - ]); - - // make the layer move by mouse - player.onUpdate(() => { - player.pos = mousePos(); - }); - - // game over if player eats a fruit - player.onCollide("fruit", () => { - go("lose", score); - play("hit"); - }); - - // move the food every frame, destroy it if far outside of screen - onUpdate("food", (food) => { - food.move(-food.speed, 0); - if (food.pos.x < -120) { - destroy(food); - } - }); - - onUpdate("heart", (heart) => { - if (heart.pos.x <= 0) { - go("lose", score); - play("hit"); - addKaboom(heart.pos); - } - }); - - // score counter - let score = 0; - - const scoreLabel = add([ - text(score.toString(), { - size: 32, - }), - pos(12, 12), - ]); - - // increment score if player eats a heart - player.onCollide("heart", (heart) => { - addKaboom(player.pos); - score += 1; - destroy(heart); - scoreLabel.text = score.toString(); - burp(); - shake(12); - }); - - // do this every 0.3 seconds - loop(0.3, () => { - // spawn from right side of the screen - const x = width() + 24; - // spawn from a random y position - const y = rand(0, height()); - // get a random speed - const speed = rand(SPEED_MIN, SPEED_MAX); - // 50% percent chance is heart - const isHeart = chance(0.5); - const spriteName = isHeart ? "heart" : choose(fruits); - - add([ - sprite(spriteName), - pos(x, y), - area({ scale: 0.5 }), - anchor("center"), - "food", - isHeart ? "heart" : "fruit", - { speed: speed }, - ]); - }); + const SPEED_MIN = 120; + const SPEED_MAX = 640; + + // add the player game object + const player = add([ + sprite("bean"), + pos(40, 20), + area({ scale: 0.5 }), + anchor("center"), + ]); + + // make the layer move by mouse + player.onUpdate(() => { + player.pos = mousePos(); + }); + + // game over if player eats a fruit + player.onCollide("fruit", () => { + go("lose", score); + play("hit"); + }); + + // move the food every frame, destroy it if far outside of screen + onUpdate("food", (food) => { + food.move(-food.speed, 0); + if (food.pos.x < -120) { + destroy(food); + } + }); + + onUpdate("heart", (heart) => { + if (heart.pos.x <= 0) { + go("lose", score); + play("hit"); + addKaboom(heart.pos); + } + }); + + // score counter + let score = 0; + + const scoreLabel = add([ + text(score.toString(), { + size: 32, + }), + pos(12, 12), + ]); + + // increment score if player eats a heart + player.onCollide("heart", (heart) => { + addKaboom(player.pos); + score += 1; + destroy(heart); + scoreLabel.text = score.toString(); + burp(); + shake(12); + }); + + // do this every 0.3 seconds + loop(0.3, () => { + // spawn from right side of the screen + const x = width() + 24; + // spawn from a random y position + const y = rand(0, height()); + // get a random speed + const speed = rand(SPEED_MIN, SPEED_MAX); + // 50% percent chance is heart + const isHeart = chance(0.5); + const spriteName = isHeart ? "heart" : choose(fruits); + + add([ + sprite(spriteName), + pos(x, y), + area({ scale: 0.5 }), + anchor("center"), + "food", + isHeart ? "heart" : "fruit", + { speed: speed }, + ]); + }); }); // game over scene scene("lose", (score) => { - add([ - sprite("bean"), - pos(width() / 2, height() / 2 - 108), - scale(3), - anchor("center"), - ]); - - // display score - add([ - text(score), - pos(width() / 2, height() / 2 + 108), - scale(3), - anchor("center"), - ]); - - // go back to game with space is pressed - onKeyPress("space", () => go("start")); - onClick(() => go("start")); + add([ + sprite("bean"), + pos(width() / 2, height() / 2 - 108), + scale(3), + anchor("center"), + ]); + + // display score + add([ + text(score), + pos(width() / 2, height() / 2 + 108), + scale(3), + anchor("center"), + ]); + + // go back to game with space is pressed + onKeyPress("space", () => go("start")); + onClick(() => go("start")); }); // start with the "game" scene diff --git a/examples/platformBox.js b/examples/platformBox.js index 43f87201..f1eef5bc 100644 --- a/examples/platformBox.js +++ b/examples/platformBox.js @@ -46,7 +46,7 @@ const level = addLevel([ if (normal.sub(LEFT).len() < Number.EPSILON) return false; if (normal.sub(RIGHT).len() < Number.EPSILON) return false; return true; - } + }, }), ], }, diff --git a/examples/prettyDebug.js b/examples/prettyDebug.js index ae2a3288..df837326 100644 --- a/examples/prettyDebug.js +++ b/examples/prettyDebug.js @@ -7,12 +7,11 @@ const pretty = { nested: "objects", }, arrays: ["show", "like", "you", "would", "write", "them"], - "own toString is used": vec2(10, 10) -} + "own toString is used": vec2(10, 10), +}; debug.log("Text in [brackets] doesn't cause issues"); debug.log(pretty); debug.error("This is an error message"); - diff --git a/examples/tsconfig.json b/examples/tsconfig.json index 318c8ae1..2350c385 100644 --- a/examples/tsconfig.json +++ b/examples/tsconfig.json @@ -14,4 +14,4 @@ "moduleDetection": "force", "noImplicitAny": false } -} \ No newline at end of file +} diff --git a/examples/weirdTextTags.js b/examples/weirdTextTags.js index 36ddfe34..956ba41c 100644 --- a/examples/weirdTextTags.js +++ b/examples/weirdTextTags.js @@ -6,7 +6,7 @@ const txtEl = add([ pink: { color: MAGENTA, }, - } + }, }), pos(100, 100), ]); diff --git a/src/audio/play.ts b/src/audio/play.ts index 686e6d06..51712dbe 100644 --- a/src/audio/play.ts +++ b/src/audio/play.ts @@ -53,7 +53,7 @@ export interface AudioPlayOpt { * If the audio node should start out connected to another audio node rather than * KAPLAY's default volume node. Defaults to undefined, i.e. use KAPLAY's volume node. */ - connectTo?: AudioNode + connectTo?: AudioNode; } export interface AudioPlay { diff --git a/src/components/draw/sprite.ts b/src/components/draw/sprite.ts index 7cdcd5b3..e3265beb 100644 --- a/src/components/draw/sprite.ts +++ b/src/components/draw/sprite.ts @@ -232,14 +232,14 @@ export function sprite( animSpeed: opt.animSpeed ?? 1, flipX: opt.flipX ?? false, flipY: opt.flipY ?? false, - + get sprite() { return src.toString(); }, - + set sprite(src) { const spr = resolveSprite(src); - + if (spr) { spr.onLoad(spr => setSpriteData(this as unknown as GameObj, spr) @@ -251,13 +251,13 @@ export function sprite( if (!spriteData || !curAnim || curAnimDir === null) { return this.frame; } - + const anim = spriteData.anims[curAnim.name]; - + if (typeof anim === "number") { return anim; } - + return this.frame - Math.min(anim.from, anim.to); }, diff --git a/src/components/physics/effectors.ts b/src/components/physics/effectors.ts index f1465dc3..c532857f 100644 --- a/src/components/physics/effectors.ts +++ b/src/components/physics/effectors.ts @@ -116,8 +116,8 @@ export function pointEffector(opts: PointEffectorCompOpt): PointEffectorComp { const forceScale = this.forceMode === "constant" ? 1 : this.forceMode === "inverseLinear" - ? 1 / distance - : 1 / distance ** 2; + ? 1 / distance + : 1 / distance ** 2; const force = dir.scale( this.forceMagnitude * forceScale / length, ); @@ -155,23 +155,27 @@ export type PlatformEffectorCompOpt = { /** * If the object is about to collide and the collision normal direction is * in here, the object won't collide. - * + * * Should be a list of unit vectors `LEFT`, `RIGHT`, `UP`, or `DOWN`. */ - ignoreSides?: Vec2[] + ignoreSides?: Vec2[]; /** * A function that determines whether the object should collide. - * + * * If present, it overrides the `ignoreSides`; if absent, it is * automatically created from `ignoreSides`. */ - shouldCollide?: (this: GameObj, obj: GameObj, normal: Vec2) => boolean + shouldCollide?: ( + this: GameObj, + obj: GameObj, + normal: Vec2, + ) => boolean; }; export interface PlatformEffectorComp extends Comp { /** * A set of the objects that should not collide with this, because `shouldCollide` returned true. - * + * * Objects in here are automatically removed when they stop colliding, so the casual user shouldn't * need to touch this much. However, if an object is added to this set before the object collides * with the platform effector, it won't collide even if `shouldCollide` returns true. @@ -184,8 +188,9 @@ export function platformEffector( ): PlatformEffectorComp { opt.ignoreSides ??= [Vec2.UP]; opt.shouldCollide ??= (_, normal) => { - return opt.ignoreSides?.findIndex(s => s.sdist(normal) < Number.EPSILON) == -1; - } + return opt.ignoreSides?.findIndex(s => s.sdist(normal) < Number.EPSILON) + == -1; + }; return { id: "platformEffector", require: ["area", "body"], @@ -195,7 +200,13 @@ export function platformEffector( if (this.platformIgnore.has(collision.target)) { collision.preventResolution(); } - else if (!opt.shouldCollide!.call(this, collision.target, collision.normal)) { + else if ( + !opt.shouldCollide!.call( + this, + collision.target, + collision.normal, + ) + ) { collision.preventResolution(); this.platformIgnore.add(collision.target); } diff --git a/src/gfx/draw/drawDebug.ts b/src/gfx/draw/drawDebug.ts index 3c509ea9..1b16bf66 100644 --- a/src/gfx/draw/drawDebug.ts +++ b/src/gfx/draw/drawDebug.ts @@ -228,21 +228,31 @@ function prettyDebug(object: any | undefined, inside: boolean = false): string { outStr = [ "[", object.map(e => prettyDebug(e, true)).join(", "), - "]" + "]", ].join(""); object = outStr; } - if (typeof object === "object" - && object.toString === Object.prototype.toString) { - if (object.constructor !== Object) outStr += object.constructor.name + " "; - outStr += [ - "{", - (tmp = Object.getOwnPropertyNames(object) - .map(p => `${/^\w+$/.test(p) ? p : JSON.stringify(p)}: ${prettyDebug(object[p], true)}`) - .join(", ")) ? ` ${tmp} ` : "", - "}" - ].join(""); - object = outStr; + if ( + typeof object === "object" + && object.toString === Object.prototype.toString + ) { + if (object.constructor !== Object) { + outStr += object.constructor.name + " "; + } + outStr += [ + "{", + (tmp = Object.getOwnPropertyNames(object) + .map(p => + `${/^\w+$/.test(p) ? p : JSON.stringify(p)}: ${ + prettyDebug(object[p], true) + }` + ) + .join(", ")) + ? ` ${tmp} ` + : "", + "}", + ].join(""); + object = outStr; } return String(object).replaceAll(/(? { - if (styleStack.length > 0) + if (styleStack.length > 0) { charStyleMap[renderText.length] = styleStack.slice(); + } renderText += ch; }; while (text !== "") { if (text[0] === "\\") { - if (text.length === 1) + if (text.length === 1) { throw new Error("Styled text error: \\ at end of string"); + } emit(text[1]); text = text.slice(2); continue; @@ -72,14 +74,18 @@ export function compileStyledText(text: string): { if (e !== undefined) { const x = styleStack.pop(); if (x !== gn) { - if (x !== undefined) + if (x !== undefined) { throw new Error( "Styled text error: mismatched tags. " - + `Expected [/${x}], got [/${gn}]`); - else throw new Error( - `Styled text error: stray end tag [/${gn}]`) + + `Expected [/${x}], got [/${gn}]`, + ); + } + else {throw new Error( + `Styled text error: stray end tag [/${gn}]`, + );} } - } else styleStack.push(gn); + } + else styleStack.push(gn); text = text.slice(m.length); continue; } @@ -87,10 +93,11 @@ export function compileStyledText(text: string): { text = text.slice(1); } - if (styleStack.length > 0) + if (styleStack.length > 0) { throw new Error( - `Styled text error: unclosed tags ${styleStack}` + `Styled text error: unclosed tags ${styleStack}`, ); + } return { charStyleMap, @@ -128,14 +135,14 @@ export function formatText(opt: DrawTextOpt): FormattedText { outline: Outline | null; filter: TexFilter; } = font instanceof FontData - ? { - outline: font.outline, - filter: font.filter, - } - : { - outline: null, - filter: DEF_FONT_FILTER, - }; + ? { + outline: font.outline, + filter: font.filter, + } + : { + outline: null, + filter: DEF_FONT_FILTER, + }; // TODO: customizable font tex filter const atlas: FontAtlas = fontAtlases[fontName] ?? { diff --git a/src/math/math.ts b/src/math/math.ts index a43db93b..f818e7e8 100644 --- a/src/math/math.ts +++ b/src/math/math.ts @@ -116,9 +116,11 @@ export class Vec2 { /** Closest orthogonal direction: LEFT, RIGHT, UP, or DOWN */ toAxis(): Vec2 { - return Math.abs(this.x) > Math.abs(this.y) ? - this.x < 0 ? Vec2.LEFT : Vec2.RIGHT : - this.y < 0 ? Vec2.UP : Vec2.DOWN; + return Math.abs(this.x) > Math.abs(this.y) + ? this.x < 0 ? Vec2.LEFT : Vec2.RIGHT + : this.y < 0 + ? Vec2.UP + : Vec2.DOWN; } /** Clone the vector */ diff --git a/tsconfig.dts.json b/tsconfig.dts.json index ee4f0020..e8fef5ac 100644 --- a/tsconfig.dts.json +++ b/tsconfig.dts.json @@ -1,18 +1,18 @@ { - "compilerOptions": { - "esModuleInterop": true, - "target": "ESNext", - "module": "ESNext", - "moduleResolution": "Bundler", - "skipLibCheck": true, - "resolveJsonModule": true, - "emitDeclarationOnly": true, - "declaration": true, - "declarationMap": true, - "declarationDir": "dist/declaration", - "strict": true - }, - "include": [ - "src/**/*", - ] -} \ No newline at end of file + "compilerOptions": { + "esModuleInterop": true, + "target": "ESNext", + "module": "ESNext", + "moduleResolution": "Bundler", + "skipLibCheck": true, + "resolveJsonModule": true, + "emitDeclarationOnly": true, + "declaration": true, + "declarationMap": true, + "declarationDir": "dist/declaration", + "strict": true + }, + "include": [ + "src/**/*" + ] +} diff --git a/tsconfig.json b/tsconfig.json index f8438b60..e720729d 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -19,4 +19,4 @@ "src/**/*", "scripts/**/*" ] -} \ No newline at end of file +}