Skip to content

Instantly share code, notes, and snippets.

@jonathontoon
Last active June 30, 2022 08:57
Show Gist options
  • Save jonathontoon/d814438760ec4480f57759d136a32bda to your computer and use it in GitHub Desktop.
Save jonathontoon/d814438760ec4480f57759d136a32bda to your computer and use it in GitHub Desktop.
WASM4 Game Progress
import * as g from "./globals";
import Tile from "./tile";
import Point from "./point";
import { getNeighboringPoints, getRandomPoint, inBounds } from "./helpers";
class Level {
private size: i32 = 20;
public numberOfMines: i32;
public data: Array<Array<Tile>>;
constructor(numberOfMines: i32) {
this.numberOfMines = numberOfMines;
this.data = new Array<Array<Tile>>();
this.generate();
};
private placeRandomTiles(numberOfTiles: i32, type: string): void {
for (let i: i32 = 0; i < numberOfTiles; i++) {
const randomPoint: Point = getRandomPoint(this.data, this.size);
this.data[randomPoint.x][randomPoint.y] = new Tile(randomPoint.x, randomPoint.y, type);
}
};
private calculateAdjacentMines(): void {
for (let i: i32 = 0; i < this.size; i++) {
for (let j: i32 = 0; j < this.size; j++) {
let adjacentMines: i32 = 0;
const points: Array<Point> = getNeighboringPoints(i, j, this.size, g.ALL_DIRECTIONS);
for (let p: i32 = 0; p < points.length; p++) {
const point: Point = points[p];
if (this.data[point.x][point.y].type == "@") {
adjacentMines += 1;
};
}
if (this.data[i][j].type == "-") {
this.data[i][j].type = adjacentMines.toString();
}
}
}
};
public generate(): void {
for (let i: i32 = 0; i < this.size; i++) {
this.data[i] = new Array<Tile>();
for (let j: i32 = 0; j < this.size; j++) {
if (inBounds(i, j, this.size, this.size)) {
this.data[i][j] = new Tile(i, j, "-");
} else {
this.data[i][j] = new Tile(i, j, "#");
}
}
}
this.placeRandomTiles(this.numberOfMines, "@");
this.calculateAdjacentMines();
};
public draw(): void {
for (let i: i32 = 0; i < this.size; i++) {
for (let j: i32 = 0; j < this.size; j++) {
this.data[i][j].draw();
}
}
};
};
export default Level;
import * as w4 from "./wasm4";
import Level from "./level";
let previousGamepad: u8;
let currentLevel!: Level;
function setPallete(): void {
store<u32>(w4.PALETTE, 0x332c50, 0 * sizeof<u32>());
store<u32>(w4.PALETTE, 0x46878f, 1 * sizeof<u32>());
store<u32>(w4.PALETTE, 0x94e344, 2 * sizeof<u32>());
store<u32>(w4.PALETTE, 0xe2f3e4, 3 * sizeof<u32>());
};
function input(): void {
const gamepad = load<u8>(w4.GAMEPAD1);
// Only the buttons that were pressed down this frame
const pressedThisFrame = gamepad & (gamepad ^ previousGamepad);
previousGamepad = gamepad;
if (pressedThisFrame & w4.BUTTON_2) {
w4.trace("pressed");
currentLevel.generate();
}
};
export function start(): void {
setPallete();
currentLevel = new Level(40);
};
export function update(): void {
input();
currentLevel.draw();
};
class Point {
public x: i32;
public y: i32;
constructor(x: i32, y: i32) {
this.x = x;
this.y = y;
}
};
export default Point;
import * as w4 from "./wasm4";
import Point from "./point";
class Tile extends Point {
public type: string;
constructor(x: i32, y: i32, type: string) {
super(x, y);
this.type = type;
};
public draw(): void {
store<u16>(w4.DRAW_COLORS, 0x42);
w4.text(this.type, this.x * 8, this.y * 8);
};
};
export default Tile;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment