Skip to content

Commit

Permalink
feat: show health gain in green
Browse files Browse the repository at this point in the history
  • Loading branch information
ker0olos committed Feb 12, 2024
1 parent e4993dd commit 8b87c3f
Show file tree
Hide file tree
Showing 10 changed files with 66 additions and 12 deletions.
4 changes: 2 additions & 2 deletions build/dyn_images.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
/* eslint-disable */
/**
* @param {number} left
* @param {number} damage
* @param {number} diff
* @returns {Uint8Array}
*/
export function hp(left: number, damage: number): Uint8Array;
export function hp(left: number, diff: number): Uint8Array;
6 changes: 3 additions & 3 deletions build/dyn_images.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,11 @@ function getStringFromWasm0(ptr, len) {
}
/**
* @param {number} left
* @param {number} damage
* @param {number} diff
* @returns {Uint8Array}
*/
export function hp(left, damage) {
const ret = wasm.hp(left, damage);
export function hp(left, diff) {
const ret = wasm.hp(left, diff);
return takeObject(ret);
}

Expand Down
Binary file modified build/dyn_images_bg.wasm
Binary file not shown.
2 changes: 1 addition & 1 deletion deno.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"tasks": {
"build": "wasm-pack build --target deno --out-dir build",
"watch": "cargo watch -w src/lib.rs -s \"deno task build\""
"dev": "cargo watch -w src/lib.rs -s \"deno task build\""
},
"fmt": {
"useTabs": false,
Expand Down
11 changes: 7 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,22 @@ use wasm_bindgen::prelude::*;
const WHITE: image::Rgba<u8> = image::Rgba([255, 255, 255, 255]);
const GREY: image::Rgba<u8> = image::Rgba([163, 163, 163, 255]);
const RED: image::Rgba<u8> = image::Rgba([222, 38, 38, 255]);
const GREEN: image::Rgba<u8> = image::Rgba([43, 181, 64, 255]);
// const BLANK: image::Rgba<u8> = image::Rgba([0, 0, 0, 0]);

#[wasm_bindgen]
pub fn hp(left: u32, damage: u32) -> js_sys::Uint8Array {
pub fn hp(left: u32, diff: i32) -> js_sys::Uint8Array {
console_error_panic_hook::set_once();

let w: u32 = 350 * u32::max(left, 0) / 100;
let d: u32 = 350 * u32::max(damage, 0) / 100;
let w: u32 = 350 * u32::max(if diff > 0 { left - diff as u32 } else { left }, 0) / 100;
let d: u32 = 350 * u32::max(diff.abs() as u32, 0) / 100;

let img = image::RgbaImage::from_fn(350, 15, |x, _| {
if (0..w).contains(&x) {
WHITE
} else if damage > 0 && (w..w + d + 1).contains(&x) {
} else if diff > 0 && (w..w + d + 1).contains(&x) {
GREEN
} else if diff < 0 && (w..w + d + 1).contains(&x) {
RED
} else {
GREY
Expand Down
Binary file added test/__snapshots__/hp 10 heal 10.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added test/__snapshots__/hp 100 heal 10.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added test/__snapshots__/hp 50 heal 1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added test/__snapshots__/hp 50 heal 10.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
55 changes: 53 additions & 2 deletions test/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ Deno.test('hp 90 damage 10', async (test) => {
join(directory, `__snapshots__/${test.name}.png`),
);

const image = hp(90, 10);
const image = hp(90, -10);

if (!existsSync(snapShotPath)) {
await Deno.writeFile(
Expand All @@ -119,7 +119,7 @@ Deno.test('hp 83 damage 17', async (test) => {
join(directory, `__snapshots__/${test.name}.png`),
);

const image = hp(83, 17);
const image = hp(83, -17);

if (!existsSync(snapShotPath)) {
await Deno.writeFile(
Expand All @@ -136,6 +136,40 @@ Deno.test('hp 50 damage 10', async (test) => {
join(directory, `__snapshots__/${test.name}.png`),
);

const image = hp(50, -10);

if (!existsSync(snapShotPath)) {
await Deno.writeFile(
snapShotPath,
image,
);
} else {
assertEquals(await compare(snapShotPath, image), 0);
}
});

Deno.test('hp 10 heal 10', async (test) => {
const snapShotPath = new URL(
join(directory, `__snapshots__/${test.name}.png`),
);

const image = hp(10, 10);

if (!existsSync(snapShotPath)) {
await Deno.writeFile(
snapShotPath,
image,
);
} else {
assertEquals(await compare(snapShotPath, image), 0);
}
});

Deno.test('hp 50 heal 10', async (test) => {
const snapShotPath = new URL(
join(directory, `__snapshots__/${test.name}.png`),
);

const image = hp(50, 10);

if (!existsSync(snapShotPath)) {
Expand All @@ -147,3 +181,20 @@ Deno.test('hp 50 damage 10', async (test) => {
assertEquals(await compare(snapShotPath, image), 0);
}
});

Deno.test('hp 50 heal 1', async (test) => {
const snapShotPath = new URL(
join(directory, `__snapshots__/${test.name}.png`),
);

const image = hp(50, 1);

if (!existsSync(snapShotPath)) {
await Deno.writeFile(
snapShotPath,
image,
);
} else {
assertEquals(await compare(snapShotPath, image), 0);
}
});

0 comments on commit 8b87c3f

Please sign in to comment.