Created
May 23, 2019 23:01
-
-
Save xyzshantaram/10305bb13aff43c605804146b5687c3d to your computer and use it in GitHub Desktop.
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
public class Constants { | |
public static final int tileSize = 32; | |
public static final int framerate = 30; | |
public static final String bgColor = "007CDF"; | |
public static final String defaultEnemyClr = "FF0000"; | |
public static final String defaultPlayerClr = "00FF00"; | |
} |
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
public class Entity { | |
int x = 0; | |
int y = 0; | |
int colour_R = 0; | |
int colour_G = 0; | |
int colour_B = 0; | |
String text; | |
PImage img; | |
int type; | |
boolean collides; | |
/* | |
* 0 --> background tile | |
* 1 --> player | |
* 2 --> enemy | |
* 3 --> background tile but with collision | |
*/ | |
int frameNo = 0; | |
String movePattern = ""; | |
Entity(int _x, int _y, String clr, int _type, String _text, String _movePattern) { | |
this.x = _x; | |
this.y = _y; | |
this.movePattern = _movePattern; | |
this.setColour(clr); | |
this.text = _text; | |
this.type = _type; | |
} | |
void setColour(String hex) { | |
String[] colourHexes = {hex.substring(1, 3), | |
hex.substring(3, 5), | |
hex.substring(5, 6) | |
}; | |
int colours[] = {0, 0, 0}; | |
int index = 0; | |
for (String x: colourHexes) { | |
colours[index] = hexToInteger(x); | |
index++; | |
} | |
colour_R = colours[0]; | |
colour_G = colours[1]; | |
colour_B = colours[2]; | |
} | |
void show() { | |
if (img == null) { | |
fill(colour_R, colour_G, colour_B); | |
if (this.movePattern.equals("followP")) { | |
follow(player, this); | |
} | |
rect(x, y, Constants.tileSize, Constants.tileSize); | |
fill(0, 0, 0); | |
text(this.text, x + 10, y + 20); | |
} | |
} | |
void move(String direction) { | |
System.out.println(this.text + " currently at " + this.x/Constants.tileSize + ", " + this.y / Constants.tileSize); | |
if (direction.equals("up")) { | |
if (y != 0) { | |
int newY = this.y - Constants.tileSize; | |
if (map.getEntity(this.x/Constants.tileSize,newY/Constants.tileSize).type == 3); | |
else this.y = newY; | |
} | |
} | |
if (direction.equals("down")) { | |
if (y != height - Constants.tileSize) { | |
int newY = this.y + Constants.tileSize; | |
if (map.getEntity(this.x/Constants.tileSize,newY/Constants.tileSize).type == 3); | |
else this.y = newY; | |
} | |
} | |
if (direction.equals("left")) { | |
if (x != 0) { | |
int newX = this.x - Constants.tileSize; | |
if (map.getEntity(this.y/Constants.tileSize,newX/Constants.tileSize).type == 3); | |
else this.x = newX; | |
} | |
} | |
if (direction.equals("right")) { | |
if (x != width - Constants.tileSize) { | |
int newX = this.x + Constants.tileSize; | |
if (map.getEntity((this.y/Constants.tileSize),(newX/Constants.tileSize)).type == 3); | |
else this.x = newX; | |
} | |
} | |
} | |
String getMovementPattern() { | |
return this.movePattern; | |
} | |
} |
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
Entity player; | |
Entity[] enemies; | |
public Map map; | |
void setup() { | |
size(640, 640); | |
map = new Map(30, 30, Constants.bgColor, this.getViewport()); | |
setBG("007CDF"); | |
frameRate(Constants.framerate); | |
player = new Entity(0, 0, Constants.defaultPlayerClr, 1, "P", "none"); | |
int numEnemies = (int) random(2, 4); | |
enemies = new Entity[numEnemies]; | |
for (int x = 0; x < numEnemies; x++) { | |
enemies[x] = new Entity((int) ceil(abs(random(0, width) / Constants.tileSize)) * Constants.tileSize, | |
(int) ceil(abs(random(0, height) / Constants.tileSize)) * Constants.tileSize, | |
Constants.defaultEnemyClr, 2, "E", "followdswawddd"); | |
} | |
} | |
void draw() { | |
map.show(); | |
player.show(); | |
for (Entity x: enemies) { | |
x.show(); | |
} | |
} | |
void setBG(String hex) { | |
String[] colourHexes = { hex.substring(0, 2), | |
hex.substring(2, 4), | |
hex.substring(4, 6) | |
}; | |
int colours[] = {0, 0, 0}; | |
int index = 0; | |
for (String x: colourHexes) { | |
colours[index] = hexToInteger(x); | |
index++; | |
} | |
background(colours[0], colours[1], colours[2]); | |
} | |
int hexToInteger(String hex) { | |
return Integer.parseInt(hex, 16); | |
} | |
int signum (int x) { | |
if (x == 0) { | |
return 0; | |
} | |
else { | |
return x > 0 ? 1 : -1; | |
} | |
} | |
void follow(Entity subject, Entity follower) { | |
int[] moves = calculateMoves(subject, follower); | |
if (frameCount % Constants.framerate == 0) { | |
if (signum(moves[0]) == 1) { | |
if (abs(deltaX(subject, follower)) >= Constants.tileSize*2) { | |
follower.move("right"); | |
} | |
} | |
else { | |
if (abs(deltaX(subject, follower)) >= Constants.tileSize*2) { | |
follower.move("left"); | |
} | |
} | |
if (signum(moves[1]) == 1) { | |
if (abs(deltaY(subject, follower)) >= Constants.tileSize*2) { | |
follower.move("down"); | |
} | |
} | |
else { | |
if (abs(deltaY(subject, follower)) >= Constants.tileSize*2) { | |
follower.move("up"); | |
} | |
} | |
} | |
} | |
int[] calculateMoves(Entity e1, Entity e2) { | |
int[] moves = {signum(deltaX(e1, e2)) * floor(abs(deltaX(e1, e2) / Constants.tileSize)) * Constants.tileSize, | |
signum(deltaY(e1, e2)) * floor(abs(deltaY(e1, e2) / Constants.tileSize)) * Constants.tileSize}; | |
return moves; | |
} | |
int deltaX (Entity e1, Entity e2) { | |
return e1.x - e2.x; | |
} | |
int deltaY(Entity e1, Entity e2) { | |
return e1.y - e2.y; | |
} | |
void keyPressed() { | |
if (key == 'w') { | |
player.move("up"); | |
} | |
if (key == 's') { | |
player.move("down"); | |
} | |
if (key == 'a') { | |
player.move("left"); | |
} | |
if (key == 'd') { | |
player.move("right"); | |
} | |
} | |
int[] getViewport() { | |
int[] vp = {floor(width/Constants.tileSize), floor(height/Constants.tileSize)}; | |
return vp; | |
} |
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
import java.util.Random; | |
public class Map { | |
Random rng = new Random(); | |
Entity[][] map; | |
int mapWidth = 0; | |
int mapHeight = 0; | |
int[] viewportSize; | |
int i = 0; | |
Map(String _filename, int[] _viewportSize) { | |
throw new UnsupportedOperationException("Not implemented yet."); | |
} | |
Map(int _width, int _height, String clr, int[] _viewportSize) { | |
this.mapWidth = _width; | |
this.mapHeight = _height; | |
this.viewportSize = _viewportSize; | |
map = new Entity[mapWidth][mapHeight]; | |
for (int x = 0; x < _width; x++) { | |
for (int y = 0; y < _height; y++) { | |
int type; | |
String toSet; | |
if (i % 5 == 0) type = 3; | |
else type = 0; | |
if (type == 3) { | |
toSet = "FF0000"; | |
System.out.println(i + "has type 3ad"); | |
} | |
else toSet = clr; | |
map[x][y] = new Entity(x * Constants.tileSize, y * Constants.tileSize, toSet, type, Integer.toString(i), "static"); | |
i++; | |
} | |
} | |
} | |
void show() { | |
for (Entity[] x: map) { | |
for (Entity y: x) { | |
y.show(); | |
} | |
} | |
} | |
Entity getEntity(int x, int y) { | |
return map[x][y]; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment