Skip to content

Commit

Permalink
Refactored WeaponsSystem.java to grant better overview of logic - mad…
Browse files Browse the repository at this point in the history
…e sure Guns work with a validity check, cost deduction, projectil creation and a sound. Added a few more sounds to GameSounds.java and AudioTypes.java #69
  • Loading branch information
assofohdz committed Jan 22, 2023
1 parent c54e10b commit eff3269
Show file tree
Hide file tree
Showing 8 changed files with 498 additions and 397 deletions.
20 changes: 20 additions & 0 deletions api/src/infinity/es/AudioTypes.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,11 @@ public class AudioTypes {
public static final String EXPLOSION2 = "explosion2";
public static final String BURST = "burst";
public static final String REPEL = "repel";
public static final String FIRE_MINE_L1 = "fire_mine_l1";
public static final String FIRE_MINE_L2 = "fire_mine_l2";
public static final String FIRE_MINE_L3 = "fire_mine_l3";
public static final String FIRE_MINE_L4 = "fire_mine_l4";


public static AudioType repel(final EntityData ed) {
return AudioType.create(REPEL, ed);
Expand All @@ -67,6 +72,21 @@ public static AudioType fire_gravbomb(final EntityData ed) {
return AudioType.create(FIRE_GRAVBOMB, ed);
}

public static AudioType fire_mine(final EntityData ed, final BombLevelEnum level){
switch(level.level){
case 1:
return AudioType.create(FIRE_MINE_L1, ed);
case 2:
return AudioType.create(FIRE_MINE_L2, ed);
case 3:
return AudioType.create(FIRE_MINE_L3, ed);
case 4:
return AudioType.create(FIRE_MINE_L4, ed);
default:
throw new IllegalArgumentException("Unknown level: " + level);
}
}

// Bombs
public static AudioType fire_bomb(final EntityData ed, final BombLevelEnum level) {
switch (level.level) {
Expand Down
6 changes: 5 additions & 1 deletion api/src/infinity/sim/CoreGameConstants.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ public class CoreGameConstants {
public static final double THORPROJECTILESPEED = 30;
public static final double BURSTPROJECTILESPEED = 50;

// Decays
// Decays must be in milliseconds
public static final long BULLETDECAY = 1500;
public static final long PRIZEDECAY = 20000;
Expand All @@ -35,6 +34,10 @@ public class CoreGameConstants {
// Cooldowns
public static final long THORCOOLDOWN = 500;
public static final long BURSTCOOLDOWN = 250;
public static final long GUNCOOLDOWN = 250;

// Cost of firing
public static final int GUNCOST = 10;

public static final long BURSTPROJECTILECOUNT = 30;

Expand All @@ -55,4 +58,5 @@ public class CoreGameConstants {
public static final int TOWERCOST = 1000;

public static final String DEFAULTARENAID = "default";
public static final String BOMBLEVELPREPENDTEXT = "bomb_l";
}
30 changes: 16 additions & 14 deletions api/src/infinity/sim/GameEntities.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,17 +56,18 @@
*/
public class GameEntities {

// TODO: All constants should come through the parameters - for now, they come
// from the constants
// TODO: All parameters should be dumb types and should be the basis of the
// complex types used in the backend
private GameEntities() {
}

// TODO: All constants should come through the parameters - for now, they come from the constants
// TODO: All parameters should be dumb types and should be the basis of the complex types used in the backend
public static EntityId createGravSphere(
final EntityData ed,
@SuppressWarnings("unused") final EntityId owner,
final EntityId owner,
final PhysicsSpace<?, ?> phys,
final long createdTime,
final Vec3d pos,
@SuppressWarnings("unused") final double radius) {
final double radius) {
final EntityId result = ed.createEntity();
ed.setComponents(
result, ShapeInfo.create("gravitysphere", 1, ed), new SpawnPosition(phys.getGrid(), pos));
Expand All @@ -85,11 +86,11 @@ public static EntityId createDelayedBomb(
final long decayMillis,
final long scheduledMillis,
final HashSet<EntityComponent> delayedComponents,
final BombLevelEnum level) {
final String shapeName) {

final EntityId lastDelayedBomb =
GameEntities.createBomb(
ed, owner, phys, createdTime, pos, linearVelocity, decayMillis, level);
ed, owner, phys, createdTime, pos, linearVelocity, decayMillis, shapeName);

ed.setComponents(lastDelayedBomb, new Delay(scheduledMillis, delayedComponents, Delay.SET));
ed.setComponents(lastDelayedBomb, WeaponTypes.gravityBomb(ed));
Expand All @@ -105,12 +106,12 @@ public static EntityId createBomb(
final Vec3d pos,
final Vec3d linearVelocity,
final long decayMillis,
final BombLevelEnum level) {
final String shapeName) {
final EntityId lastBomb = ed.createEntity();

ed.setComponents(
lastBomb,
ShapeInfo.create("bomb_l" + level.level, 0.5, ed),
ShapeInfo.create(shapeName, 0.5, ed),
new SpawnPosition(phys.getGrid(), pos),
new Mass(5),
new Decay(
Expand All @@ -135,13 +136,12 @@ public static EntityId createBullet(
final Vec3d pos,
final Vec3d linearVelocity,
final long decayMillis,
@SuppressWarnings("unused") final GunLevelEnum level,
final String shapeName) {
final EntityId lastBullet = ed.createEntity();

ed.setComponents(
lastBullet,
ShapeInfo.create(shapeName, 0.125, ed),
ShapeInfo.create(shapeName, CorePhysicsConstants.BULLETSIZERADIUS, ed),
new SpawnPosition(phys.getGrid(), pos),
new Mass(1),
new Decay(
Expand Down Expand Up @@ -454,6 +454,8 @@ public static EntityId createShip(
ShapeInfo.create(ShapeNames.SHIP_SHARK, CorePhysicsConstants.SHIPSIZERADIUS, ed);
ed.setComponents(result, shark);
break;
default:
throw new RuntimeException("Unknown ship type: " + ship);
}

// ViewTypes.ship_warbird(ed),
Expand Down Expand Up @@ -489,8 +491,8 @@ public static EntityId createShip(

// Add guns:
ed.setComponent(result, new Gun(GunLevelEnum.LEVEL_1));
ed.setComponent(result, new GunCost(10));
ed.setComponent(result, new GunFireDelay(250));
ed.setComponent(result, new GunCost(CoreGameConstants.GUNCOST));
ed.setComponent(result, new GunFireDelay(CoreGameConstants.GUNCOOLDOWN));

// Add gravity bombs
ed.setComponent(result, new GravityBomb(BombLevelEnum.BOMB_1));
Expand Down
41 changes: 40 additions & 1 deletion api/src/infinity/sim/GameSounds.java
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,12 @@ public static void createBurstSound(
ed.setComponent(result, new Meta(createdTime));
}

public static void createPrizeSound(final @NotNull EntityData ed, final long createdTime, EntityId parent, Vec3d loc, @NotNull PhysicsSpace phys) {
public static void createPrizeSound(
final @NotNull EntityData ed,
final long createdTime,
EntityId parent,
Vec3d loc,
@NotNull PhysicsSpace phys) {
final EntityId result = ed.createEntity();
ed.setComponents(
result,
Expand All @@ -131,4 +136,38 @@ public static void createPrizeSound(final @NotNull EntityData ed, final long cre
createdTime, createdTime + TimeUnit.NANOSECONDS.convert(3000, TimeUnit.MILLISECONDS)));
ed.setComponent(result, new Meta(createdTime));
}

public static void createThorSound(
final @NotNull EntityData ed,
final long createdTime,
EntityId parent,
Vec3d loc,
@NotNull PhysicsSpace phys) {
final EntityId result = ed.createEntity();
ed.setComponents(
result,
AudioTypes.fire_thor(ed),
new Parent(parent),
new SpawnPosition(phys.getGrid(), loc),
new Decay(
createdTime, createdTime + TimeUnit.NANOSECONDS.convert(3000, TimeUnit.MILLISECONDS)));
ed.setComponent(result, new Meta(createdTime));
}

public static void createMineSound(
EntityData ed,
EntityId requester,
@NotNull PhysicsSpace phys,
long time,
Vec3d location,
BombLevelEnum level) {
final EntityId result = ed.createEntity();
ed.setComponents(
result,
AudioTypes.fire_mine(ed, level),
new Parent(requester),
new SpawnPosition(phys.getGrid(), location),
new Decay(time, time + TimeUnit.NANOSECONDS.convert(3000, TimeUnit.MILLISECONDS)));
ed.setComponent(result, new Meta(time));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -199,15 +199,15 @@ private class GameSessionImpl implements GameSession {
// private PlayerDriver driver;
private final EntityId playerEntityId;
// private final BinIndex binIndex;
private final WeaponsSystem attackSystem;
private final WeaponsSystem weaponsSystem;
// private MapSystem mapSystem;

public GameSessionImpl(final HostedConnection conn) {
this.conn = conn;

phys = gameSystems.get(PhysicsSpace.class, true);
// mphys = gameSystems.get(MPhysSystem.class, true);
attackSystem = gameSystems.get(WeaponsSystem.class, true);
weaponsSystem = gameSystems.get(WeaponsSystem.class, true);
// this.mapSystem = gameSystems.get(MapSystem.class, true);

// binIndex = phys.getBinIndex();
Expand Down Expand Up @@ -337,7 +337,7 @@ public void action(final byte actionInput) {

@Override
public void attack(final byte attackInput) {
attackSystem.sessionAttack(avatarEntityId, attackInput);
weaponsSystem.sessionAttack(avatarEntityId, attackInput);
}

@Override
Expand Down
6 changes: 3 additions & 3 deletions infinity/src/main/java/infinity/systems/EnergySystem.java
Original file line number Diff line number Diff line change
Expand Up @@ -135,13 +135,13 @@ public void update(final SimTime time) {
final double tpf = time.getTpf();
final Recharge recharge = e.get(Recharge.class);
final int charge = Math.toIntExact(Math.round(tpf * recharge.getRechargePerSecond()));
createHealthChange(e.getId(), charge);
damage(e.getId(), charge);
}
} else {
final double tpf = time.getTpf();
final Recharge recharge = e.get(Recharge.class);
final int charge = Math.toIntExact(Math.round(tpf * recharge.getRechargePerSecond()));
createHealthChange(e.getId(), charge);
damage(e.getId(), charge);
}
}

Expand Down Expand Up @@ -220,7 +220,7 @@ public int getMaxHealth(final EntityId eId) {
* @param deltaHitPoints the change in hitpoints (can be both positive an
* negative)
*/
public void createHealthChange(final EntityId eId, final int deltaHitPoints) {
public void damage(final EntityId eId, final int deltaHitPoints) {
final EntityId healthChange = ed.createEntity();
ed.setComponents(healthChange, new Buff(eId, 0), new HealthChange(deltaHitPoints));
}
Expand Down
Loading

0 comments on commit eff3269

Please sign in to comment.