use of de.adesso.anki.battle.world.bodies.Vehicle in project anki-battle-showcase by adessoAG.
the class AnkiInitializer method connectToAnki.
@SneakyThrows
private void connectToAnki() {
log.info("Connecting to Anki Gateway at 10.200.100.12...");
gateway = new AnkiGateway("10.200.100.12", 5000);
ankiVehicles = gateway.findVehicles();
for (AnkiVehicle anki : ankiVehicles.subList(0, 1)) {
myVehicle = new Vehicle();
myVehicle.setAnkiReference(anki);
anki.connect();
anki.sendMessage(new SdkModeMessage());
world.addBody(myVehicle);
}
}
use of de.adesso.anki.battle.world.bodies.Vehicle in project anki-battle-showcase by adessoAG.
the class SimulationInitializer method addVehicles.
private void addVehicles() {
Vehicle vehicle = new Vehicle();
vehicle.setName("vehicle1");
vehicle.setWorld(world);
vehicle.setTrack(3);
vehicle.setCurrentRoadpiece(world.getRoadmap().getAnchor().reverse());
vehicle.setPosition(world.getRoadmap().getAnchor().getEntry().reverse());
vehicle.setTargetSpeed(250);
vehicle.setMineReady(true);
vehicle.setRocketReady(true);
world.addBody(vehicle);
/* Vehicle vehicle2 = new Vehicle();
vehicle2.setName("vehicle2");
vehicle2.setWorld(world);
vehicle2.setTrack(3);
vehicle2.setCurrentRoadpiece(world.getRoadmap().getAnchor().reverse());
vehicle2.setPosition(world.getRoadmap().getAnchor().getEntry().reverse());
vehicle2.setTargetSpeed(800);
vehicle2.setMineReady(true);
vehicle2.setRocketReady(true);
world.addBody(vehicle2);*/
Mine mine = new Mine();
mine.setWorld(world);
mine.setTargetSpeed(0);
mine.setCurrentRoadpiece(world.getRoadmap().getAnchor().getNext().getNext().getNext().getNext().getNext().reverse());
mine.setPosition(world.getRoadmap().getAnchor().getNext().getNext().getNext().getNext().getNext().getEntry().reverse());
world.addBody(mine);
Mine mine2 = new Mine();
mine2.setWorld(world);
mine2.setTargetSpeed(0);
mine2.setCurrentRoadpiece(world.getRoadmap().getAnchor().getNext().getNext().getNext().reverse());
mine2.setPosition(world.getRoadmap().getAnchor().getNext().getNext().getNext().getEntry().reverse());
world.addBody(mine2);
/*Vehicle vehicle2 = new Vehicle();
vehicle2.setName("vehicle2");
vehicle2.setWorld(world);
vehicle2.setTrack(1);
vehicle2.setCurrentRoadpiece(world.getRoadmap().getAnchor());
vehicle2.setPosition(world.getRoadmap().getAnchor().getEntry());
vehicle2.setTargetSpeed(750);
vehicle2.setRocketReady(true);
world.addBody(vehicle2);
*/
}
use of de.adesso.anki.battle.world.bodies.Vehicle in project anki-battle-showcase by adessoAG.
the class MqttService method connect.
public void connect() {
try {
// test.mosquitto.org broker.hivemq.com
mqttClient = new MqttClient("tcp://localhost:1883", "anki-battle", new MemoryPersistence());
MqttConnectOptions connOpts = new MqttConnectOptions();
connOpts.setCleanSession(false);
log.info("Connecting to MQTT broker: localhost:1883");
mqttClient.connect(connOpts);
log.info("Connected to MQTT broker");
mqttClient.setCallback(new MqttCallback() {
@Override
public void connectionLost(Throwable throwable) {
log.info("MQTT connection lost");
}
@Override
public void messageArrived(String s, MqttMessage mqttMessage) {
try {
log.debug("MQTT message arrived: topic=" + s + "; message=" + mqttMessage.toString());
String temp = mqttMessage.toString();
byte[] hm = mqttMessage.getPayload();
JSONObject json = new JSONObject(mqttMessage.toString());
// get topic, where topic is identifier for vehicle
// get corresponding vehicle v
// v.exexute command
String topic = s.split("_")[0];
List<DynamicBody> vehicles = world.getDynamicBodies();
Vehicle wantedVehicle = null;
for (int i = 0; i < vehicles.size(); i++) {
Vehicle currentVehicle = ((Vehicle) vehicles.get(i));
if (topic.equals(currentVehicle.getName())) {
wantedVehicle = currentVehicle;
break;
}
}
String commandType = (String) json.get("type");
int speed;
Command command = null;
int track;
switch(commandType) {
case ("accelerate"):
speed = Integer.parseInt((String) json.get("velocity"));
command = new AccelerateCommand(speed);
break;
case ("brake"):
speed = Integer.parseInt((String) json.get("velocity"));
command = new BrakeCommand(speed);
break;
case ("change track"):
val lane = Double.parseDouble((String) json.get("track"));
command = new ChangeLaneCommand(lane);
break;
case ("uTurn"):
command = new UTurnCommand();
break;
case ("fireRocket"):
command = new FireRocketCommand("");
break;
case ("putMine"):
command = new PutMineCommand();
break;
}
if (command != null) {
command.execute(wantedVehicle);
} else {
log.debug("No Command");
}
} catch (Exception e) {
log.error("Error while parsing MQTT message", e);
}
}
@Override
public void deliveryComplete(IMqttDeliveryToken iMqttDeliveryToken) {
}
});
} catch (MqttException e) {
log.debug("exception during initialising service");
e.printStackTrace();
}
}
use of de.adesso.anki.battle.world.bodies.Vehicle in project anki-battle-showcase by adessoAG.
the class GameEngine method checkCollision.
private boolean checkCollision(Body weapon) {
if (weapon instanceof Vehicle) {
return false;
}
// merge into new superclass weapon ?
if (weapon instanceof Rocket && !((Rocket) weapon).isActive()) {
return false;
}
if (weapon instanceof Mine && !((Mine) weapon).isActive()) {
return false;
}
List<Vehicle> vehicles = world.getVehicles();
Position pos1 = weapon.getPosition();
boolean succesfulHit = false;
// TODO find damage values for weapon types
int damage = ((weapon instanceof Rocket) ? 10 : 20);
for (Vehicle vehicle : vehicles) {
Position pos2 = vehicle.getPosition();
double distance = pos1.distance(pos2);
// TODO find distance value that indicates a collision
double dummyValue = 30;
if (distance < dummyValue) {
System.out.println("BOOM: " + weapon.getClass().getSimpleName());
vehicle.setEnergy(vehicle.getEnergy() - damage);
System.out.println(vehicle.getEnergy());
succesfulHit = true;
}
}
return succesfulHit;
}
use of de.adesso.anki.battle.world.bodies.Vehicle in project anki-battle-showcase by adessoAG.
the class GameEngine method gameLoop.
@Scheduled(fixedRate = 50)
public void gameLoop() {
VehicleStateProvider vehicleStateProvider = new VehicleStateProvider();
if (running) {
// Step 0: Calculate elapsed nanoseconds since last loop
long step = System.nanoTime();
long deltaNanos = step - lastStep;
lastStep = step;
// TODO: Synchronize with Anki vehicles
if (stepCount == 0) {
for (DynamicBody body : world.getDynamicBodies()) {
if (body instanceof Vehicle)
anki.synchronizeState((Vehicle) body);
}
}
// Step 2: Simulate movement
updateSimulation(deltaNanos);
// Step 3: Process input
// TODO: Process input from frontend
// Step 4: Evaluate behavior
stepCount++;
if (stepCount > 3) {
List<DynamicBody> dynBodies = world.getDynamicBodies();
for (DynamicBody body : dynBodies) {
log.debug(body.toString());
if (!(body instanceof Vehicle)) {
continue;
}
List<GameState> factsRoad = vehicleStateProvider.getRoadFacts((Vehicle) body);
List<GameState> factsInventory = vehicleStateProvider.getInventoryFacts((Vehicle) body);
List<GameState> factsObstacles = vehicleStateProvider.getObstacleFacts((Vehicle) body);
body.setFacts(factsRoad, factsInventory, factsObstacles);
}
evaluateBehavior();
stepCount = 0;
}
// Remove while iterating, leads to exception
// java.util.ConcurrentModificationException: in renderer?
// collisionHandling();
collectOrphanedWeapons();
// Step 5: Render world
renderWorld();
calculateLaptime();
}
}
Aggregations