Final project

This commit is contained in:
Gnarwhal 2024-08-07 05:04:19 +00:00
parent 0e85a338c5
commit 8a6d28bf20
Signed by: Gnarwhal
GPG key ID: 0989A73D8C421174
14 changed files with 463 additions and 143 deletions

BIN
DJam 2.jar Normal file

Binary file not shown.

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

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.4 KiB

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

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.7 KiB

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

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.6 KiB

View file

@ -0,0 +1,210 @@
16
4
10 10 0
4 1 4 1
732 0
0 32
-732 0
0 -32
30 1
1 5
17 0.12903225806
1 0.38709677418
11 0.12903225806
4
10 10 0.624
4 1 4 1
732 0
0 32
-732 0
0 -32
30 1
1 5
14 0.12903225806
1 0.38709677418
14 0.12903225806
4
10 10 1.25
4 1 4 1
732 0
0 32
-732 0
0 -32
30 1
1 5
17 0.12903225806
1 0.38709677418
11 0.12903225806
4
10 10 1.875
4 1 4 1
732 0
0 32
-732 0
0 -32
30 1
1 5
17 0.12903225806
1 0.38709677418
11 0.12903225806
4
10 10 2.5
4 1 4 1
732 0
0 32
-732 0
0 -32
30 1
1 5
21 0.12903225806
1 0.38709677418
7 0.12903225806
4
10 10 3.125
4 1 4 1
732 0
0 32
-732 0
0 -32
30 1
1 5
16 0.12903225806
1 0.38709677418
12 0.12903225806
4
10 10 3.75
4 1 4 1
732 0
0 32
-732 0
0 -32
30 1
1 5
15 0.12903225806
1 0.38709677418
13 0.12903225806
4
10 10 4.375
4 1 4 1
732 0
0 32
-732 0
0 -32
30 1
1 5
12 0.12903225806
1 0.38709677418
16 0.12903225806
4
10 10 5.0
4 1 4 1
732 0
0 32
-732 0
0 -32
30 1
1 5
7 0.12903225806
1 0.38709677418
21 0.12903225806
4
10 10 5.625
4 1 4 1
732 0
0 32
-732 0
0 -32
30 1
1 5
9 0.12903225806
1 0.38709677418
19 0.12903225806
4
10 10 6.25
4 1 4 1
732 0
0 32
-732 0
0 -32
30 1
1 5
14 0.12903225806
1 0.38709677418
14 0.12903225806
4
10 10 6.875
4 1 4 1
732 0
0 32
-732 0
0 -32
30 1
1 5
17 0.12903225806
1 0.38709677418
11 0.12903225806
4
10 10 7.5
4 1 4 1
732 0
0 32
-732 0
0 -32
30 1
1 5
15 0.12903225806
1 0.38709677418
13 0.12903225806
4
10 10 8.125
4 1 4 1
732 0
0 32
-732 0
0 -32
30 1
1 5
12 0.12903225806
1 0.38709677418
16 0.12903225806
4
10 10 8.75
4 1 4 1
732 0
0 32
-732 0
0 -32
30 1
1 5
13 0.12903225806
1 0.38709677418
15 0.12903225806
4
10 10 9.375
4 1 4 1
732 0
0 32
-732 0
0 -32
30 1
1 5
17 0.12903225806
1 0.38709677418
11 0.12903225806

3
src/META-INF/MANIFEST.MF Normal file
View file

@ -0,0 +1,3 @@
Manifest-Version: 1.0
Main-Class: com.gnarly.game.Main

View file

@ -100,6 +100,8 @@ public class BulletList {
enemies.remove(j);
--j;
}
bullets.remove(i);
--i;
}
}
bullet.position.add(deltaV);
@ -120,8 +122,11 @@ public class BulletList {
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))
if (Collision.collide(bounds, segments, player.getVelocity(), bullet)) {
player.damage(bullet.damage);
bullets.remove(i);
--i;
}
bullet.position.add(deltaV);
bullet.boundingPosition.add(deltaV);

View file

@ -42,7 +42,6 @@ public class Collision {
Vector2f scaledBV = bullet.velocity.mul((float) Main.dtime, new Vector2f());
Bounds bounds = getBounds(position, dimensions, boundVelocity);
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,
@ -97,9 +96,6 @@ public class Collision {
public static boolean segmentCollision(Vector2f P1, Vector2f D1, Vector2f V1, Vector2f P2, Vector2f D2, Vector2f V2) {
final float EPSILON = 0.000001f;
if ((V1.length() == 0 && V2.length() > 0)
|| (V2.length() == 0 && V1.length() > 0)
|| 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)) {
@ -144,7 +140,6 @@ public class Collision {
return true;
}
}
}
return false;
}

View file

