This week as stated before I have been focusing on getting the collision working within the game. This means that the character will not be able to move where there is a block being rendered on the screen. As I had originally thought, this was extremely hard to get the grasp of but finally after looking through many different resources I understand how this is done and have implemented it successfully into the game. The basics of the collision system are that the TileD map that I created is loaded as a blockmap allowing Slick2D to read each tile and see if anything is being drawn there according to a specific tile ID. If there is a tile meant to be there then this is added to an array of entities which store the size of the block and where it is. Once this is done a polygon box is drawn around the players character and all of the blocks that are in the map, when the user is attempting to move the game checks to see if these polygon boxes ever intersect by looking through each object in the array and making sure the co-ordinates never intersect, if they do the movement is blocked and stops the character from moving through this object. All of the code that was written for this is down below.
BlockMap Class
import java.util.ArrayList;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.tiled.TiledMap;
public class BlockMap {
public static TiledMap tmap;
public static int mapWidth;
public static int mapHeight;
private int square[] = {1,1,31,1,31,31,1,31}; //square shaped tile
public static ArrayList(objects) entities;
public BlockMap(String ref) throws SlickException {
entities = new ArrayList(objects)();
tmap = new TiledMap(ref, "data");
mapWidth = tmap.getWidth() * tmap.getTileWidth();
mapHeight = tmap.getHeight() * tmap.getTileHeight();
for (int x = 0; x < tmap.getWidth(); x++) {
for (int y = 0; y < tmap.getHeight(); y++) {
int tileID = tmap.getTileId(x, y, 0);
if (tileID == 1) {
entities.add(
new Block(x * 32, y * 32, square, "square")
);
}
}
}
}
}
Block Class
import org.newdawn.slick.Graphics;
import org.newdawn.slick.geom.Polygon;
public class Block {
public Polygon poly;
public Block(int x, int y, int test[],String type) {
poly = new Polygon(new float[]{
x+test[0], y+test[1],
x+test[2], y+test[3],
x+test[4], y+test[5],
x+test[6], y+test[7],
});
}
public void update(int delta) {
}
public void draw(Graphics g) {
g.draw(poly);
}
}
Collision Check Method
public boolean entityCollisionWith() throws SlickException {
for (int i = 0; i < BlockMap.entities.size(); i++) {
Block entity1 = (Block) BlockMap.entities.get(i);
if (playerPoly.intersects(entity1.poly)) {
return true;
}
}
return false;
}
Updated Game Code - Movement
if (input.isKeyPressed(Input.KEY_UP)){
y -= floatDelta;
playerPoly.setY(y);
if (entityCollisionWith()){
y += floatDelta;
playerPoly.setY(y);
}
}
if (input.isKeyDown(Input.KEY_DOWN))
{
y += floatDelta;
playerPoly.setY(y);
if (entityCollisionWith()){
y -= floatDelta;
playerPoly.setY(y);
}
}
if (input.isKeyDown(Input.KEY_LEFT))
{
x -= floatDelta;
playerPoly.setX(x);
if (entityCollisionWith()){
x += floatDelta;
playerPoly.setX(x);
}
}
if (input.isKeyDown(Input.KEY_RIGHT))
{
x += floatDelta;
playerPoly.setX(x);
if (entityCollisionWith()){
x -= floatDelta;
playerPoly.setX(x);
}
}
}
No comments:
Post a Comment