Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Basic tile rendering complete. Next up should be collision checking, …
…but we'll see if I can focus on one thing for that long.
  • Loading branch information
wjg12004 committed Apr 19, 2016
1 parent a32d09a commit 58879c4
Show file tree
Hide file tree
Showing 22 changed files with 207 additions and 104 deletions.
Binary file added res/rawImage/playerBlue.xcf
Binary file not shown.
Binary file added res/rawImage/playerGreen.xcf
Binary file not shown.
Binary file added res/rawImage/playerRed.xcf
Binary file not shown.
Binary file added res/rawImage/smoothFloor.xcf
Binary file not shown.
Binary file added res/rawImage/stoneWall.xcf
Binary file not shown.
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
Binary file added res/sprites/stoneWall.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 0 additions & 1 deletion shaders/smoothFloor.frag → shaders/tile.frag
Expand Up @@ -8,7 +8,6 @@ in DATA
vec3 position;
} fs_in;

uniform vec2 player;
uniform sampler2D tex;

void main()
Expand Down
7 changes: 4 additions & 3 deletions shaders/smoothFloor.vert → shaders/tile.vert
Expand Up @@ -4,7 +4,8 @@ layout (location = 0) in vec4 position;
layout (location = 1) in vec2 tc;

uniform mat4 pr_matrix;
uniform mat4 vw_matrix;
uniform mat4 vw_matrix = mat4(1.0);
uniform mat4 ml_matrix = mat4(1.0);

