Skip to content

Instantly share code, notes, and snippets.

@Kyrremann
Created October 19, 2015 19:57
Show Gist options
  • Save Kyrremann/2b5622ba1a96119a29df to your computer and use it in GitHub Desktop.
Save Kyrremann/2b5622ba1a96119a29df to your computer and use it in GitHub Desktop.
A simple map editor, using some of the images from Kenney Game Assets (http://kenney.itch.io/kenney-donation)
int TILE_SIZE = 64;
boolean DEBUG = false;
PImage tileImages[];
int tileIndex = 1;
int map[][];
PImage backgroundImage;
void setup() {
size(768, 512);
noCursor();
tileImages = new PImage[] {
loadImage("data/land_grass11.png"),
loadImage("data/road_asphalt01.png"),
loadImage("data/road_asphalt02.png"),
loadImage("data/road_asphalt03.png"),
loadImage("data/road_asphalt05.png"),
loadImage("data/road_asphalt39.png"),
loadImage("data/road_asphalt41.png"),
loadImage("data/road_asphalt42.png"),
loadImage("data/road_asphalt43.png"),
loadImage("data/tent_blue.png"),
loadImage("data/tent_red.png"),
loadImage("data/tree.png"),
loadImage("data/barrier_white01.png"),
loadImage("data/barrier_white02.png"),
loadImage("data/barrier_white03.png"),
loadImage("data/barrier_white04.png")
};
map = new int[512/TILE_SIZE][768/TILE_SIZE];
backgroundImage = loadImage("data/land_grass11.png");
}
void draw() {
background(255);
drawBackground();
drawMap();
if (DEBUG) {
drawGrid();
}
drawMouseTile();
}
void drawBackground() {
imageMode(CORNER);
for (int y = 0; y < height; y += TILE_SIZE) {
for (int x = 0; x < width; x += TILE_SIZE) {
image(backgroundImage, x, y, TILE_SIZE, TILE_SIZE);
}
}
}
void drawMap() {
for (int y = 0; y < map.length; y++) {
for (int x = 0; x < map[y].length; x++) {
int tileIndex = map[y][x];
PImage tileImage = tileImages[tileIndex];
image(tileImage, x * TILE_SIZE, y * TILE_SIZE, TILE_SIZE, TILE_SIZE);
}
}
}
void drawGrid() {
for (int y = 0; y < map.length; y++) {
line(0, y * TILE_SIZE, 768, y * TILE_SIZE);
for (int x = 0; x < map[y].length; x++) {
line(x * TILE_SIZE, 0, x * TILE_SIZE, 512);
}
}
}
void drawMouseTile() {
int x = mouseX / TILE_SIZE;
int y = mouseY / TILE_SIZE;
PImage tileImage = tileImages[tileIndex];
image(tileImage, x * TILE_SIZE, y * TILE_SIZE, TILE_SIZE, TILE_SIZE);
}
void mouseWheel(MouseEvent event) {
float e = event.getCount();
if (e > 0) {
nextTile();
} else {
previousTile();
}
}
void mousePressed() {
if (mouseButton == LEFT) {
int x = mouseX / TILE_SIZE;
int y = mouseY / TILE_SIZE;
map[y][x] = tileIndex;
}
}
void keyPressed() {
switch(key) {
case 'd':
DEBUG = !DEBUG;
return;
case '+':
nextTile();
return;
case '-':
previousTile();
return;
}
}
void nextTile() {
tileIndex++;
if (tileIndex >= tileImages.length) {
tileIndex = 0;
}
}
void previousTile() {
tileIndex--;
if (tileIndex < 0) {
tileIndex = tileImages.length - 1;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment