Started on platforms. Updated player movement.

This commit is contained in:
Gnarwhal 2020-04-19 06:54:11 -07:00 committed by Gnarwhal
parent d5ac654043
commit 814f49fd4b
Signed by: Gnarwhal
GPG key ID: 0989A73D8C421174
24 changed files with 1035 additions and 717 deletions

View file

@ -1,4 +1,3 @@
Manifest-Version: 1.0
Main-Class: com.gnarwhal.ld46.game.Main

View file

@ -122,7 +122,7 @@ public class Window {
glEnable(GL_MULTISAMPLE);
glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_HIDDEN);
//glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_HIDDEN);
int[] awidth = new int[1], aheight = new int[1];
glfwGetWindowSize(window, awidth, aheight);

View file

@ -34,7 +34,7 @@ public class TexRect extends Rect {
}
public void setCenter(float x, float y) {
position.x = x - width / 2;
position.x = x - width / 2;
position.y = y - height / 2;
}

View file

@ -13,8 +13,7 @@ public abstract class Shader {
public static Shader2c SHADER2C;
public static Shader2t SHADER2T;
public static Shader2b SHADER2B;
protected int program;
protected int mvpLoc;
@ -35,7 +34,7 @@ public abstract class Shader {
glDeleteShader(vert);
glDeleteShader(frag);
mvpLoc = glGetUniformLocation(program, "mvp");
}
@ -80,6 +79,5 @@ public abstract class Shader {
public static void init() {
SHADER2C = new Shader2c();
SHADER2T = new Shader2t();
SHADER2B = new Shader2b();
}
}

View file

@ -1,32 +0,0 @@
package com.gnarwhal.ld46.engine.shaders;
import static org.lwjgl.opengl.GL20.*;
public class Shader2b extends Shader {
private int offset0Loc;
private int offset1Loc;
protected Shader2b() {
super("res/shaders/s2b/vert.gls", "res/shaders/s2b/frag.gls");
getUniforms();
}
public void setOffset(float offset0, float offset1) {
glUniform1f(offset0Loc, offset0);
glUniform1f(offset1Loc, offset1);
}
@Override
protected void getUniforms() {
offset0Loc = glGetUniformLocation(program, "offset0");
offset1Loc = glGetUniformLocation(program, "offset1");
int layer0 = glGetUniformLocation(program, "layer0");
int layer1 = glGetUniformLocation(program, "layer1");
enable();
glUniform1i(layer0, 0);
glUniform1i(layer1, 1);
}
}

View file

@ -5,7 +5,7 @@ import static org.lwjgl.opengl.GL20.glUniform4f;
public class Shader2c extends Shader {
int colorLoc;
private int colorLoc;
protected Shader2c(String vert, String frag) {
super(vert, frag);

View file

@ -0,0 +1,25 @@
package com.gnarwhal.ld46.engine.shaders;
import org.joml.Vector2f;
import static org.lwjgl.opengl.GL20.glGetUniformLocation;
import static org.lwjgl.opengl.GL20.glUniform4f;
public class Shader2t extends Shader {
private int subtextureLoc;
public Shader2t() {
super("res/shaders/s2t/vert.gls", "res/shaders/s2t/frag.gls");
getUniforms();
}
@Override
protected void getUniforms() {
subtextureLoc = glGetUniformLocation(program, "subtexture");
}
public void setSubtexture(Vector2f position, Vector2f dimensions) {
glUniform4f(subtextureLoc, position.x, position.y, dimensions.x, dimensions.y);
}
}

View file

@ -1,12 +1,25 @@
package com.gnarwhal.ld46.engine.shaders;
import org.joml.Vector2f;
import static org.lwjgl.opengl.GL20.glGetUniformLocation;
import static org.lwjgl.opengl.GL20.glUniform4f;
public class Shader2t extends Shader {
protected Shader2t() {
private int subtextureLoc;
public Shader2t() {
super("res/shaders/s2t/vert.gls", "res/shaders/s2t/frag.gls");
getUniforms();
}
@Override
protected void getUniforms() {}
protected void getUniforms() {
subtextureLoc = glGetUniformLocation(program, "subtexture");
}
public void setSubtexture(Vector2f position, Vector2f dimensions) {
glUniform4f(subtextureLoc, position.x, position.y, dimensions.x, dimensions.y);
}
}

View file

@ -32,7 +32,7 @@ public class Texture {
height = bi.getHeight();
int[] pixels = bi.getRGB(0, 0, width, height, null, 0, width);
ByteBuffer buffer = BufferUtils.createByteBuffer(width * height * 4);
for (int i = 0; i < height; i++) {
for (int j = 0; j < width; j++) {
int pixel = pixels[i * width + j];

View file

@ -8,16 +8,20 @@ public class GamePanel {
private Window window;
private Camera camera;
private Player player;
public GamePanel(Window window, Camera camera) {
this.window = window;
this.camera = camera;
player = new Player(window, camera);
}
public void update() {
player.update();
}
public void render() {
player.render();
}
}

View file

@ -51,9 +51,9 @@ public class Main {
private void init() {
al = new ALManagement();
final int WIN_WIDTH = 768, WIN_HEIGHT = 432;
window = new Window("Ludum Dare 46", true);
//window = new Window(WIN_WIDTH, WIN_HEIGHT, "Ludum Dare 46", true, true, true);
final int WIN_WIDTH = 1920, WIN_HEIGHT = 1080;
//window = new Window("Ludum Dare 46", true);
window = new Window(WIN_WIDTH * 3/4, WIN_HEIGHT * 3/4, "Ludum Dare 46", true, true, true);
camera = new Camera(WIN_WIDTH, WIN_HEIGHT);
Shader.init();

View file

@ -0,0 +1,4 @@
package com.gnarwhal.ld46.game;
public class Platform {
}

View file

@ -0,0 +1,207 @@
package com.gnarwhal.ld46.game;
import com.gnarwhal.ld46.engine.display.Camera;
import com.gnarwhal.ld46.engine.display.Window;
import com.gnarwhal.ld46.engine.model.Rect;
import com.gnarwhal.ld46.engine.shaders.Shader2t;
import com.gnarwhal.ld46.engine.texture.Texture;
import jdk.jfr.Percentage;
import org.joml.Vector2f;
import org.joml.Vector3d;
import org.joml.Vector3f;
import static com.gnarwhal.ld46.engine.display.Window.BUTTON_PRESSED;
import static com.gnarwhal.ld46.game.Main.dtime;
import static org.lwjgl.glfw.GLFW.*;
public class Player extends Rect {
private Texture[] textures;
private Shader2t shader;
private Window window;
private Vector3f[] positions;
private Vector3f offset;
private float idleTime;
private Vector3f velocity;
private boolean grounded = false;
public Player(Window window, Camera camera) {
super(camera, 0, 0, -0.1f, 200, 200, 0, false);
this.window = window;
textures = new Texture[] {
new Texture("res/img/player/player_legs.png"),
new Texture("res/img/player/player_head.png"),
new Texture("res/img/player/player_arms.png"),
new Texture("res/img/player/player_effect.png"),
};
positions = new Vector3f[] {
new Vector3f(),
new Vector3f(),
new Vector3f()
};
offset = new Vector3f();
idleTime = 0;
shader = new Shader2t();
velocity = new Vector3f();
}
public void update() {
final float JUMP_SPEED = 812;
final float DECELERATION = 1762;
final float HIGH_SPEED_DECELERATION = 2048;
final float TERMINAL_VELOCITY = 1762;
final float GRAVITY = 1762;
final float speed = 768;
float friction = 0;
float floor = 0;
Vector3f acceleration = new Vector3f(0, GRAVITY, 0);
if (window.keyPressed(GLFW_KEY_A) >= BUTTON_PRESSED) {
floor = -speed;
if (velocity.x > -speed)
velocity.x = -speed;
else
friction = HIGH_SPEED_DECELERATION;
}
else if (window.keyPressed(GLFW_KEY_D) >= BUTTON_PRESSED) {
floor = speed;
if (velocity.x < speed) {
velocity.x = speed;
} else {
friction = -HIGH_SPEED_DECELERATION;
}
} else if (velocity.x != 0) {
float absVelX = Math.abs(velocity.x);
if (absVelX > speed) {
friction = -HIGH_SPEED_DECELERATION * (velocity.x / absVelX);
} else {
friction = -DECELERATION * (velocity.x / absVelX);
}
}
if (window.keyPressed(GLFW_KEY_S) == BUTTON_PRESSED && velocity.y >= 0) {
velocity.y = TERMINAL_VELOCITY;
} else {
float absVelocityY = Math.abs(velocity.y());
if (absVelocityY > TERMINAL_VELOCITY) {
velocity.y = (velocity.y / absVelocityY) * TERMINAL_VELOCITY;
}
}
if ((window.keyPressed(GLFW_KEY_W) == BUTTON_PRESSED
|| window.keyPressed(GLFW_KEY_SPACE) == BUTTON_PRESSED)) {
velocity.y = -JUMP_SPEED;
}
if (velocity.x == 0 && grounded) {
final float SCALE = 10;
final float RATE = (float) (2 * Math.PI / 2.5); // Last number is seconds per loop
offset.y = (float) (SCALE * Math.sin(RATE * idleTime));
idleTime += dtime;
} else {
idleTime = 0;
offset.y = 0;
}
grounded = false;
velocity.add(acceleration.mul((float) dtime, new Vector3f()));
float degradedVelocity = velocity.x() + friction * (float) dtime;
// If the acceleration due to friction would accelerate it past the speed floor set it to the speed floor
if ((degradedVelocity - floor) * (velocity.x() - floor) < 0) {
velocity.x = floor;
// otherwise just apply the friction
} else {
velocity.x = degradedVelocity;
}
position.add(velocity.mul((float) dtime, new Vector3f()));
/*
if (state == STATE_GROUNDED) {
final float MAX_HORZ_VELOCITY = 1024;
final float HORZ_ACCELERATION = 4096;
if (window.keyPressed(GLFW_KEY_A) >= BUTTON_PRESSED) {
velocity.x -= HORZ_ACCELERATION * dtime;
if (velocity.x < -MAX_HORZ_VELOCITY) {
velocity.x = -MAX_HORZ_VELOCITY;
}
} else if (window.keyPressed(GLFW_KEY_D) >= BUTTON_PRESSED) {
velocity.x += HORZ_ACCELERATION * dtime;
if (velocity.x > MAX_HORZ_VELOCITY) {
velocity.x = MAX_HORZ_VELOCITY;
}
} else {
velocity.x *=
}
}
velocity.y += (float) (GRAVITY * dtime);
if (velocity.y > TERMINAL_VELOCITY) {
velocity.y = TERMINAL_VELOCITY;
}
position.add(velocity.mul((float) Main.dtime, new Vector3f()));*/
// Check y bounds
if (position.y + height > camera.getHeight()) {
position.y = camera.getHeight() - height;
velocity.y = 0;
grounded = true;
} else {
grounded = false;
if (position.y < 0) {
position.y = 0;
velocity.y = 0;
}
}
// Check X bounds
if (position.x < 0) {
position.x = 0;
velocity.x = 0;
} else if (position.x + width > camera.getWidth()) {
position.x = camera.getWidth() - width;
velocity.x = 0;
}
final float MAX_DISTANCE = 10;
final float ELASTICITY = 0.5f;
positions[0].set(position).add(offset);
Vector3f displacement = positions[2].sub(positions[0], new Vector3f()).mul(1 - ELASTICITY);
displacement.x = 0;
float length = displacement.length();
if (length > MAX_DISTANCE) {
displacement.div(length).mul(MAX_DISTANCE);
}
positions[2].set(positions[0]).add(displacement);
displacement.set(positions[1]).sub(positions[2]).mul(1 - ELASTICITY);
displacement.x = 0;
length = displacement.length();
if (length > MAX_DISTANCE) {
displacement.div(length).mul(MAX_DISTANCE);
}
System.out.println(displacement);
positions[1].set(positions[2]).add(displacement);
}
public void render() {
shader.enable();
shader.setSubtexture(new Vector2f(0, 0), new Vector2f(0.5f, 0.2f));
for (int i = 0; i < 3; ++i) {
textures[i].bind();
shader.setMVP(camera.getMatrix().translate(positions[i].add(width * scale / 2, height * scale / 2, 0.01f * i, new Vector3f())).rotateZ(rotation).scale(width * scale, height * scale, 1).translate(-0.5f, -0.5f, 0));
vao.render();
}
}
}