Enemy movement patterns

This commit is contained in:
Gnarwhal 2024-08-07 05:04:18 +00:00
parent a6a8938a14
commit 0e85a338c5
Signed by: Gnarwhal
GPG key ID: 0989A73D8C421174
11 changed files with 291 additions and 88 deletions

BIN
res/audio/theme.wav Normal file

Binary file not shown.

BIN
res/img/ui/highscore.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 293 B

BIN
res/img/ui/numbers.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 312 B

0
res/levels/level1.txt Normal file
View file

View file

@ -2,7 +2,6 @@ package com.gnarly.game;
import com.gnarly.engine.display.Camera;
import com.gnarly.engine.model.ColRect;
import com.gnarly.engine.model.Rect;
import com.gnarly.engine.model.Vao;
import com.gnarly.engine.shaders.Shader;
import com.gnarly.engine.shaders.Shader2t;
@ -34,18 +33,18 @@ public class BulletList {
public Vector2f boundingPosition;
public Vector2f bounds;
public int damage;
}
public static final Vector2f[] DIMENSIONS = new Vector2f[] {
new Vector2f(4, -8),
new Vector2f(4, -8)
};
public static final float[] SPEEDS = new float[] {
200
};
public static final int
TYPE_PLAYER = 0x00;
TYPE_PLAYER = 0x00,
TYPE_SMOL = 0x01;
private Camera camera;
@ -56,12 +55,18 @@ public class BulletList {
bullets = new ArrayList<>();
if (sprites == null) {
sprites = new TextureSet[1];
sprites = new TextureSet[2];
sprites[0] = new TextureSet(new String[] {
"res/img/bullets/bullet-0/bullet-0.png",
"res/img/bullets/bullet-0/bullet-1.png",
"res/img/bullets/bullet-0/bullet-2.png",
"res/img/bullets/bullet-0/bullet-3.png",
"res/img/bullets/bullet-0/bullet-3.png"
});
sprites[1] = new TextureSet(new String[] {
"res/img/bullets/bullet-1/bullet-0.png",
"res/img/bullets/bullet-1/bullet-1.png",
"res/img/bullets/bullet-1/bullet-2.png",
"res/img/bullets/bullet-1/bullet-3.png"
});
float vertices[] = {
1, 1, 0, // Top left
@ -89,11 +94,13 @@ public class BulletList {
Bullet bullet = bullets.get(i);
Vector2f deltaV = bullet.velocity.mul((float) Main.dtime, new Vector2f());
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;
if (Collision.collide(enemies.get(j).getPosition(), enemies.get(j).getDimensions(), enemies.get(j).getVelocity(), bullet)) {
enemies.get(j).damage(bullet.damage);
if (enemies.get(j).isDead()) {
enemies.remove(j);
--j;
}
}
System.out.println("Wat");
}
bullet.position.add(deltaV);
bullet.boundingPosition.add(deltaV);
@ -107,6 +114,27 @@ public class BulletList {
}
}
public void update(Player player) {
for (int i = 0; i < bullets.size(); ++i) {
Bullet bullet = bullets.get(i);
Vector2f deltaV = bullet.velocity.mul((float) Main.dtime, new Vector2f());
Collision.Bounds bounds = player.getBounds();
Vector2f[] segments = player.getSegments();
if (Collision.collide(bounds, segments, player.getVelocity(), bullet))
player.damage(bullet.damage);
bullet.position.add(deltaV);
bullet.boundingPosition.add(deltaV);
bullet.lifetime += Main.dtime;
if (bullet.boundingPosition.x > camera.getWidth() || bullet.boundingPosition.y > camera.getHeight()
|| bullet.boundingPosition.x + bullet.bounds.x < 0 || bullet.boundingPosition.y + bullet.bounds.y < 0) {
bullets.remove(i);
--i;
}
}
}
public void render() {
for (int i = 0; i < bullets.size(); ++i) {
Bullet bullet = bullets.get(i);
@ -117,11 +145,11 @@ public class BulletList {
}
}
public void spawnBullet(Vector2f position, float rotation, int type) {
spawnBullet(position, DIMENSIONS[type], rotation, SPEEDS[type], type);
public void spawnBullet(Vector2f position, float rotation, int type, int speed, int damage) {
spawnBullet(position, DIMENSIONS[type], rotation, speed, type, damage);
}
private void spawnBullet(Vector2f position, Vector2f dimensions, float rotation, float velocity, int sprite) {
private void spawnBullet(Vector2f position, Vector2f dimensions, float rotation, float velocity, int sprite, int damage) {
float sin = (float) -Math.sin(rotation);
float cos = (float) Math.cos(rotation);
@ -150,6 +178,7 @@ public class BulletList {
bullet.lifetime = 0;
bullet.sprite = sprite;
bullet.damage = damage;
Vector2f p0 = new Vector2f(bullet.position);
Vector2f p1 = new Vector2f(bullet.position).add(bullet.side0);
@ -168,11 +197,11 @@ public class BulletList {
bullets.add(bullet);
}
public float getWidth(int type) {
public static float getWidth(int type) {
return DIMENSIONS[type].x;
}
public float getHeight(int type) {
public static float getHeight(int type) {
return DIMENSIONS[type].y;
}

View file

@ -13,9 +13,36 @@ public class Collision {
}
}
public static boolean collide(Bounds bounds, Vector2f[] segments, Vector2f velocity, BulletList.Bullet bullet) {
Vector2f scaledBV = bullet.velocity.mul((float) Main.dtime, new Vector2f());
Bounds bulletBounds = getBounds(bullet.boundingPosition, bullet.bounds, scaledBV);
if (overlap(bounds, bulletBounds)) {
Vector2f[] bulletSegments = new Vector2f[] {
bullet.position,
bullet.side0,
bullet.side1,
bullet.side0.negate(new Vector2f()),
bullet.side1.negate(new Vector2f())
};
Vector2f curBoundsSegment = new Vector2f();
for (int i = 0; i < segments.length - 1; ++i) {
curBoundsSegment.add(segments[i]);
Vector2f curBulletSegment = new Vector2f();
for (int j = 0; j < bulletSegments.length - 1; ++j) {
curBulletSegment.add(bulletSegments[j]);
if (segmentCollision(curBoundsSegment, segments[i + 1], velocity, curBulletSegment, bulletSegments[j + 1], scaledBV))
return true;
}
}
}
return false;
}
public static boolean collide(Vector2f position, Vector2f dimensions, Vector2f boundVelocity, BulletList.Bullet bullet) {
Vector2f scaledBV = bullet.velocity.mul((float) Main.dtime, new Vector2f());
Bounds bounds = getBounds(position, dimensions, boundVelocity);
Bounds bulletBounds = getBounds(bullet.boundingPosition, bullet.bounds, bullet.velocity);
Bounds bulletBounds = getBounds(bullet.boundingPosition, bullet.bounds, scaledBV);
GamePanel.show.set(position.x, position.y, bounds.dimensions.x, bounds.dimensions.y);
if (overlap(bounds, bulletBounds)) {
Vector2f[] boundsSegments = new Vector2f[] {
position,
@ -36,8 +63,8 @@ public class Collision {
curBoundsSegment.add(boundsSegments[i]);
Vector2f curBulletSegment = new Vector2f();
for (int j = 0; j < bulletSegments.length - 1; ++j) {
curBulletSegment.add(bulletSegments[i]);
if (segmentCollision(curBoundsSegment, boundsSegments[i + 1], boundVelocity, curBulletSegment, bulletSegments[j + 1], bullet.velocity))
curBulletSegment.add(bulletSegments[j]);
if (segmentCollision(curBoundsSegment, boundsSegments[i + 1], boundVelocity, curBulletSegment, bulletSegments[j + 1], scaledBV))
return true;
}
}
@ -47,51 +74,75 @@ public class Collision {
public static boolean overlap(Bounds bounds0, Bounds bounds1) {
return bounds0.position.x < bounds1.position.x + bounds1.dimensions.x && bounds0.position.x + bounds0.dimensions.x > bounds1.position.x
&& bounds0.position.y < bounds1.position.y + bounds1.dimensions.y && bounds1.position.y + bounds1.dimensions.y > bounds1.position.y;
&& bounds0.position.y < bounds1.position.y + bounds1.dimensions.y && bounds0.position.y + bounds0.dimensions.y > bounds1.position.y;
}
public static Bounds getBounds(Vector2f position, Vector2f dimensions, Vector2f velocity) {
Bounds bounds = new Bounds(position, dimensions);
if (velocity.x < 0)
if (velocity.x < 0) {
bounds.position.x += velocity.x;
bounds.dimensions.x -= velocity.x;
}
else
bounds.dimensions.x += velocity.x;
if (velocity.y < 0)
if (velocity.y < 0) {
bounds.position.y += velocity.y;
bounds.dimensions.y -= velocity.y;
}
else
bounds.dimensions.y += velocity.y;
return bounds;
}
public static boolean segmentCollision(Vector2f P1, Vector2f D1, Vector2f V1, Vector2f P2, Vector2f D2, Vector2f V2) {
final float DELTA = 0.000001f;
final float EPSILON = 0.000001f;
if ((V1.length() == 0 && V2.length() > 0)
|| (V2.length() == 0 && V1.length() > 0)
|| V1.angle(V2) > DELTA) {
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;
|| Math.abs(V1.angle(V2)) > EPSILON) {
if (V1.length() > 0) {
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;
if (V1.y == 0)
t = (P1.x + D1.x - P2.x - D2.x * b) / (V2.x - V1.x);
else
t = (P1.y + D1.y - P2.y - D2.y * b) / (V2.y - V1.y);
if (rangeCheck(t))
return true;
}
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;
if (V1.y == 0)
t = (P1.x + D1.x - P2.x - D2.x * b) / (V2.x - V1.x);
else
t = (P1.y + D1.y - P2.y - D2.y * b) / (V2.y - V1.y);
if (rangeCheck(t))
return true;
}
}
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.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.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))
return true;
if (V2.length() > 0) {
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;
if (V2.y == 0)
t = (P1.x + D1.x * a - P2.x - D2.x) / (V2.x - V1.x);
else
t = (P1.y + D1.y * a - P2.y - D2.y) / (V2.y - V1.y);
if (rangeCheck(t))
return true;
}
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;
if (V2.y == 0)
t = (P1.x + D1.x * a - P2.x - D2.x) / (V2.x - V1.x);
else
t = (P1.y + D1.y * a - P2.y - D2.y) / (V2.y - V1.y);
if (rangeCheck(t))
return true;
}
}
}
return false;

View file

@ -16,48 +16,78 @@ public class GamePanel extends Panel {
private Window window;
private Camera camera;
private BulletList bullets;
private BulletList playerBullets;
private BulletList enemyBullets;
private Background background;
private Player player;
private float rotation = 0;
private ArrayList<Enemy> enemies;
public static ColRect show;
public GamePanel(Window window, Camera camera) {
this.window = window;
this.camera = camera;
enemies = new ArrayList<Enemy>();
enemies.add(new BasicEnemy(camera, 200, 200));
enemies.add(new BasicEnemy(camera, 200, 200, 4,
new float[] {
3, 1, 3, 1
},
new Vector2f[] {
new Vector2f( 300, 0),
new Vector2f( 0, 100),
new Vector2f(-300, 0),
new Vector2f( 0, -100),
}
));
enemies.add(new BasicEnemy(camera, 200, 200, 0,
new float[] {
3, 1, 3, 1
},
new Vector2f[] {
new Vector2f( 300, 0),
new Vector2f( 0, 100),
new Vector2f(-300, 0),
new Vector2f( 0, -100),
}
));
bullets = new BulletList(camera);
playerBullets = new BulletList(camera);
enemyBullets = new BulletList(camera);
enemyBullets.spawnBullet(new Vector2f(camera.getWidth() / 2, 25), (float) Math.PI, BulletList.TYPE_SMOL, 400, 1);
background = new Background(camera);
player = new Player(window, camera);
player = new Player(window, camera, playerBullets);
state = Main.GAME_PANEL;
show = new ColRect(camera, 0, 0, 0, 100, 100, 1, 0, 0, 0.5f, false);
}
public void update() {
background.update();
player.update();
for (int i = 0; i < enemies.size(); ++i)
enemies.get(i).update();
bullets.update(enemies);
background.update();
player.update();
playerBullets.update(enemies);
enemyBullets.update(player);
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)), 0, BulletList.TYPE_PLAYER);
player.applyMovement();
for (int i = 0; i < enemies.size(); ++i)
enemies.get(i).applyMovement();
}
public void render() {
background.render();
enemyBullets.render();
playerBullets.render();
for(int i = 0; i < enemies.size(); ++i)
enemies.get(i).render();
bullets.render();
background.render();
player.render();
show.render();
}
public void reset() {

View file

@ -1,6 +1,7 @@
package com.gnarly.game;
import com.gnarly.engine.audio.ALManagement;
import com.gnarly.engine.audio.Sound;
import com.gnarly.engine.display.Camera;
import com.gnarly.engine.display.Window;
import com.gnarly.engine.shaders.Shader;
@ -24,12 +25,10 @@ public class Main {
private Panel[] panels;
private int panel;
private Sound sound;
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;
@ -61,14 +60,17 @@ 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();
panels = new Panel[NUM_PANELS];
panels[GAME_PANEL] = new GamePanel(window, camera);
panel = GAME_PANEL;
sound = new Sound("res/audio/theme.wav");
sound.play(true);
}
private void update() {

View file

@ -4,22 +4,29 @@ import com.gnarly.engine.display.Camera;
import com.gnarly.engine.display.Window;
import com.gnarly.engine.model.TexRect;
import com.gnarly.engine.texture.Texture;
import org.joml.Vector2f;
import static org.lwjgl.glfw.GLFW.*;
public class Player extends TexRect {
private static Texture[] frames = null;
private double spf = 1.0 / 14.0;
private double time = 0;
private int frame = 0;
private float speed = 150;
private int health = 5;
private static Texture[] frames = null;
private Vector2f velocity = new Vector2f();
private Window window;
private BulletList bullets;
public Player(Window window, Camera camera) {
super(camera, (Texture) null, 0, 0, -0.1f, 32, 32, 0, false);
public Player(Window window, Camera camera, BulletList bullets) {
super(camera, (Texture) null, camera.getWidth() / 2 - 16, camera.getHeight() / 2 - 16, -0.1f, 32, 32, 0, false);
this.window = window;
this.bullets = bullets;
if (frames == null) {
frames = new Texture[4];
@ -33,25 +40,32 @@ public class Player extends TexRect {
}
public void update() {
final int SPEED = 150;
float speedMultiplier = 1;
if (window.keyPressed(GLFW_KEY_LEFT_SHIFT) >= Window.BUTTON_PRESSED)
speedMultiplier *= 1.5;
else if (window.keyPressed(GLFW_KEY_LEFT_CONTROL) >= Window.BUTTON_PRESSED)
speedMultiplier *= 0.5;
velocity.set(0, 0);
if (window.keyPressed(GLFW_KEY_W) >= Window.BUTTON_PRESSED)
position.y -= SPEED * Main.dtime;
velocity.y = -speed * (float) Main.dtime * speedMultiplier;
if (window.keyPressed(GLFW_KEY_A) >= Window.BUTTON_PRESSED)
position.x -= SPEED * Main.dtime;
velocity.x = -speed * (float) Main.dtime * speedMultiplier;
if (window.keyPressed(GLFW_KEY_S) >= Window.BUTTON_PRESSED)
position.y += SPEED * Main.dtime;
velocity.y = speed * (float) Main.dtime * speedMultiplier;
if (window.keyPressed(GLFW_KEY_D) >= Window.BUTTON_PRESSED)
position.x += SPEED * Main.dtime;
velocity.x = speed * (float) Main.dtime * speedMultiplier;
if (position.x < 0)
position.x = 0;
else if (position.x + width >= camera.getWidth())
position.x = camera.getWidth() - width;
if (position.x + velocity.x < 0)
velocity.x = -position.x;
else if (position.x + width + velocity.x >= camera.getWidth())
velocity.x = camera.getWidth() - width - position.x;
if (position.y < 0)
position.y = 0;
else if (position.y + height >= camera.getHeight())
position.y = camera.getHeight() - height;
if (position.y + velocity.y < 0)
velocity.y = -position.y;
else if (position.y + height + velocity.y >= camera.getHeight())
velocity.y = camera.getHeight() - height - position.y;
time += Main.dtime;
while (time >= spf) {
@ -59,5 +73,44 @@ public class Player extends TexRect {
frame = (frame + 1) % frames.length;
setTexture(frames[frame]);
}
if (window.keyPressed(GLFW_KEY_SPACE) == Window.BUTTON_PRESSED)
bullets.spawnBullet(new Vector2f(position.x + width / 2, position.y - BulletList.getHeight(BulletList.TYPE_PLAYER)), 0, BulletList.TYPE_PLAYER, 400, 1);
}
public Collision.Bounds getBounds() {
return Collision.getBounds(new Vector2f(position.x, position.y), getDimensions(), velocity);
}
public Vector2f[] getSegments() {
return new Vector2f[] {
new Vector2f(position.x, position.y).add(16, 0),
new Vector2f( 4, 10),
new Vector2f( 12, 12),
new Vector2f(-3, 5 ),
new Vector2f(-8, 0 ),
new Vector2f(-5, 5 ),
new Vector2f(-5, -5 ),
new Vector2f(-8, 0 ),
new Vector2f(-3, -5 ),
new Vector2f( 12, -12),
new Vector2f( 4, -10),
};
}
public void applyMovement() {
position.add(velocity.x, velocity.y, 0);
}
public Vector2f getVelocity() {
return velocity;
}
public void damage(int damage) {
health -= damage;
}
public int getHealth() {
return health;
}
}

View file

@ -5,6 +5,7 @@ import com.gnarly.engine.shaders.Shader;
import com.gnarly.engine.shaders.Shader2t;
import com.gnarly.engine.texture.TextureSet;
import com.gnarly.game.Main;
import org.joml.Vector2f;
public class BasicEnemy extends Enemy {
@ -16,9 +17,19 @@ public class BasicEnemy extends Enemy {
private Shader2t shader = Shader.SHADER2T;
private float time = 0;
private float passedTime = 0;
private int timeIndex = 0;
public BasicEnemy(Camera camera, float x, float y) {
super(camera, x, y, DIMS, DIMS, 1);
private float[] timing;
private Vector2f[] path;
private Vector2f start;
private Vector2f origin;
public BasicEnemy(Camera camera, float x, float y, float timeOffset, float[] times, Vector2f[] path) {
super(camera, 0, 0, DIMS, DIMS, 1);
this.timing = times;
this.path = path;
velocity = new Vector2f();
if (textures == null) {
textures = new TextureSet(new String[] {
"res/img/enemies/basic/basic-0.png",
@ -27,16 +38,33 @@ public class BasicEnemy extends Enemy {
"res/img/enemies/basic/basic-3.png"
});
}
time = timeOffset;
start = new Vector2f();
for (; (timeOffset -= times[timeIndex]) >= 0; timeIndex = (timeIndex + 1) % path.length) {
passedTime += times[timeIndex];
start.add(path[timeIndex]);
}
origin = new Vector2f(x, y);
}
@Override
public void update() {
time = (float) (time + Main.dtime) % (spf * textures.length());
position.sub(origin.x, origin.y, 0);
time += Main.dtime;
float interp;
while ((interp = (time - passedTime) / timing[timeIndex]) > 1) {
passedTime += timing[timeIndex];
start.add(path[timeIndex]);
timeIndex = (timeIndex + 1) % timing.length;
}
velocity = path[timeIndex].mul(interp, new Vector2f()).add(start).sub(position.x, position.y);
position.add(origin.x, origin.y, 0);
System.out.println(position);
}
@Override
public void render() {
textures.get((int) (time / spf)).bind();
textures.get((int) (time / spf) % textures.length()).bind();
shader.enable();
shader.setMVP(camera.getMatrix().translate(position).scale(width * scale, height * scale, 1));
vao.render();

View file

@ -2,12 +2,14 @@ package com.gnarly.game.enemies;
import com.gnarly.engine.display.Camera;
import com.gnarly.engine.model.Rect;
import com.gnarly.engine.texture.TextureSet;
import org.joml.Vector2f;
public abstract class Enemy extends Rect {
protected int life;
protected Vector2f velocity;
Enemy(Camera camera, float x, float y, float width, float height, int life) {
super(camera, x, y, -0.1f, width, height, 0, false);
}
@ -15,6 +17,14 @@ public abstract class Enemy extends Rect {
public abstract void update();
public abstract void render();
public Vector2f getVelocity() {
return velocity;
}
public void applyMovement() {
position.add(velocity.x, velocity.y, 0);
}
public void damage(int damage) {
life -= damage;
}