2024-06-21 07:45:28 +00:00
|
|
|
/**
|
|
|
|
* returns an RGBA buffer
|
|
|
|
* @param {number} r red
|
|
|
|
* @param {number} g green
|
|
|
|
* @param {number} b blue
|
|
|
|
* @param {number} a alpha
|
|
|
|
* @returns {RGBA}
|
|
|
|
*/
|
2024-06-20 06:37:41 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2024-06-19 07:39:01 +00:00
|
|
|
const PAD_BUTTON_START = 0x1000;
|
|
|
|
const PAD_BUTTON_A = 0x0100;
|
2024-06-20 06:37:41 +00:00
|
|
|
const RED_COLOR = rgba(255, 0, 0, 255);
|
|
|
|
const GREEN_COLOR = rgba(0, 255, 0, 255);
|
2024-06-21 01:41:49 +00:00
|
|
|
const WHITE_COLOR = rgba(255, 255, 255, 255);
|
2024-06-22 05:57:08 +00:00
|
|
|
const FALLING_SPEED = 250;
|
|
|
|
const TERMINAL_VELOCITY_SPEED = 500;
|
2024-06-20 06:37:41 +00:00
|
|
|
|
|
|
|
function rgba_compare(rgba1, rgba2) {
|
|
|
|
return (rgba1[0] == rgba2[0]) &&
|
|
|
|
(rgba1[1] == rgba2[1]) &&
|
|
|
|
(rgba1[2] == rgba2[2]) &&
|
|
|
|
(rgba1[3] == rgba2[3]);
|
|
|
|
}
|
|
|
|
|
2024-06-22 05:57:08 +00:00
|
|
|
/**
|
|
|
|
* 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);
|
|
|
|
}
|
2024-06-21 07:45:28 +00:00
|
|
|
|
2024-06-22 05:57:08 +00:00
|
|
|
globalThis.rectangle_color = RED_COLOR;
|
2024-06-21 07:45:28 +00:00
|
|
|
/** @type {GRRLibTexture} */
|
2024-06-21 01:41:49 +00:00
|
|
|
globalThis.background = null;
|
2024-06-21 07:45:28 +00:00
|
|
|
/** @type {GRRLibTexture} */
|
|
|
|
globalThis.bird = null;
|
2024-06-20 06:37:41 +00:00
|
|
|
|
2024-06-22 05:57:08 +00:00
|
|
|
globalThis.bird_velocity = 0;
|
|
|
|
globalThis.bird_position = 0;
|
|
|
|
|
2024-06-20 06:37:41 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
2024-06-19 07:39:01 +00:00
|
|
|
|
2024-06-20 04:08:16 +00:00
|
|
|
function start() {
|
2024-06-21 01:41:49 +00:00
|
|
|
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);
|
2024-06-20 04:08:16 +00:00
|
|
|
}
|
2024-06-19 07:39:01 +00:00
|
|
|
|
2024-06-22 05:57:08 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2024-06-20 04:08:16 +00:00
|
|
|
function update() {
|
2024-06-19 07:39:01 +00:00
|
|
|
const pressed = wii.pad.buttons_down();
|
|
|
|
|
|
|
|
if (pressed & PAD_BUTTON_START) {
|
|
|
|
wii.print("exiting from js");
|
|
|
|
wii.exit();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (pressed & PAD_BUTTON_A) {
|
2024-06-22 05:57:08 +00:00
|
|
|
bird_velocity = FALLING_SPEED;
|
2024-06-19 07:39:01 +00:00
|
|
|
}
|
|
|
|
|
2024-06-21 07:45:28 +00:00
|
|
|
wii.grrlib.draw_img(0, 0, background, 0, 1, 1, WHITE_COLOR);
|
2024-06-22 05:57:08 +00:00
|
|
|
update_bird();
|
|
|
|
wii.print("deltatime: " + wii.get_deltatime())
|
2024-06-19 07:39:01 +00:00
|
|
|
}
|