Skip to content
Permalink
58879c4dcb
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
100 lines (84 sloc) 2.04 KB
package game.entity;
import org.lwjgl.glfw.GLFW;
import game.Options;
import game.graphics.Shader;
import game.graphics.VertexArray;
import game.input.Input;
import game.math.Matrix4f;
public class Player extends Entity{
private int playerKeyUp, playerKeyDown, playerKeyLeft, playerKeyRight;
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,
};
indices = new byte[] {
0, 1, 2,
2, 3, 0
};
tcs = new float[] {
0, 1,
0, 0,
1, 0,
1, 1
};
mesh = new VertexArray(vertices, indices, tcs);
// Just for testing at the moment
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 += speed;
}
if (Input.keys[playerKeyDown]) {
position.y -= speed;
}
if (Input.keys[playerKeyLeft]) {
position.x -= speed;
}
if (Input.keys[playerKeyRight]) {
position.x += speed;
}
// 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();
}
}