Skip to content

Instantly share code, notes, and snippets.

@xyzshantaram
Created October 25, 2019 19:44
Show Gist options
  • Save xyzshantaram/5fda744acd33cb59470c3e327a37cecf to your computer and use it in GitHub Desktop.
Save xyzshantaram/5fda744acd33cb59470c3e327a37cecf to your computer and use it in GitHub Desktop.
import javax.swing.JOptionPane;
import java.util.Arrays;
import java.util.Map;
import java.util.HashMap;
import java.util.List;
import java.util.Iterator;
int gridSpacing = 32;
int[] numCells = {0, 0};
int[] playerPos = {0, 0};
int[][] tiles;
PGraphics pg;
Map tileNames;
static boolean isNullOrEmpty(String str) {
if(str != null && !str.isEmpty())
return false;
return true;
}
void setup() {
size(600, 600);
String num = JOptionPane.showInputDialog(null, "Number of cells");
String[] numArray = split(num, ",");
if (!isNullOrEmpty(num) && !isNullOrEmpty(numArray[0]) && !isNullOrEmpty(numArray[1])) {
numCells[0] = Integer.parseInt(numArray[0].trim());
numCells[1] = Integer.parseInt(numArray[1].trim());
}
else {
numCells[0] = 5;
numCells[1] = 5;
}
tiles = new int[numCells[0]][numCells[1]];
for (int i = 0; i < numCells[0]; i++) {
for (int j = 0; j < numCells[1]; j++) {
tiles[i][j] = 0;
}
}
tileNames = new HashMap<String, String>();
tileNames.put("0", "ground.png");
rectMode(CORNERS);
}
int camX = 0;
int camY = 0;
boolean initialised = false;
void draw() {
//draw vertical lines for grid
translate(camX, camY);
background(0);
for (int i = 0; i < numCells[0]; i++) {
for (int j = 0; j < numCells[1]; j++) {
text(tiles[i][j]+"", i * gridSpacing + gridSpacing/2, j * gridSpacing + gridSpacing/2);
}
}
}
void mousePressed() {
if (mouseButton == LEFT) {
int locationX = (mouseX - mouseX % gridSpacing);
int locationY = (mouseY - mouseY % gridSpacing);
locationX -= camX;
locationY -= camY;
println(locationX + " " + locationY);
int i = constrain(locationX/gridSpacing, 0, numCells[0] - 1);
int j = constrain(locationY/gridSpacing, 0, numCells[1] - 1);
tiles[i][j] = input();
}
if (mouseButton == RIGHT) {
String kv = "";
Iterator<Map.Entry<String, String>> itr = tileNames.entrySet().iterator();
while(itr.hasNext()) {
Map.Entry<String, String> entry = itr.next();
kv += "Key = " + entry.getKey() + ", Value = " + entry.getValue() + "\n";
}
String k = JOptionPane.showInputDialog(null, "Enter tiletype value\nExisting values:\n" + kv);
String[] kvPair = new String[2];
if (!isNullOrEmpty(k))
kvPair = k.split(",");
String k_ = kvPair[0].trim();
String str = kvPair[1].trim();
if (!isNullOrEmpty(k_) && !isNullOrEmpty(str)) {
if (tileNames.containsKey(k_)) {
tileNames.remove(k_);
}
tileNames.put(k_, str);
JOptionPane.showMessageDialog(null, "Successfully added type " + tileNames.get(k_));
}
}
}
void keyPressed( ){
if (key == 'w') {
camY += gridSpacing;
}
if (key == 's') {
camY -= gridSpacing;
}
if (key == 'a') {
camX += gridSpacing;
}
if (key == 'd') {
camX -= gridSpacing;
}
if (key == 'm') {
String str = JOptionPane.showInputDialog(null, "Player pos: ");
String[] pos = str.split(",");
playerPos[0] = constrain(Integer.parseInt(pos[0].trim()), 0, numCells[0] - 1);
playerPos[1] = constrain(Integer.parseInt(pos[1].trim()), 0, numCells[1] - 1);
JOptionPane.showMessageDialog(null, "Successfully set player map position to (" + playerPos[0] + ", " + playerPos[1] + ")");
}
if (key == 'v') {
String kv = "";
Iterator<Map.Entry<String, String>> itr = tileNames.entrySet().iterator();
while(itr.hasNext()) {
Map.Entry<String, String> entry = itr.next();
kv += "Key = " + entry.getKey() + ", Value = " + entry.getValue() + "\n";
}
JOptionPane.showMessageDialog(null, "Tile List: \n" + kv);
}
}
void exit() {
System.out.println(generate());
super.exit();
}
int input() {
Object[] options = tileNames.keySet().toArray();
String n = (String)JOptionPane.showInputDialog(null, "Tile type",
"Tile type:", JOptionPane.QUESTION_MESSAGE, null, options, options[0]);
System.out.println(n);
int toRet;
try {
toRet = Integer.parseInt(n);
}
catch(NumberFormatException e) {
toRet = 0;
}
return toRet;
}
String toJSArray(int[][] tiles, int ind) {
String toRet = "[\n";
for (int i = 0; i < numCells[0]; i++) {
toRet += indent(8);
for (int j = 0; j < numCells[1]; j++) {
if (j == 0)
toRet += "[";
if (j != 0)
toRet += ",";
toRet += tiles[i][j];
if (j == numCells[1] - 1)
if (i == numCells[0] - 1)
toRet += "]\n";
else
toRet += "],\n ";
}
}
toRet += indent(4) + "]";
return toRet;
}
String generate() {
String toRet = "{\n"+ indent(4) + "\"tiles\": ";
toRet += toJSArray(tiles, 4) + ",\n";
toRet += indent(4) + "\"tileTypes\": {\n";
toRet += indent(8);
String tileTypeStr = "";
Iterator<Map.Entry<String, String>> itr = tileNames.entrySet().iterator();
int i = 0;
while(itr.hasNext()) {
Map.Entry<String, String> entry = itr.next();
tileTypeStr += "\"" + entry.getKey() + "\": " + "\"" + entry.getValue() + "\"";
if (itr.hasNext())
tileTypeStr += ",\n" + indent(8);
else
tileTypeStr += "\n";
}
tileTypeStr += indent(4) + "},\n";
toRet += tileTypeStr;
toRet += indent(4) + "\"width\": " + numCells[0] + "," + "\n";
toRet += indent(4) + "\"height\": " + numCells[1] + "," + "\n";
toRet += indent(4) + "\"mapPos\": {\n" + indent(8) + "\"x\": " + playerPos[0] + ",\n" + indent(8) + "\"y\": " + playerPos[1];
toRet += "\n" + indent(4) + "}";
toRet += "\n}";
return toRet;
}
String indent(int n) {
String toRet = "";
for (int i = 0; i < n; i++) {
toRet += " ";
}
return toRet;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment