PrettyPrint

Sunday, 17 February 2013

Creating The Game - 17/02/2013

This week I have been concentrating on getting the collision within the game working, and this was one of the hardest things that I have done with the project to date. But first I will discuss the what was said at the short group discussion that we had. Stephen has created some more tiles for me to use within the map creation but we have decided that we are not going to use them, because of this he has started creating the animation frames for the character walking and jumping that I will use to animate the character in the game. Sam has completed the jump sound which will be played when the character jumps within the game to his liking and has started on a collection sound for picking up coins. Andy has started to create the real flash animation from one of his designs that he has chosen for this and the whole project is still moving in the right direction.

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