Skip to content
Permalink
40854a4d79
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Go to file
 
 
Cannot retrieve contributors at this time
118 lines (99 sloc) 2.88 KB
package game.level;
import game.entity.Entity;
import game.entity.Player;
import game.level.tiles.*;
public class Level {
private Player player;
private int currentLevel;
private int width, height; // Number of tiles
private Tile[][] tiles;
private Entity[] entities;
private float originX, originY; // Where the 'origin' of the level is the lower left of the floor area
private static final int SPRITE_PIXELS = 64;
public Level() {
currentLevel = -1;
loadLevel();
}
private void loadLevel() {
// This is a test level
if (currentLevel == -1) {
width = 7;
height = 7;
tiles = new Tile[width][height];
// Tile coordinates are double entity coordinates, i.e. the entity at (1, 1) is on the tile at (2, 2)
originX = -1.5f;
originY = -1.5f;
// Load entities and positions, load into array
player = new Player(0f, 0f);
entities = new Entity[1];
entities[0] = player;
// Load tiles. Sample, 5 x 5 grid of StoneFloor surrounded by StoneWalls
Tile.create();
for (int y = 0; y < width; y++) {
for (int x = 0; x < height; x++) {
float convertX = x/2f; // Tile coordinates are double entity coordinates
float convertY = y/2f;
if (y == 0 || y == height - 1 || x == 0 || x == width - 1) {
tiles[x][y] = new StoneWall(convertX + originX, convertY + originY);
} else {
tiles[x][y] = new StoneFloor(convertX + originX, convertY + originY);
}
}
}
}
}
public void update() {
updateTiles();
updateEntities();
handleCollisions();
}
public void render() {
renderTiles();
renderEntities();
}
private void updateTiles() {
for (Tile[] tileRow : tiles) {
for (Tile tile : tileRow) {
tile.update();
}
}
}
private void updateEntities() {
for (Entity entity : entities) {
entity.update();
}
}
private void renderTiles() {
for (Tile[] tileRow : tiles) {
for (Tile tile : tileRow) {
tile.render();
}
}
}
private void renderEntities() {
for (Entity entity : entities) {
entity.render();
}
}
// TODO: This
private void handleCollisions() {
playerWallCollisions();
// Player-entity collisions
// Entity-wall collisions
}
private void playerWallCollisions() {
// Check for collisions, flip flag, if flag is true move player
//TODO: Make this better and stop sending player to middle.
for (Tile[] tileRow : tiles) {
for (Tile tile : tileRow) {
if (tile.isWall()) {
if ((2 * tile.getX() - .5f < player.getX() + .25f && 2 * tile.getX() + .5f > player.getX() + .25f) || ((2 * tile.getX() - .5f < player.getX() - .25f && 2 * tile.getX() + .5f > player.getX() - .25f))){
if ((2 * tile.getY() - .5f < player.getY() + .25f && 2 * tile.getY() + .5f > player.getY() + .25f) || ((2 * tile.getY() - .5f < player.getY() - .25f && 2 * tile.getY() + .5f > player.getY() - .25f))){
player.setPosition(0f, 0f);
}
}
}
}
}
}
}