Skip to content

Instantly share code, notes, and snippets.

@icarito
Forked from Kethku/README.md
Created February 19, 2019 00:17
Show Gist options
  • Save icarito/e826e26bbee12a183b00dd2b433b3a5a to your computer and use it in GitHub Desktop.
Save icarito/e826e26bbee12a183b00dd2b433b3a5a to your computer and use it in GitHub Desktop.
SCRIPT-8
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);
}
function pressed(state, input, key) {
let result = false;
if (input[key]) {
if (!state[key]) {
result = true;
}
state[key] = true;
} else {
state[key] = false;
}
return result;
}
update = (state, input) => {
if (input.a) {
state.terrain[state.cursorY][state.cursorX] = true;
}
if (input.b) {
state.terrain[state.cursorY][state.cursorX] = false;
}
if (pressed(state, input, "left")) {
state.cursorX = state.cursorX - 1;
if (state.cursorX < 0) {
state.cursorX += 17;
}
}
if (pressed(state, input, "right")) {
state.cursorX = state.cursorX + 1;
if (state.cursorX >= 17) {
state.cursorX -= 17;
}
}
if (pressed(state, input, "up")) {
state.cursorY = state.cursorY - 1;
if (state.cursorY < 0) {
state.cursorY += 17;
}
}
if (pressed(state, input, "down")) {
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");
}
{
"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