WiiDuktape/source/game.js
Fries fc7ca18fd7 get the bird to jump!
i added deltatime so the bird should hopefully have the same speed if
the framerate drops or if you play in PAL 50hz mode.
2024-06-21 22:57:08 -07:00

92 lines
2.2 KiB
JavaScript

/**
* returns an RGBA buffer
* @param {number} r red
* @param {number} g green
* @param {number} b blue
* @param {number} a alpha
* @returns {RGBA}
*/
function rgba(r, g, b, a) {
const buffer = Uint8Array.allocPlain(4);
buffer[0] = r;
buffer[1] = g;
buffer[2] = b;
buffer[3] = a;
return buffer;
}
const PAD_BUTTON_START = 0x1000;
const PAD_BUTTON_A = 0x0100;
const RED_COLOR = rgba(255, 0, 0, 255);
const GREEN_COLOR = rgba(0, 255, 0, 255);
const WHITE_COLOR = rgba(255, 255, 255, 255);
const FALLING_SPEED = 250;
const TERMINAL_VELOCITY_SPEED = 500;
function rgba_compare(rgba1, rgba2) {
return (rgba1[0] == rgba2[0]) &&
(rgba1[1] == rgba2[1]) &&
(rgba1[2] == rgba2[2]) &&
(rgba1[3] == rgba2[3]);
}
/**
* clamps a number to the min and max values.
* @param {number} num
* @param {number} min
* @param {number} max
* @returns a clamped number
*/
function clamp(num, min, max) {
return Math.min(Math.max(num, min), max);
}
globalThis.rectangle_color = RED_COLOR;
/** @type {GRRLibTexture} */
globalThis.background = null;
/** @type {GRRLibTexture} */
globalThis.bird = null;
globalThis.bird_velocity = 0;
globalThis.bird_position = 0;
function swap_colors() {
if (rgba_compare(rectangle_color, RED_COLOR)) {
rectangle_color = GREEN_COLOR;
} else if (rgba_compare(rectangle_color, GREEN_COLOR)) {
rectangle_color = RED_COLOR;
}
}
function start() {
const background = wii.get_file("background-day.png");
const bird = wii.get_file("yellowbird-midflap.png");
globalThis.background = wii.grrlib.load_texture(background);
globalThis.bird = wii.grrlib.load_texture(bird);
}
function update_bird() {
const deltatime = wii.get_deltatime();
bird_position -= bird_velocity * deltatime;
bird_velocity = clamp(bird_velocity - (FALLING_SPEED * deltatime), -TERMINAL_VELOCITY_SPEED, TERMINAL_VELOCITY_SPEED);
wii.grrlib.draw_img(0, bird_position, bird, 0, 1, 1, WHITE_COLOR);
}
function update() {
const pressed = wii.pad.buttons_down();
if (pressed & PAD_BUTTON_START) {
wii.print("exiting from js");
wii.exit();
}
if (pressed & PAD_BUTTON_A) {
bird_velocity = FALLING_SPEED;
}
wii.grrlib.draw_img(0, 0, background, 0, 1, 1, WHITE_COLOR);
update_bird();
wii.print("deltatime: " + wii.get_deltatime())
}