Skip to content
Permalink
d89f901d17
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
104 lines (86 sloc) 2.18 KB
package game.entity;
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();
private int playerKeyUp, playerKeyDown, playerKeyLeft, playerKeyRight;
public Player() {
float[] 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[] {
0, 1, 2,
2, 3, 0
};
float[] tcs = new float[] {
0, 1,
0, 0,
1, 0,
1, 1
};
mesh = new VertexArray(vertices, indices, tcs);
setColor();
if (Options.isWASD) {
playerKeyUp = GLFW.GLFW_KEY_W;
playerKeyDown = GLFW.GLFW_KEY_S;
playerKeyLeft = GLFW.GLFW_KEY_A;
playerKeyRight = GLFW.GLFW_KEY_D;
} else {
playerKeyUp = GLFW.GLFW_KEY_UP;
playerKeyDown = GLFW.GLFW_KEY_DOWN;
playerKeyLeft = GLFW.GLFW_KEY_LEFT;
playerKeyRight = GLFW.GLFW_KEY_RIGHT;
}
}
public void update() {
if (Input.keys[playerKeyUp]) {
position.y += 0.1f;
}
if (Input.keys[playerKeyDown]) {
position.y -= 0.1f;
}
if (Input.keys[playerKeyLeft]) {
position.x -= 0.1f;
}
if (Input.keys[playerKeyRight]) {
position.x += 0.1f;
}
// 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);
setColor();
} else if (Input.keys[GLFW.GLFW_KEY_1]) {
Options.setBallColor(1);
setColor();
} else if (Input.keys[GLFW.GLFW_KEY_2]) {
Options.setBallColor(2);
setColor();
}
}
public void render() {
Shader.PLAYER.enable();
Shader.PLAYER.setUniformMat4f("ml_matrix", Matrix4f.translate(position));
texture.bind();
mesh.render();
Shader.PLAYER.disable();
}
public float getY() {
return position.y;
}
private void setColor() {
texture = Options.getBallTexture();
}
}