//
// Part of you-will-return
// Copyright (c) 2022 Sander in 't Veld
// License: MIT
//

use crate::wasm4::*;

pub fn draw_tile(x: i32, y: i32, seed: u32)
{
	let w = ALIEN_TILE_WIDTH as i32;
	let h = ALIEN_TILE_HEIGHT as i32;
	draw(x + 1, y + 1, (seed & 0xFF) as u8);
	draw(x + w, y + 1, ((seed >> 8) & 0xFF) as u8);
	draw(x + 1, y + h, ((seed >> 16) & 0xFF) as u8);
	draw(x + w, y + h, ((seed >> 24) & 0xFF) as u8);
}

pub fn draw(x: i32, y: i32, seed: u8)
{
	let offset = (seed as u32) % NUM_ALIEN_TILES;
	blit_sub(
		&ALIEN_TILE_SHEET,
		x,
		y,
		ALIEN_TILE_WIDTH,
		ALIEN_TILE_HEIGHT,
		(offset % ALIEN_TILE_FRAMES_PER_ROW) * ALIEN_TILE_WIDTH,
		(offset / ALIEN_TILE_FRAMES_PER_ROW) * ALIEN_TILE_HEIGHT,
		ALIEN_TILE_SHEET_WIDTH,
		ALIEN_TILE_SHEET_FLAGS,
	);
}

// alien_tile
const ALIEN_TILE_WIDTH: u32 = 8;
const ALIEN_TILE_HEIGHT: u32 = 8;
const ALIEN_TILE_FRAMES_PER_ROW: u32 =
	ALIEN_TILE_SHEET_WIDTH / ALIEN_TILE_WIDTH;
const ALIEN_TILE_FRAMES_PER_COL: u32 =
	ALIEN_TILE_SHEET_HEIGHT / ALIEN_TILE_HEIGHT;
const NUM_ALIEN_TILES: u32 =
	ALIEN_TILE_FRAMES_PER_ROW * ALIEN_TILE_FRAMES_PER_COL;

// alien_tile_sheet
const ALIEN_TILE_SHEET_WIDTH: u32 = 64;
const ALIEN_TILE_SHEET_HEIGHT: u32 = 32;
const ALIEN_TILE_SHEET_FLAGS: u32 = BLIT_1BPP;
const ALIEN_TILE_SHEET: [u8; 256] = [
	0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x55, 0x7d, 0x6d, 0x7d,
	0x7d, 0x55, 0x7d, 0x7d, 0x45, 0x11, 0x45, 0x05, 0x45, 0x45, 0x11, 0x45,
	0x55, 0x5d, 0x55, 0x75, 0x55, 0x7d, 0x5d, 0x7d, 0x45, 0x01, 0x55, 0x05,
	0x45, 0x45, 0x11, 0x45, 0x7d, 0x7d, 0x55, 0x7d, 0x7d, 0x55, 0x7d, 0x7d,
	0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0xff, 0xff, 0xff, 0xff,
	0xff, 0xff, 0xff, 0xff, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
	0x7d, 0x75, 0x7d, 0x6d, 0x6d, 0x7d, 0x75, 0x75, 0x55, 0x55, 0x01, 0x45,
	0x45, 0x55, 0x15, 0x45, 0x7d, 0x5d, 0x7d, 0x11, 0x55, 0x11, 0x75, 0x75,
	0x55, 0x55, 0x01, 0x45, 0x45, 0x45, 0x41, 0x15, 0x7d, 0x55, 0x7d, 0x7d,
	0x6d, 0x7d, 0x7d, 0x75, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
	0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x01, 0x01, 0x01, 0x01,
	0x01, 0x01, 0x01, 0x01, 0x5d, 0x75, 0x55, 0x7d, 0x7d, 0x55, 0x5d, 0x55,
	0x41, 0x55, 0x45, 0x05, 0x01, 0x55, 0x11, 0x55, 0x75, 0x55, 0x55, 0x55,
	0x6d, 0x01, 0x7d, 0x7d, 0x05, 0x55, 0x45, 0x41, 0x01, 0x55, 0x11, 0x55,
	0x7d, 0x5d, 0x55, 0x7d, 0x7d, 0x55, 0x75, 0x55, 0x01, 0x01, 0x01, 0x01,
	0x01, 0x01, 0x01, 0x01, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
	0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x7d, 0x5d, 0x7d, 0x75,
	0x75, 0x7d, 0x7d, 0x55, 0x45, 0x45, 0x01, 0x45, 0x51, 0x45, 0x55, 0x15,
	0x7d, 0x5d, 0x7d, 0x5d, 0x7d, 0x6d, 0x55, 0x75, 0x41, 0x45, 0x45, 0x45,
	0x15, 0x45, 0x45, 0x05, 0x7d, 0x5d, 0x7d, 0x75, 0x5d, 0x7d, 0x6d, 0x7d,
	0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0xff, 0xff, 0xff, 0xff,
	0xff, 0xff, 0xff, 0xff,
];
