Last active
June 21, 2022 20:41
-
-
Save matt-dray/389af1afa61a778bbd1f286912b43627 to your computer and use it in GitHub Desktop.
Testing out {isocubes} with a Link sprite, procedural dungeon and the logo for rostrum.blog
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Testing the {isocube} package by coolbutuseless (mikefc) | |
# Package on GitHub: https://github.com/coolbutuseless/isocubes | |
# Matt Dray, June 2022 | |
# Attach generally-needed packages | |
library(grid) # installed with base R | |
library(purrr) # install from CRAN | |
library(isocubes) # remotes::install_github("coolbutuseless/isocubes") | |
# 1. Link ---------------------------------------------------------------- | |
# Prepare vector of pixels for a face-on sprite of Link from Legend of Zelda, | |
# via https://www.rostrum.blog/2021/06/28/pixel-art/#sprite-delight | |
link_v <- c( | |
0,0,0,0,0,1,1,1,1,1,1,0,0,0,0,0, | |
0,0,0,0,1,1,1,1,1,1,1,1,0,0,0,0, | |
0,0,2,0,1,3,3,3,3,3,3,1,0,2,0,0, | |
0,0,2,0,3,3,3,3,3,3,3,3,0,2,0,0, | |
0,0,2,2,3,2,1,2,2,1,2,3,2,2,0,0, | |
0,0,2,2,3,2,3,2,2,3,2,3,2,2,0,0, | |
0,0,0,2,2,2,2,2,2,2,2,2,2,3,0,0, | |
0,0,0,1,1,2,2,3,3,2,2,1,1,3,0,0, | |
0,3,3,3,3,3,2,2,2,2,1,1,3,3,3,0, | |
3,3,2,3,3,3,3,1,1,1,1,1,2,3,3,0, | |
3,2,2,2,3,3,2,3,3,1,1,2,2,2,3,0, | |
3,3,2,3,3,3,2,1,3,3,3,3,2,2,2,0, | |
3,3,2,3,3,3,2,3,3,1,1,1,1,2,0,0, | |
3,3,3,3,3,3,2,1,1,1,1,1,0,0,0,0, | |
0,2,2,2,2,2,3,0,0,3,3,3,0,0,0,0, | |
0,0,0,0,3,3,3,0,0,0,0,0,0,0,0,0 | |
) | |
# Prepare matrix | |
link_m <- matrix(link_v, ncol = 16) | |
link_m <- link_m[, ncol(link_m):1] # invert columns | |
link_m <- t(link_m) # transpose | |
link_ind <- rbind( | |
transform( | |
which(link_m == 1, arr.ind = TRUE), | |
fill = "#7bc702" | |
), | |
transform( | |
which(link_m == 2, arr.ind = TRUE), | |
fill = "#cc8f2d" | |
), | |
transform( | |
which(link_m == 3, arr.ind = TRUE), | |
fill = "#6c430a" | |
) | |
) | |
# Prepare cubes | |
coords <- data.frame(x = link_ind$col, y = link_ind$row, z = 0) | |
cubes <- isocubesGrob(coords, max_y = 25, fill = link_ind$fill, xc = 0.4) | |
# Render | |
grid.newpage(); grid.draw(cubes) | |
# 2. Dungeon ------------------------------------------------------------- | |
# Make a procedural dungeon via the {r.oguelike} package, see | |
# https://www.rostrum.blog/2022/05/01/dungeon/ | |
# Install package from GitHub | |
remotes::install_github("matt-dray/r.oguelike") # if not yet installed | |
# Prepare common column count | |
columns <- 50 | |
# Generate dungeon | |
dungeon_h <- r.oguelike::generate_dungeon(n_col = columns) | |
# Prepare height matrix | |
dungeon_h[which(dungeon_h == ".")] <- 1 | |
dungeon_h[which(dungeon_h == "#")] <- 2 | |
dungeon_h <- matrix(as.numeric(dungeon_h), ncol = columns) | |
# Prepare colour matrix | |
dungeon_c <- dungeon_h | |
dungeon_c[which(dungeon_c == 1)] <- "#000000" | |
dungeon_c[which(dungeon_c == 2)] <- "#964B00" | |
# Prepare cubes | |
coords <- coords_heightmap(dungeon_h, col = dungeon_c) | |
cubes <- isocubesGrob(coords, max_y = columns, fill = coords$col, xc = 1) | |
# Render | |
grid.newpage(); grid.draw(cubes) | |
# 3. Hex logo for rostrum.blog -------------------------------------------- | |
# Prepare vector of pixels for the insect logo of the rostrum.blog, | |
# via https://www.rostrum.blog/2021/06/28/pixel-art/#reprologoducibility | |
logo_v <- c( | |
0,0,0,0,1,0,0,0,0,0,0,1,0,0,0,0, | |
0,0,0,0,0,1,0,0,0,0,1,0,0,0,0,0, | |
0,0,0,0,0,0,1,0,0,1,0,0,0,0,0,0, | |
0,0,1,0,0,1,0,1,1,0,1,0,0,1,0,0, | |
0,0,0,1,0,1,0,1,1,0,1,0,1,0,0,0, | |
0,0,0,1,0,0,1,1,1,1,0,0,1,0,0,0, | |
0,0,0,0,1,1,0,0,0,0,1,1,0,0,0,0, | |
0,0,0,0,0,0,1,1,1,1,0,0,0,0,0,0, | |
0,0,1,0,1,1,0,1,1,0,1,1,0,1,0,0, | |
0,0,0,1,0,1,0,1,1,0,1,0,1,0,0,0, | |
0,0,0,0,0,1,0,1,1,0,1,0,0,0,0,0, | |
0,0,0,0,1,1,0,1,1,0,1,1,0,0,0,0, | |
0,0,0,1,0,1,0,1,1,0,1,0,1,0,0,0, | |
0,0,0,1,0,1,0,1,1,0,1,0,1,0,0,0, | |
0,0,1,0,0,1,0,1,1,0,1,0,0,1,0,0, | |
0,0,0,0,0,0,1,0,0,1,0,0,0,0,0,0 | |
) | |
# Prepare matrix | |
logo_m <- matrix(logo_v, ncol = 16) | |
logo_m <- logo_m[, ncol(logo_m):1] # invert columns | |
logo_m <- t(logo_m) # transpose | |
# Prepare coordinates | |
logo_ind <- as.data.frame(which(logo_m == 1, arr.ind = TRUE)) | |
x <- logo_ind$col | |
y <- logo_ind$row | |
# Prepare cubes | |
coords <- data.frame(x = x, y = y, z = 0) | |
cubes <- isocubesGrob(coords, max_y = 25, fill = rainbow(nrow(coords))) | |
# Render | |
grid.newpage(); grid.draw(cubes) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment