예제 #1
0
파일: Tilemap.java 프로젝트: ZeroPage/CJG
  void checkNutrition(int x, int y) {
    MapProperties curPro = mapLayer.getCell(x, y).getTile().getProperties();

    if (curPro.containsKey("blocked")) {
      int curNutri = Integer.parseInt((String) curPro.get("nutrient"));
      System.out.println(curNutri);
      if (curNutri < 3) {
        mapLayer.setCell(x, y, blockLayer.getCell(curNutri + 5, 0));
      }
    }
  }
예제 #2
0
파일: Tilemap.java 프로젝트: ZeroPage/CJG
  boolean checkDigged(int x, int y) {
    MapProperties curPro = mapLayer.getCell(x, y).getTile().getProperties();

    if (x < 0 || x > 29 || y < 0 || y > 26) {
      System.out.println("out of map");
      return false;
    }

    if (curPro.containsKey("unblocked")) return true;
    else {
      System.out.println(x + ", " + y + " is blocked");
      return false;
    }
  }
 public boolean isTileCollidable(int column, int row) {
   // System.out.println(column + "," + row + " testing");
   Cell tileCell = metaLayer.getCell(column, row);
   if (tileCell != null) {
     TiledMapTile tile = tileCell.getTile();
     MapProperties properties = tile.getProperties();
     if (properties.containsKey("collidable")) {
       Object value = properties.get("collidable");
       if (value.toString().compareTo("1") == 0) {
         return true;
       }
     }
   }
   return false;
 }
 public boolean loadTiledMap(TiledMap map) throws STiledLevelException {
   this.map = map;
   levelRenderer = new OrthogonalTiledMapRenderer(this.map, 1);
   MapProperties mapProperties = this.map.getProperties();
   Iterator<String> keyIterator = mapProperties.getKeys();
   while (keyIterator.hasNext()) {
     System.out.println(keyIterator.next());
   }
   _loadSpecialTilesRoutine(this.map);
   _loadLayersRoutine(this.map);
   _loadObjectsRoutine(objectLayer);
   loaded = true;
   if (listener != null) {
     listener.tiledMapLoaded(this);
   }
   return getLoaded();
 }
예제 #5
0
파일: Tilemap.java 프로젝트: ZeroPage/CJG
  boolean digging(int x, int y) {
    MapProperties curPro = mapLayer.getCell(x, y).getTile().getProperties();

    if (curPro.containsKey("ground")) {
      return false;
    } else {
      if (curPro.containsKey("blocked")) {
        if (checkDigged(x - 1, y)
            || checkDigged(x + 1, y)
            || checkDigged(x, y - 1)
            || checkDigged(x, y + 1)) {
          mapLayer.setCell(x, y, blockLayer.getCell(3, 0));
          return true;
        } else return false;
      } else return false;
    }
  }
예제 #6
0
  private void loadProperties(MapProperties properties, Element element) {
    if (element.getName().equals("Properties")) {
      for (Element property : element.getChildrenByName("Property")) {
        String key = property.getAttribute("Key", null);
        String type = property.getAttribute("Type", null);
        String value = property.getText();

        if (type.equals("Int32")) {
          properties.put(key, Integer.parseInt(value));
        } else if (type.equals("String")) {
          properties.put(key, value);
        } else if (type.equals("Boolean")) {
          properties.put(key, value.equalsIgnoreCase("true"));
        } else {
          properties.put(key, value);
        }
      }
    }
  }
예제 #7
0
 protected void loadProperties(MapProperties properties, Element element) {
   if (element.getName().equals("properties")) {
     for (Element property : element.getChildrenByName("property")) {
       String name = property.getAttribute("name", null);
       String value = property.getAttribute("value", null);
       if (value == null) {
         value = property.getText();
       }
       properties.put(name, value);
     }
   }
 }
예제 #8
0
  protected TiledMap loadMap(
      Element root,
      FileHandle tmxFile,
      AtlasResolver resolver,
      AtlasTiledMapLoaderParameters parameter) {
    TiledMap map = new TiledMap();

    String mapOrientation = root.getAttribute("orientation", null);
    int mapWidth = root.getIntAttribute("width", 0);
    int mapHeight = root.getIntAttribute("height", 0);
    int tileWidth = root.getIntAttribute("tilewidth", 0);
    int tileHeight = root.getIntAttribute("tileheight", 0);
    String mapBackgroundColor = root.getAttribute("backgroundcolor", null);

    MapProperties mapProperties = map.getProperties();
    if (mapOrientation != null) {
      mapProperties.put("orientation", mapOrientation);
    }
    mapProperties.put("width", mapWidth);
    mapProperties.put("height", mapHeight);
    mapProperties.put("tilewidth", tileWidth);
    mapProperties.put("tileheight", tileHeight);
    if (mapBackgroundColor != null) {
      mapProperties.put("backgroundcolor", mapBackgroundColor);
    }
    mapWidthInPixels = mapWidth * tileWidth;
    mapHeightInPixels = mapHeight * tileHeight;

    for (int i = 0, j = root.getChildCount(); i < j; i++) {
      Element element = root.getChild(i);
      String elementName = element.getName();
      if (elementName.equals("properties")) {
        loadProperties(map.getProperties(), element);
      } else if (elementName.equals("tileset")) {
        loadTileset(map, element, tmxFile, resolver, parameter);
      } else if (elementName.equals("layer")) {
        loadTileLayer(map, element);
      } else if (elementName.equals("objectgroup")) {
        loadObjectGroup(map, element);
      }
    }
    return map;
  }
