import java.net.InetAddress; | import java.net.InetAddress; | ||||
import java.util.Arrays; | import java.util.Arrays; | ||||
public class ArduinoCommunicator { | |||||
public class ArduinoCommunication { | |||||
private InetAddress address; | private InetAddress address; | ||||
private int port; | private int port; | ||||
private OSCPortOut oscPortOut; | private OSCPortOut oscPortOut; | ||||
public ArduinoCommunicator(String ipAdress, int portNr) { | |||||
public ArduinoCommunication(String ipAdress, int portNr) { | |||||
try{ | try{ | ||||
address = InetAddress.getByName(ipAdress); | address = InetAddress.getByName(ipAdress); | ||||
port = portNr; | port = portNr; | ||||
} | } | ||||
} | } | ||||
public void sendMessage(double speed, double steer, boolean light){ | |||||
public void sendMessage(int a, double x, double y){ | |||||
String oscAddress = "/arduino/command"; // OSC-Adresse für die Nachricht | String oscAddress = "/arduino/command"; // OSC-Adresse für die Nachricht | ||||
OSCMessage message = new OSCMessage(oscAddress, Arrays.asList(speed, steer, light)); | |||||
OSCMessage message = new OSCMessage(oscAddress, Arrays.asList(a, x, y)); | |||||
try { | try { | ||||
oscPortOut.send(message); | oscPortOut.send(message); | ||||
} | } |
public class GameLoop { | public class GameLoop { | ||||
int activePlayer = 0; | |||||
Player player1; | |||||
Player player2; | |||||
boolean wasSmthTracked = false; | |||||
public GameLoop(){ | |||||
player1 = new Player(0, "ip", 9000); | |||||
player2 = new Player(1, "ip", 9000); | |||||
run(); | |||||
} | |||||
private void run(){ | |||||
while(true){ | |||||
communicate(); | |||||
if(!wasSmthTracked) { | |||||
track(); | |||||
} | |||||
draw(); | |||||
} | |||||
} | |||||
private void communicate() { | |||||
if(wasSmthTracked){ | |||||
//Beide sollen ausgehen | |||||
player1.sendToCar(0, 0.0, 0.0); | |||||
player2.sendToCar(0, 0.0, 0.0); | |||||
activePlayer++; | |||||
} else { | |||||
//Schicke die Nachrichten fürs angehen | |||||
activePlayer = activePlayer%2; | |||||
if (activePlayer == 0){ | |||||
player1.sendToCar(1, 0.0, 0.0); | |||||
player2.sendToCar(0, 0.0, 0.0); | |||||
} | |||||
else { | |||||
player1.sendToCar(0, 0.0, 0.0); | |||||
player2.sendToCar(1, 0.0, 0.0); | |||||
} | |||||
} | |||||
} | |||||
private void track(){ | |||||
//suche Lampe | |||||
//Wenn geklappt, setze wasSmthTracked auf true | |||||
} | |||||
private void draw(){ | |||||
//Schicke für active Player letzte Koordinaten | |||||
} | |||||
} | } |
private int y; | private int y; | ||||
Queue<int[]> trail = new LinkedList<>(); | Queue<int[]> trail = new LinkedList<>(); | ||||
ArduinoCommunicator arduinoCommunicator; | |||||
ArduinoCommunication arduinoCommunicator; | |||||
// Konstruktor | // Konstruktor | ||||
public Player(int id, String finalIpAddress, int finalPortNr) { | public Player(int id, String finalIpAddress, int finalPortNr) { | ||||
this.ID = id; | this.ID = id; | ||||
arduinoCommunicator = new ArduinoCommunicator(finalIpAddress, finalPortNr); | |||||
arduinoCommunicator = new ArduinoCommunication(finalIpAddress, finalPortNr); | |||||
} | } | ||||
return trail; | return trail; | ||||
} | } | ||||
public void sendToCar(double speed, double steer, boolean light){ | |||||
arduinoCommunicator.sendMessage(speed, steer, light); | |||||
public void sendToCar(int light, double steer, double speed){ | |||||
arduinoCommunicator.sendMessage(light, steer, speed); | |||||
} | } | ||||