Improved efficiency with multiple particles with constant velocity

This commit is contained in:
josua 2019-09-25 13:49:52 +10:00
parent bc43eade55
commit 3a25f34520
6 changed files with 85 additions and 74 deletions

View File

@ -23,6 +23,7 @@ public class EntityBullet extends EntityParticle
private double height = 0.2; private double height = 0.2;
private double height_angle = 0; private double height_angle = 0;
private Vec3d velocity;
public EntityBullet(Vec2d pos, Entity parent, double angle, double damage, int breakchance, int despawn_time) { public EntityBullet(Vec2d pos, Entity parent, double angle, double damage, int breakchance, int despawn_time) {
super(pos, 0.2, 0.4); super(pos, 0.2, 0.4);
@ -34,6 +35,10 @@ public class EntityBullet extends EntityParticle
this.breakchance = breakchance; this.breakchance = breakchance;
this.time = despawn_time; this.time = despawn_time;
// Calculate the velocity vector
this.velocity = MathHelpers.moveTowards3(0.2, new Vec2d(
Math.toRadians(this.angle), Math.toRadians(this.height_angle)));
// Play the gun sound // Play the gun sound
Sounds.GUN.play(new Vec3d(pos.x, pos.y, 0.4), 2); Sounds.GUN.play(new Vec3d(pos.x, pos.y, 0.4), 2);
} }
@ -41,6 +46,8 @@ public class EntityBullet extends EntityParticle
public EntityBullet withHeight(double angle, double height) { public EntityBullet withHeight(double angle, double height) {
this.height_angle = angle; this.height_angle = angle;
this.height = height; this.height = height;
this.velocity = MathHelpers.moveTowards3(0.2, new Vec2d(
Math.toRadians(this.angle), Math.toRadians(this.height_angle)));
return this; return this;
} }
@ -49,9 +56,7 @@ public class EntityBullet extends EntityParticle
super.tick(chunk, layer); super.tick(chunk, layer);
// Move forward in the bullets angle, very quickly // Move forward in the bullets angle, very quickly
Vec3d pos3 = MathHelpers.moveTowards3(0.2, new Vec2d(Math.toRadians(this.angle), Vec3d pos3 = velocity.add(new Vec3d(pos.x, pos.y, height));
Math.toRadians(this.height_angle))).add(
new Vec3d(pos.x, pos.y, height));
height = pos3.z; height = pos3.z;
if(moveIsLegal(new Vec2d(pos3.x, pos.y))) { if(moveIsLegal(new Vec2d(pos3.x, pos.y))) {

View File

@ -2,6 +2,7 @@ package shootergame.entity.particle;
import java.util.Random; import java.util.Random;
import shootergame.Main;
import shootergame.display.Camera; import shootergame.display.Camera;
import shootergame.entity.EntityParticle; import shootergame.entity.EntityParticle;
import shootergame.util.gl.GlHelpers; import shootergame.util.gl.GlHelpers;
@ -9,27 +10,27 @@ import shootergame.util.math.MathHelpers;
import shootergame.util.math.random.RandomHelpers; import shootergame.util.math.random.RandomHelpers;
import shootergame.util.math.vec.Vec2d; import shootergame.util.math.vec.Vec2d;
import shootergame.util.math.vec.Vec2i; import shootergame.util.math.vec.Vec2i;
import shootergame.util.math.vec.Vec3d;
import shootergame.world.chunk.Chunk; import shootergame.world.chunk.Chunk;
import shootergame.world.layer.Layer; import shootergame.world.layer.Layer;
public class ParticleBlood extends EntityParticle public class ParticleBlood extends EntityParticle
{ {
private double r_color; private double r_color;
private double speed = 0.1;
private double time = 1000; private double time = 1000;
private double height = 0; private double height = 0;
private double height_velocity = 0; private Vec3d velocity;
private double angle_height;
public ParticleBlood(Random rand, Vec2d pos, double angle) { public ParticleBlood(Random rand, Vec2d pos, double angle) {
super(pos, rand.nextDouble() / 5, 0); super(pos, rand.nextDouble() / 5, 0);
this.angle = angle + RandomHelpers.randrange(rand, -100, 100); this.angle = angle + RandomHelpers.randrange(rand, -100, 100);
this.speed = rand.nextDouble() / 10; this.angle_height = RandomHelpers.randrange(rand, 9000, 18000) / 100;
this.height = rand.nextDouble(); this.height = rand.nextDouble();
this.height_velocity = rand.nextDouble() / 10;
this.pos.x += 0.5;
this.pos.y += 0.5;
r_color = RandomHelpers.randrange(rand, 200, 800) / 1000.0; r_color = RandomHelpers.randrange(rand, 200, 800) / 1000.0;
velocity = MathHelpers.moveTowards3(0.1,
new Vec2d(Math.toRadians(this.angle), Math.toRadians(angle_height)));
} }
@Override @Override
@ -38,14 +39,18 @@ public class ParticleBlood extends EntityParticle
// Call super // Call super
super.tick(chunk, layer); super.tick(chunk, layer);
Main.player.setHealth(100);
// Move in the velocity and remove some of it // Move in the velocity and remove some of it
moveBackward(speed); pos.x += velocity.x;
height += height_velocity; pos.y += velocity.y;
speed /= 1.05; height += velocity.z;
height_velocity -= 0.001; velocity.x /= 1.05;
velocity.y /= 1.05;
velocity.z -= 0.001;
if(height < 0) { if(height < 0) {
height = 0; height = 0;
height_velocity = 0; velocity.z = 0;
} }
// Remove some time // Remove some time

View File

@ -11,13 +11,14 @@ import shootergame.util.math.TileState;
import shootergame.util.math.random.RandomHelpers; import shootergame.util.math.random.RandomHelpers;
import shootergame.util.math.vec.Vec2d; import shootergame.util.math.vec.Vec2d;
import shootergame.util.math.vec.Vec2i; import shootergame.util.math.vec.Vec2i;
import shootergame.util.math.vec.Vec3d;
import shootergame.world.chunk.Chunk; import shootergame.world.chunk.Chunk;
import shootergame.world.layer.Layer; import shootergame.world.layer.Layer;
public class ParticleBreak extends EntityVertical public class ParticleBreak extends EntityVertical
{ {
private double height = 0; private double height = 0;
private double height_speed; private Vec3d velocity;
private int time = 1000; private int time = 1000;
private static TextureReference getTexture(TileState ts) private static TextureReference getTexture(TileState ts)
@ -39,7 +40,11 @@ public class ParticleBreak extends EntityVertical
super(pos, getTexture(ts), new Vec2d(1/4.0, 1/4.0)); super(pos, getTexture(ts), new Vec2d(1/4.0, 1/4.0));
this.opaqueTile = ts.tile.opaqueTile; this.opaqueTile = ts.tile.opaqueTile;
this.angle = RandomHelpers.randrange(rand, 360); this.angle = RandomHelpers.randrange(rand, 360);
height_speed = RandomHelpers.randrange(rand, 10000) / 200000.0;
Vec2d side_v = MathHelpers.moveTowards2(0.01, Math.toRadians(angle));
velocity = new Vec3d(
side_v.x, side_v.y,
RandomHelpers.randrange(rand, 10000) / 200000.0);
} }
@Override @Override
@ -53,15 +58,16 @@ public class ParticleBreak extends EntityVertical
time -= 1; time -= 1;
height += height_speed; height += velocity.z;
height_speed -= 0.001; velocity.z -= 0.001;
if(height <= 0) { if(height <= 0) {
height_speed = 0; velocity.z = 0;
} }
else { else {
moveForward(0.01); pos.x += velocity.x;
pos.y += velocity.y;
} }
} }

View File

@ -47,7 +47,7 @@ public class ParticleSmoke extends EntityVertical
public void tick(Chunk chunk, Layer layer) { public void tick(Chunk chunk, Layer layer) {
super.tick(chunk, layer); super.tick(chunk, layer);
if(pos.squareDistance(Main.player.pos) > 32 || opacity <= 0) { if(pos.squareDistance(Main.player.pos) > 32 || opacity <= 0 || height > 16) {
kill(); kill();
} }

View File

@ -1,5 +1,6 @@
package shootergame.input; package shootergame.input;
import java.io.IOException;
import java.nio.ByteBuffer; import java.nio.ByteBuffer;
import java.nio.FloatBuffer; import java.nio.FloatBuffer;
import java.util.ArrayList; import java.util.ArrayList;
@ -32,20 +33,9 @@ public class JoystickCallback implements GLFWJoystickCallbackI, IMainloopTask
// Gamepad disconnect // Gamepad disconnect
else if(event == GLFW.GLFW_DISCONNECTED) else if(event == GLFW.GLFW_DISCONNECTED)
{ {
// Log the event // Log the event and remove the connection
System.out.println("Gamepad "+jid+" disconnected"); System.out.println("Gamepad "+jid+" disconnected");
connections.remove((Object) jid);
// Loop over the connections
for(int i=0;i<connections.size();i++)
{
// Is this connection the connection that disconnected
if(connections.get(i).intValue() == jid)
{
// Remove the connection and reduce i
connections.remove(i);
i -= 1;
}
}
} }
} }
@ -106,44 +96,49 @@ public class JoystickCallback implements GLFWJoystickCallbackI, IMainloopTask
boolean shoulder_left = false; boolean shoulder_left = false;
boolean shoulder_right = false; boolean shoulder_right = false;
// Loop over all the connected gamepads int jid2 = 0;
for(int jid : connections)
{
// Get all the axes
FloatBuffer axes = GLFW.glfwGetJoystickAxes(jid);
// Check if the axes are undefined try {
if(axes == null) { // Loop over all the connected gamepads
continue; for(int jid : connections)
{
// Get all the axes
jid2 = jid;
FloatBuffer axes = GLFW.glfwGetJoystickAxes(jid);
// Store all the axes data
left_x = combineJoystickAxis(left_x, axes.get(0));
left_y = combineJoystickAxis(left_y, axes.get(1));
right_x = combineJoystickAxis(right_x, axes.get(3));
right_y = combineJoystickAxis(right_y, axes.get(4));
left_trigger = combineJoystickAxis(left_trigger, axes.get(2));
right_trigger = combineJoystickAxis(right_trigger, axes.get(5));
// Get all the buttons
ByteBuffer buttons = GLFW.glfwGetJoystickButtons(jid);
// Store all the button data
button_a = buttons.get(0) == 1 || button_a;
button_b = buttons.get(1) == 1 || button_b;
button_x = buttons.get(2) == 1 || button_x;
button_y = buttons.get(3) == 1 || button_y;
shoulder_left = buttons.get(4) == 1 || shoulder_left;
shoulder_right = buttons.get(5) == 1 || shoulder_right;
button_back = buttons.get(6) == 1 || button_back;
button_start = buttons.get(7) == 1 || button_start;
button_home = buttons.get(8) == 1 || button_home;
left_stick_button = buttons.get(9) == 1 || left_stick_button;
right_stick_button = buttons.get(10) == 1 || right_stick_button;
dpad_left = buttons.get(11) == 1 || buttons.get(18) == 1 || dpad_left;
dpad_right = buttons.get(12) == 1 || buttons.get(16) == 1 || dpad_right;
dpad_up = buttons.get(13) == 1 || buttons.get(15) == 1 || dpad_up;
dpad_down = buttons.get(14) == 1 || buttons.get(17) == 1 || dpad_down;
} }
}
// Store all the axes data catch(NullPointerException e) {
left_x = combineJoystickAxis(left_x, axes.get(0)); connections.remove((Object) jid2);
left_y = combineJoystickAxis(left_y, axes.get(1)); System.err.println("Removed controller "+jid2+" due to NullPointerException");
right_x = combineJoystickAxis(right_x, axes.get(3));
right_y = combineJoystickAxis(right_y, axes.get(4));
left_trigger = combineJoystickAxis(left_trigger, axes.get(2));
right_trigger = combineJoystickAxis(right_trigger, axes.get(5));
// Get all the buttons
ByteBuffer buttons = GLFW.glfwGetJoystickButtons(jid);
// Store all the button data
button_a = buttons.get(0) == 1 || button_a;
button_b = buttons.get(1) == 1 || button_b;
button_x = buttons.get(2) == 1 || button_x;
button_y = buttons.get(3) == 1 || button_y;
shoulder_left = buttons.get(4) == 1 || shoulder_left;
shoulder_right = buttons.get(5) == 1 || shoulder_right;
button_back = buttons.get(6) == 1 || button_back;
button_start = buttons.get(7) == 1 || button_start;
button_home = buttons.get(8) == 1 || button_home;
left_stick_button = buttons.get(9) == 1 || left_stick_button;
right_stick_button = buttons.get(10) == 1 || right_stick_button;
dpad_left = buttons.get(11) == 1 || buttons.get(18) == 1 || dpad_left;
dpad_right = buttons.get(12) == 1 || buttons.get(16) == 1 || dpad_right;
dpad_up = buttons.get(13) == 1 || buttons.get(15) == 1 || dpad_up;
dpad_down = buttons.get(14) == 1 || buttons.get(17) == 1 || dpad_down;
} }
Input input = Main.menu.input; Input input = Main.menu.input;

View File

@ -8,8 +8,8 @@ public class MainloopEventHandler implements IMainloopEvent, IMainloopTask
{ {
public static final MainloopEventHandler MAINLOOP_EVENT_HANDLER = new MainloopEventHandler(); public static final MainloopEventHandler MAINLOOP_EVENT_HANDLER = new MainloopEventHandler();
public long mspf = 1000/60; private long max_mspf = 1000/60;
private long max_mspf = 1; public long mspf = max_mspf;
@Override @Override
public void onClose() public void onClose()
@ -25,7 +25,7 @@ public class MainloopEventHandler implements IMainloopEvent, IMainloopTask
@Override @Override
public void onEarly() { public void onEarly() {
mspf -= 1; mspf -= 1;
if(mspf < max_mspf) mspf = 1; if(mspf < max_mspf) mspf = max_mspf;
} }
@Override @Override