Beispiel #1
0
 void handleFire(Person person, Room room) {
   if (person.isFire()) {
     AbstractWeapon weapon = person.getWeapon();
     long now = System.currentTimeMillis();
     if (!person.isReload() && weapon.getCurrentClip() <= 0) {
       if (weapon.getTotalClip() > 0) {
         person.setIsReload(true);
         person.setReloadCooldown(now + person.getWeapon().getReloadTimeout());
       }
     } else if (!person.isReload()
         && weapon.getCurrentClip() > 0
         && person.getShotCooldown() < now) {
       fire(person, room);
     }
   }
 }
Beispiel #2
0
 public void onReload(Person person, long now) {
   if (person.isReload() && person.getShotCooldown() < now && person.getReloadCooldown() < now) {
     AbstractWeapon weapon = person.getWeapon();
     if (weapon.getTotalClip() <= 0) {
       return;
     }
     person.setIsReload(false);
     int clipToReload = weapon.getClipSize();
     if (clipToReload > weapon.getTotalClip()) {
       clipToReload = weapon.getTotalClip();
     }
     weapon.setCurrentClip(clipToReload);
   }
 }
Beispiel #3
0
 private void handleWeaponPick(Person player, AbstractWeapon weapon) {
   for (AbstractWeapon personHaving : player.getWeaponList()) {
     if (personHaving.getClass().equals(weapon.getClass())) {
       personHaving.setTotalClip(personHaving.getTotalClip() + weapon.getTotalClip());
       personHaving.setCurrentClip(personHaving.getClipSize());
       if (player.isReload() && personHaving == player.getWeapon()) {
         player.setReload(false);
         player.setReloadCooldown(0);
       }
       if (personHaving.getTotalClip() > personHaving.getMaxClip()) {
         personHaving.setTotalClip(weapon.getMaxClip());
       }
       return;
     }
   }
   player.getWeaponList().add(weapon);
 }
Beispiel #4
0
  private void fire(Person person, Room room) {
    AbstractWeapon gun = person.getWeapon();
    person.setShotCooldown(System.currentTimeMillis() + gun.getShotTimeout());

    gun.setTotalClip(gun.getTotalClip() - 1);
    gun.setCurrentClip(gun.getCurrentClip() - 1);

    Integer id = gameContext.getProjectilesIds().getAndIncrement();
    Projectile[] projectilesBatch = new Projectile[gun.getBulletsPerShot()];
    for (int i = 0; i < projectilesBatch.length; i++) {
      Projectile projectile = getCompatibleProjectile(person);
      projectile.setId(i);
      projectilesBatch[i] = projectile;

      float angle = projectile.getAngle();
      if (angle == 90) {
        projectile.setxEnd((int) person.getX());
        int gunLimit = (int) (projectile.getRadius() + person.getY());
        projectile.setyEnd(gunLimit > room.getMap().getY() ? room.getMap().getY() : gunLimit);
      } else if (angle == 180) {
        projectile.setyEnd((int) person.getY());
        int gunLimit = (int) person.getX() - (int) projectile.getRadius();
        projectile.setyEnd(gunLimit > 0 ? gunLimit : 0);
      } else if (angle == 270) {
        projectile.setxEnd((int) person.getX());
        int gunLimit = (int) person.getY() - (int) projectile.getRadius();
        projectile.setyEnd(gunLimit > 0 ? gunLimit : 0);
      } else if (angle == 0) {
        projectile.setyEnd((int) person.getY());
        int gunLimit = (int) person.getX() + (int) projectile.getRadius();
        projectile.setyEnd(gunLimit > room.getMap().getX() ? room.getMap().getX() : gunLimit);
      } else {
        double y = projectile.getRadius() * Math.sin(angle * Math.PI / 180);
        double x = projectile.getRadius() * Math.cos(angle * Math.PI / 180);
        projectile.setxEnd((int) x + (int) person.getX());
        projectile.setyEnd((int) y + (int) person.getY());
      }
      if (projectile.isInstant()) {
        calculateInstantImpacts(person, projectile, room);
      }
    }

    room.getProjectiles().put(id, projectilesBatch);
  }
Beispiel #5
0
  private Projectile getCompatibleProjectile(Person person) {
    float angle = getNewProjectileAngle(person);

    if (person.getWeapon() instanceof Knife) {
      return new KnifeAmmo((int) person.getX(), (int) person.getY(), angle);
    } else if (person.getWeapon() instanceof Pistol
        || person.getWeapon() instanceof AssaultRifle
        || person.getWeapon() instanceof Minigun) {
      return new Bullet((int) person.getX(), (int) person.getY(), angle);
    } else if (person.getWeapon() instanceof Shotgun) {
      return new Shot((int) person.getX(), (int) person.getY(), angle);
    } else if (person.getWeapon() instanceof SniperRifle) {
      return new SniperBullet((int) person.getX(), (int) person.getY(), angle);
    } else if (person.getWeapon() instanceof RocketLauncher) {
      return new Rocket((int) person.getX(), (int) person.getY(), angle, person.getId());
    } else if (person.getWeapon() instanceof Flamethrower) {
      return new Flame((int) person.getX(), (int) person.getY(), angle, person.getId());
    } else {
      throw new RuntimeException("Person have no weapon!");
    }
  }
Beispiel #6
0
 private float getNewProjectileAngle(Person person) {
   float spread = (float) person.getWeapon().getSpread();
   return person.getAngle() + random.nextFloat() * (spread * 2) - spread;
 }