2019-09-25 14:40:10 +00:00
|
|
|
const SNES_WIDTH = 256;
|
|
|
|
const SNES_HEIGHT = 224;
|
|
|
|
|
2019-09-24 23:44:08 +00:00
|
|
|
const Orientation = {
|
|
|
|
UP: 'up',
|
|
|
|
DOWN: 'down',
|
|
|
|
LEFT: 'left',
|
|
|
|
RIGHT: 'right'
|
|
|
|
}
|
|
|
|
|
2019-09-25 19:49:19 +00:00
|
|
|
function bound(low, x, high) {
|
|
|
|
return Math.max(low, Math.min(x, high));
|
|
|
|
}
|
|
|
|
|
2019-09-25 20:17:44 +00:00
|
|
|
// Representation of the state of the buttons on an SNES controller. (This may
|
|
|
|
// be the result of keyboard or gamepad input, which get mapped to the
|
|
|
|
// standard SNES buttons.)
|
2019-09-25 20:10:03 +00:00
|
|
|
class SnesInput {
|
2019-09-24 18:57:24 +00:00
|
|
|
constructor() {
|
2019-09-25 21:22:04 +00:00
|
|
|
this.reset();
|
|
|
|
}
|
|
|
|
|
|
|
|
reset() {
|
2019-09-24 18:57:24 +00:00
|
|
|
this.up = false;
|
|
|
|
this.down = false;
|
2019-09-25 00:16:01 +00:00
|
|
|
this.left = false;
|
|
|
|
this.right = false;
|
2019-09-24 18:57:24 +00:00
|
|
|
this.a = false;
|
|
|
|
this.b = false;
|
|
|
|
this.x = false;
|
|
|
|
this.y = false;
|
|
|
|
this.l = false;
|
|
|
|
this.r = false;
|
|
|
|
this.select = false;
|
|
|
|
this.start = false;
|
|
|
|
}
|
|
|
|
|
2019-09-25 20:10:03 +00:00
|
|
|
copyFrom(other) {
|
|
|
|
this.up = other.up;
|
|
|
|
this.down = other.down;
|
|
|
|
this.left = other.left;
|
|
|
|
this.right = other.right;
|
|
|
|
this.a = other.a;
|
|
|
|
this.b = other.b;
|
|
|
|
this.x = other.x;
|
|
|
|
this.y = other.y;
|
|
|
|
this.l = other.l;
|
|
|
|
this.r = other.r;
|
|
|
|
this.select = other.select;
|
|
|
|
this.start = other.start;
|
2019-09-24 18:57:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
toString() {
|
|
|
|
let result = '';
|
|
|
|
|
|
|
|
if (this.up) {
|
|
|
|
result += '^';
|
|
|
|
} else if (this.down) {
|
|
|
|
result += 'v';
|
|
|
|
} else {
|
|
|
|
result += '-';
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.left) {
|
|
|
|
result += '<';
|
|
|
|
} else if (this.right) {
|
|
|
|
result += '>';
|
|
|
|
} else {
|
|
|
|
result += '-';
|
|
|
|
}
|
|
|
|
|
|
|
|
result += ' ';
|
|
|
|
|
|
|
|
if (this.a) {
|
|
|
|
result += 'A';
|
|
|
|
}
|
|
|
|
if (this.b) {
|
|
|
|
result += 'B';
|
|
|
|
}
|
|
|
|
if (this.x) {
|
|
|
|
result += 'X';
|
|
|
|
}
|
|
|
|
if (this.y) {
|
|
|
|
result += 'Y';
|
|
|
|
}
|
|
|
|
if (this.l) {
|
|
|
|
result += 'L';
|
|
|
|
}
|
|
|
|
if (this.r) {
|
|
|
|
result += 'R';
|
|
|
|
}
|
|
|
|
if (this.select) {
|
|
|
|
result += 's';
|
|
|
|
}
|
|
|
|
if (this.start) {
|
|
|
|
result += 'S';
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-25 21:22:04 +00:00
|
|
|
class SnesGamepad {
|
|
|
|
update(input) {
|
|
|
|
const gamepads = navigator.getGamepads();
|
|
|
|
if (gamepads.length < 1 || !gamepads[0] || !gamepads[0].connected) {
|
|
|
|
input.reset();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
this.wasConnected = true;
|
|
|
|
// TODO: have a config screen instead of hard-coding the 8Bitdo SNES30 pad.
|
|
|
|
const gamepad = gamepads[0];
|
|
|
|
input.up = gamepad.axes[1] < 0;
|
|
|
|
input.down = gamepad.axes[1] > 0;
|
|
|
|
input.left = gamepad.axes[0] < 0;
|
|
|
|
input.right = gamepad.axes[0] > 0;
|
|
|
|
input.a = gamepad.buttons[0].pressed;
|
|
|
|
input.b = gamepad.buttons[1].pressed;
|
|
|
|
input.x = gamepad.buttons[3].pressed;
|
|
|
|
input.y = gamepad.buttons[4].pressed;
|
|
|
|
input.l = gamepad.buttons[6].pressed;
|
|
|
|
input.r = gamepad.buttons[7].pressed;
|
|
|
|
input.select = gamepad.buttons[10].pressed;
|
|
|
|
input.start = gamepad.buttons[11].pressed;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-25 20:10:03 +00:00
|
|
|
class InputHandler {
|
|
|
|
constructor() {
|
|
|
|
this.keysPressed = {};
|
2019-09-25 21:22:04 +00:00
|
|
|
this.gamepad = new SnesGamepad();
|
|
|
|
window.addEventListener(
|
|
|
|
'gamepadconnected', (e) => this.gamepadConnected(e));
|
|
|
|
window.addEventListener(
|
|
|
|
'gamepaddisconnected', (e) => this.gamepadDisconnected(e));
|
2019-09-25 20:10:03 +00:00
|
|
|
document.addEventListener('keydown', (e) => this.keyDown(e));
|
|
|
|
document.addEventListener('keyup', (e) => this.keyUp(e));
|
|
|
|
}
|
|
|
|
|
|
|
|
keyDown(e) {
|
|
|
|
this.keysPressed[e.key] = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
keyUp(e) {
|
|
|
|
this.keysPressed[e.key] = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
update(input) {
|
2019-09-25 21:22:04 +00:00
|
|
|
this.gamepad.update(input);
|
|
|
|
|
2019-09-25 20:10:03 +00:00
|
|
|
// Default ZSNES keybindings. See:
|
|
|
|
// http://zsnes-docs.sourceforge.net/html/readme.htm#default_keys
|
2019-09-25 21:22:04 +00:00
|
|
|
input.up |= this.keysPressed['ArrowUp'];
|
|
|
|
input.down |= this.keysPressed['ArrowDown'];
|
|
|
|
input.left |= this.keysPressed['ArrowLeft'];
|
|
|
|
input.right |= this.keysPressed['ArrowRight'];
|
|
|
|
input.start |= this.keysPressed['Enter'];
|
|
|
|
input.select |= this.keysPressed['Shift'];
|
|
|
|
input.a |= this.keysPressed['x'];
|
|
|
|
input.b |= this.keysPressed['z'];
|
|
|
|
input.x |= this.keysPressed['s'];
|
|
|
|
input.y |= this.keysPressed['a'];
|
|
|
|
input.l |= this.keysPressed['d'];
|
|
|
|
input.r |= this.keysPressed['c'];
|
|
|
|
|
2019-09-25 20:10:03 +00:00
|
|
|
debug(input.toString());
|
|
|
|
}
|
|
|
|
|
|
|
|
gamepadConnected(e) {
|
|
|
|
debug('gamepad connected! :)');
|
|
|
|
console.log('gamepad connected @ index %d: %d buttons, %d axes\n[%s]',
|
|
|
|
e.gamepad.index, e.gamepad.buttons.length, e.gamepad.axes.length,
|
|
|
|
e.gamepad.id);
|
|
|
|
}
|
|
|
|
|
|
|
|
gamepadDisconnected(e) {
|
|
|
|
debug('gamepad disconnected :(');
|
|
|
|
console.log('gamepad disconnected @ index %d:\n[%s]', e.gamepad.index,
|
|
|
|
e.gamepad.id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-24 18:57:24 +00:00
|
|
|
class Graphics {
|
|
|
|
constructor(canvas) {
|
|
|
|
this.canvas_ = canvas;
|
|
|
|
this.ctx_ = canvas.getContext('2d');
|
2019-09-25 01:21:23 +00:00
|
|
|
this.ctx_.imageSmoothingEnabled = false;
|
2019-09-24 18:57:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
get width() {
|
|
|
|
return this.canvas_.width;
|
|
|
|
}
|
|
|
|
|
|
|
|
get height() {
|
|
|
|
return this.canvas_.height;
|
|
|
|
}
|
|
|
|
|
|
|
|
fill(color) {
|
|
|
|
this.ctx_.fillStyle = color;
|
|
|
|
this.ctx_.beginPath();
|
|
|
|
this.ctx_.rect(0, 0, this.canvas_.width, this.canvas_.height);
|
|
|
|
this.ctx_.fill();
|
|
|
|
this.ctx_.closePath();
|
|
|
|
}
|
|
|
|
|
|
|
|
circle(x, y, radius, color) {
|
|
|
|
this.ctx_.fillStyle = color;
|
|
|
|
this.ctx_.beginPath();
|
|
|
|
this.ctx_.arc(x, y, radius, 0, 2 * Math.PI)
|
|
|
|
this.ctx_.fill();
|
|
|
|
this.ctx_.closePath();
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: replace with custom sprite-based text rendering.
|
|
|
|
text(string, x, y, size, color) {
|
|
|
|
this.ctx_.fillStyle = color;
|
|
|
|
this.ctx_.font = '' + size + 'px monospace';
|
|
|
|
this.ctx_.fillText(string, x, y);
|
|
|
|
}
|
2019-09-24 23:44:08 +00:00
|
|
|
|
2019-09-25 19:06:27 +00:00
|
|
|
drawSprite(sprite, dx, dy) {
|
2019-09-24 23:44:08 +00:00
|
|
|
this.ctx_.drawImage(
|
2019-09-25 19:06:27 +00:00
|
|
|
sprite.image,
|
|
|
|
sprite.ulx, sprite.uly,
|
|
|
|
sprite.width, sprite.height,
|
|
|
|
dx, dy,
|
|
|
|
sprite.width, sprite.height);
|
2019-09-24 23:44:08 +00:00
|
|
|
}
|
2019-09-24 18:57:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
class FpsCounter {
|
|
|
|
constructor() {
|
|
|
|
this.fps = 0;
|
|
|
|
this.frameTimes_ = new Array(60);
|
|
|
|
this.idx_ = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
update(timestampMs) {
|
|
|
|
if (this.frameTimes_[this.idx_]) {
|
|
|
|
const timeElapsed = (timestampMs - this.frameTimes_[this.idx_]) / 1000;
|
|
|
|
this.fps = this.frameTimes_.length / timeElapsed;
|
|
|
|
}
|
|
|
|
this.frameTimes_[this.idx_] = timestampMs;
|
|
|
|
this.idx_++;
|
|
|
|
if (this.idx_ == this.frameTimes_.length) {
|
|
|
|
this.idx_ = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
draw(gfx) {
|
2019-09-24 23:44:08 +00:00
|
|
|
const fpsDiv = document.getElementById('fps');
|
|
|
|
fpsDiv.innerText = 'FPS: ' + Math.round(this.fps);
|
2019-09-24 18:57:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class World {
|
|
|
|
constructor() {
|
|
|
|
this.state_ = null;
|
|
|
|
this.fpsCounter_ = new FpsCounter();
|
2019-09-25 20:10:03 +00:00
|
|
|
this.inputHandler_ = new InputHandler();
|
|
|
|
this.input_ = new SnesInput();
|
|
|
|
this.lastInput_ = new SnesInput();
|
2019-09-24 23:44:08 +00:00
|
|
|
this.player_ = new Player();
|
|
|
|
|
|
|
|
// TODO: move rendering stuff to a separate object.
|
|
|
|
this.resources_ = new Resources();
|
|
|
|
this.tileRenderer_ = new TileRenderer();
|
|
|
|
this.playerRenderer_ = new PlayerRenderer();
|
2019-09-24 18:57:24 +00:00
|
|
|
this.gamepadRenderer_ = new GamepadRenderer();
|
|
|
|
}
|
|
|
|
|
|
|
|
update(timestampMs) {
|
|
|
|
this.fpsCounter_.update(timestampMs);
|
2019-09-25 20:10:03 +00:00
|
|
|
|
|
|
|
// We copy values to avoid allocating new SnesInput objects every frame.
|
|
|
|
// TODO: is this actually worth it?
|
|
|
|
this.lastInput_.copyFrom(this.input_);
|
|
|
|
this.inputHandler_.update(this.input_);
|
|
|
|
|
|
|
|
if (!this.lastInput_.r && this.input_.r) {
|
2019-09-25 19:06:27 +00:00
|
|
|
this.player_.cycleSprite();
|
|
|
|
}
|
2019-09-24 23:44:08 +00:00
|
|
|
if (this.input_.left) {
|
|
|
|
this.player_.moveLeft();
|
|
|
|
}
|
|
|
|
if (this.input_.right) {
|
|
|
|
this.player_.moveRight();
|
|
|
|
}
|
|
|
|
if (this.input_.up) {
|
|
|
|
this.player_.moveUp();
|
|
|
|
}
|
|
|
|
if (this.input_.down) {
|
|
|
|
this.player_.moveDown();
|
|
|
|
}
|
2019-09-24 18:57:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
draw(gfx) {
|
2019-09-24 23:44:08 +00:00
|
|
|
gfx.fill('black');
|
|
|
|
this.tileRenderer_.draw(gfx, this.resources_.sprites);
|
|
|
|
this.playerRenderer_.draw(gfx, this.resources_.sprites, this.player_);
|
|
|
|
// this.gamepadRenderer_.draw(gfx, this.input_);
|
2019-09-24 18:57:24 +00:00
|
|
|
this.fpsCounter_.draw(gfx);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-25 19:06:27 +00:00
|
|
|
class Sprite {
|
|
|
|
constructor(image, ulx, uly, width, height) {
|
|
|
|
this.image = image;
|
|
|
|
this.ulx = ulx;
|
|
|
|
this.uly = uly;
|
|
|
|
this.width = width;
|
|
|
|
this.height = height;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class CharacterSprite {
|
|
|
|
constructor(image, ulx, uly, tileWidth, tileHeight) {
|
|
|
|
// Assumption: a character sprite consists of 4 rows, which include the
|
|
|
|
// character facing down, left, right, up (in that order). Each row has 3
|
|
|
|
// columns, which can be used for a walking animation.
|
|
|
|
this.down = [];
|
|
|
|
this.left = [];
|
|
|
|
this.right = [];
|
|
|
|
this.up = [];
|
|
|
|
for (let i = 0; i < 3; i++) {
|
|
|
|
const x = ulx + i * tileWidth;
|
|
|
|
this.down.push(new Sprite(
|
|
|
|
image, x, uly, tileWidth, tileHeight));
|
|
|
|
this.left.push(new Sprite(
|
|
|
|
image, x, uly + tileHeight, tileWidth, tileHeight));
|
|
|
|
this.right.push(new Sprite(
|
|
|
|
image, x, uly + tileHeight * 2, tileWidth, tileHeight));
|
|
|
|
this.up.push(new Sprite(
|
|
|
|
image, x, uly + tileHeight * 3, tileWidth, tileHeight));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-24 23:44:08 +00:00
|
|
|
class Resources {
|
|
|
|
constructor() {
|
|
|
|
const atlantis = document.getElementById('atlantis');
|
|
|
|
const ghost = document.getElementById('ghost');
|
2019-09-25 19:06:27 +00:00
|
|
|
const cats = document.getElementById('cats');
|
2019-09-24 23:44:08 +00:00
|
|
|
const ts = 16;
|
|
|
|
this.sprites = {
|
2019-09-25 19:06:27 +00:00
|
|
|
'ground0': new Sprite(atlantis, 2 * ts, 1 * ts, 16, 16),
|
|
|
|
'ground1': new Sprite(atlantis, 3 * ts, 1 * ts, 16, 16),
|
|
|
|
'ground2': new Sprite(atlantis, 4 * ts, 1 * ts, 16, 16),
|
|
|
|
'ground3': new Sprite(atlantis, 5 * ts, 1 * ts, 16, 16),
|
|
|
|
'ground4': new Sprite(atlantis, 6 * ts, 1 * ts, 16, 16),
|
|
|
|
'ground5': new Sprite(atlantis, 7 * ts, 1 * ts, 16, 16),
|
|
|
|
'ground6': new Sprite(atlantis, 8 * ts, 1 * ts, 16, 16),
|
|
|
|
'rock0': new Sprite(atlantis, 1 * ts, 2 * ts, 16, 16),
|
|
|
|
'rock1': new Sprite(atlantis, 2 * ts, 2 * ts, 16, 16),
|
|
|
|
'rock2': new Sprite(atlantis, 3 * ts, 2 * ts, 16, 16),
|
|
|
|
'anchor0': new Sprite(atlantis, 21 * ts, 1 * ts, 16, 16),
|
|
|
|
'seaweed0': new Sprite(atlantis, 20 * ts, 2 * ts, 16, 32),
|
|
|
|
'seaweed1': new Sprite(atlantis, 16 * ts, 2 * ts, 16, 32),
|
|
|
|
'coral0': new Sprite(atlantis, 15 * ts, 9 * ts, 32, 16),
|
|
|
|
'rockpile0': new Sprite(atlantis, 17 * ts, 10 * ts, 32, 32),
|
|
|
|
|
|
|
|
'ghost': new CharacterSprite(ghost, 0, 0, 26, 36),
|
|
|
|
'cat0': new CharacterSprite(cats, 0, 0, 26, 36),
|
|
|
|
'cat1': new CharacterSprite(cats, 26 * 3, 0, 26, 36),
|
|
|
|
'cat2': new CharacterSprite(cats, 26 * 6, 0, 26, 36),
|
|
|
|
'cat3': new CharacterSprite(cats, 26 * 9, 0, 26, 36),
|
|
|
|
'cat4': new CharacterSprite(cats, 0, 36 * 4, 26, 36),
|
|
|
|
'cat5': new CharacterSprite(cats, 26 * 3, 36 * 4, 26, 36),
|
|
|
|
'cat6': new CharacterSprite(cats, 26 * 6, 36 * 4, 26, 36),
|
|
|
|
'cat7': new CharacterSprite(cats, 26 * 9, 36 * 4, 26, 36),
|
2019-09-24 23:44:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class Player {
|
2019-09-25 14:40:10 +00:00
|
|
|
// TODO: stop hard-coding player bounding box.
|
2019-09-24 23:44:08 +00:00
|
|
|
constructor() {
|
2019-09-25 14:40:10 +00:00
|
|
|
this.x = (SNES_WIDTH - 26) / 2;
|
|
|
|
this.y = (SNES_HEIGHT - 36) / 2;
|
2019-09-24 23:44:08 +00:00
|
|
|
this.orientation = Orientation.DOWN;
|
2019-09-25 19:06:27 +00:00
|
|
|
this.spriteNames_ = [
|
|
|
|
'ghost', 'cat0', 'cat1', 'cat2', 'cat3', 'cat4', 'cat5', 'cat6',
|
|
|
|
'cat7'];
|
|
|
|
this.spriteNamesIdx_ = 3;
|
|
|
|
}
|
|
|
|
|
|
|
|
get spriteName() {
|
|
|
|
return this.spriteNames_[this.spriteNamesIdx_];
|
|
|
|
}
|
|
|
|
|
|
|
|
cycleSprite() {
|
|
|
|
this.spriteNamesIdx_++;
|
|
|
|
if (this.spriteNamesIdx_ >= this.spriteNames_.length) {
|
|
|
|
this.spriteNamesIdx_ = 0;
|
|
|
|
}
|
2019-09-24 23:44:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
moveLeft() {
|
|
|
|
this.orientation = Orientation.LEFT;
|
|
|
|
this.x -= 2;
|
|
|
|
if (this.x < -4) {
|
|
|
|
this.x = -4;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
moveRight() {
|
|
|
|
this.orientation = Orientation.RIGHT;
|
|
|
|
this.x += 2;
|
2019-09-25 14:40:10 +00:00
|
|
|
if (this.x > SNES_WIDTH - 21) {
|
|
|
|
this.x = SNES_WIDTH - 21;
|
2019-09-24 23:44:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
moveUp() {
|
|
|
|
this.orientation = Orientation.UP;
|
|
|
|
this.y -= 2;
|
|
|
|
if (this.y < -7) {
|
|
|
|
this.y = -7;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
moveDown() {
|
|
|
|
this.orientation = Orientation.DOWN;
|
|
|
|
this.y += 2;
|
2019-09-25 14:40:10 +00:00
|
|
|
if (this.y > SNES_HEIGHT - 36) {
|
|
|
|
this.y = SNES_HEIGHT - 36;
|
2019-09-24 23:44:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class PlayerRenderer {
|
|
|
|
constructor() {
|
|
|
|
this.frameNum = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
draw(gfx, sprites, player) {
|
|
|
|
let spriteIndex = Math.floor((this.frameNum % 40) / 10);
|
|
|
|
if (spriteIndex == 3) { spriteIndex = 1; }
|
2019-09-25 19:06:27 +00:00
|
|
|
const charSprite = sprites[player.spriteName][player.orientation][spriteIndex];
|
|
|
|
gfx.drawSprite(charSprite, player.x, player.y);
|
2019-09-24 23:44:08 +00:00
|
|
|
this.frameNum++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class TileRenderer {
|
|
|
|
draw(gfx, sprites) {
|
|
|
|
const tileSize = 16;
|
|
|
|
const rows = gfx.height / tileSize;
|
|
|
|
const columns = gfx.width / tileSize;
|
|
|
|
const layer1 = ["-,*-...*'.,-_'`o",
|
|
|
|
"_..'-_**,',_.'oo",
|
|
|
|
"-*-''_-'o,0O_```",
|
|
|
|
"o`0_._,*O'`--'-'",
|
|
|
|
"`0O-_'',`o*o*`-,",
|
|
|
|
"*,`'---o'O'_*''-",
|
|
|
|
"'-.**.'_'`.,'-.'",
|
|
|
|
".O'``*``'`*,,_o`",
|
|
|
|
"_*_''*O'`_OO-_'o",
|
|
|
|
"0`0,*-,`_*'`O'*.",
|
|
|
|
".o'-*.*_',`,,`.'",
|
|
|
|
"`o`O',.`OO,*-'**",
|
|
|
|
"-..*'-''',*'.'.O",
|
|
|
|
"*-_'-0.--__O`O`_",
|
|
|
|
"*-_,O_'*'`*'_._.",
|
|
|
|
"-.*,`OO'_`'*-0-O"];
|
|
|
|
const layer2 = [" ",
|
|
|
|
" ",
|
2019-09-25 00:54:10 +00:00
|
|
|
" iil ",
|
2019-09-24 23:44:08 +00:00
|
|
|
" ",
|
|
|
|
" A ",
|
|
|
|
" ",
|
|
|
|
" ",
|
|
|
|
" ",
|
|
|
|
" ",
|
|
|
|
" i ",
|
|
|
|
" l ",
|
|
|
|
" ",
|
|
|
|
" c R ",
|
2019-09-25 00:54:10 +00:00
|
|
|
" c "];
|
2019-09-24 23:44:08 +00:00
|
|
|
const spriteLookup = {
|
|
|
|
'.': sprites.ground0,
|
|
|
|
',': sprites.ground1,
|
|
|
|
'_': sprites.ground2,
|
|
|
|
'`': sprites.ground3,
|
|
|
|
'-': sprites.ground4,
|
|
|
|
'*': sprites.ground5,
|
|
|
|
"'": sprites.ground6,
|
|
|
|
'o': sprites.rock0,
|
|
|
|
'O': sprites.rock1,
|
|
|
|
'0': sprites.rock2,
|
|
|
|
'A': sprites.anchor0,
|
|
|
|
'i': sprites.seaweed0,
|
|
|
|
'l': sprites.seaweed1,
|
|
|
|
'c': sprites.coral0,
|
|
|
|
'R': sprites.rockpile0,
|
|
|
|
};
|
|
|
|
for (let j = 0; j < columns; j++) {
|
|
|
|
for (let i = 0; i < rows; i++) {
|
|
|
|
const dx = tileSize * j;
|
|
|
|
const dy = tileSize * i;
|
|
|
|
const sprite = spriteLookup[layer1[i][j]];
|
|
|
|
if (sprite) {
|
2019-09-25 19:06:27 +00:00
|
|
|
gfx.drawSprite(sprite, dx, dy);
|
2019-09-24 23:44:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for (let j = 0; j < columns; j++) {
|
|
|
|
for (let i = 0; i < rows; i++) {
|
|
|
|
const dx = tileSize * j;
|
|
|
|
const dy = tileSize * i;
|
|
|
|
const sprite = spriteLookup[layer2[i][j]];
|
|
|
|
if (sprite) {
|
2019-09-25 19:06:27 +00:00
|
|
|
gfx.drawSprite(sprite, dx, dy);
|
2019-09-24 23:44:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-24 18:57:24 +00:00
|
|
|
class GamepadRenderer {
|
|
|
|
draw(gfx, input) {
|
|
|
|
const centerX = gfx.width / 2;
|
|
|
|
const centerY = gfx.height / 2;
|
|
|
|
|
|
|
|
// Select & Start
|
|
|
|
gfx.circle(centerX + 12, centerY, 8, input.start ? 'cyan' : 'grey');
|
|
|
|
gfx.circle(centerX - 12, centerY, 8, input.select ? 'cyan' : 'grey');
|
|
|
|
|
|
|
|
// Y X B A
|
|
|
|
gfx.circle(centerX + 48, centerY, 8, input.y ? 'cyan' : 'grey');
|
|
|
|
gfx.circle(centerX + 64, centerY - 16, 8, input.x ? 'cyan' : 'grey');
|
|
|
|
gfx.circle(centerX + 64, centerY + 16, 8, input.b ? 'cyan' : 'grey');
|
|
|
|
gfx.circle(centerX + 80, centerY, 8, input.a ? 'cyan' : 'grey');
|
|
|
|
|
|
|
|
// dpad
|
|
|
|
gfx.circle(centerX - 48, centerY, 8, input.right ? 'cyan' : 'grey');
|
|
|
|
gfx.circle(centerX - 64, centerY - 16, 8, input.up ? 'cyan' : 'grey');
|
|
|
|
gfx.circle(centerX - 64, centerY + 16, 8, input.down ? 'cyan' : 'grey');
|
|
|
|
gfx.circle(centerX - 80, centerY, 8, input.left ? 'cyan' : 'grey');
|
|
|
|
|
|
|
|
// L & R
|
|
|
|
gfx.circle(centerX + 30, centerY - 32, 8, input.r ? 'cyan' : 'grey');
|
|
|
|
gfx.circle(centerX - 30, centerY - 32, 8, input.l ? 'cyan' : 'grey');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function debug(message) {
|
|
|
|
const debugDiv = document.getElementById('debug');
|
|
|
|
debugDiv.innerText = message;
|
|
|
|
}
|
|
|
|
|
|
|
|
function loop(world, gfx) {
|
|
|
|
return timestampMs => {
|
|
|
|
world.update(timestampMs);
|
|
|
|
world.draw(gfx);
|
|
|
|
window.requestAnimationFrame(loop(world, gfx));
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
function setCanvasScale(scale) {
|
|
|
|
const canvas = document.getElementById('canvas');
|
2019-09-25 14:40:10 +00:00
|
|
|
canvas.style.width = '' + SNES_WIDTH * scale + 'px';
|
|
|
|
canvas.style.height = '' + SNES_HEIGHT * scale + 'px';
|
2019-09-25 14:24:31 +00:00
|
|
|
canvas.style.display = '';
|
2019-09-25 14:40:10 +00:00
|
|
|
debug('set scale to ' + scale + 'x');
|
2019-09-25 14:24:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function setAutoCanvasScale() {
|
2019-09-25 14:40:10 +00:00
|
|
|
const widthAspect = Math.floor(window.innerWidth / SNES_WIDTH);
|
|
|
|
const heightAspect = Math.floor(window.innerHeight / SNES_HEIGHT);
|
2019-09-25 19:49:19 +00:00
|
|
|
const scale = bound(1, Math.min(widthAspect, heightAspect), 8);
|
2019-09-25 14:24:31 +00:00
|
|
|
setCanvasScale(scale);
|
2019-09-24 18:57:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function init() {
|
|
|
|
const world = new World();
|
|
|
|
const gfx = new Graphics(document.getElementById('canvas'));
|
|
|
|
|
|
|
|
document.getElementById('1x').onclick = () => setCanvasScale(1);
|
|
|
|
document.getElementById('2x').onclick = () => setCanvasScale(2);
|
|
|
|
document.getElementById('3x').onclick = () => setCanvasScale(3);
|
|
|
|
document.getElementById('4x').onclick = () => setCanvasScale(4);
|
|
|
|
document.getElementById('5x').onclick = () => setCanvasScale(5);
|
2019-09-25 14:24:31 +00:00
|
|
|
document.getElementById('6x').onclick = () => setCanvasScale(6);
|
|
|
|
document.getElementById('7x').onclick = () => setCanvasScale(7);
|
|
|
|
document.getElementById('8x').onclick = () => setCanvasScale(8);
|
|
|
|
setAutoCanvasScale();
|
2019-09-24 18:57:24 +00:00
|
|
|
window.requestAnimationFrame(loop(world, gfx));
|
|
|
|
}
|
|
|
|
|
|
|
|
init();
|