Skip to content

Instantly share code, notes, and snippets.

@Kethku
Last active February 22, 2019 08:45
Show Gist options
  • Save Kethku/46394495fbd4c3b6cae31a9a0f749521 to your computer and use it in GitHub Desktop.
Save Kethku/46394495fbd4c3b6cae31a9a0f749521 to your computer and use it in GitHub Desktop.
SCRIPT-8
// title: Marching Squares
// Dev log can be found at http://02credits.com/projects/8bomb/
// The change set is described here: http://02credits.com/blog/day11-marching-squares/
const tileWidth = 8;
const tileHeight = 8;
const dirtColor = 1;
function initTerrain() {
return [
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0],
[0, 0, 1, 0, 1, 0, 0, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0],
[0, 0, 1, 1, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0],
[0, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
]
}
initialState = {
terrain: initTerrain(),
cursorX: 12,
cursorY: 1
};
function drawTile(tx, ty, topLeft, topRight, bottomRight, bottomLeft) {
let s = 0;
if (topLeft) s += 1;
if (topRight) s += 2;
if (bottomRight) s += 4;
if (bottomLeft) s += 8;
sprite(tx * tileWidth, ty * tileHeight, s);
}
function drawTerrain(terrain) {
for (let y = 0; y < terrain.length - 1; y++) {
for (let x = 0; x < terrain[y].length - 1; x++) {
drawTile(x, y, terrain[y][x], terrain[y][x + 1], terrain[y + 1][x + 1], terrain[y + 1][x]);
}
}
}
function drawCursor(cx, cy) {
circStroke(cx * tileWidth, cy * tileHeight, 8, 3);
}
update = (state, input) => {
if (input.aPressed) {
state.terrain[state.cursorY][state.cursorX] = true;
}
if (input.bPressed) {
state.terrain[state.cursorY][state.cursorX] = false;
}
if (input.leftPressed) {
state.cursorX = state.cursorX - 1;
if (state.cursorX < 0) {
state.cursorX += 17;
}
}
if (input.rightPressed) {
state.cursorX = state.cursorX + 1;
if (state.cursorX >= 17) {
state.cursorX -= 17;
}
}
if (input.upPressed) {
state.cursorY = state.cursorY - 1;
if (state.cursorY < 0) {
state.cursorY += 17;
}
}
if (input.downPressed) {
state.cursorY = state.cursorY + 1;
if (state.cursorY >= 17) {
state.cursorY -= 17;
}
}
}
draw = state => {
clear();
drawTerrain(state.terrain);
drawCursor(state.cursorX, state.cursorY);
print(0, 0, "Arrows to move cursor");
print(0, 8, "A to draw");
print(0, 16, "B to erase");
}
{
"lines": [
104,
0,
0,
0,
0,
0,
0,
0
]
}
{
"1": [
"1111 ",
"1112 ",
"112 ",
"12 ",
"2 ",
" ",
" ",
" "
],
"2": [
" 1111",
" 2111",
" 211",
" 21",
" 2",
" ",
" ",
" "
],
"3": [
"11111111",
"11111111",
"11111111",
"11111111",
"22222222",
" ",
" ",
" "
],
"4": [
" ",
" ",
" ",
" 0",
" 01",
" 011",
" 0111",
" 1111"
],
"5": [
"1111 ",
"11110 ",
"111110 ",
"11111100",
"22111111",
" 211111",
" 21111",
" 1111"
],
"6": [
" 1111",
" 1111",
" 1111",
" 1111",
" 1111",
" 1111",
" 1111",
" 1111"
],
"7": [
"11111111",
"11111111",
"11111111",
"11111111",
"21111111",
" 1111111",
" 2111111",
" 221111"
],
"8": [
" ",
" ",
" ",
"0 ",
"10 ",
"110 ",
"1110 ",
"1111 "
],
"9": [
"1111 ",
"1111 ",
"1111 ",
"1111 ",
"1111 ",
"1111 ",
"1111 ",
"1111 "
],
"10": [
" 1111",
" 01111",
" 011111",
"00111111",
"11111122",
"111112 ",
"11112 ",
"1111 "
],
"11": [
"11111111",
"11111111",
"11111111",
"11111111",
"11111112",
"1111111 ",
"1111112 ",
"111122 "
],
"12": [
" ",
" ",
" ",
"00000000",
"11111111",
"11111111",
"11111111",
"11111111"
],
"13": [
"111100 ",
"1111110 ",
"1111111 ",
"11111110",
"11111111",
"11111111",
"11111111",
"11111111"
],
"14": [
" 001111",
" 0111111",
" 1111111",
"01111111",
"11111111",
"11111111",
"11111111",
"11111111"
],
"15": [
"11111111",
"11111111",
"11111111",
"11111111",
"11111111",
"11111111",
"11111111",
"11111111"
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment