Added basic enemy animation, maybe collision?

This commit is contained in:
Gnarwhal 2024-08-07 05:04:17 +00:00
parent 0605e4d91b
commit a6a8938a14
Signed by: Gnarwhal
GPG key ID: 0989A73D8C421174
14 changed files with 115 additions and 27 deletions

Binary file not shown.

After

Width:  |  Height:  |  Size: 184 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 169 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 181 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 164 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 404 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 406 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 407 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 407 B

View file

@ -7,6 +7,7 @@ import com.gnarly.engine.model.Vao;
import com.gnarly.engine.shaders.Shader; import com.gnarly.engine.shaders.Shader;
import com.gnarly.engine.shaders.Shader2t; import com.gnarly.engine.shaders.Shader2t;
import com.gnarly.engine.texture.TextureSet; import com.gnarly.engine.texture.TextureSet;
import com.gnarly.game.enemies.Enemy;
import org.joml.Vector2f; import org.joml.Vector2f;
import java.util.ArrayList; import java.util.ArrayList;
@ -40,7 +41,7 @@ public class BulletList {
}; };
public static final float[] SPEEDS = new float[] { public static final float[] SPEEDS = new float[] {
20 200
}; };
public static final int public static final int
@ -83,12 +84,17 @@ public class BulletList {
} }
} }
public void update(Rect rect, Vector2f velocity) { public void update(ArrayList<Enemy> enemies) {
for (int i = 0; i < bullets.size(); ++i) { for (int i = 0; i < bullets.size(); ++i) {
Bullet bullet = bullets.get(i); Bullet bullet = bullets.get(i);
Vector2f deltaV = bullet.velocity.mul((float) Main.dtime, new Vector2f()); Vector2f deltaV = bullet.velocity.mul((float) Main.dtime, new Vector2f());
if (Collision.collide(rect.getPosition(), rect.getDimensions(), velocity, bullet)) for (int j = 0; j < enemies.size(); ++j) {
System.out.println("collision"); 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.position.add(deltaV);
bullet.boundingPosition.add(deltaV); bullet.boundingPosition.add(deltaV);
bullet.lifetime += Main.dtime; bullet.lifetime += Main.dtime;

View file

@ -64,30 +64,30 @@ public class Collision {
} }
public static boolean segmentCollision(Vector2f P1, Vector2f D1, Vector2f V1, Vector2f P2, Vector2f D2, Vector2f V2) { 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) if ((V1.length() == 0 && V2.length() > 0)
|| (V2.length() == 0 && V1.length() > 0) || (V2.length() == 0 && V1.length() > 0)
|| V1.angle(V2) > DELTA) { || 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)) { if (rangeCheck(b)) {
float t = (P1.x - P2.x - D2.x * b) / (V2.x - V1.x); float t = (P1.x - P2.x - D2.x * b) / (V2.x - V1.x);
if (rangeCheck(t)) if (rangeCheck(t))
return true; 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)) { if (rangeCheck(a)) {
float t = (P1.x + D1.x * a - P2.x) / (V2.x - V1.x); float t = (P1.x + D1.x * a - P2.x) / (V2.x - V1.x);
if (rangeCheck(t)) if (rangeCheck(t))
return true; 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)) { if (rangeCheck(b)) {
float t = (P1.x + D1.x - P2.x - D2.x * b) / (V2.x - V1.x); float t = (P1.x + D1.x - P2.x - D2.x * b) / (V2.x - V1.x);
if (rangeCheck(t)) if (rangeCheck(t))
return true; 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)) { if (rangeCheck(a)) {
float t = (P1.x + D1.x * a - P2.x - D2.x) / (V2.x - V1.x); float t = (P1.x + D1.x * a - P2.x - D2.x) / (V2.x - V1.x);
if (rangeCheck(t)) if (rangeCheck(t))

View file

@ -3,8 +3,12 @@ package com.gnarly.game;
import com.gnarly.engine.display.Camera; import com.gnarly.engine.display.Camera;
import com.gnarly.engine.display.Window; import com.gnarly.engine.display.Window;
import com.gnarly.engine.model.ColRect; import com.gnarly.engine.model.ColRect;
import com.gnarly.game.enemies.BasicEnemy;
import com.gnarly.game.enemies.Enemy;
import org.joml.Vector2f; import org.joml.Vector2f;
import java.util.ArrayList;
import static org.lwjgl.glfw.GLFW.GLFW_KEY_SPACE; import static org.lwjgl.glfw.GLFW.GLFW_KEY_SPACE;
public class GamePanel extends Panel { public class GamePanel extends Panel {
@ -19,33 +23,38 @@ public class GamePanel extends Panel {
private float rotation = 0; private float rotation = 0;
private ColRect testRect; private ArrayList<Enemy> enemies;
public GamePanel(Window window, Camera camera) { public GamePanel(Window window, Camera camera) {
this.window = window; this.window = window;
this.camera = camera; this.camera = camera;
enemies = new ArrayList<Enemy>();
enemies.add(new BasicEnemy(camera, 200, 200));
bullets = new BulletList(camera); bullets = new BulletList(camera);
background = new Background(camera); background = new Background(camera);
player = new Player(window, camera); player = new Player(window, camera);
testRect = new ColRect(camera, 100, 100, 0, 25, 25, 1, 0, 0, 1, false);
state = Main.GAME_PANEL; state = Main.GAME_PANEL;
} }
public void update() { 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(); background.update();
player.update(); player.update();
if (window.keyPressed(GLFW_KEY_SPACE) == Window.BUTTON_PRESSED) 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() { public void render() {
testRect.render(); for(int i = 0; i < enemies.size(); ++i)
enemies.get(i).render();
bullets.render(); bullets.render();
background.render(); background.render();
player.render(); player.render();

View file

@ -26,6 +26,10 @@ public class Main {
private int panel; private int panel;
public void start() { 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(); init();
int frames = 0; int frames = 0;
long curTime, pastTime, pastSec, nspf = 1000000000 / Window.REFRESH_RATE; long curTime, pastTime, pastSec, nspf = 1000000000 / Window.REFRESH_RATE;
@ -39,16 +43,16 @@ public class Main {
render(); render();
pastTime += nspf; pastTime += nspf;
++frames; ++frames;
if (curTime - pastSec > 1000000000) { }
fps = frames; if (curTime - pastSec > 1000000000) {
frames = 0; fps = frames;
pastSec += 1000000000; frames = 0;
} pastSec += 1000000000;
try { }
Thread.sleep(nspf / 1000000); if (nspf - curTime + pastTime > 10000000) try {
} catch (Exception e) { Thread.sleep(1);
e.printStackTrace(); } catch (Exception e) {
} e.printStackTrace();
} }
} }
al.destroy(); al.destroy();
@ -57,8 +61,8 @@ public class Main {
private void init() { private void init() {
al = new ALManagement(); al = new ALManagement();
//window = new Window("Gamer Time", true); window = new Window("Gamer Time", true);
window = new Window(768, 432, "Gamer Time", true, true, true); //window = new Window(768, 432, "Gamer Time", true, true, true);
camera = new Camera(768, 432); camera = new Camera(768, 432);
Shader.init(); Shader.init();

View file

@ -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();
}
}

View file

@ -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;
}
}