@ -2,16 +2,23 @@ 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.engine.model.TexRect;
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 {
public class GamePanel {
private static final int
STATE_PRE = 0x00,
STATE_PLAYING = 0x01,
STATE_DEAD = 0x02,
STATE_WIN = 0x03;
private static final float
DELAY = 0.75f;
private Window window;
private Camera camera;
@ -24,50 +31,45 @@ public class GamePanel extends Panel {
private ArrayList<Enemy> enemies;
public static ColRect show;
private Level level;
private int state = STATE_PRE;
private TexRect begin;
private TexRect dead;
private TexRect win;
private boolean restart = false;
private float time;
public GamePanel(Window window, Camera camera) {
this.window = window;
this.camera = camera;
enemies = new ArrayList<Enemy>();
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),
}
));
playerBullets = new BulletList(camera);
enemyBullets = new BulletList(camera);
enemyBullets.spawnBullet(new Vector2f(camera.getWidth() / 2, 25), (float) Math.PI, BulletList.TYPE_SMOL, 400, 1);
level = new Level(camera, "res/levels/level1.txt", enemies, enemyBullets);
background = new Background(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);
begin = new TexRect(camera, "res/img/ui/begin.png", 0, 0, 0, camera.getWidth(), camera.getHeight(), 0, false);
dead = new TexRect(camera, "res/img/ui/dead.png", 0, 0, 0, camera.getWidth(), camera.getHeight(), 0, false);
win = new TexRect(camera, "res/img/ui/win.png", 0, 0, 0, camera.getWidth(), camera.getHeight(), 0, false);
}
public void update() {
background.update();
if (state == STATE_PRE) {
player.updateAnim();
if (window.keyPressed(GLFW_KEY_SPACE) == Window.BUTTON_PRESSED)
state = STATE_PLAYING;
}
else if (state == STATE_PLAYING) {
player.update();
for (int i = 0; i < enemies.size(); ++i)
enemies.get(i).update();
@ -75,28 +77,53 @@ public class GamePanel extends Panel {
playerBullets.update(enemies);
enemyBullets.update(player);
if (player.getHealth() <= 0) {
state = STATE_DEAD;
}
else if (enemies.size() == 0) {
state = STATE_WIN;
}
player.applyMovement();
for (int i = 0; i < enemies.size(); ++i)
enemies.get(i).applyMovement();
}
else if (state == STATE_DEAD) {
time += Main.dtime;
if (window.keyPressed(GLFW_KEY_SPACE) == Window.BUTTON_PRESSED && time > DELAY)
restart = true;
}
else if (state == STATE_WIN) {
player.updateAnim();
time += Main.dtime;
if (window.keyPressed(GLFW_KEY_SPACE) == Window.BUTTON_PRESSED && time > DELAY)
restart = true;
}
}
public void render() {
background.render();
if (state == STATE_PRE) {
player.render();
begin.render();
}
else if (state == STATE_PLAYING) {
enemyBullets.render();
playerBullets.render();
for(int i = 0; i < enemies.size(); ++i)
for (int i = 0; i < enemies.size(); ++i)
enemies.get(i).render();
player.render();
show.render();
}
else if (state == STATE_DEAD) {
dead.render();
}
else if (state == STATE_WIN) {
player.render();
win.render();
}
}
public void reset() {
}
public int checkState() {
int state = this.state;
this.state = Main.GAME_PANEL;
return state;
public boolean restart() {
return restart;
}
}

View file

@ -0,0 +1,58 @@
package com.gnarly.game;
import com.gnarly.engine.display.Camera;
import com.gnarly.game.enemies.BasicEnemy;
import com.gnarly.game.enemies.Enemy;
import org.joml.Vector2f;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Scanner;
public class Level {
private String path;
private Camera camera;
private ArrayList<Enemy> enemies;
public Level(Camera camera, String path, ArrayList<Enemy> enemies, BulletList bullets) {
try {
FileInputStream stream = new FileInputStream(path);
Scanner scanner = new Scanner(stream);
int numShips = scanner.nextInt();
for (int i = 0; i < numShips; ++i) {
int numPath = scanner.nextInt();
float x = scanner.nextFloat();
float y = scanner.nextFloat();
float timeOffset = scanner.nextFloat();
float[] times = new float[numPath];
for (int j = 0; j < numPath; ++j)
times[j] = scanner.nextFloat();
Vector2f[] shipPath = new Vector2f[numPath];
for (int j = 0; j < numPath; ++j)
shipPath[j] = new Vector2f(scanner.nextFloat(), scanner.nextFloat());
int fireCount = scanner.nextInt();
float padEnd = scanner.nextFloat();
float[] fireTimes = new float[fireCount + 1];
for (int writeIndex = 0; writeIndex < fireCount;) {
int reps = scanner.nextInt();
float delta = scanner.nextFloat();
for (int j = writeIndex; j < writeIndex + reps; ++j)
fireTimes[j] = delta;
writeIndex += reps;
}
fireTimes[fireCount] = padEnd;
enemies.add(new BasicEnemy(camera, x, y, timeOffset, times, shipPath, fireTimes, bullets));
}
scanner.close();
stream.close();
this.camera = camera;
this.path = path;
this.enemies = enemies;
} catch (IOException e) {
e.printStackTrace();
}
}
}

View file

@ -14,17 +14,12 @@ public class Main {
public static int fps;
public static double dtime;
public static final int
NUM_PANELS = 1,
GAME_PANEL = 0;
private ALManagement al;
private Window window;
private Camera camera;
private Panel[] panels;
private int panel;
private GamePanel panel;
private Sound sound;
@ -59,15 +54,28 @@ public class Main {
}
private void init() {
int position = 15;
for (int i = 0; i < 16; ++i) {
int movement = (int) (Math.random() * 11) - 5;
if (position + movement < 5 || position + movement > 27)
position -= movement;
else
position += movement;
/*System.out.println(" --- ");
System.out.println("30 1");
System.out.println("1 5");
System.out.println(position + " 0.12903225806");
System.out.println("1 0.38709677418 ");
System.out.println((28 - position) + " 0.12903225806");*/
}
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;
panel = new GamePanel(window, camera);
sound = new Sound("res/audio/theme.wav");
sound.play(true);
@ -75,22 +83,15 @@ public class Main {
private void update() {
window.update();
int state = panels[panel].checkState();
if (state != panel) {
switch (state) {
case GAME_PANEL:
GamePanel game = (GamePanel) panels[GAME_PANEL];
game.reset();
}
panel = state;
}
panels[panel].update();
panel.update();
if (panel.restart())
panel = new GamePanel(window, camera);
camera.update();
}
private void render() {
window.clear();
panels[panel].render();
panel.render();
window.swap();
}

View file

@ -1,10 +0,0 @@
package com.gnarly.game;
public abstract class Panel {
protected int state;
public abstract void update();
public abstract void render();
public abstract int checkState();
}

View file

@ -15,7 +15,7 @@ public class Player extends TexRect {
private double spf = 1.0 / 14.0;
private double time = 0;
private int frame = 0;
private float speed = 150;
private float speed = 250;
private int health = 5;
private Vector2f velocity = new Vector2f();
@ -24,7 +24,7 @@ public class Player extends TexRect {
private BulletList bullets;
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);
super(camera, (Texture) null, camera.getWidth() / 2 - 16, camera.getHeight() * 3 / 4 - 16, -0.1f, 32, 32, 0, false);
this.window = window;
this.bullets = bullets;
@ -42,7 +42,7 @@ public class Player extends TexRect {
public void update() {
float speedMultiplier = 1;
if (window.keyPressed(GLFW_KEY_LEFT_SHIFT) >= Window.BUTTON_PRESSED)
speedMultiplier *= 1.5;
speedMultiplier *= 2;
else if (window.keyPressed(GLFW_KEY_LEFT_CONTROL) >= Window.BUTTON_PRESSED)
speedMultiplier *= 0.5;
@ -67,15 +67,19 @@ public class Player extends TexRect {
else if (position.y + height + velocity.y >= camera.getHeight())
velocity.y = camera.getHeight() - height - position.y;
updateAnim();
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 void updateAnim() {
time += Main.dtime;
while (time >= spf) {
time -= spf;
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() {

View file

@ -4,6 +4,7 @@ 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.BulletList;
import com.gnarly.game.Main;
import org.joml.Vector2f;
@ -13,6 +14,9 @@ public class BasicEnemy extends Enemy {
public static final float DIMS = 16;
private static final Vector2f FIRE_OFFSET = new Vector2f(DIMS / 2, DIMS);
private static TextureSet textures = null;
private Shader2t shader = Shader.SHADER2T;
@ -25,7 +29,13 @@ public class BasicEnemy extends Enemy {
private Vector2f start;
private Vector2f origin;
public BasicEnemy(Camera camera, float x, float y, float timeOffset, float[] times, Vector2f[] path) {
private float pastFire;
private int fireIndex;
private float[] fireTimes;
private BulletList bullets;
public BasicEnemy(Camera camera, float x, float y, float timeOffset, float[] times, Vector2f[] path, float[] fireTimes, BulletList bullets) {
super(camera, 0, 0, DIMS, DIMS, 1);
this.timing = times;
this.path = path;
@ -44,13 +54,20 @@ public class BasicEnemy extends Enemy {
passedTime += times[timeIndex];
start.add(path[timeIndex]);
}
timeOffset = time;
for (; (timeOffset -= fireTimes[fireIndex]) >= 0; fireIndex = (fireIndex + 1) % fireTimes.length)
pastFire += fireTimes[fireIndex];
origin = new Vector2f(x, y);
this.fireTimes = fireTimes;
this.bullets = bullets;
}
@Override
public void update() {
position.sub(origin.x, origin.y, 0);
time += Main.dtime;
position.sub(origin.x, origin.y, 0);
float interp;
while ((interp = (time - passedTime) / timing[timeIndex]) > 1) {
passedTime += timing[timeIndex];
@ -59,7 +76,17 @@ public class BasicEnemy extends Enemy {
}
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);
if (time - pastFire > fireTimes[fireIndex]) {
if (fireIndex != fireTimes.length - 1)
fire();
pastFire += fireTimes[fireIndex];
fireIndex = (fireIndex + 1) % fireTimes.length;
}
}
public void fire() {
bullets.spawnBullet(new Vector2f(position.x, position.y).add(FIRE_OFFSET), (float) Math.PI, BulletList.TYPE_SMOL, 200, 1);
}
@Override