diff --git a/res/img/bullets/bullet-1/bullet-0.png b/res/img/bullets/bullet-1/bullet-0.png new file mode 100644 index 0000000..c73e337 Binary files /dev/null and b/res/img/bullets/bullet-1/bullet-0.png differ diff --git a/res/img/bullets/bullet-1/bullet-1.png b/res/img/bullets/bullet-1/bullet-1.png new file mode 100644 index 0000000..2054253 Binary files /dev/null and b/res/img/bullets/bullet-1/bullet-1.png differ diff --git a/res/img/bullets/bullet-1/bullet-2.png b/res/img/bullets/bullet-1/bullet-2.png new file mode 100644 index 0000000..669ce9b Binary files /dev/null and b/res/img/bullets/bullet-1/bullet-2.png differ diff --git a/res/img/bullets/bullet-1/bullet-3.png b/res/img/bullets/bullet-1/bullet-3.png new file mode 100644 index 0000000..e100fa0 Binary files /dev/null and b/res/img/bullets/bullet-1/bullet-3.png differ diff --git a/res/img/enemies/basic/basic-0.png b/res/img/enemies/basic/basic-0.png new file mode 100644 index 0000000..23aacaf Binary files /dev/null and b/res/img/enemies/basic/basic-0.png differ diff --git a/res/img/enemies/basic/basic-1.png b/res/img/enemies/basic/basic-1.png new file mode 100644 index 0000000..fd7d668 Binary files /dev/null and b/res/img/enemies/basic/basic-1.png differ diff --git a/res/img/enemies/basic/basic-2.png b/res/img/enemies/basic/basic-2.png new file mode 100644 index 0000000..e8345b6 Binary files /dev/null and b/res/img/enemies/basic/basic-2.png differ diff --git a/res/img/enemies/basic/basic-3.png b/res/img/enemies/basic/basic-3.png new file mode 100644 index 0000000..f45d68b Binary files /dev/null and b/res/img/enemies/basic/basic-3.png differ diff --git a/src/com/gnarly/game/BulletList.java b/src/com/gnarly/game/BulletList.java index 3a16f88..8eac96f 100644 --- a/src/com/gnarly/game/BulletList.java +++ b/src/com/gnarly/game/BulletList.java @@ -7,6 +7,7 @@ import com.gnarly.engine.model.Vao; import com.gnarly.engine.shaders.Shader; import com.gnarly.engine.shaders.Shader2t; import com.gnarly.engine.texture.TextureSet; +import com.gnarly.game.enemies.Enemy; import org.joml.Vector2f; import java.util.ArrayList; @@ -40,7 +41,7 @@ public class BulletList { }; public static final float[] SPEEDS = new float[] { - 20 + 200 }; public static final int @@ -83,12 +84,17 @@ public class BulletList { } } - public void update(Rect rect, Vector2f velocity) { + public void update(ArrayList enemies) { for (int i = 0; i < bullets.size(); ++i) { Bullet bullet = bullets.get(i); Vector2f deltaV = bullet.velocity.mul((float) Main.dtime, new Vector2f()); - if (Collision.collide(rect.getPosition(), rect.getDimensions(), velocity, bullet)) - System.out.println("collision"); + for (int j = 0; j < enemies.size(); ++j) { + if (Collision.collide(enemies.get(j).getPosition(), enemies.get(j).getDimensions(), new Vector2f(), bullet)) { + enemies.remove(j); + --j; + } + System.out.println("Wat"); + } bullet.position.add(deltaV); bullet.boundingPosition.add(deltaV); bullet.lifetime += Main.dtime; diff --git a/src/com/gnarly/game/Collision.java b/src/com/gnarly/game/Collision.java index edb1e8e..7980a4a 100644 --- a/src/com/gnarly/game/Collision.java +++ b/src/com/gnarly/game/Collision.java @@ -64,30 +64,30 @@ public class Collision { } public static boolean segmentCollision(Vector2f P1, Vector2f D1, Vector2f V1, Vector2f P2, Vector2f D2, Vector2f V2) { - final float DELTA = 0.00001f; + final float DELTA = 0.000001f; if ((V1.length() == 0 && V2.length() > 0) || (V2.length() == 0 && V1.length() > 0) || V1.angle(V2) > DELTA) { - float b = (P1.y - P2.y - (V2.y - V1.y) * (P1.x + D1.x - P2.x) / (V2.x - V1.x)) / (D2.y + (V2.y - V1.y) / (V2.x - V1.x)); + float b = (P1.x - P2.x + (V1.x - V2.x) * (P2.y - P1.y) / (V1.y - V2.y)) / (D2.x - D2.y * (V1.x - V2.x) / (V1.y - V2.y)); if (rangeCheck(b)) { float t = (P1.x - P2.x - D2.x * b) / (V2.x - V1.x); if (rangeCheck(t)) return true; } - float a = (P2.y - P1.y - (V1.y - V2.y) * (P2.x + D2.x - P1.x) / (V1.x - V2.x)) / (D1.y + (V1.y - V2.y) / (V1.x - V2.x)); + float a = (P2.x - P1.x - (V1.x - V2.x) * (P1.y - P2.y) / (V2.y - V1.y)) / (D1.x + D1.y * (V1.x - V2.x) / (V2.y - V1.y)); if (rangeCheck(a)) { float t = (P1.x + D1.x * a - P2.x) / (V2.x - V1.x); if (rangeCheck(t)) return true; } - b = (P1.y + D1.y - P2.y - (V2.y - V1.y) * (P1.x + D1.x - P2.x) / (V2.x - V1.x)) / (D2.y + (V2.y - V1.y) / (V2.x - V1.x)); + b = (P1.x + D1.x - P2.x + (V1.x - V2.x) * (P2.y - P1.y - D1.y) / (V1.y - V2.y)) / (D2.x - D2.y * (V1.x - V2.x) / (V1.y - V2.y)); if (rangeCheck(b)) { float t = (P1.x + D1.x - P2.x - D2.x * b) / (V2.x - V1.x); if (rangeCheck(t)) return true; } - a = (P2.y + D2.y - P1.y - (V1.y - V2.y) * (P2.x + D2.x - P1.x) / (V1.x - V2.x)) / (D1.y + (V1.y - V2.y) / (V1.x - V2.x)); + a = (P2.x + D2.x - P1.x - (V1.x - V2.x) * (P1.y - P2.y - D2.y) / (V2.y - V1.y)) / (D1.x + D1.y * (V1.x - V2.x) / (V2.y - V1.y)); if (rangeCheck(a)) { float t = (P1.x + D1.x * a - P2.x - D2.x) / (V2.x - V1.x); if (rangeCheck(t)) diff --git a/src/com/gnarly/game/GamePanel.java b/src/com/gnarly/game/GamePanel.java index b866369..534ffd5 100644 --- a/src/com/gnarly/game/GamePanel.java +++ b/src/com/gnarly/game/GamePanel.java @@ -3,8 +3,12 @@ package com.gnarly.game; import com.gnarly.engine.display.Camera; import com.gnarly.engine.display.Window; import com.gnarly.engine.model.ColRect; +import com.gnarly.game.enemies.BasicEnemy; +import com.gnarly.game.enemies.Enemy; import org.joml.Vector2f; +import java.util.ArrayList; + import static org.lwjgl.glfw.GLFW.GLFW_KEY_SPACE; public class GamePanel extends Panel { @@ -19,33 +23,38 @@ public class GamePanel extends Panel { private float rotation = 0; - private ColRect testRect; + private ArrayList enemies; public GamePanel(Window window, Camera camera) { this.window = window; this.camera = camera; + enemies = new ArrayList(); + enemies.add(new BasicEnemy(camera, 200, 200)); + bullets = new BulletList(camera); background = new Background(camera); player = new Player(window, camera); - testRect = new ColRect(camera, 100, 100, 0, 25, 25, 1, 0, 0, 1, false); - state = Main.GAME_PANEL; } public void update() { - bullets.update(testRect, new Vector2f(0, 100)); + for (int i = 0; i < enemies.size(); ++i) + enemies.get(i).update(); + + bullets.update(enemies); background.update(); player.update(); if (window.keyPressed(GLFW_KEY_SPACE) == Window.BUTTON_PRESSED) - bullets.spawnBullet(new Vector2f(player.getX() + (player.getWidth()) / 2, player.getY() + bullets.getHeight(BulletList.TYPE_PLAYER)), rotation += Math.PI / 16, BulletList.TYPE_PLAYER); + bullets.spawnBullet(new Vector2f(player.getX() + (player.getWidth()) / 2, player.getY() - bullets.getHeight(BulletList.TYPE_PLAYER)), 0, BulletList.TYPE_PLAYER); } public void render() { - testRect.render(); + for(int i = 0; i < enemies.size(); ++i) + enemies.get(i).render(); bullets.render(); background.render(); player.render(); diff --git a/src/com/gnarly/game/Main.java b/src/com/gnarly/game/Main.java index 659af86..5832d77 100644 --- a/src/com/gnarly/game/Main.java +++ b/src/com/gnarly/game/Main.java @@ -26,6 +26,10 @@ public class Main { private int panel; public void start() { + System.out.println(Collision.segmentCollision( + new Vector2f(100, 100), new Vector2f(25, 0), new Vector2f(0, 100), + new Vector2f(110.9f, 165.3f), new Vector2f(3.923f, -0.7804f), new Vector2f(-3.902f, -19.62f) + )); init(); int frames = 0; long curTime, pastTime, pastSec, nspf = 1000000000 / Window.REFRESH_RATE; @@ -39,16 +43,16 @@ public class Main { render(); pastTime += nspf; ++frames; - if (curTime - pastSec > 1000000000) { - fps = frames; - frames = 0; - pastSec += 1000000000; - } - try { - Thread.sleep(nspf / 1000000); - } catch (Exception e) { - e.printStackTrace(); - } + } + if (curTime - pastSec > 1000000000) { + fps = frames; + frames = 0; + pastSec += 1000000000; + } + if (nspf - curTime + pastTime > 10000000) try { + Thread.sleep(1); + } catch (Exception e) { + e.printStackTrace(); } } al.destroy(); @@ -57,8 +61,8 @@ public class Main { private void init() { al = new ALManagement(); - //window = new Window("Gamer Time", true); - window = new Window(768, 432, "Gamer Time", true, true, true); + window = new Window("Gamer Time", true); + //window = new Window(768, 432, "Gamer Time", true, true, true); camera = new Camera(768, 432); Shader.init(); diff --git a/src/com/gnarly/game/enemies/BasicEnemy.java b/src/com/gnarly/game/enemies/BasicEnemy.java new file mode 100644 index 0000000..ce97a2c --- /dev/null +++ b/src/com/gnarly/game/enemies/BasicEnemy.java @@ -0,0 +1,44 @@ +package com.gnarly.game.enemies; + +import com.gnarly.engine.display.Camera; +import com.gnarly.engine.shaders.Shader; +import com.gnarly.engine.shaders.Shader2t; +import com.gnarly.engine.texture.TextureSet; +import com.gnarly.game.Main; + +public class BasicEnemy extends Enemy { + + final float spf = 1.0f / 16.0f; + + public static final float DIMS = 16; + + private static TextureSet textures = null; + private Shader2t shader = Shader.SHADER2T; + + private float time = 0; + + public BasicEnemy(Camera camera, float x, float y) { + super(camera, x, y, DIMS, DIMS, 1); + if (textures == null) { + textures = new TextureSet(new String[] { + "res/img/enemies/basic/basic-0.png", + "res/img/enemies/basic/basic-1.png", + "res/img/enemies/basic/basic-2.png", + "res/img/enemies/basic/basic-3.png" + }); + } + } + + @Override + public void update() { + time = (float) (time + Main.dtime) % (spf * textures.length()); + } + + @Override + public void render() { + textures.get((int) (time / spf)).bind(); + shader.enable(); + shader.setMVP(camera.getMatrix().translate(position).scale(width * scale, height * scale, 1)); + vao.render(); + } +} diff --git a/src/com/gnarly/game/enemies/Enemy.java b/src/com/gnarly/game/enemies/Enemy.java new file mode 100644 index 0000000..01052a0 --- /dev/null +++ b/src/com/gnarly/game/enemies/Enemy.java @@ -0,0 +1,25 @@ +package com.gnarly.game.enemies; + +import com.gnarly.engine.display.Camera; +import com.gnarly.engine.model.Rect; +import com.gnarly.engine.texture.TextureSet; + +public abstract class Enemy extends Rect { + + protected int life; + + Enemy(Camera camera, float x, float y, float width, float height, int life) { + super(camera, x, y, -0.1f, width, height, 0, false); + } + + public abstract void update(); + public abstract void render(); + + public void damage(int damage) { + life -= damage; + } + + public boolean isDead() { + return life <= 0; + } +}