out DATA
{
Expand All @@ -14,7 +15,7 @@ out DATA

void main()
{
gl_Position = pr_matrix * vw_matrix * position;
gl_Position = pr_matrix * vw_matrix * ml_matrix * position;
vs_out.tc = tc;
vs_out.position = vec3(vw_matrix * position);
vs_out.position = vec3(vw_matrix * ml_matrix * position);
}
8 changes: 4 additions & 4 deletions src/game/Main.java
Expand Up @@ -16,8 +16,8 @@ import game.math.Matrix4f;

public class Main implements Runnable {

private int width = 1280;
private int height = 720;
private int width = 1920;
private int height = 1080;
private boolean running = false;
private Thread gameThread;
private long gameWindow;
Expand Down Expand Up @@ -64,8 +64,8 @@ public class Main implements Runnable {

// Set all shader uniform variables here
Matrix4f projectionMatrix = Matrix4f.orthographic(-10.0f, 10.0f, -10.0f * 9.0f / 16.0f, 10.0f * 9.0f / 16.0f, -1.0f, 1.0f);
Shader.SMOOTH_FLOOR.setUniformMat4f("pr_matrix", projectionMatrix);
Shader.SMOOTH_FLOOR.setUniform1i("tex", 1);
Shader.TILE.setUniformMat4f("pr_matrix", projectionMatrix);
Shader.TILE.setUniform1i("tex", 1);

Shader.PLAYER.setUniformMat4f("pr_matrix", projectionMatrix);
Shader.PLAYER.setUniform1i("tex", 1);
Expand Down
6 changes: 3 additions & 3 deletions src/game/Options.java
Expand Up @@ -8,9 +8,9 @@ public class Options {
private static int chosenBallColor = 0;

private static Texture[] ballColors = new Texture[] {
new Texture("res/playerBlue.png"),
new Texture("res/playerRed.png"),
new Texture("res/playerGreen.png"),
new Texture("res/sprites/playerBlue.png"),
new Texture("res/sprites/playerRed.png"),
new Texture("res/sprites/playerGreen.png"),
};

public Options() {
Expand Down
22 changes: 22 additions & 0 deletions src/game/entity/Entity.java
@@ -0,0 +1,22 @@
package game.entity;

import game.graphics.Texture;
import game.graphics.VertexArray;
import game.math.Vector3f;

public abstract class Entity {

protected float SIZE;
protected Vector3f position = new Vector3f();
protected float speed;

protected VertexArray mesh;
protected Texture texture;
protected float[] vertices;
protected byte[] indices;
protected float[] tcs;

public abstract void update();
public abstract void render();

}
34 changes: 15 additions & 19 deletions src/game/entity/Player.java
Expand Up @@ -4,36 +4,33 @@ import org.lwjgl.glfw.GLFW;

import game.Options;
import game.graphics.Shader;
import game.graphics.Texture;
import game.graphics.VertexArray;
import game.input.Input;
import game.math.Matrix4f;
import game.math.Vector3f;

public class Player {

private float SIZE = 1.0f;
private VertexArray mesh;
private Texture texture;

private Vector3f position = new Vector3f();
public class Player extends Entity{

private int playerKeyUp, playerKeyDown, playerKeyLeft, playerKeyRight;

public Player() {
float[] vertices = new float[] {
public Player(float x, float y) {
SIZE = 0.5f;
position.x = x;
position.y = y;
speed = .05f;

vertices = new float[] {
-SIZE / 2.0f, -SIZE / 2.0f, 0.2f,
-SIZE / 2.0f, SIZE / 2.0f, 0.2f,
SIZE / 2.0f, SIZE / 2.0f, 0.2f,
SIZE / 2.0f, -SIZE / 2.0f, 0.2f,
};

byte[] indices = new byte[] {
indices = new byte[] {
0, 1, 2,
2, 3, 0
};

float[] tcs = new float[] {
tcs = new float[] {
0, 1,
0, 0,
1, 0,
Expand All @@ -42,6 +39,7 @@ public class Player {

mesh = new VertexArray(vertices, indices, tcs);

// Just for testing at the moment
setColor();

if (Options.isWASD) {
Expand All @@ -59,20 +57,18 @@ public class Player {

public void update() {
if (Input.keys[playerKeyUp]) {
position.y += 0.1f;
position.y += speed;
}
if (Input.keys[playerKeyDown]) {
position.y -= 0.1f;
position.y -= speed;
}
if (Input.keys[playerKeyLeft]) {
position.x -= 0.1f;
position.x -= speed;
}
if (Input.keys[playerKeyRight]) {
position.x += 0.1f;
position.x += speed;
}

// System.out.println("At (" + position.x + ", " + position.y + ")");

// Just for testing, change color by pressing corresponding number buttons
if (Input.keys[GLFW.GLFW_KEY_0]) {
Options.setBallColor(0);
Expand Down
4 changes: 2 additions & 2 deletions src/game/graphics/Shader.java
Expand Up @@ -15,7 +15,7 @@ public class Shader {
public static final int VERTEX_ATTRIB = 0;
public static final int TCOORD_ATTRIB = 1;

public static Shader SMOOTH_FLOOR, PLAYER;
public static Shader TILE, PLAYER;

private boolean enabled = false;

Expand All @@ -27,7 +27,7 @@ public class Shader {
}

public static void loadAll() {
SMOOTH_FLOOR = new Shader("shaders/smoothFloor.vert", "shaders/smoothFloor.frag");
TILE = new Shader("shaders/tile.vert", "shaders/tile.frag");
PLAYER = new Shader("shaders/player.vert", "shaders/player.frag");
}

Expand Down
119 changes: 85 additions & 34 deletions src/game/level/Level.java
@@ -1,53 +1,104 @@
package game.level;

import game.entity.Entity;
import game.entity.Player;
import game.graphics.Shader;
import game.graphics.Texture;
import game.graphics.VertexArray;
import game.level.tiles.*;
import game.level.tiles.Tile;

public class Level {
// number of tiles
// private int width, height;

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() {
float[] vertices = new float[] {
-10.0f, -10.0f * 9.0f / 16.0f, 0.0f,
-10.0f, 10.0f * 9.0f / 16.0f, 0.0f,
0.0f, 10.0f * 9.0f / 16.0f, 0.0f,
0.0f, -10.0f * 9.0f / 16.0f, 0.0f,
};
currentLevel = -1;
loadLevel();
}

private void loadLevel() {

byte[] indices = new byte[] {
0, 1, 2,
2, 3, 0
};
// This is a test level
if (currentLevel == -1) {
width = 7;
height = 7;
tiles = new Tile[width][height];

float[] tcs = new float[] {
0, 1,
0, 0,
1, 0,
1, 1
};

player = new Player();

// 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() {
// tile update
// player update
// enemy update

player.update();
updateTiles();
updateEntities();
}

public void render() {
// render tiles
// render player
// render other entities

player.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
}
}
26 changes: 0 additions & 26 deletions src/game/level/tiles/SmoothFloor.java

This file was deleted.

21 changes: 21 additions & 0 deletions src/game/level/tiles/StoneFloor.java
@@ -0,0 +1,21 @@
package game.level.tiles;

import game.graphics.Texture;
import game.math.Matrix4f;

public class StoneFloor extends Tile{

public StoneFloor(float x, float y) {
position.x = x;
position.y = y;
texture = new Texture("res/sprites/stoneFloor.png");
ml_matrix = Matrix4f.translate(position);
isWall = false;
}

@Override
public void update() {
return;
}

}
22 changes: 22 additions & 0 deletions src/game/level/tiles/StoneWall.java
@@ -0,0 +1,22 @@
package game.level.tiles;

import game.graphics.Texture;
import game.math.Matrix4f;

public class StoneWall extends Tile {

public StoneWall(float x, float y) {
position.x = x;
position.y = y;
texture = new Texture("res/sprites/stoneWall.png");
ml_matrix = Matrix4f.translate(position);
isWall = true;
}

@Override
public void update() {
return;
}


}

0 comments on commit 58879c4

Please sign in to comment.