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.display.Camera;
import com.gnarly.engine.model.ColRect; import com.gnarly.engine.model.ColRect;
import com.gnarly.engine.model.Rect;
import com.gnarly.engine.model.Vao; 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;
@ -34,18 +33,18 @@ public class BulletList {
public Vector2f boundingPosition; public Vector2f boundingPosition;
public Vector2f bounds; public Vector2f bounds;
public int damage;
} }
public static final Vector2f[] DIMENSIONS = new Vector2f[] { public static final Vector2f[] DIMENSIONS = new Vector2f[] {
new Vector2f(4, -8),
new Vector2f(4, -8) new Vector2f(4, -8)
}; };
public static final float[] SPEEDS = new float[] {
200
};
public static final int public static final int
TYPE_PLAYER = 0x00; TYPE_PLAYER = 0x00,
TYPE_SMOL = 0x01;
private Camera camera; private Camera camera;
@ -56,12 +55,18 @@ public class BulletList {
bullets = new ArrayList<>(); bullets = new ArrayList<>();
if (sprites == null) { if (sprites == null) {
sprites = new TextureSet[1]; sprites = new TextureSet[2];
sprites[0] = new TextureSet(new String[] { sprites[0] = new TextureSet(new String[] {
"res/img/bullets/bullet-0/bullet-0.png", "res/img/bullets/bullet-0/bullet-0.png",
"res/img/bullets/bullet-0/bullet-1.png", "res/img/bullets/bullet-0/bullet-1.png",
"res/img/bullets/bullet-0/bullet-2.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[] = { float vertices[] = {
1, 1, 0, // Top left 1, 1, 0, // Top left
@ -89,12 +94,35 @@ public class BulletList {
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());
for (int j = 0; j < enemies.size(); ++j) { for (int j = 0; j < enemies.size(); ++j) {
if (Collision.collide(enemies.get(j).getPosition(), enemies.get(j).getDimensions(), new Vector2f(), bullet)) { 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); enemies.remove(j);
--j; --j;
} }
System.out.println("Wat");
} }
}
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 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.position.add(deltaV);
bullet.boundingPosition.add(deltaV); bullet.boundingPosition.add(deltaV);
bullet.lifetime += Main.dtime; bullet.lifetime += Main.dtime;
@ -117,11 +145,11 @@ public class BulletList {
} }
} }
public void spawnBullet(Vector2f position, float rotation, int type) { public void spawnBullet(Vector2f position, float rotation, int type, int speed, int damage) {
spawnBullet(position, DIMENSIONS[type], rotation, SPEEDS[type], type); 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 sin = (float) -Math.sin(rotation);
float cos = (float) Math.cos(rotation); float cos = (float) Math.cos(rotation);
@ -150,6 +178,7 @@ public class BulletList {
bullet.lifetime = 0; bullet.lifetime = 0;
bullet.sprite = sprite; bullet.sprite = sprite;
bullet.damage = damage;
Vector2f p0 = new Vector2f(bullet.position); Vector2f p0 = new Vector2f(bullet.position);
Vector2f p1 = new Vector2f(bullet.position).add(bullet.side0); Vector2f p1 = new Vector2f(bullet.position).add(bullet.side0);
@ -168,11 +197,11 @@ public class BulletList {
bullets.add(bullet); bullets.add(bullet);
} }
public float getWidth(int type) { public static float getWidth(int type) {
return DIMENSIONS[type].x; return DIMENSIONS[type].x;
} }
public float getHeight(int type) { public static float getHeight(int type) {
return DIMENSIONS[type].y; 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) { 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 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)) { if (overlap(bounds, bulletBounds)) {
Vector2f[] boundsSegments = new Vector2f[] { Vector2f[] boundsSegments = new Vector2f[] {
position, position,
@ -36,8 +63,8 @@ public class Collision {
curBoundsSegment.add(boundsSegments[i]); curBoundsSegment.add(boundsSegments[i]);
Vector2f curBulletSegment = new Vector2f(); Vector2f curBulletSegment = new Vector2f();
for (int j = 0; j < bulletSegments.length - 1; ++j) { for (int j = 0; j < bulletSegments.length - 1; ++j) {
curBulletSegment.add(bulletSegments[i]); curBulletSegment.add(bulletSegments[j]);
if (segmentCollision(curBoundsSegment, boundsSegments[i + 1], boundVelocity, curBulletSegment, bulletSegments[j + 1], bullet.velocity)) if (segmentCollision(curBoundsSegment, boundsSegments[i + 1], boundVelocity, curBulletSegment, bulletSegments[j + 1], scaledBV))
return true; return true;
} }
} }
@ -47,53 +74,77 @@ public class Collision {
public static boolean overlap(Bounds bounds0, Bounds bounds1) { 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 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) { public static Bounds getBounds(Vector2f position, Vector2f dimensions, Vector2f velocity) {
Bounds bounds = new Bounds(position, dimensions); Bounds bounds = new Bounds(position, dimensions);
if (velocity.x < 0) if (velocity.x < 0) {
bounds.position.x += velocity.x; bounds.position.x += velocity.x;
bounds.dimensions.x -= velocity.x;
}
else else
bounds.dimensions.x += velocity.x; bounds.dimensions.x += velocity.x;
if (velocity.y < 0) if (velocity.y < 0) {
bounds.position.y += velocity.y; bounds.position.y += velocity.y;
bounds.dimensions.y -= velocity.y;
}
else else
bounds.dimensions.y += velocity.y; bounds.dimensions.y += velocity.y;
return bounds; return bounds;
} }
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.000001f; final float EPSILON = 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) { || 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)); 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;
if (rangeCheck(t)) if (V1.y == 0)
return true; t = (P1.x + D1.x - P2.x - D2.x * b) / (V2.x - V1.x);
} else
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)); t = (P1.y + D1.y - P2.y - D2.y * b) / (V2.y - V1.y);
if (rangeCheck(a)) {
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.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)); 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;
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;
}
}
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)) if (rangeCheck(t))
return true; 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)); 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;
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)) if (rangeCheck(t))
return true; return true;
} }
} }
}
return false; return false;
} }

