Hello,
am currently using lwjgl, but i think my question has nothing to do with it so i didn’t mention that in the title
anyway…
am trying to start with a little platformer game and i think a tiled map is something necessary to do that (even if it’s not, i want to know how it works )
so i was trying to do some codes for the last 2 hours, and i managed to :
- draw the map grid based on the “block” size and type
- print out the x and y coordinate of the mouse based on its position on the grid
what am stacked with :
-draw and KEEP a specific type if “blocks” in the clicked position (it appears in the wrong location)
what i want to know :
-as a start, is this a good way to do it ?
codes here :
This class will handle the map creation
public class World {
private Block[][] blocks;
private Block block;
private int width, height;
public World(int width, int height) {
this.width = width;
this.height = height;
blocks = new Block[width][height];
}
public void setWorld() {
for (int x = 0; x < width; x++) {
for (int y = 0; y < height; y++) {
Block block = new Block(0, 0, 32, 0);
block.setX(x * block.getSize());
block.setY(y * block.getSize());
block.draw();
blocks[x][y]=block;
}
}
}
public void drawInWorld(int x,int y){
Block block = new Block(0, 0, 32, 1);
block.setX(x * block.getSize());
block.setY(y * block.getSize());
block.draw();
}
public int getWidth() {
return width;
}
public int getHeight() {
return height;
}
}
this class will handle a single block creation
public class Block {
private int x, y, size, type;
private Drawing draw = new Drawing();
public Block(int x, int y, int size, int type) {
super();
this.x = x;
this.y = y;
this.size = size;
this.type = type;
}
public void draw(){
if(type==0){
draw.coloring("blue");
}else{
draw.coloring("red");
}
draw.drawRect(x, y, size, size);
}
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
public int getSize() {
return size;
}
public void setSize(int size) {
this.size = size;
}
public int getType() {
return type;
}
public void setType(int type) {
this.type = type;
}
}
this is the Board class (logic,calling etc…)
PS :
i removed the player method to make it easiest to follow
import java.util.Arrays;
import org.lwjgl.input.Keyboard;
import org.lwjgl.input.Mouse;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.GL11;
public class Board {
// varibable of other classes
private Loop loop = new Loop(); // the loop class
private World world;
int delta;
public void init() {
// initialize the loop
loop.init();
world = new World(25,19);
}
// the main game logic "caller"
public void myBoardLoop() {
delta = loop.getDelta();
update(delta);
}
// the update method
private void update(int delta) {
input();
painting();
loop.updateFPS();
}
private void painting() {
// clear the screen and depth buffer
GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT);
world.setWorld();
}
private void input(){
int mouseX = Mouse.getX();
int mouseY = Mouse.getY();
boolean mouseClick = Mouse.isButtonDown(0);
if(mouseClick){
int mapX = Math.round(mouseX/32);
int mapY = Math.round(mouseY/32);
System.out.println(mapX+"||"+mapY);
world.drawInWorld(mapX, mapY);
}
}
}
thank you