I have my map stored in 2 arrays:
- Tiles : Tile names
- TileTypes : SOLID or BASIC
I want to be able to walk through BASIC Tiles (Like ground tiles). But not the SOLID Tiles (Like wall tiles).
I have tried many ways of doing this and I always fail.
Map Code:
package game;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.Random;
import javax.imageio.ImageIO;
import org.lwjgl.LWJGLException;
import org.lwjgl.input.Mouse;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.DisplayMode;
import org.lwjgl.opengl.GL11;
import org.newdawn.slick.Color;
import org.newdawn.slick.opengl.Texture;
import org.newdawn.slick.opengl.TextureLoader;
import org.newdawn.slick.util.ResourceLoader;
public class Map {
Random random=new Random();
public int BushProbability=8;
public int ticks;
public int waterType;
public int MX=384;
public int MY=284;
int mapWidth=1000;
int mapSize;
public String[][] Tiles = new String[(mapWidth+1)*2][(mapWidth+1)*2];
public String[][] TileTypes = new String[(mapWidth+1)*2][(mapWidth+1)*2];
int Tile;
public Texture grass;
public Texture stone;
public Texture water;
public Texture bush;
public Texture water1;
public Texture water2;
public Texture water3;
public Texture stone1;
BufferedImage map = null;
public void Init()
{
try {
grass = TextureLoader.getTexture("PNG", ResourceLoader.getResourceAsStream("res/Tiles/Grass.png"));
bush = TextureLoader.getTexture("PNG", ResourceLoader.getResourceAsStream("res/Tiles/Bush.png"));
stone = TextureLoader.getTexture("PNG", ResourceLoader.getResourceAsStream("res/Tiles/Stone.png"));
water1 = TextureLoader.getTexture("PNG", ResourceLoader.getResourceAsStream("res/Tiles/Water.png"));
water2 = TextureLoader.getTexture("PNG", ResourceLoader.getResourceAsStream("res/Tiles/Water1.png"));
water3 = TextureLoader.getTexture("PNG", ResourceLoader.getResourceAsStream("res/Tiles/Water2.png"));
stone1 = TextureLoader.getTexture("PNG", ResourceLoader.getResourceAsStream("res/Tiles/Stone1.png"));
} catch (IOException e) {
e.printStackTrace();
}
}
public void LoadMap()
{
File file= new File("res/map.png");
try {
map = ImageIO.read(file);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
mapWidth=map.getWidth();
mapSize=mapWidth*mapWidth;
for(int Y = 0;Y<mapWidth;Y++){
for(int X = 0;X<mapWidth;X++){
// Getting pixel color by position x=100 and y=40
int clr= map.getRGB(X,Y);
int red = (clr & 0x00ff0000) >> 16;
int green = (clr & 0x0000ff00) >> 8;
int blue = clr & 0x000000ff;
if(red==0&&green==0&&blue==0){
Tiles[X][Y]="VOID";
TileTypes[X][Y]="SOLID";
//System.out.println("BLACK "+X+" "+Y);
}
if(red==255&&green==0&&blue==0){
if(random.nextInt(5)==0){
Tiles[X][Y]="SOLIDSTONE1";
TileTypes[X][Y]="SOLID";
}else{
Tiles[X][Y]="SOLIDSTONE2";
TileTypes[X][Y]="SOLID";
}
//System.out.println("RED "+X+" "+Y);
}
if(red==0&&green==255&&blue==0){
if(random.nextInt(BushProbability)==0){
Tiles[X][Y]="BUSH";
TileTypes[X][Y]="BASIC";
}else{
Tiles[X][Y]="GRASS";}
//System.out.println("GREEN "+X+" "+Y);
}
if(red==0&&green==0&&blue==255){
Tiles[X][Y]="WATER";
TileTypes[X][Y]="SOLID";
// System.out.println("BLUE "+X+" "+Y);
}
}
}
}
public void RenderMap()
{ ticks++;
if(ticks==10){
waterType++;
if(waterType==3){waterType=1;}
ticks=0;
}
if(waterType==0){
water=water1;
}if(waterType==1){
water=water2;
}
if(waterType==2){
water=water3;
}
for(int X=0;X<mapWidth;X++){
for(int Y=0;Y<mapWidth;Y++){
if(Tiles[X][Y]=="GRASS"){
Color.white.bind();
grass.bind(); // or GL11.glBind(texture.getTextureID());
GL11.glBegin(GL11.GL_QUADS);
GL11.glTexCoord2f(0,0);
GL11.glVertex2f((MX+(X*32)),(MY+(Y*32)));
GL11.glTexCoord2f(1,0);
GL11.glVertex2f(grass.getTextureWidth()+(MX+(X*32)),(MY+(Y*32)));
GL11.glTexCoord2f(1,1);
GL11.glVertex2f(grass.getTextureWidth()+(MX+(X*32)),grass.getTextureHeight()+(MY+(Y*32)));
GL11.glTexCoord2f(0,1);
GL11.glVertex2f((MX+(X*32)),grass.getTextureHeight()+(MY+(Y*32)));
GL11.glEnd();
}
if(Tiles[X][Y]=="SOLIDSTONE2"){
Color.white.bind();
stone.bind(); // or GL11.glBind(texture.getTextureID());
GL11.glBegin(GL11.GL_QUADS);
GL11.glTexCoord2f(0,0);
GL11.glVertex2f((MX+(X*32)),(MY+(Y*32)));
GL11.glTexCoord2f(1,0);
GL11.glVertex2f(stone.getTextureWidth()+(MX+(X*32)),(MY+(Y*32)));
GL11.glTexCoord2f(1,1);
GL11.glVertex2f(stone.getTextureWidth()+(MX+(X*32)),stone.getTextureHeight()+(MY+(Y*32)));
GL11.glTexCoord2f(0,1);
GL11.glVertex2f((MX+(X*32)),stone.getTextureHeight()+(MY+(Y*32)));
GL11.glEnd();
}
if(Tiles[X][Y]=="SOLIDSTONE1"){
Color.white.bind();
stone1.bind(); // or GL11.glBind(texture.getTextureID());
GL11.glBegin(GL11.GL_QUADS);
GL11.glTexCoord2f(0,0);
GL11.glVertex2f((MX+(X*32)),(MY+(Y*32)));
GL11.glTexCoord2f(1,0);
GL11.glVertex2f(stone1.getTextureWidth()+(MX+(X*32)),(MY+(Y*32)));
GL11.glTexCoord2f(1,1);
GL11.glVertex2f(stone1.getTextureWidth()+(MX+(X*32)),stone1.getTextureHeight()+(MY+(Y*32)));
GL11.glTexCoord2f(0,1);
GL11.glVertex2f((MX+(X*32)),stone1.getTextureHeight()+(MY+(Y*32)));
GL11.glEnd();
}
if(Tiles[X][Y]=="WATER"){
Color.white.bind();
water.bind(); // or GL11.glBind(texture.getTextureID());
GL11.glBegin(GL11.GL_QUADS);
GL11.glTexCoord2f(0,0);
GL11.glVertex2f((MX+(X*32)),(MY+(Y*32)));
GL11.glTexCoord2f(1,0);
GL11.glVertex2f(water.getTextureWidth()+(MX+(X*32)),(MY+(Y*32)));
GL11.glTexCoord2f(1,1);
GL11.glVertex2f(water.getTextureWidth()+(MX+(X*32)),water.getTextureHeight()+(MY+(Y*32)));
GL11.glTexCoord2f(0,1);
GL11.glVertex2f((MX+(X*32)),water.getTextureHeight()+(MY+(Y*32)));
GL11.glEnd();
}
if(Tiles[X][Y]=="BUSH"){
Color.white.bind();
bush.bind(); // or GL11.glBind(texture.getTextureID());
GL11.glBegin(GL11.GL_QUADS);
GL11.glTexCoord2f(0,0);
GL11.glVertex2f((MX+(X*32)),(MY+(Y*32)));
GL11.glTexCoord2f(1,0);
GL11.glVertex2f(bush.getTextureWidth()+(MX+(X*32)),(MY+(Y*32)));
GL11.glTexCoord2f(1,1);
GL11.glVertex2f(bush.getTextureWidth()+(MX+(X*32)),bush.getTextureHeight()+(MY+(Y*32)));
GL11.glTexCoord2f(0,1);
GL11.glVertex2f((MX+(X*32)),bush.getTextureHeight()+(MY+(Y*32)));
GL11.glEnd();
}
}
}
}
}
Player code:
package mob;
import java.io.IOException;
import org.lwjgl.input.Keyboard;
import org.lwjgl.opengl.GL11;
import org.newdawn.slick.Color;
import org.newdawn.slick.opengl.Texture;
import org.newdawn.slick.opengl.TextureLoader;
import org.newdawn.slick.util.ResourceLoader;
public class Player extends game.Map {
public int SPEED=10;
public String Colour="WHITE";
public int mapWidth;
int DX = (800/2)-16;
int DY = (600/2)-16;
public int X;
public int Y;
int RX; int RY;
boolean UP;
boolean DOWN;
boolean LEFT;
boolean RIGHT;
private Texture texture;
public void Init()
{
try {
// load texture from PNG file
if(Colour == "WHITE"){
texture = TextureLoader.getTexture("PNG", ResourceLoader.getResourceAsStream("res/Mob/Slime/White.png"));
}
if(Colour == "YELLOW"){
texture = TextureLoader.getTexture("PNG", ResourceLoader.getResourceAsStream("res/Mob/Slime/Yellow.png"));
}
if(Colour == "RED"){
texture = TextureLoader.getTexture("PNG", ResourceLoader.getResourceAsStream("res/Mob/Slime/Red.png"));
}
if(Colour == "PURPLE"){
texture = TextureLoader.getTexture("PNG", ResourceLoader.getResourceAsStream("res/Mob/Slime/Purple.png"));
}
if(Colour == "PINK"){
texture = TextureLoader.getTexture("PNG", ResourceLoader.getResourceAsStream("res/Mob/Slime/Pink.png"));
}
if(Colour == "ORRANGE"){
texture = TextureLoader.getTexture("PNG", ResourceLoader.getResourceAsStream("res/Mob/Slime/Orrange.png"));
}
if(Colour == "LIGHT-GREEN"){
texture = TextureLoader.getTexture("PNG", ResourceLoader.getResourceAsStream("res/Mob/Slime/Lime-Green.png"));
}
if(Colour == "LIGHT-BLUE"){
texture = TextureLoader.getTexture("PNG", ResourceLoader.getResourceAsStream("res/Mob/Slime/Light-Blue.png"));
}
if(Colour == "GREY"){
texture = TextureLoader.getTexture("PNG", ResourceLoader.getResourceAsStream("res/Mob/Slime/Grey.png"));
}
if(Colour == "GREEN"){
texture = TextureLoader.getTexture("PNG", ResourceLoader.getResourceAsStream("res/Mob/Slime/Green.png"));
}
if(Colour == "BLUE"){
texture = TextureLoader.getTexture("PNG", ResourceLoader.getResourceAsStream("res/Mob/Slime/Blue.png"));
}
} catch (IOException e) {
e.printStackTrace();
}
}
public void Render()
{
Color.white.bind();
texture.bind(); // or GL11.glBind(texture.getTextureID());
GL11.glBegin(GL11.GL_QUADS);
GL11.glTexCoord2f(0,0);
GL11.glVertex2f(DX,DY);
GL11.glTexCoord2f(1,0);
GL11.glVertex2f(DX+texture.getTextureWidth(),DY);
GL11.glTexCoord2f(1,1);
GL11.glVertex2f(DX+texture.getTextureWidth(),DY+texture.getTextureHeight());
GL11.glTexCoord2f(0,1);
GL11.glVertex2f(DX,DY+texture.getTextureHeight());
GL11.glEnd();
}
public void Update()
{
UP = Keyboard.isKeyDown(Keyboard.KEY_W);
DOWN = Keyboard.isKeyDown(Keyboard.KEY_S);
LEFT = Keyboard.isKeyDown(Keyboard.KEY_A);
RIGHT = Keyboard.isKeyDown(Keyboard.KEY_D);
if(UP){Y=Y+SPEED;}
if(DOWN){Y=Y-SPEED;}
if(LEFT){X=X+SPEED;}
if(RIGHT){X=X-SPEED;}
}
}
I have heard of RECTANGLE collision checking but I have no idea how to implement it into a tile based game.
Thanks, Griff.