Permalink
Cannot retrieve contributors at this time
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?
GameNoodle/src/game/level/Level.java
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
104 lines (86 sloc)
2.23 KB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package game.level; | |
import game.entity.Entity; | |
import game.entity.Player; | |
import game.level.tiles.*; | |
import game.level.tiles.Tile; | |
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 | |
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(); | |
} | |
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(); | |
} | |
} | |
private void handleCollisions() { | |
playerWallCollisions(); | |
// Player-entity collisions | |
// Entity-wall collisions | |
} | |
private void playerWallCollisions() { | |
boolean collided = false; | |
// Check for collisions, flip flag, move player | |
} | |
} |