Improved efficiency with multiple particles with constant velocity
This commit is contained in:
parent
bc43eade55
commit
3a25f34520
|
|
@ -23,6 +23,7 @@ public class EntityBullet extends EntityParticle
|
|||
|
||||
private double height = 0.2;
|
||||
private double height_angle = 0;
|
||||
private Vec3d velocity;
|
||||
|
||||
public EntityBullet(Vec2d pos, Entity parent, double angle, double damage, int breakchance, int despawn_time) {
|
||||
super(pos, 0.2, 0.4);
|
||||
|
|
@ -34,6 +35,10 @@ public class EntityBullet extends EntityParticle
|
|||
this.breakchance = breakchance;
|
||||
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
|
||||
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) {
|
||||
this.height_angle = angle;
|
||||
this.height = height;
|
||||
this.velocity = MathHelpers.moveTowards3(0.2, new Vec2d(
|
||||
Math.toRadians(this.angle), Math.toRadians(this.height_angle)));
|
||||
return this;
|
||||
}
|
||||
|
||||
|
|
@ -49,9 +56,7 @@ public class EntityBullet extends EntityParticle
|
|||
super.tick(chunk, layer);
|
||||
|
||||
// Move forward in the bullets angle, very quickly
|
||||
Vec3d pos3 = MathHelpers.moveTowards3(0.2, new Vec2d(Math.toRadians(this.angle),
|
||||
Math.toRadians(this.height_angle))).add(
|
||||
new Vec3d(pos.x, pos.y, height));
|
||||
Vec3d pos3 = velocity.add(new Vec3d(pos.x, pos.y, height));
|
||||
height = pos3.z;
|
||||
|
||||
if(moveIsLegal(new Vec2d(pos3.x, pos.y))) {
|
||||
|
|
|
|||
|
|
@ -2,6 +2,7 @@ package shootergame.entity.particle;
|
|||
|
||||
import java.util.Random;
|
||||
|
||||
import shootergame.Main;
|
||||
import shootergame.display.Camera;
|
||||
import shootergame.entity.EntityParticle;
|
||||
import shootergame.util.gl.GlHelpers;
|
||||
|
|
@ -9,27 +10,27 @@ import shootergame.util.math.MathHelpers;
|
|||
import shootergame.util.math.random.RandomHelpers;
|
||||
import shootergame.util.math.vec.Vec2d;
|
||||
import shootergame.util.math.vec.Vec2i;
|
||||
import shootergame.util.math.vec.Vec3d;
|
||||
import shootergame.world.chunk.Chunk;
|
||||
import shootergame.world.layer.Layer;
|
||||
|
||||
public class ParticleBlood extends EntityParticle
|
||||
{
|
||||
private double r_color;
|
||||
private double speed = 0.1;
|
||||
private double time = 1000;
|
||||
private double height = 0;
|
||||
private double height_velocity = 0;
|
||||
private Vec3d velocity;
|
||||
private double angle_height;
|
||||
|
||||
public ParticleBlood(Random rand, Vec2d pos, double angle) {
|
||||
super(pos, rand.nextDouble() / 5, 0);
|
||||
|
||||
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_velocity = rand.nextDouble() / 10;
|
||||
this.pos.x += 0.5;
|
||||
this.pos.y += 0.5;
|
||||
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
|
||||
|
|
@ -38,14 +39,18 @@ public class ParticleBlood extends EntityParticle
|
|||
// Call super
|
||||
super.tick(chunk, layer);
|
||||
|
||||
Main.player.setHealth(100);
|
||||
|
||||
// Move in the velocity and remove some of it
|
||||
moveBackward(speed);
|
||||
height += height_velocity;
|
||||
speed /= 1.05;
|
||||
height_velocity -= 0.001;
|
||||
pos.x += velocity.x;
|
||||
pos.y += velocity.y;
|
||||
height += velocity.z;
|
||||
velocity.x /= 1.05;
|
||||
velocity.y /= 1.05;
|
||||
velocity.z -= 0.001;
|
||||
if(height < 0) {
|
||||
height = 0;
|
||||
height_velocity = 0;
|
||||
velocity.z = 0;
|
||||
}
|
||||
|
||||
// Remove some time
|
||||
|
|
|
|||
|
|
@ -11,13 +11,14 @@ import shootergame.util.math.TileState;
|
|||
import shootergame.util.math.random.RandomHelpers;
|
||||
import shootergame.util.math.vec.Vec2d;
|
||||
import shootergame.util.math.vec.Vec2i;
|
||||
import shootergame.util.math.vec.Vec3d;
|
||||
import shootergame.world.chunk.Chunk;
|
||||
import shootergame.world.layer.Layer;
|
||||
|
||||
public class ParticleBreak extends EntityVertical
|
||||
{
|
||||
private double height = 0;
|
||||
private double height_speed;
|
||||
private Vec3d velocity;
|
||||
private int time = 1000;
|
||||
|
||||
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));
|
||||
this.opaqueTile = ts.tile.opaqueTile;
|
||||
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
|
||||
|
|
@ -53,15 +58,16 @@ public class ParticleBreak extends EntityVertical
|
|||
|
||||
time -= 1;
|
||||
|
||||
height += height_speed;
|
||||
height_speed -= 0.001;
|
||||
height += velocity.z;
|
||||
velocity.z -= 0.001;
|
||||
|
||||
if(height <= 0) {
|
||||
height_speed = 0;
|
||||
velocity.z = 0;
|
||||
}
|
||||
|
||||
else {
|
||||
moveForward(0.01);
|
||||
pos.x += velocity.x;
|
||||
pos.y += velocity.y;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -47,7 +47,7 @@ public class ParticleSmoke extends EntityVertical
|
|||
public void tick(Chunk chunk, Layer 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();
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -1,5 +1,6 @@
|
|||
package shootergame.input;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.nio.ByteBuffer;
|
||||
import java.nio.FloatBuffer;
|
||||
import java.util.ArrayList;
|
||||
|
|
@ -32,20 +33,9 @@ public class JoystickCallback implements GLFWJoystickCallbackI, IMainloopTask
|
|||
// Gamepad disconnect
|
||||
else if(event == GLFW.GLFW_DISCONNECTED)
|
||||
{
|
||||
// Log the event
|
||||
// Log the event and remove the connection
|
||||
System.out.println("Gamepad "+jid+" disconnected");
|
||||
|
||||
// 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;
|
||||
}
|
||||
}
|
||||
connections.remove((Object) jid);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -106,44 +96,49 @@ public class JoystickCallback implements GLFWJoystickCallbackI, IMainloopTask
|
|||
boolean shoulder_left = false;
|
||||
boolean shoulder_right = false;
|
||||
|
||||
// Loop over all the connected gamepads
|
||||
for(int jid : connections)
|
||||
{
|
||||
// Get all the axes
|
||||
FloatBuffer axes = GLFW.glfwGetJoystickAxes(jid);
|
||||
int jid2 = 0;
|
||||
|
||||
// Check if the axes are undefined
|
||||
if(axes == null) {
|
||||
continue;
|
||||
try {
|
||||
// Loop over all the connected gamepads
|
||||
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
|
||||
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;
|
||||
catch(NullPointerException e) {
|
||||
connections.remove((Object) jid2);
|
||||
System.err.println("Removed controller "+jid2+" due to NullPointerException");
|
||||
}
|
||||
|
||||
Input input = Main.menu.input;
|
||||
|
|
|
|||
|
|
@ -8,8 +8,8 @@ public class MainloopEventHandler implements IMainloopEvent, IMainloopTask
|
|||
{
|
||||
public static final MainloopEventHandler MAINLOOP_EVENT_HANDLER = new MainloopEventHandler();
|
||||
|
||||
public long mspf = 1000/60;
|
||||
private long max_mspf = 1;
|
||||
private long max_mspf = 1000/60;
|
||||
public long mspf = max_mspf;
|
||||
|
||||
@Override
|
||||
public void onClose()
|
||||
|
|
@ -25,7 +25,7 @@ public class MainloopEventHandler implements IMainloopEvent, IMainloopTask
|
|||
@Override
|
||||
public void onEarly() {
|
||||
mspf -= 1;
|
||||
if(mspf < max_mspf) mspf = 1;
|
||||
if(mspf < max_mspf) mspf = max_mspf;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
|||
Loading…
Reference in New Issue