Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package fi.utu.tech.distributed.gorilla.logic;
- import java.net.InetAddress;
- import java.net.UnknownHostException;
- import fi.utu.tech.distributed.gorilla.logic.mesh.InfoPacket;
- import fi.utu.tech.distributed.gorilla.logic.mesh.Mesh;
- import fi.utu.tech.distributed.gorilla.logic.mesh.MovementPacket;
- import fi.utu.tech.distributed.gorilla.logic.mesh.Packet;
- import fi.utu.tech.distributed.gorilla.views.MainCanvas;
- import javafx.application.Platform;
- /**
- * Luokka, johon toteutetaan GorillaPelin moninplein logiikka.
- */
- public class GorillaLogicMultiplayer extends GorillaLogic {
- // Moninpeliin vaadittavan Mesh-verkon luokka.
- private Mesh mesh;
- // Grafiikoille tärkeä olio.
- public MainCanvas mainCanvas = new MainCanvas();
- // Asetetaan peliin liittyvien pelaajien maksimilukumäärä.
- public final int maxPlayers = 6;
- // Yhdistettävän koneen IP ja portti
- String friendIP;
- String friendPort;
- boolean ipinputmode = false;
- @Override
- protected void handleMultiplayer() {
- ipinputmode = true;
- }
- protected void parseConnectionInfo(String cmd) throws IllegalArgumentException {
- String[] info = new String[2];
- int count = 0;
- for (int j = 0; j < cmd.length(); j++) {
- if (cmd.charAt(j) == ' ') {
- count++;
- }
- }
- if (count != 1) {
- throw new IllegalArgumentException();
- }
- for (int i = 0; i < cmd.length(); i++) {
- if (cmd.charAt(i) == ' ') {
- info[0] = cmd.substring(0, i);
- info[1] = cmd.substring(i, cmd.length());
- }
- }
- friendIP = info[0];
- friendPort = info[1];
- inforead();
- }
- @Override
- public void parseCommandLine(String cmd) {
- if (cmd.contains(" ")) {
- String rest = cmd.substring(cmd.split(" ")[0].length() + 1);
- switch (cmd.split(" ")[0]) {
- case "q":
- case "quit":
- case "exit":
- Platform.exit();
- break;
- case "name":
- handleNameChange(rest);
- break;
- case "s":
- case "chat":
- case "say":
- handleChatMessage(new ChatMessage(myName, "all", rest));
- break;
- case "a":
- case "k":
- case "angle":
- case "kulma":
- if (gameMode != GameMode.Game)
- return;
- try {
- double angle = Double.parseDouble(rest);
- MoveThrowBanana mtb = new MoveThrowBanana(angle, Double.NaN);
- handleThrowBanana(mtb);
- System.out.println("Asetettu kulma: " + angle);
- } catch (NumberFormatException e) {
- System.out.println("Virheellinen komento, oikea on: angle <liukuluku -45..225>");
- } catch (IllegalArgumentException e) {
- System.out.println(e.getMessage());
- }
- break;
- case "v":
- case "n":
- case "velocity":
- case "nopeus":
- if (gameMode != GameMode.Game)
- return;
- try {
- double velocity = Double.parseDouble(rest);
- MoveThrowBanana mtb = new MoveThrowBanana(Double.NaN, velocity);
- handleThrowBanana(mtb);
- System.out.println("Asetettu nopeus: " + velocity);
- } catch (NumberFormatException e) {
- System.out.println("Virheellinen komento, oikea on: velocity <liukuluku 0..150>");
- } catch (IllegalArgumentException e) {
- System.out.println(e.getMessage());
- }
- break;
- }
- }
- if (Character.isDigit(cmd.charAt(0)) && ipinputmode == true) {
- try{
- parseConnectionInfo(cmd);
- } catch (IllegalArgumentException e){
- System.out.println("Virheellinen IP ja portti.");
- }
- //connectToServer(friendIP, friendPort); //kaverin kone
- try {
- connectToServer(InetAddress.getLocalHost().getHostAddress(), "5000"); //oma kone
- } catch (UnknownHostException e) {
- System.out.println("Ongelma switchissä!!!!!!");
- e.printStackTrace();
- }
- ipinputmode = false;
- }
- else {
- switch (cmd) {
- case "test":
- System.out.println("TOIMI NYT PERRRRRRRRRRRRRRRKELE!!!!");
- break;
- case "inforead":
- inforead();
- break;
- case "infoput":
- handleMultiplayer();
- break;
- }
- }
- }
- public void inforead() {
- System.out.println("FriendIP on: " + friendIP);
- System.out.println("FriendPort on: " + friendPort);
- }
- @Override
- public void startServer(String port) {
- try {
- this.mesh = new Mesh(5000, InetAddress.getLocalHost().getHostAddress());
- } catch (UnknownHostException e) {
- System.out.println("Ongelma startServerissä!!!");
- e.printStackTrace();
- }
- mesh.setLogic(this);
- mesh.setName("Mää");
- mesh.run();
- }
- @Override
- public void connectToServer(String address, String port){
- mesh.connect(address, Integer.parseInt(port));
- }
- @Override
- public void handleNameChange(String newName){
- myName = newName;
- mesh.setName(myName);
- }
- @Override
- public void handleChatMessage(ChatMessage msg){
- System.out.printf("Sinä sanot: %s%n", msg.contents);
- mesh.broadcast((Packet) msg);
- }
- public void handlePacket(Packet p){
- if (p instanceof ChatMessage){
- ChatMessage msg = (ChatMessage) p;
- System.out.println(msg.getName() + " sanoo: " + msg.contents);
- }
- else if (p instanceof MovementPacket){}
- else if (p instanceof InfoPacket){
- InfoPacket info = (InfoPacket) p;
- GameState newGameState = info.getGameState();
- setMode(GameMode.Game);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement