Olá pessoal!,
eu estou desenvolvendo um RPG 2d, só que estou usando uma tecnica que acredito
ser muito primitiva!..
code:
package source;
import org.newdawn.slick.AppGameContainer;
import org.newdawn.slick.BasicGame;
import org.newdawn.slick.Color;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Image;
import org.newdawn.slick.Input;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.SpriteSheet;
public class Component extends BasicGame {
public static int[][] WorldMap = {
{2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2},
{2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2},
{2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2},
{2, 1, 1, 1, 1, 1, 1, 9, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2},
{2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 11, 1, 1, 1, 1, 1, 2},
{2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2},
{2, 1, 4, 4, 4, 4, 3, 1, 1, 1, 9, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2},
{2, 1, 3, 5, 5, 5, 3, 1, 1, 1, 1, 1, 1, 1, 10, 1, 1, 1, 1, 1, 1, 1, 1, 2},
{2, 1, 3, 5, 5, 5, 6, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2},
{2, 1, 3, 5, 5, 5, 3, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2},
{2, 1, 4, 4, 4, 4, 3, 1, 1, 1, 1, 1, 1, 1, 1, 1,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32;
public static SpriteSheet tile;
public static boolean up = true, down = true, left = true, right = true;
public void drawPlayer(Graphics g) {
g.drawImage(tile.getSprite(0, 0), 288, 288-64);
px = 288-sX;
py = (288-64)-sY;
}
public static boolean isColl(int x1, int y1, int w1, int h1, int x2, int y2, int w2, int h2) {
return(x1 <= x2 && x1+w1 >= x2 && y1 <= y2 && y1+h1 >= y2 || x2 <= x1 && x2+w2 >= x1 && y2 <= y1 && y2+h2 >= y1);
}
public void move() {
int speed = 1;
if(dir == 1) {
Component.sY -= speed*32;
} else if(dir == 2) {
Component.sY += speed*32;
} else if(dir == 3) {
Component.sX += speed*32;
} else if(dir == 4) {
Component.sX -= speed*32;
}
else if(dir == 0) {}
}
public Component() {
super("MyRPG");
}
//tiles
public static Image
grass, character, fence, wallDown, wallUp,
doorOpen, doorClosed, ground, tree, flowers1,
flowers2;
public void init(GameContainer container) throws SlickException {
tile = new SpriteSheet("res/maptiles.png", 32, 32);
grass = tile.getSprite(1, 7);
character = tile.getSprite(0, 0);
fence = tile.getSprite(4, 7);
wallUp = tile.getSprite(0, 7);
wallDown = tile.getSprite(6, 7);
doorOpen = tile.getSprite(2, 7);
doorClosed = tile.getSprite(3, 7);
ground = tile.getSprite(7,7);
tree = tile.getSprite(5,6);
flowers1 = tile.getSprite(8,7);
flowers2 = tile.getSprite(9,7);
}
public void update(GameContainer container, int delta) throws SlickException {
Input input = container.getInput();
dir = 0;
for(int x = 0; x < WorldMap[0].length; x++) {
for(int y = 0; y < WorldMap.length; y++) {
if(input.isKeyPressed(Input.KEY_W) && up) {
dir = 2;
}
if(input.isKeyPressed(Input.KEY_S) && down) {
dir = 1;
}
if(input.isKeyPressed(Input.KEY_A) && left) {
dir = 3;
}
if(input.isKeyPressed(Input.KEY_D) && right) {
dir = 4;
}
}
}
move();
}
public void render(GameContainer container, Graphics g) throws SlickException {
int dx = px/tileSize;
int dy = py/tileSize;
for(int x = 0; x < WorldMap[0].length; x++) {
for(int y = 0; y < WorldMap.length; y++) {
if(WorldMap[x][y]==1) {
g.drawImage(grass, sX+x*tileSize, sY+y*tileSize);
} else if(WorldMap[x][y]==2) {
g.drawImage(fence, sX+x*tileSize, sY+y*tileSize);
} else if(WorldMap[x][y]==3) {
g.drawImage(wallUp, sX+x*tileSize, sY+y*tileSize);
} else if(WorldMap[x][y]==4) {
g.drawImage(wallDown, sX+x*tileSize, sY+y*tileSize);
} else if(WorldMap[x][y]==5) {
g.setColor(Color.black);
g.fillRect(sX+x*tileSize, sY+y*tileSize, tileSize, tileSize);
} else if(WorldMap[x][y]==6) {
g.drawImage(doorOpen, sX+x*tileSize, sY+y*tileSize);
} else if(WorldMap[x][y]==7) {
g.drawImage(doorClosed, sX+x*tileSize, sY+y*tileSize);
} else if(WorldMap[x][y]==8) {
g.drawImage(ground, sX+x*tileSize, sY+y*tileSize);
} else if(WorldMap[x][y]==9) {
g.drawImage(tree, sX+x*tileSize, sY+y*tileSize);
} else if(WorldMap[x][y]==10) {
g.drawImage(flowers1, sX+x*tileSize, sY+y*tileSize);
} else if(WorldMap[x][y]==11) {
g.drawImage(flowers2, sX+x*tileSize, sY+y*tileSize);
}
if(dx > 0 && dx < WorldMap[0].length && dy > 0 && dy < WorldMap.length) {
if(WorldMap[dx][dy] == 6) {
for(int i = 0; i < 3; i++) {
for(int j = 0; j < 3; j++) {
WorldMap[7+i][3+j] = 8;
}
}
WorldMap[dx][dy] = 7;
} else if(WorldMap[dx][dy-1] != 1
&& WorldMap[dx][dy-1] != 6
&& WorldMap[dx][dy-1] != 0
&& WorldMap[dx][dy-1] != 7
&& WorldMap[dx][dy-1] != 8
&& WorldMap[dx][dy-1] != 11
&& WorldMap[dx][dy-1] != 10) {
up = false;
} else {
up = true;
}
if(WorldMap[dx][dy+1] != 1
&& WorldMap[dx][dy+1] != 6
&& WorldMap[dx][dy+1] != 0
&& WorldMap[dx][dy+1] != 7
&& WorldMap[dx][dy+1] != 8
&& WorldMap[dx][dy+1] != 11
&& WorldMap[dx][dy+1] != 10) {
down = false;
} else {
down = true;
}
if(WorldMap[dx-1][dy] != 1
&& WorldMap[dx-1][dy] != 6
&& WorldMap[dx-1][dy] != 0
&& WorldMap[dx-1][dy] != 7
&& WorldMap[dx-1][dy] != 8
&& WorldMap[dx-1][dy] != 11
&& WorldMap[dx-1][dy] != 10) {
left = false;
} else {
left = true;
}
if(WorldMap[dx+1][dy] != 1
&& WorldMap[dx+1][dy] != 6
&& WorldMap[dx+1][dy] != 0
&& WorldMap[dx+1][dy] != 7
&& WorldMap[dx+1][dy] != 8
&& WorldMap[dx+1][dy] != 11
&& WorldMap[dx+1][dy] != 10) {
right = false;
} else {
right = true;
}
}
}
}
drawPlayer(g);
}
public static void main(String[] args) {
try {
AppGameContainer app = new AppGameContainer(new Component());
app.setShowFPS(false);
app.start();
} catch (SlickException e) {
e.printStackTrace();
}
}
}
Eu queria ajuda para simplificar o sistema de colisao e o de inicializar images:
else if(WorldMap[x][y]==11) {
g.drawImage(flowers2, sX+x*tileSize, sY+y*tileSize);
}
se alguem puder me ajudar agradeco!.