2024-06-19 07:39:01 +00:00
|
|
|
const PAD_BUTTON_START = 0x1000;
|
|
|
|
const PAD_BUTTON_A = 0x0100;
|
2024-06-25 00:37:38 +00:00
|
|
|
const RED_COLOR = wii.rgba(255, 0, 0, 255);
|
|
|
|
const GREEN_COLOR = wii.rgba(0, 255, 0, 255);
|
|
|
|
const WHITE_COLOR = wii.rgba(255, 255, 255, 255);
|
|
|
|
|
2024-06-22 05:57:08 +00:00
|
|
|
const FALLING_SPEED = 250;
|
|
|
|
const TERMINAL_VELOCITY_SPEED = 500;
|
2024-06-25 00:37:38 +00:00
|
|
|
const PIPE_SPAWN_TIME = 4;
|
|
|
|
const PIPE_MAX_TIME = 5;
|
2024-06-20 06:37:41 +00:00
|
|
|
|
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-25 00:37:38 +00:00
|
|
|
function random_range(min, max) {
|
|
|
|
const minCeiled = Math.ceil(min);
|
|
|
|
const maxFloored = Math.floor(max);
|
|
|
|
return Math.floor(Math.random() * (maxFloored - minCeiled) + minCeiled);
|
|
|
|
}
|
|
|
|
|
2024-06-21 07:45:28 +00:00
|
|
|
/** @type {GRRLibTexture} */
|
2024-06-21 01:41:49 +00:00
|
|
|
globalThis.background = null;
|
2024-06-25 00:37:38 +00:00
|
|
|
|
|
|
|
/** @type {DrawableObject} */
|
|
|
|
globalThis.bird = {
|
|
|
|
velocity: 0,
|
|
|
|
position: 0
|
|
|
|
};
|
|
|
|
|
2024-06-21 07:45:28 +00:00
|
|
|
/** @type {GRRLibTexture} */
|
2024-06-25 00:37:38 +00:00
|
|
|
globalThis.pipe = null;
|
2024-06-20 06:37:41 +00:00
|
|
|
|
2024-06-25 00:37:38 +00:00
|
|
|
/** @type {GRRLibTexture} */
|
|
|
|
globalThis.pipe_flipped = null;
|
2024-06-22 05:57:08 +00:00
|
|
|
|
2024-06-25 00:37:38 +00:00
|
|
|
/** @type {Pipe[]} */
|
|
|
|
globalThis.pipes = [];
|
|
|
|
|
|
|
|
/** @type {number} */
|
|
|
|
globalThis.timer = 0;
|
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");
|
2024-06-25 00:37:38 +00:00
|
|
|
const pipe = wii.get_file("pipe-green.png");
|
|
|
|
const pipe_flipped = wii.get_file("pipe-green-flipped.png");
|
2024-06-21 01:41:49 +00:00
|
|
|
|
|
|
|
globalThis.background = wii.grrlib.load_texture(background);
|
2024-06-25 00:37:38 +00:00
|
|
|
globalThis.bird.texture = wii.grrlib.load_texture(bird);
|
|
|
|
globalThis.pipe = wii.grrlib.load_texture(pipe);
|
|
|
|
globalThis.pipe_flipped = wii.grrlib.load_texture(pipe_flipped);
|
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();
|
2024-06-25 00:37:38 +00:00
|
|
|
bird.position -= bird.velocity * deltatime;
|
|
|
|
bird.velocity = clamp(bird.velocity - (FALLING_SPEED * deltatime), -TERMINAL_VELOCITY_SPEED, TERMINAL_VELOCITY_SPEED);
|
2024-06-22 05:57:08 +00:00
|
|
|
|
2024-06-25 00:37:38 +00:00
|
|
|
wii.grrlib.draw_img(0, bird.position, bird.texture, 0, 1, 1, WHITE_COLOR);
|
|
|
|
}
|
|
|
|
|
|
|
|
function create_pipe() {
|
|
|
|
const top_max = 200;
|
|
|
|
const top_y = random_range(0, top_max);
|
|
|
|
|
|
|
|
wii.grrlib.draw_img(100, -top_y, pipe_flipped, 0, 1, 1, WHITE_COLOR);
|
|
|
|
wii.grrlib.draw_img(100, top_y + 400, pipe, 0, 1, 1, WHITE_COLOR);
|
|
|
|
|
|
|
|
return {
|
|
|
|
top_y: -top_y,
|
|
|
|
bottom_y: -top_y + 400,
|
|
|
|
position: 650,
|
|
|
|
time_active: 0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {Pipe} pipe
|
|
|
|
*/
|
|
|
|
function update_pipe(pipe) {
|
|
|
|
if (pipe.time_active >= PIPE_MAX_TIME) {
|
|
|
|
const index = pipes.indexOf(pipe);
|
|
|
|
pipes.splice(index, -1);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
wii.grrlib.draw_img(pipe.position, pipe.top_y, pipe_flipped, 0, 1, 1, WHITE_COLOR);
|
|
|
|
wii.grrlib.draw_img(pipe.position, pipe.bottom_y, globalThis.pipe, 0, 1, 1, WHITE_COLOR);
|
|
|
|
pipe.position -= 100 * wii.get_deltatime();
|
|
|
|
pipe.time_active += wii.get_deltatime();
|
2024-06-22 05:57:08 +00:00
|
|
|
}
|
|
|
|
|
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-25 00:37:38 +00:00
|
|
|
bird.velocity = FALLING_SPEED;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (timer >= PIPE_SPAWN_TIME) {
|
|
|
|
timer = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (timer == 0) {
|
|
|
|
pipes.push(create_pipe());
|
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();
|
2024-06-25 00:37:38 +00:00
|
|
|
for (var i = 0; i < pipes.length; i++) {
|
|
|
|
update_pipe(pipes[i]);
|
|
|
|
}
|
2024-06-22 05:57:08 +00:00
|
|
|
wii.print("deltatime: " + wii.get_deltatime())
|
2024-06-25 00:37:38 +00:00
|
|
|
timer += wii.get_deltatime();
|
2024-06-19 07:39:01 +00:00
|
|
|
}
|