예제 #9
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);
  }
예제 #10
0
  public void Create() {
    // set tilemap file name
    m_MapFileName = "map/test.tmx";

    // set up the tilemap
    m_TiledMap = new TmxMapLoader().load(m_MapFileName);
    m_TiledMapRenderer = new OrthogonalTiledMapRenderer(m_TiledMap, 1f);

    // set up the box2d physics
    m_PhysicsWorld = new com.badlogic.gdx.physics.box2d.World(new Vector2(0, 0), true);

    // attach contact listener to physics world
    m_PhysicsWorld.setContactListener(new GameContactListener(this));

    // set up box2dlights
    m_RayHandler = new RayHandler(m_PhysicsWorld);
    m_RayHandler.setAmbientLight(0.0f);

    // add tilemap collision boxes to physics world
    for (int i = 0; i < m_TiledMap.getLayers().getCount(); i++) {
      if (m_TiledMap.getLayers().get(i) instanceof TiledMapTileLayer) {
        TiledMapTileLayer layer = (TiledMapTileLayer) m_TiledMap.getLayers().get(i);
        for (int j = 0; j < layer.getWidth(); j++) {
          for (int k = 0; k < layer.getHeight(); k++) {
            Cell cell = layer.getCell(j, k);
            if (cell != null) {
              TiledMapTile tile = cell.getTile();
              if (tile != null) {
                MapProperties props = tile.getProperties();
                if (props.containsKey("blocked")) {
                  float worldX = j * 64 + 32;
                  float worldY = k * 64 + 32;
                  BodyDef bodyDef = new BodyDef();
                  bodyDef.type = BodyType.StaticBody;
                  bodyDef.position.set(new Vector2(worldX * WORLD_TO_BOX, worldY * WORLD_TO_BOX));

                  Body newBody = m_PhysicsWorld.createBody(bodyDef);

                  PolygonShape boxShape = new PolygonShape();
                  boxShape.setAsBox(32 * WORLD_TO_BOX, 32 * WORLD_TO_BOX);

                  // Create a fixture definition to apply our shape to
                  FixtureDef fixtureDef = new FixtureDef();
                  fixtureDef.shape = boxShape;

                  newBody.createFixture(fixtureDef);

                  boxShape.dispose();
                }

                if (props.containsKey("rotate")) {
                  // flip some random tiles to break pattern
                  if (k * j % 3 == 0) layer.getCell(j, k).setFlipVertically(true);
                  if (k * j % 2 == 0) layer.getCell(j, k).setFlipHorizontally(true);
                }
              }
            }
          }
        }
      }
    }

    // set up artemis entity system
    m_ArtemisWorld = new com.artemis.World();

    // add the passive systems
    m_SpineRenderSystem = new SpineRenderSystem(m_GameScreen);
    m_ArtemisWorld.setSystem(m_SpineRenderSystem, true);

    // add the systems
    m_ArtemisWorld.setSystem(new PlayerInputSystem(this));
    m_ArtemisWorld.setSystem(new CameraSystem(m_GameScreen.m_Camera));
    m_ArtemisWorld.setSystem(new PhysicsSystem());
    m_ArtemisWorld.setSystem(new LifecycleSystem(this));
    m_ArtemisWorld.setSystem(new SteeringSystem());

    // init the world
    m_ArtemisWorld.initialize();
    EntityHelper.LoadObjects(
        m_GameScreen, m_ArtemisWorld, m_TiledMap, m_PhysicsWorld, m_RayHandler);

    // add player to entity list
    m_PlayerEntity = new PlayerEntity();
    m_PlayerEntity.AddToWorld(this);
    checkpointPos = new Vector2(0, 0);
    checkpointPos.x = m_PlayerEntity.m_Entity.getComponent(PositionComponent.class).m_X;
    checkpointPos.y = m_PlayerEntity.m_Entity.getComponent(PositionComponent.class).m_Y;

    // hud items are special cases since they needs to draw on top of border
    m_VialOne = new SpineComponent("map/vial", 5, 0.75f, -25);
    m_VialTwo = new SpineComponent("map/vial", 5, 0.65f, -5);
    m_VialThree = new SpineComponent("map/vial", 5, 0.75f, -50);
    m_WatchSpine = new SpineComponent("map/watch", 4, 0.75f, 0);

    if (m_GameScreen.m_Settings.m_WatchFound) {
      ActivateWatch();
      BodyComponent bodyComponent = m_PlayerEntity.m_Entity.getComponent(BodyComponent.class);
      bodyComponent.m_Body.setTransform(621 * WORLD_TO_BOX, 5961 * WORLD_TO_BOX, 0);
    }
    if (m_GameScreen.m_Settings.m_WeaponFound) {
      ActivateSword();
      BodyComponent bodyComponent = m_PlayerEntity.m_Entity.getComponent(BodyComponent.class);
      bodyComponent.m_Body.setTransform(1965 * WORLD_TO_BOX, 4842 * WORLD_TO_BOX, 0);
    }
    if (m_GameScreen.m_Settings.m_WeaponLightFound) {
      ActivateSwordLight();
      BodyComponent bodyComponent = m_PlayerEntity.m_Entity.getComponent(BodyComponent.class);
      bodyComponent.m_Body.setTransform(6390 * WORLD_TO_BOX, 6462 * WORLD_TO_BOX, 0);
    }
  }
