From a6a8938a14b46fe6d4aaffaa361ec223e1b58ffd Mon Sep 17 00:00:00 2001 From: Gnarwhal Date: Wed, 7 Aug 2024 05:04:17 +0000 Subject: [PATCH] Added basic enemy animation, maybe collision? --- res/img/bullets/bullet-1/bullet-0.png | Bin 0 -> 184 bytes res/img/bullets/bullet-1/bullet-1.png | Bin 0 -> 169 bytes res/img/bullets/bullet-1/bullet-2.png | Bin 0 -> 181 bytes res/img/bullets/bullet-1/bullet-3.png | Bin 0 -> 164 bytes res/img/enemies/basic/basic-0.png | Bin 0 -> 404 bytes res/img/enemies/basic/basic-1.png | Bin 0 -> 406 bytes res/img/enemies/basic/basic-2.png | Bin 0 -> 407 bytes res/img/enemies/basic/basic-3.png | Bin 0 -> 407 bytes src/com/gnarly/game/BulletList.java | 14 +++++-- src/com/gnarly/game/Collision.java | 10 ++--- src/com/gnarly/game/GamePanel.java | 21 +++++++--- src/com/gnarly/game/Main.java | 28 +++++++------ src/com/gnarly/game/enemies/BasicEnemy.java | 44 ++++++++++++++++++++ src/com/gnarly/game/enemies/Enemy.java | 25 +++++++++++ 14 files changed, 115 insertions(+), 27 deletions(-) create mode 100644 res/img/bullets/bullet-1/bullet-0.png create mode 100644 res/img/bullets/bullet-1/bullet-1.png create mode 100644 res/img/bullets/bullet-1/bullet-2.png create mode 100644 res/img/bullets/bullet-1/bullet-3.png create mode 100644 res/img/enemies/basic/basic-0.png create mode 100644 res/img/enemies/basic/basic-1.png create mode 100644 res/img/enemies/basic/basic-2.png create mode 100644 res/img/enemies/basic/basic-3.png create mode 100644 src/com/gnarly/game/enemies/BasicEnemy.java create mode 100644 src/com/gnarly/game/enemies/Enemy.java 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 0000000000000000000000000000000000000000..c73e337dc958162d31910bc0fdfa94a1201d187b GIT binary patch literal 184 zcmeAS@N?(olHy`uVBq!ia0vp^EI`b`!3HGn8ON~#DaPU;cPEB*=VV@jWYZme9T^xl z_H+M91W9rhctjR6Fz_7!VaBX$ot!{HUr!gu5Q*^6o{NGF3Ovrysf-Z^Drd|nEnqbJ zprl`3_oMEXiEmKQowb@~*P|i@{NHCtv!AwXy>kAj3Fj(41oqSc)I$ztaD0e0sxTAHX+D3yL&){Cv0>Xb6L+tDnm{r-UW| D_o*vW literal 0 HcmV?d00001 diff --git a/res/img/enemies/basic/basic-0.png b/res/img/enemies/basic/basic-0.png new file mode 100644 index 0000000000000000000000000000000000000000..23aacafbe9302651ae462d0feeda2d2904ee62df GIT binary patch literal 404 zcmeAS@N?(olHy`uVBq!ia0vp^0wB!61|;P_|4#%`jKx9jP7LeL$-D$|SkfJR9T^xl z_H+M9WCij$3p^r=85sBufiR<}hF1engQlm8V~B-+@{j-j?Pb(cO&B#<{x>dA;F52- zc-6pgL1V*&GyenBBy2>4nm%8A>cEiaP?Yxh|MvCs8*ffCi<4+OaKs_YA?u)!L&L+S z>H2a_nG@71Wo6j}+8AfZ7$va$WWHIJ;(C1MzJ|=x^)?AoEJswI9Ne;Dg~;JMFSqve zEO0ouOj6##tc7!#r1XxWj|LKtnH2U)Br-4?^zPZSr-C=})`m1IdA;F52- zc-6pgL1V*&GyenBWNbu)n7R`uH%m{LbMw#tx3AL^wk_?wUOz)kf>((7qJpWyRs}PT zgdGo_cDDzJF*on&a+h^%>VPUY_>9;(!T9hxdu*%)~&=ww;%= zB_$mjn|zJ!8ge*Zto<2%6IL7KwWq62zEkUZXvht(Up)HALOWZW1-Tp~HMJ~E~_&q$cHu~A!`zu8ca;ZnCCn*qb+6Q@siUl&-$o4w4~>1M(5gA5+V zM!ZZXvK*=o>izigQZ|Bt}85o!hp00i_>zopr0QuLDE&u=k literal 0 HcmV?d00001 diff --git a/res/img/enemies/basic/basic-3.png b/res/img/enemies/basic/basic-3.png new file mode 100644 index 0000000000000000000000000000000000000000..f45d68bc6d03fcbf47bbb9393493ed1acff4501c GIT binary patch literal 407 zcmV;I0cie-P)Px#1ZP1_K>z@;j|==^1poj532;bRa{vGi!TtA&9vk=(Hu@u|`~f5uT53T<0tuaowSfYHF<205 z7T&OPj(c}}$>z=O&U?3eOV-+Cv^)1Z`J2m_7K=N{?g_}F+3H$X(kzOS8t6SC3ApV( z*wy!f>-y!j6|*STpy)v_YLGg?*K+16qDr;usy%v7Ft~^Qx^5Z=zpVAm5i2+?RalQt z%JkT~GX_5;%X|nO5{NmAkM|*uJm5|J13`>o7#<7>o}YTg)ydHWn6(1k123Ooz5d|^ z;D>1Ro_IK5@YXkZA#ohD`et}g^D_#Ks~cM=L~cej8NIO9F7_cno58;T;D=~@Gf2IH zzRqO)$^&GXLGCl^*U`04>I$0j**R}6=AStML|gGZxrU9W{tEyA002ovPDHLkV1hZB BsjL71 literal 0 HcmV?d00001 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; + } +}