Ejemplo n.º 1
0
  // Start Game
  public void init() {
    // map size
    level = new Level();

    MapProperties prop = level.map.getProperties();
    int mapWidth = prop.get("width", Integer.class);
    int mapHeight = prop.get("height", Integer.class);
    System.out.println("mapWidth: " + mapWidth + ", " + "mapHeight: " + mapHeight);

    tilePixelWidth = prop.get("tilewidth", Integer.class);
    tilePixelHeight = prop.get("tileheight", Integer.class);

    System.out.println(
        "tilePixelWidth: " + tilePixelWidth + ", " + "tilePixelHeight: " + tilePixelHeight);

    mapPixelWidth = mapWidth * tilePixelWidth;
    mapPixelHeight = mapHeight * tilePixelHeight;

    System.out.println(
        "mapPixelWidth: " + mapPixelWidth + ", " + "mapPixelHeight: " + mapPixelHeight);

    // set bounding boxes for tilemap sprites
    // stones
    TiledMapTileLayer layer = (TiledMapTileLayer) level.map.getLayers().get("stones");
    System.out.println("Layer: " + layer);

    freeCells = new boolean[layer.getWidth()][layer.getHeight()];
    mapCellWidth = layer.getWidth();
    mapCellHeight = layer.getHeight();
    for (int x = 0; x < layer.getWidth(); x++) {
      for (int y = 0; y < layer.getHeight(); y++) {
        freeCells[x][y] = true;
        if (layer.getCell(x, y) != null) {
          // Spawn Walls
          WallGameObject wall =
              new WallGameObject(x * tilePixelWidth + 25, y * tilePixelWidth + 25, 100, 100);
          this.objs.addObject(wall);

          // safe blocked cell coordinates for random player position
          freeCells[x][y] = false;
        }
      }
    }

    // bushs
    layer = (TiledMapTileLayer) level.map.getLayers().get("bushs");

    for (int x = 0; x < layer.getWidth(); x++) {
      for (int y = 0; y < layer.getHeight(); y++) {
        if (layer.getCell(x, y) != null) {
          // Spawn Bush
          BushGameObject bush =
              new BushGameObject(x * tilePixelWidth + 25, y * tilePixelWidth + 25, 90, 90);
          this.objs.addObject(bush);
        }
      }
    }

    // checkpoints
    layer = (TiledMapTileLayer) level.map.getLayers().get("checkpoint");

    int ci = 0;
    for (int x = 0; x < layer.getWidth(); x++) {
      for (int y = 0; y < layer.getHeight(); y++) {
        if (layer.getCell(x, y) != null) {
          // Spawn Checkpoint
          CheckpointGameObject checkpoint =
              new CheckpointGameObject(x * tilePixelWidth, y * tilePixelWidth, 125, 125);
          checkpoint.client = this.client;
          checkpoint.checkpointID = ci;
          this.objs.addObject(checkpoint);
          checkpointsNeeded++;
          ci++;
        }
      }
    }

    this.client.start();

    // For consistency, the classes to be sent over the network are registered by the same method
    // for both the client and server
    TurirunNetwork.register(client);

    client.addListener(
        new ThreadedListener(
            new Listener() {
              public void connected(Connection connection) {}

              public void received(Connection connection, Object object) {
                WorldController.events.add(object);
              }

              public void disconnected(Connection connection) {}
            }));

    try {
      // Block for max. 3000ms // 172.18.12.25
      client.connect(3000, this.game.host, this.game.port, TurirunNetwork.udpPort);
      // Server communication after connection can go here, or in Listener#connected()
    } catch (IOException e) {
      Gdx.app.error("Could not connect to server", e.getMessage());

      // Create local player as fallback
      TouriCharacterObject playerObj = new TouriCharacterObject(10, 10);
      playerObj.setNick(game.nickname);
      objs.addObject(playerObj);
      controller.setPlayerObj(playerObj);
    }

    Register register = new Register();

    register.nick = this.game.nickname;
    register.type = 0;

    client.sendTCP(register);
  }
Ejemplo n.º 2
0
  private void updateEvents() {
    ArrayList<Object> del = new ArrayList<Object>();

    synchronized (WorldController.events) {
      for (Object event : WorldController.events) {
        // Add Player
        if (event instanceof AddCharacter) {
          AddCharacter msg = (AddCharacter) event;
          CharacterObject newPlayer;

          if (msg.character.type == 1) {
            newPlayer = new TouriCharacterObject(msg.character.x, msg.character.y);
          } else {
            newPlayer = new KannibaleCharacterObject(msg.character.x, msg.character.y);
          }

          newPlayer.setNick(msg.character.nick);
          objs.addObject(msg.character.id, newPlayer);

          // System.out.println("New Character #"+msg.character.id);

          del.add(event);
          continue;
        }
        if (event instanceof AssignCharacter) {
          AssignCharacter msg = (AssignCharacter) event;

          CharacterObject playerObj;

          if (msg.type == 1) {
            Vector2 vec = getRandomPosition();
            playerObj = new TouriCharacterObject(vec.x, vec.y);
          } else {
            Vector2 vec = getRandomPosition();
            playerObj = new KannibaleCharacterObject(vec.x, vec.y);
          }

          playerObj.setNick(game.nickname);
          objs.addObject(msg.id, playerObj);
          controller.setPlayerObj(playerObj);

          // System.out.println("Set owen Character");

          del.add(event);
          continue;
        }
        // Update Player
        if (event instanceof UpdateCharacter) {
          UpdateCharacter msg = (UpdateCharacter) event;
          CharacterObject player = (CharacterObject) objs.getObject(msg.id);
          if (player != null) {
            player.currentPosition = new Vector2(msg.x, msg.y);
            player.direction = Direction.values()[msg.direction];
            player.idle = msg.idle;
          }
          del.add(event);
          continue;
        }
        // Remove Player
        if (event instanceof RemoveCharacter) {
          RemoveCharacter msg = (RemoveCharacter) event;
          CharacterObject player = (CharacterObject) objs.getObject(msg.id);
          if (player != null) {
            objs.removeObject(player);
            // System.out.println("Remove Player #"+msg.id);
          }
          del.add(event);
          continue;
        }
        // HitCharacter
        if (event instanceof HitCharacter) {
          HitCharacter msg = (HitCharacter) event;
          CharacterObject player = (CharacterObject) objs.getObject(msg.id);
          if (player != null) {
            // System.out.println("Player HIT "+msg.id);
          }
          del.add(event);
          continue;
        }
        // Dead Character
        if (event instanceof DeadCharacter) {
          DeadCharacter msg = (DeadCharacter) event;
          CharacterObject player = (CharacterObject) objs.getObject(msg.id);
          if (player != null) {
            player.isDead = true;
          }
          del.add(event);
          continue;
        }
        // Checkpoint Checked
        if (event instanceof CheckpointCheck) {
          CheckpointCheck msg = (CheckpointCheck) event;

          for (GameObject obj : objs.getObjects()) {
            if (obj instanceof CheckpointGameObject) {
              CheckpointGameObject cObj = (CheckpointGameObject) obj;
              if (cObj.checkpointID == obj.id) {
                cObj.checked = true;
              }
            }
          }
        }
      }
    }

    for (Object event : del) {
      WorldController.events.remove(event);
    }
  }