View file

@ -16,48 +16,78 @@ public class GamePanel extends Panel {
private Window window; private Window window;
private Camera camera; private Camera camera;
private BulletList bullets; private BulletList playerBullets;
private BulletList enemyBullets;
private Background background; private Background background;
private Player player; private Player player;
private float rotation = 0;
private ArrayList<Enemy> enemies; private ArrayList<Enemy> enemies;
public static ColRect show;
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 = 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); background = new Background(camera);
player = new Player(window, camera); player = new Player(window, camera, playerBullets);
state = Main.GAME_PANEL; state = Main.GAME_PANEL;
show = new ColRect(camera, 0, 0, 0, 100, 100, 1, 0, 0, 0.5f, false);
} }
public void update() { public void update() {
background.update();
player.update();
for (int i = 0; i < enemies.size(); ++i) for (int i = 0; i < enemies.size(); ++i)
enemies.get(i).update(); enemies.get(i).update();
bullets.update(enemies); playerBullets.update(enemies);
background.update(); enemyBullets.update(player);
player.update();
if (window.keyPressed(GLFW_KEY_SPACE) == Window.BUTTON_PRESSED) player.applyMovement();
bullets.spawnBullet(new Vector2f(player.getX() + (player.getWidth()) / 2, player.getY() - bullets.getHeight(BulletList.TYPE_PLAYER)), 0, BulletList.TYPE_PLAYER); for (int i = 0; i < enemies.size(); ++i)
enemies.get(i).applyMovement();
} }
public void render() { public void render() {
background.render();
enemyBullets.render();
playerBullets.render();
for(int i = 0; i < enemies.size(); ++i) for(int i = 0; i < enemies.size(); ++i)
enemies.get(i).render(); enemies.get(i).render();
bullets.render();
background.render();
player.render(); player.render();
show.render();
} }
public void reset() { public void reset() {

View file

@ -1,6 +1,7 @@
package com.gnarly.game; package com.gnarly.game;
import com.gnarly.engine.audio.ALManagement; import com.gnarly.engine.audio.ALManagement;
import com.gnarly.engine.audio.Sound;
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.shaders.Shader; import com.gnarly.engine.shaders.Shader;
@ -25,11 +26,9 @@ public class Main {
private Panel[] panels; private Panel[] panels;
private int panel; private int panel;
private Sound sound;
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;
@ -61,14 +60,17 @@ 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();
panels = new Panel[NUM_PANELS]; panels = new Panel[NUM_PANELS];
panels[GAME_PANEL] = new GamePanel(window, camera); panels[GAME_PANEL] = new GamePanel(window, camera);
panel = GAME_PANEL; panel = GAME_PANEL;
sound = new Sound("res/audio/theme.wav");
sound.play(true);
} }
private void update() { 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.display.Window;
import com.gnarly.engine.model.TexRect; import com.gnarly.engine.model.TexRect;
import com.gnarly.engine.texture.Texture; import com.gnarly.engine.texture.Texture;
import org.joml.Vector2f;
import static org.lwjgl.glfw.GLFW.*; import static org.lwjgl.glfw.GLFW.*;
public class Player extends TexRect { public class Player extends TexRect {
private static Texture[] frames = null;
private double spf = 1.0 / 14.0; private double spf = 1.0 / 14.0;
private double time = 0; private double time = 0;
private int frame = 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 Window window;
private BulletList bullets;
public Player(Window window, Camera camera) { public Player(Window window, Camera camera, BulletList bullets) {
super(camera, (Texture) null, 0, 0, -0.1f, 32, 32, 0, false); super(camera, (Texture) null, camera.getWidth() / 2 - 16, camera.getHeight() / 2 - 16, -0.1f, 32, 32, 0, false);
this.window = window; this.window = window;
this.bullets = bullets;
if (frames == null) { if (frames == null) {
frames = new Texture[4]; frames = new Texture[4];
@ -33,25 +40,32 @@ public class Player extends TexRect {
} }
public void update() { 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) 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) 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) 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) 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) if (position.x + velocity.x < 0)
position.x = 0; velocity.x = -position.x;
else if (position.x + width >= camera.getWidth()) else if (position.x + width + velocity.x >= camera.getWidth())
position.x = camera.getWidth() - width; velocity.x = camera.getWidth() - width - position.x;
if (position.y < 0) if (position.y + velocity.y < 0)
position.y = 0; velocity.y = -position.y;
else if (position.y + height >= camera.getHeight()) else if (position.y + height + velocity.y >= camera.getHeight())
position.y = camera.getHeight() - height; velocity.y = camera.getHeight() - height - position.y;
time += Main.dtime; time += Main.dtime;
while (time >= spf) { while (time >= spf) {
@ -59,5 +73,44 @@ public class Player extends TexRect {
frame = (frame + 1) % frames.length; frame = (frame + 1) % frames.length;
setTexture(frames[frame]); 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.shaders.Shader2t;
import com.gnarly.engine.texture.TextureSet; import com.gnarly.engine.texture.TextureSet;
import com.gnarly.game.Main; import com.gnarly.game.Main;
import org.joml.Vector2f;
public class BasicEnemy extends Enemy { public class BasicEnemy extends Enemy {
@ -16,9 +17,19 @@ public class BasicEnemy extends Enemy {
private Shader2t shader = Shader.SHADER2T; private Shader2t shader = Shader.SHADER2T;
private float time = 0; private float time = 0;
private float passedTime = 0;
private int timeIndex = 0;
public BasicEnemy(Camera camera, float x, float y) { private float[] timing;
super(camera, x, y, DIMS, DIMS, 1); 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) { if (textures == null) {
textures = new TextureSet(new String[] { textures = new TextureSet(new String[] {
"res/img/enemies/basic/basic-0.png", "res/img/enemies/basic/basic-0.png",
@ -27,16 +38,33 @@ public class BasicEnemy extends Enemy {
"res/img/enemies/basic/basic-3.png" "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 @Override
public void update() { 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 @Override
public void render() { public void render() {
textures.get((int) (time / spf)).bind(); textures.get((int) (time / spf) % textures.length()).bind();
shader.enable(); shader.enable();
shader.setMVP(camera.getMatrix().translate(position).scale(width * scale, height * scale, 1)); shader.setMVP(camera.getMatrix().translate(position).scale(width * scale, height * scale, 1));
vao.render(); vao.render();

View file

@ -2,12 +2,14 @@ package com.gnarly.game.enemies;
import com.gnarly.engine.display.Camera; import com.gnarly.engine.display.Camera;
import com.gnarly.engine.model.Rect; import com.gnarly.engine.model.Rect;
import com.gnarly.engine.texture.TextureSet; import org.joml.Vector2f;
public abstract class Enemy extends Rect { public abstract class Enemy extends Rect {
protected int life; protected int life;
protected Vector2f velocity;
Enemy(Camera camera, float x, float y, float width, float height, 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); 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 update();
public abstract void render(); public abstract void render();
public Vector2f getVelocity() {
return velocity;
}
public void applyMovement() {
position.add(velocity.x, velocity.y, 0);
}
public void damage(int damage) { public void damage(int damage) {
life -= damage; life -= damage;
} }