Added shotgun!

This commit is contained in:
Andrew Lalis 2022-07-26 16:40:40 +02:00
parent 0f31f32607
commit 2fb608de08
7 changed files with 61 additions and 24 deletions

View File

@ -31,6 +31,7 @@ public class InputHandler {
if (glfwGetKey(window, GLFW_KEY_1) == GLFW_PRESS) selectedInventoryIndex = 0; if (glfwGetKey(window, GLFW_KEY_1) == GLFW_PRESS) selectedInventoryIndex = 0;
if (glfwGetKey(window, GLFW_KEY_2) == GLFW_PRESS) selectedInventoryIndex = 1; if (glfwGetKey(window, GLFW_KEY_2) == GLFW_PRESS) selectedInventoryIndex = 1;
if (glfwGetKey(window, GLFW_KEY_3) == GLFW_PRESS) selectedInventoryIndex = 2; if (glfwGetKey(window, GLFW_KEY_3) == GLFW_PRESS) selectedInventoryIndex = 2;
if (glfwGetKey(window, GLFW_KEY_4) == GLFW_PRESS) selectedInventoryIndex = 3;
ClientInputState currentInputState = new ClientInputState( ClientInputState currentInputState = new ClientInputState(
comm.getClientId(), comm.getClientId(),

View File

@ -66,6 +66,7 @@ public class SoundManager {
load("footsteps_4", "sound/m_footsteps_4.wav"); load("footsteps_4", "sound/m_footsteps_4.wav");
load("shot_m1-garand_1", "sound/m_shot_m1-garand_1.wav"); load("shot_m1-garand_1", "sound/m_shot_m1-garand_1.wav");
load("shot_ak-47_1", "sound/m_shot_ak-47_1.wav"); load("shot_ak-47_1", "sound/m_shot_ak-47_1.wav");
load("shot_winchester_1", "sound/m_shot_winchester_1.wav");
load("bullet_impact_1", "sound/m_bullet_impact_1.wav"); load("bullet_impact_1", "sound/m_bullet_impact_1.wav");
load("bullet_impact_2", "sound/m_bullet_impact_2.wav"); load("bullet_impact_2", "sound/m_bullet_impact_2.wav");
load("bullet_impact_3", "sound/m_bullet_impact_3.wav"); load("bullet_impact_3", "sound/m_bullet_impact_3.wav");

View File

@ -2,6 +2,7 @@ package nl.andrewl.aos_core.model.item;
import nl.andrewl.aos_core.model.item.gun.Ak47; import nl.andrewl.aos_core.model.item.gun.Ak47;
import nl.andrewl.aos_core.model.item.gun.Rifle; import nl.andrewl.aos_core.model.item.gun.Rifle;
import nl.andrewl.aos_core.model.item.gun.Winchester;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
@ -16,11 +17,13 @@ public final class ItemTypes {
public static final BlockItem BLOCK = new BlockItem(1); public static final BlockItem BLOCK = new BlockItem(1);
public static final Rifle RIFLE = new Rifle(2); public static final Rifle RIFLE = new Rifle(2);
public static final Ak47 AK_47 = new Ak47(3); public static final Ak47 AK_47 = new Ak47(3);
public static final Winchester WINCHESTER = new Winchester(4);
static { static {
registerType(BLOCK); registerType(BLOCK);
registerType(RIFLE); registerType(RIFLE);
registerType(AK_47); registerType(AK_47);
registerType(WINCHESTER);
} }
public static void registerType(Item type) { public static void registerType(Item type) {

View File

@ -0,0 +1,21 @@
package nl.andrewl.aos_core.model.item.gun;
import nl.andrewl.aos_core.model.item.Gun;
public class Winchester extends Gun {
public Winchester(int id) {
super(
id,
"Winchester",
10,
6,
4,
0.85f,
0.75f,
2.5f,
0.3f,
60f,
false
);
}
}

View File

@ -34,33 +34,39 @@ public class ProjectileManager {
this.removalQueue = new LinkedList<>(); this.removalQueue = new LinkedList<>();
} }
public void spawnBullet(ServerPlayer player, Gun gun) { public void spawnBullets(ServerPlayer player, Gun gun) {
int id = nextProjectileId++;
if (nextProjectileId == Integer.MAX_VALUE) nextProjectileId = 1;
Random rand = ThreadLocalRandom.current(); Random rand = ThreadLocalRandom.current();
Vector3f pos = new Vector3f(); Vector3f pos = new Vector3f();
Matrix4f bulletTransform = new Matrix4f() Vector3f direction = new Vector3f();
.translate(player.getEyePosition()) Matrix4f bulletTransform = new Matrix4f();
.rotate(player.getOrientation().x + (float) Math.PI, Directions.UPf)
.translate(-0.35f, -0.4f, 0.35f);
bulletTransform.transformPosition(pos);
Vector3f direction = new Vector3f(player.getViewVector()).normalize(); for (int i = 0; i < gun.getBulletsPerRound(); i++) {
float accuracy = gun.getAccuracy(); int id = nextProjectileId++;
accuracy -= server.getConfig().actions.movementAccuracyDecreaseFactor * player.getVelocity().length(); if (nextProjectileId == Integer.MAX_VALUE) nextProjectileId = 1;
float perturbationFactor = (1 - accuracy) / 8;
direction.x += rand.nextGaussian(0, perturbationFactor);
direction.y += rand.nextGaussian(0, perturbationFactor);
direction.z += rand.nextGaussian(0, perturbationFactor);
Vector3f vel = new Vector3f(direction).normalize() pos.set(0);
.mul(200 * MOVEMENT_FACTOR) bulletTransform.identity()
.add(player.getVelocity()); .translate(player.getEyePosition())
.rotate(player.getOrientation().x + (float) Math.PI, Directions.UPf)
.translate(-0.35f, -0.4f, 0.35f);
bulletTransform.transformPosition(pos);
ServerProjectile bullet = new ServerProjectile(id, pos, vel, Projectile.Type.BULLET, player); direction.set(player.getViewVector()).normalize();
projectiles.put(bullet.getId(), bullet); float accuracy = gun.getAccuracy();
server.getPlayerManager().broadcastUdpMessage(bullet.toMessage(false)); accuracy -= server.getConfig().actions.movementAccuracyDecreaseFactor * player.getVelocity().length();
float perturbationFactor = (1 - accuracy) / 8;
direction.x += rand.nextGaussian(0, perturbationFactor);
direction.y += rand.nextGaussian(0, perturbationFactor);
direction.z += rand.nextGaussian(0, perturbationFactor);
Vector3f vel = new Vector3f(direction).normalize()
.mul(200 * MOVEMENT_FACTOR)
.add(player.getVelocity());
ServerProjectile bullet = new ServerProjectile(id, new Vector3f(pos), vel, Projectile.Type.BULLET, player);
projectiles.put(bullet.getId(), bullet);
server.getPlayerManager().broadcastUdpMessage(bullet.toMessage(false));
}
} }
public void tick(float dt) { public void tick(float dt) {

View File

@ -9,6 +9,7 @@ import nl.andrewl.aos_core.model.item.GunItemStack;
import nl.andrewl.aos_core.model.item.ItemStack; import nl.andrewl.aos_core.model.item.ItemStack;
import nl.andrewl.aos_core.model.item.gun.Ak47; import nl.andrewl.aos_core.model.item.gun.Ak47;
import nl.andrewl.aos_core.model.item.gun.Rifle; import nl.andrewl.aos_core.model.item.gun.Rifle;
import nl.andrewl.aos_core.model.item.gun.Winchester;
import nl.andrewl.aos_core.model.world.World; import nl.andrewl.aos_core.model.world.World;
import nl.andrewl.aos_core.net.client.ClientInputState; import nl.andrewl.aos_core.net.client.ClientInputState;
import nl.andrewl.aos_core.net.client.InventorySelectedStackMessage; import nl.andrewl.aos_core.net.client.InventorySelectedStackMessage;
@ -111,7 +112,7 @@ public class PlayerActionManager {
now - gunLastShotAt > gun.getShotCooldownTime() * 1000 && now - gunLastShotAt > gun.getShotCooldownTime() * 1000 &&
(gun.isAutomatic() || !gunNeedsReCock) (gun.isAutomatic() || !gunNeedsReCock)
) { ) {
server.getProjectileManager().spawnBullet(player, gun); server.getProjectileManager().spawnBullets(player, gun);
g.setBulletCount(g.getBulletCount() - 1); g.setBulletCount(g.getBulletCount() - 1);
gunLastShotAt = now; gunLastShotAt = now;
if (!gun.isAutomatic()) { if (!gun.isAutomatic()) {
@ -123,6 +124,8 @@ public class PlayerActionManager {
shotSound = "shot_m1-garand_1"; shotSound = "shot_m1-garand_1";
} else if (gun instanceof Ak47) { } else if (gun instanceof Ak47) {
shotSound = "shot_ak-47_1"; shotSound = "shot_ak-47_1";
} else if (gun instanceof Winchester) {
shotSound = "shot_winchester_1";
} }
server.getPlayerManager().broadcastUdpMessage(new SoundMessage(shotSound, 1, player.getPosition(), player.getVelocity())); server.getPlayerManager().broadcastUdpMessage(new SoundMessage(shotSound, 1, player.getPosition(), player.getVelocity()));
} }

View File

@ -6,6 +6,7 @@ import nl.andrewl.aos_core.model.item.BlockItemStack;
import nl.andrewl.aos_core.model.item.GunItemStack; import nl.andrewl.aos_core.model.item.GunItemStack;
import nl.andrewl.aos_core.model.item.Inventory; import nl.andrewl.aos_core.model.item.Inventory;
import nl.andrewl.aos_core.model.item.ItemTypes; import nl.andrewl.aos_core.model.item.ItemTypes;
import nl.andrewl.aos_core.model.item.gun.Winchester;
import nl.andrewl.aos_core.net.client.PlayerUpdateMessage; import nl.andrewl.aos_core.net.client.PlayerUpdateMessage;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
@ -34,8 +35,9 @@ public class ServerPlayer extends Player {
this.health = 1f; this.health = 1f;
this.actionManager = new PlayerActionManager(this); this.actionManager = new PlayerActionManager(this);
inventory.getItemStacks().add(new GunItemStack(ItemTypes.RIFLE)); inventory.getItemStacks().add(new GunItemStack(ItemTypes.RIFLE));
inventory.getItemStacks().add(new BlockItemStack(ItemTypes.BLOCK, 50, (byte) 1));
inventory.getItemStacks().add(new GunItemStack(ItemTypes.AK_47)); inventory.getItemStacks().add(new GunItemStack(ItemTypes.AK_47));
inventory.getItemStacks().add(new GunItemStack(ItemTypes.WINCHESTER));
inventory.getItemStacks().add(new BlockItemStack(ItemTypes.BLOCK, 50, (byte) 1));
} }
public PlayerActionManager getActionManager() { public PlayerActionManager getActionManager() {