@Test public void fireBullet() throws Exception { D3PolarCoordinate direction = new D3PolarCoordinate(90, 90, 100); gun.fireBullet(direction); Assert.assertThat(publisher.getReceivedMessages().size(), is(1)); FireBulletMessage receivedMessage = (FireBulletMessage) publisher.getReceivedMessages().get(0); Assert.assertThat(receivedMessage.getDamage(), is(20)); Assert.assertThat(receivedMessage.getFiredById(), is(drone.getIdentifier())); Assert.assertThat(receivedMessage.getType(), is(BULLET)); Assert.assertThat(receivedMessage.getDirection(), is(Optional.of(direction))); Assert.assertThat(receivedMessage.getVelocity().get().length(), is(BULLET_SPEED)); Assert.assertThat(receivedMessage.getPosition(), is(Optional.of(gps.getPosition()))); int sizeReceivedMessages = publisher.getReceivedMessages().size(); gun.fireBullet(direction); Assert.assertEquals(sizeReceivedMessages, publisher.getReceivedMessages().size()); setField(gun, "lastShotAtMs", System.currentTimeMillis() - 1000); setField(gun, "nextShotAtMs", System.currentTimeMillis()); direction = new D3PolarCoordinate(90, 90, MAX_DISTANCE + 10); gun.fireBullet(direction); Assert.assertEquals(sizeReceivedMessages, publisher.getReceivedMessages().size()); }
public void fireBullet(D3PolarCoordinate direction) { long currentTimeMs = System.currentTimeMillis(); if (currentTimeMs >= nextShotAtMs && direction.getLength() <= MAX_DISTANCE) { FireBulletMessage msg = new FireBulletMessage(); msg.setDamage(20); msg.setFiredById(drone.getIdentifier()); msg.setIdentifier(UUID.randomUUID().toString()); msg.setType(EntityType.BULLET); msg.setDirection(direction); msg.setVelocity(direction.toVector().scale(BULLET_SPEED / direction.toVector().length())); msg.setPosition(gps.getPosition()); msg.setAcceleration(new D3Vector()); publisher.send(msg); lastShotAtMs = currentTimeMs; nextShotAtMs = lastShotAtMs + BASE_SHOT_TIME_BETWEEN + new Random().nextInt(MAX_OFFSET_SHOT_TIME); callbacks.forEach(callback -> callback.run(msg)); log.info("Firing bullet in direction " + direction + "! Next shot possible in " + ((double) (nextShotAtMs - currentTimeMs) / 1000) + " seconds."); } }
Gun { public void fireBullet(D3PolarCoordinate direction) { long currentTimeMs = System.currentTimeMillis(); if (currentTimeMs >= nextShotAtMs && direction.getLength() <= MAX_DISTANCE) { FireBulletMessage msg = new FireBulletMessage(); msg.setDamage(20); msg.setFiredById(drone.getIdentifier()); msg.setIdentifier(UUID.randomUUID().toString()); msg.setType(EntityType.BULLET); msg.setDirection(direction); msg.setVelocity(direction.toVector().scale(BULLET_SPEED / direction.toVector().length())); msg.setPosition(gps.getPosition()); msg.setAcceleration(new D3Vector()); publisher.send(msg); lastShotAtMs = currentTimeMs; nextShotAtMs = lastShotAtMs + BASE_SHOT_TIME_BETWEEN + new Random().nextInt(MAX_OFFSET_SHOT_TIME); callbacks.forEach(callback -> callback.run(msg)); log.info("Firing bullet in direction " + direction + "! Next shot possible in " + ((double) (nextShotAtMs - currentTimeMs) / 1000) + " seconds."); } } }
Gun { public void fireBullet(D3PolarCoordinate direction) { long currentTimeMs = System.currentTimeMillis(); if (currentTimeMs >= nextShotAtMs && direction.getLength() <= MAX_DISTANCE) { FireBulletMessage msg = new FireBulletMessage(); msg.setDamage(20); msg.setFiredById(drone.getIdentifier()); msg.setIdentifier(UUID.randomUUID().toString()); msg.setType(EntityType.BULLET); msg.setDirection(direction); msg.setVelocity(direction.toVector().scale(BULLET_SPEED / direction.toVector().length())); msg.setPosition(gps.getPosition()); msg.setAcceleration(new D3Vector()); publisher.send(msg); lastShotAtMs = currentTimeMs; nextShotAtMs = lastShotAtMs + BASE_SHOT_TIME_BETWEEN + new Random().nextInt(MAX_OFFSET_SHOT_TIME); callbacks.forEach(callback -> callback.run(msg)); log.info("Firing bullet in direction " + direction + "! Next shot possible in " + ((double) (nextShotAtMs - currentTimeMs) / 1000) + " seconds."); } } Gun();  Gun(Publisher publisher, DroneInit drone, GPS gps, long lastShotAtMs, long nextShotAtMs); }
Gun { public void fireBullet(D3PolarCoordinate direction) { long currentTimeMs = System.currentTimeMillis(); if (currentTimeMs >= nextShotAtMs && direction.getLength() <= MAX_DISTANCE) { FireBulletMessage msg = new FireBulletMessage(); msg.setDamage(20); msg.setFiredById(drone.getIdentifier()); msg.setIdentifier(UUID.randomUUID().toString()); msg.setType(EntityType.BULLET); msg.setDirection(direction); msg.setVelocity(direction.toVector().scale(BULLET_SPEED / direction.toVector().length())); msg.setPosition(gps.getPosition()); msg.setAcceleration(new D3Vector()); publisher.send(msg); lastShotAtMs = currentTimeMs; nextShotAtMs = lastShotAtMs + BASE_SHOT_TIME_BETWEEN + new Random().nextInt(MAX_OFFSET_SHOT_TIME); callbacks.forEach(callback -> callback.run(msg)); log.info("Firing bullet in direction " + direction + "! Next shot possible in " + ((double) (nextShotAtMs - currentTimeMs) / 1000) + " seconds."); } } Gun();  Gun(Publisher publisher, DroneInit drone, GPS gps, long lastShotAtMs, long nextShotAtMs); double getMaxDistance(); long msSinceLastShot(); void fireBullet(D3PolarCoordinate direction); final void registerCallback(GunCallback callback); }
Gun { public void fireBullet(D3PolarCoordinate direction) { long currentTimeMs = System.currentTimeMillis(); if (currentTimeMs >= nextShotAtMs && direction.getLength() <= MAX_DISTANCE) { FireBulletMessage msg = new FireBulletMessage(); msg.setDamage(20); msg.setFiredById(drone.getIdentifier()); msg.setIdentifier(UUID.randomUUID().toString()); msg.setType(EntityType.BULLET); msg.setDirection(direction); msg.setVelocity(direction.toVector().scale(BULLET_SPEED / direction.toVector().length())); msg.setPosition(gps.getPosition()); msg.setAcceleration(new D3Vector()); publisher.send(msg); lastShotAtMs = currentTimeMs; nextShotAtMs = lastShotAtMs + BASE_SHOT_TIME_BETWEEN + new Random().nextInt(MAX_OFFSET_SHOT_TIME); callbacks.forEach(callback -> callback.run(msg)); log.info("Firing bullet in direction " + direction + "! Next shot possible in " + ((double) (nextShotAtMs - currentTimeMs) / 1000) + " seconds."); } } Gun();  Gun(Publisher publisher, DroneInit drone, GPS gps, long lastShotAtMs, long nextShotAtMs); double getMaxDistance(); long msSinceLastShot(); void fireBullet(D3PolarCoordinate direction); final void registerCallback(GunCallback callback);  }