cool shooting pattern, cleanup, prepare for pooling
This commit is contained in:
@@ -87,20 +87,4 @@ public class Player extends Entity {
|
||||
body.setTransform(body.getPosition(), angle * (float) Math.PI / 180f);
|
||||
sprite.setRotation(body.getAngle() * (180f / (float) Math.PI));
|
||||
}
|
||||
|
||||
public Ray shootArrow() {
|
||||
Vector2 direction = new Vector2();
|
||||
|
||||
float mouseX = Gdx.input.getX();
|
||||
float mouseY = Gdx.input.getY();
|
||||
|
||||
Vector3 mousePosition = CameraHandler.getInstance().getCamera().unproject(new Vector3(mouseX, mouseY, 0));
|
||||
|
||||
direction.set(mousePosition.x, mousePosition.y).sub(getPosition()).nor();
|
||||
|
||||
Ray ray = new Ray(world, getPosition(), direction);
|
||||
ray.body.setUserData(ray);
|
||||
return ray;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package org.lumijiez.bugger.entities.weapons;
|
||||
|
||||
import com.badlogic.gdx.math.Vector2;
|
||||
import com.badlogic.gdx.physics.box2d.*;
|
||||
import org.lumijiez.bugger.entities.Entity;
|
||||
|
||||
@@ -9,8 +10,26 @@ public abstract class Projectile extends Entity {
|
||||
|
||||
public Projectile(World world, String texturePath, float size) {
|
||||
super(world, texturePath, size);
|
||||
this.body = createBody(0, 0);
|
||||
}
|
||||
|
||||
public Projectile(World world, Vector2 position, Vector2 direction, String texturePath, float size) {
|
||||
super(world, texturePath, size);
|
||||
Vector2 offsetPosition = position.cpy().add(direction.nor().scl(size + 1f));
|
||||
this.body = createBody(offsetPosition.x, offsetPosition.y);
|
||||
this.body.setTransform(offsetPosition, (float) (direction.angleRad() + Math.toRadians(270f)));
|
||||
this.body.setLinearVelocity(direction.nor().scl(5000f));
|
||||
}
|
||||
|
||||
public Projectile(World world, Vector2 position, Vector2 direction, String texturePath, float size, float speed) {
|
||||
super(world, texturePath, size);
|
||||
Vector2 offsetPosition = position.cpy().add(direction.nor().scl(size + 1f));
|
||||
this.body = createBody(offsetPosition.x, offsetPosition.y);
|
||||
this.body.setTransform(offsetPosition, (float) (direction.angleRad() + Math.toRadians(270f)));
|
||||
this.body.setLinearVelocity(direction.nor().scl(speed));
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
protected Body createBody(float x, float y) {
|
||||
BodyDef bodyDef = new BodyDef();
|
||||
@@ -34,8 +53,8 @@ public abstract class Projectile extends Entity {
|
||||
public void update(float delta) {
|
||||
timeAlive += delta;
|
||||
float lifetime = 3f;
|
||||
if (timeAlive >= lifetime || isMarkedToDestroy()) {
|
||||
destroy();
|
||||
if (timeAlive >= lifetime) {
|
||||
markedToDestroy = true;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -7,17 +7,15 @@ import org.lumijiez.bugger.Bugger;
|
||||
public class Ray extends Projectile {
|
||||
|
||||
public Ray(World world, Vector2 position, Vector2 direction) {
|
||||
super(world, "images/blaze.png", 5f);
|
||||
super(world, position, direction, "images/blaze.png", 5f);
|
||||
|
||||
Vector2 offsetPosition = position.cpy().add(direction.nor().scl(size + 1f));
|
||||
this.body.setUserData(this);
|
||||
}
|
||||
|
||||
this.body = createBody(offsetPosition.x, offsetPosition.y);
|
||||
public Ray(World world, Vector2 position, Vector2 direction, float speed) {
|
||||
super(world, position, direction, "images/blaze.png", 5f, speed);
|
||||
|
||||
this.body.setTransform(offsetPosition, (float) (direction.angleRad() + Math.toRadians(270f)));
|
||||
|
||||
float speed = 5000f;
|
||||
|
||||
this.body.setLinearVelocity(direction.nor().scl(speed));
|
||||
this.body.setUserData(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -30,5 +28,4 @@ public class Ray extends Projectile {
|
||||
sprite.draw(Bugger.spriteBatch);
|
||||
Bugger.spriteBatch.end();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -2,9 +2,8 @@ package org.lumijiez.bugger.handlers;
|
||||
|
||||
import com.badlogic.gdx.Gdx;
|
||||
import com.badlogic.gdx.Input;
|
||||
import org.lumijiez.bugger.Bugger;
|
||||
import com.badlogic.gdx.math.Vector2;
|
||||
import org.lumijiez.bugger.entities.Player;
|
||||
import org.lumijiez.bugger.entities.weapons.Ray;
|
||||
|
||||
public class InputHandler {
|
||||
private static InputHandler instance;
|
||||
@@ -20,8 +19,44 @@ public class InputHandler {
|
||||
|
||||
public void handleInput() {
|
||||
if (Gdx.input.isButtonPressed(Input.Buttons.LEFT)) {
|
||||
Ray ray = Player.getInstance().shootArrow();
|
||||
ProjectileHandler.getInstance().getProjectiles().add(ray);
|
||||
ProjectileHandler.getInstance().shootRay();
|
||||
}
|
||||
|
||||
if (Gdx.input.isButtonJustPressed(Input.Buttons.RIGHT)) {
|
||||
float numRays = 8;
|
||||
float radius = 0.5f;
|
||||
|
||||
shootRaysWithDelay(numRays, radius);
|
||||
}
|
||||
}
|
||||
|
||||
private void shootRaysWithDelay(float duration, float radius) {
|
||||
new Thread(() -> {
|
||||
int raysPerCircle = 100;
|
||||
int totalRays = (int) ((duration / 2) * raysPerCircle);
|
||||
|
||||
for (int i = 0; i < totalRays; i++) {
|
||||
float angle = (float) (i * (2 * Math.PI) / raysPerCircle);
|
||||
|
||||
float x = radius * (float) Math.cos(angle);
|
||||
float y = radius * (float) Math.sin(angle);
|
||||
|
||||
Vector2 direction = new Vector2(x, y).add(Player.getInstance().getPosition());
|
||||
Vector2 shootDirection = direction.cpy().sub(Player.getInstance().getPosition()).nor();
|
||||
|
||||
Gdx.app.postRunnable(() -> {
|
||||
ProjectileHandler.getInstance().shootRay(Player.getInstance().getPosition(), shootDirection, 20f);
|
||||
});
|
||||
|
||||
try {
|
||||
float delay = (2f / raysPerCircle) * 500;
|
||||
Thread.sleep((long) delay);
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}).start();
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -1,7 +1,13 @@
|
||||
package org.lumijiez.bugger.handlers;
|
||||
|
||||
import com.badlogic.gdx.Gdx;
|
||||
import com.badlogic.gdx.math.Vector2;
|
||||
import com.badlogic.gdx.math.Vector3;
|
||||
import com.badlogic.gdx.utils.Array;
|
||||
import org.lumijiez.bugger.Bugger;
|
||||
import org.lumijiez.bugger.entities.Player;
|
||||
import org.lumijiez.bugger.entities.weapons.Projectile;
|
||||
import org.lumijiez.bugger.entities.weapons.Ray;
|
||||
|
||||
public class ProjectileHandler {
|
||||
private final Array<Projectile> projectiles = new Array<>();
|
||||
@@ -27,6 +33,25 @@ public class ProjectileHandler {
|
||||
}
|
||||
}
|
||||
|
||||
public void shootRay() {
|
||||
Vector2 direction = new Vector2();
|
||||
|
||||
float mouseX = Gdx.input.getX();
|
||||
float mouseY = Gdx.input.getY();
|
||||
|
||||
Vector3 mousePosition = CameraHandler.getInstance().getCamera().unproject(new Vector3(mouseX, mouseY, 0));
|
||||
|
||||
Vector2 playerPos = Player.getInstance().getPosition();
|
||||
direction.set(mousePosition.x, mousePosition.y).sub(playerPos).nor();
|
||||
|
||||
projectiles.add(new Ray(Bugger.getInstance().getWorld(), playerPos, direction));
|
||||
}
|
||||
|
||||
public void shootRay(Vector2 position, Vector2 direction, float speed) {
|
||||
projectiles.add(new Ray(Bugger.getInstance().getWorld(), position, direction, speed));
|
||||
}
|
||||
|
||||
|
||||
public Array<Projectile> getProjectiles() {
|
||||
return projectiles;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user