예제 #11
0
  protected void loadTileset(
      TiledMap map,
      Element element,
      FileHandle tmxFile,
      AtlasResolver resolver,
      AtlasTiledMapLoaderParameters parameter) {
    if (element.getName().equals("tileset")) {
      String name = element.get("name", null);
      int firstgid = element.getIntAttribute("firstgid", 1);
      int tilewidth = element.getIntAttribute("tilewidth", 0);
      int tileheight = element.getIntAttribute("tileheight", 0);
      int spacing = element.getIntAttribute("spacing", 0);
      int margin = element.getIntAttribute("margin", 0);
      String source = element.getAttribute("source", null);

      String imageSource = "";
      int imageWidth = 0, imageHeight = 0;

      FileHandle image = null;
      if (source != null) {
        FileHandle tsx = getRelativeFileHandle(tmxFile, source);
        try {
          element = xml.parse(tsx);
          name = element.get("name", null);
          tilewidth = element.getIntAttribute("tilewidth", 0);
          tileheight = element.getIntAttribute("tileheight", 0);
          spacing = element.getIntAttribute("spacing", 0);
          margin = element.getIntAttribute("margin", 0);
          imageSource = element.getChildByName("image").getAttribute("source");
          imageWidth = element.getChildByName("image").getIntAttribute("width", 0);
          imageHeight = element.getChildByName("image").getIntAttribute("height", 0);
        } catch (IOException e) {
          throw new GdxRuntimeException("Error parsing external tileset.");
        }
      } else {
        imageSource = element.getChildByName("image").getAttribute("source");
        imageWidth = element.getChildByName("image").getIntAttribute("width", 0);
        imageHeight = element.getChildByName("image").getIntAttribute("height", 0);
      }

      // get the TextureAtlas for this tileset
      TextureAtlas atlas = null;
      String regionsName = "";
      if (map.getProperties().containsKey("atlas")) {
        FileHandle atlasHandle =
            getRelativeFileHandle(tmxFile, map.getProperties().get("atlas", String.class));
        atlasHandle = resolve(atlasHandle.path());
        atlas = resolver.getAtlas(atlasHandle.path());
        regionsName = atlasHandle.nameWithoutExtension();

        if (parameter != null && parameter.forceTextureFilters) {
          for (Texture texture : atlas.getTextures()) {
            trackedTextures.add(texture);
          }
        }
      }

      TiledMapTileSet tileset = new TiledMapTileSet();
      MapProperties props = tileset.getProperties();
      tileset.setName(name);
      props.put("firstgid", firstgid);
      props.put("imagesource", imageSource);
      props.put("imagewidth", imageWidth);
      props.put("imageheight", imageHeight);
      props.put("tilewidth", tilewidth);
      props.put("tileheight", tileheight);
      props.put("margin", margin);
      props.put("spacing", spacing);

      Array<AtlasRegion> regions = atlas.findRegions(regionsName);
      for (AtlasRegion region : regions) {
        // handle unused tile ids
        if (region != null) {
          StaticTiledMapTile tile = new StaticTiledMapTile(region);

          if (!yUp) {
            region.flip(false, true);
          }

          int tileid = firstgid + region.index;
          tile.setId(tileid);
          tileset.putTile(tileid, tile);
        }
      }

      Array<Element> tileElements = element.getChildrenByName("tile");

      for (Element tileElement : tileElements) {
        int localtid = tileElement.getIntAttribute("id", 0);
        TiledMapTile tile = tileset.getTile(firstgid + localtid);
        if (tile != null) {
          String terrain = tileElement.getAttribute("terrain", null);
          if (terrain != null) {
            tile.getProperties().put("terrain", terrain);
          }
          String probability = tileElement.getAttribute("probability", null);
          if (probability != null) {
            tile.getProperties().put("probability", probability);
          }
          Element properties = tileElement.getChildByName("properties");
          if (properties != null) {
            loadProperties(tile.getProperties(), properties);
          }
        }
      }

      Element properties = element.getChildByName("properties");
      if (properties != null) {
        loadProperties(tileset.getProperties(), properties);
      }
      map.getTileSets().addTileSet(tileset);
    }
  }