Example usage for com.badlogic.gdx.maps MapObjects get

List of usage examples for com.badlogic.gdx.maps MapObjects get

Introduction

In this page you can find the example usage for com.badlogic.gdx.maps MapObjects get.

Prototype

public MapObject get(String name) 

Source Link

Usage

From source file:net.lbsg.elementorum.worlds.Level.java

public Level() {
    super("Game");

    cam = new OrthographicCamera();
    // Setup camera viewport
    cam.setToOrtho(false, Gdx.graphics.getWidth() / 2, Gdx.graphics.getHeight() / 2);
    cam.update();/*from  w w w  . j a  va 2s.co  m*/

    batch = new SpriteBatch();
    // Load map
    map = new TmxMapLoader().load("maps/Lvl1.tmx");
    mapRenderer = new OrthogonalTiledMapRenderer(map);

    // Load walls as rectangles in an array
    MapObjects wallobjects = map.getLayers().get("Walls").getObjects();
    for (int i = 0; i < wallobjects.getCount(); i++) {
        RectangleMapObject obj = (RectangleMapObject) wallobjects.get(i);
        Rectangle rect = obj.getRectangle();
        walls.add(new Rectangle(rect.x, rect.y, 16, 16));
    }
    Texture[] tex = new Texture[] { new Texture("sprites/Player_Side_Left.png"),
            new Texture("sprites/Player_Side_Right.png"), new Texture("sprites/Player_Behind_1.png"),
            new Texture("sprites/Player_Behind_2.png"), new Texture("sprites/Player_Forward1.png"),
            new Texture("sprites/Player.png") };
    player = new Player("sprites/Player.png", 120, 120, walls, tex);
    wall = new Texture("tiles/Wall.png");
}

From source file:us.thirdmillenium.strategicassaultsimulator.agents.ConeAgent.java

License:Apache License

/**
 * Need to check that the Agent doesn't run through walls or off the map.
 *
 * @param newPosition/*from w  ww  . jav a2 s. c  om*/
 */
private Vector2 boundaryCheckNewPosition(Vector2 newPosition) {
    Rectangle boundRect = this.sprite.getBoundingRectangle();
    Polygon boundPoly = GraphicsHelpers.convertRectangleToPolygon(boundRect);

    /*
     *  Check World Map Boundaries First, just fudge back in if needed
     */

    if (boundRect.x < 0) {
        newPosition.x += Math.abs((int) boundRect.x);
        this.rotation += 45 - (random.nextFloat() * 90);
    } else if (boundRect.x > (Params.MapTileSize * Params.NumCellsX) - (Params.AgentTileSize)) {
        newPosition.x = (float) ((Params.MapTileSize * Params.NumCellsX) - (Params.AgentTileSize));
        this.rotation += 45 - (random.nextFloat() * 90);
    }

    if (boundRect.y < 0) {
        newPosition.y += Math.abs((int) boundRect.y);
        this.rotation += 180 - (5 - (random.nextFloat() * 10));
    } else if (boundRect.y > (Params.MapTileSize * Params.NumCellsY) - (Params.AgentTileSize)) {
        newPosition.y = (float) ((Params.MapTileSize * Params.NumCellsY) - (Params.AgentTileSize));
        this.rotation += 180 - (5 - (random.nextFloat() * 10));
    }

    /*
     *  Check Walls Second
     */
    MapObjects wallMapObjects = this.gameMap.getLayers().get(2).getObjects();

    for (int i = 0; i < wallMapObjects.getCount(); i++) {
        Object rectangleMapObject = wallMapObjects.get(i);
        Intersector.MinimumTranslationVector mtv = new Intersector.MinimumTranslationVector();

        // Make sure this is a Rectangle from Tiled describing a wall.
        if (rectangleMapObject.getClass() == RectangleMapObject.class) {
            Rectangle wallRectangle = ((RectangleMapObject) rectangleMapObject).getRectangle();
            Polygon polyBound = GraphicsHelpers.convertRectangleToPolygon(wallRectangle);

            // If hitting a wall, kick back to be off wall
            if (Intersector.overlapConvexPolygons(boundPoly, polyBound, mtv)) {
                newPosition.x += mtv.depth * mtv.normal.x;
                newPosition.y += mtv.depth * mtv.normal.y;
            }
        }
    }

    /*
     *  Check against other Agents
     */
    Rectangle intersection = new Rectangle();

    // Check Enemy Agents
    Iterator<AgentModel> enemyItr = this.enemyTracker.iterator();
    AgentModel enemy;

    while (enemyItr.hasNext()) {
        enemy = enemyItr.next();

        // If bounding rectangles overlap, shuffle Agent in X to keep from overlapping
        if (Intersector.intersectRectangles(boundRect, enemy.getBoundingRectangle(), intersection)) {
            if (boundRect.x >= intersection.x) {
                newPosition.x = intersection.x + Params.AgentTileSize;
            } else {
                newPosition.x = intersection.x - Params.AgentTileSize;
            }
        }
    }

    // Check Friendly Agents
    Iterator<AgentModel> teamItr = this.teamTracker.iterator();
    AgentModel team;

    while (teamItr.hasNext()) {
        team = teamItr.next();

        // If bounding rectangles overlap, shuffle Agent
        if (team != this
                && Intersector.intersectRectangles(boundRect, team.getBoundingRectangle(), intersection)) {
            if (boundRect.x >= intersection.x) {
                newPosition.x = intersection.x + Params.AgentTileSize;
                this.rotation += 10 - (random.nextFloat() * 20);
            } else {
                newPosition.x = intersection.x - Params.AgentTileSize;
                this.rotation += 10 - (random.nextFloat() * 20);
            }
        }
    }

    return newPosition;
}

From source file:us.thirdmillenium.strategicassaultsimulator.agents.ConePuppetAgent.java

License:Apache License

/**
 * Need to check that the Agent doesn't run through walls or off the map.
 *
 * @param newPosition//from  w  w  w .ja v a  2 s.  c om
 */
private Vector2 boundaryCheckNewPosition(Vector2 newPosition) {
    Rectangle boundRect = this.sprite.getBoundingRectangle();
    Polygon boundPoly = GraphicsHelpers.convertRectangleToPolygon(boundRect);

    /*
     *  Check World Map Boundaries First, just fudge back in if needed
     */
    float bounceAngle = 150;

    if (boundRect.x < 0) {
        newPosition.x += Math.abs((int) boundRect.x);
        this.rotation += bounceAngle - (random.nextFloat() * bounceAngle * 2);
    } else if (boundRect.x > (Params.MapTileSize * Params.NumCellsX) - (Params.AgentTileSize)) {
        newPosition.x = (float) ((Params.MapTileSize * Params.NumCellsX) - (Params.AgentTileSize));
        this.rotation += bounceAngle - (random.nextFloat() * bounceAngle * 2);
    }

    if (boundRect.y < 0) {
        newPosition.y += Math.abs((int) boundRect.y);
        this.rotation += bounceAngle - (random.nextFloat() * bounceAngle * 2);
    } else if (boundRect.y > (Params.MapTileSize * Params.NumCellsY) - (Params.AgentTileSize)) {
        newPosition.y = (float) ((Params.MapTileSize * Params.NumCellsY) - (Params.AgentTileSize));
        this.rotation += bounceAngle - (random.nextFloat() * bounceAngle * 2);
    }

    /*
     *  Check Walls Second
     */
    MapObjects wallMapObjects = this.gameMap.getLayers().get(2).getObjects();

    for (int i = 0; i < wallMapObjects.getCount(); i++) {
        Object rectangleMapObject = wallMapObjects.get(i);
        Intersector.MinimumTranslationVector mtv = new Intersector.MinimumTranslationVector();

        // Make sure this is a Rectangle from Tiled describing a wall.
        if (rectangleMapObject.getClass() == RectangleMapObject.class) {
            Rectangle wallRectangle = ((RectangleMapObject) rectangleMapObject).getRectangle();
            Polygon polyBound = GraphicsHelpers.convertRectangleToPolygon(wallRectangle);

            // If hitting a wall, kick back to be off wall
            if (Intersector.overlapConvexPolygons(boundPoly, polyBound, mtv)) {
                newPosition.x += mtv.depth * mtv.normal.x;
                newPosition.y += mtv.depth * mtv.normal.y;

                this.rotation += bounceAngle - (random.nextFloat() * bounceAngle * 2);
            }
        }
    }

    /*
     *  Check against other Agents
     */
    Rectangle intersection = new Rectangle();

    // Check Enemy Agents
    Iterator<AgentModel> enemyItr = this.enemyTracker.iterator();
    AgentModel enemy;

    while (enemyItr.hasNext()) {
        enemy = enemyItr.next();

        // If bounding rectangles overlap, shuffle Agent in X to keep from overlapping
        if (Intersector.intersectRectangles(boundRect, enemy.getBoundingRectangle(), intersection)) {
            if (boundRect.x >= intersection.x) {
                newPosition.x = intersection.x + Params.AgentTileSize;
            } else {
                newPosition.x = intersection.x - Params.AgentTileSize;
            }
        }
    }

    // Check Friendly Agents
    Iterator<AgentModel> teamItr = this.teamTracker.iterator();
    AgentModel team;

    while (teamItr.hasNext()) {
        team = teamItr.next();

        // If bounding rectangles overlap, shuffle Agent
        if (team != this
                && Intersector.intersectRectangles(boundRect, team.getBoundingRectangle(), intersection)) {
            if (boundRect.x >= intersection.x) {
                newPosition.x = intersection.x + Params.AgentTileSize;
            } else {
                newPosition.x = intersection.x - Params.AgentTileSize;
            }

            this.rotation += 45 - (random.nextFloat() * 90);
        }
    }

    return newPosition;
}

From source file:us.thirdmillenium.strategicassaultsimulator.agents.TrainingAgent.java

License:Apache License

/**
 * Need to check that the Agent doesn't run through walls or off the map.
 *
 * @param newPosition/*from   w  w w  .  j a v a  2s  .  c om*/
 */
private Vector2 boundaryCheckNewPosition(Vector2 newPosition) {
    Rectangle boundRect = this.sprite.getBoundingRectangle();

    // Check World Map Boundaries First, just fudge back in if needed
    if (boundRect.x < 0) {
        newPosition.x += Math.abs((int) boundRect.x);
    } else if (boundRect.x > (Params.MapTileSize * Params.NumCellsX) - (Params.AgentTileSize)) {
        newPosition.x = (float) ((Params.MapTileSize * Params.NumCellsX) - (Params.AgentTileSize));
    }

    if (boundRect.y < 0) {
        newPosition.y += Math.abs((int) boundRect.y);
    } else if (boundRect.y > (Params.MapTileSize * Params.NumCellsY) - (Params.AgentTileSize)) {
        newPosition.y = (float) ((Params.MapTileSize * Params.NumCellsY) - (Params.AgentTileSize));
    }

    // Check Walls Second
    MapObjects wallMapObjects = this.tileMap.getLayers().get(2).getObjects();
    Polygon spriteAsPoly = GraphicsHelpers.convertRectangleToPolygon(boundRect);

    for (int i = 0; i < wallMapObjects.getCount(); i++) {
        Object rectangleMapObject = wallMapObjects.get(i);
        Intersector.MinimumTranslationVector mtv = new Intersector.MinimumTranslationVector();

        // Make sure this is a Rectangle from Tiled describing a wall.
        if (rectangleMapObject.getClass() == RectangleMapObject.class) {
            Rectangle wallRectangle = ((RectangleMapObject) rectangleMapObject).getRectangle();
            Polygon polyBound = GraphicsHelpers.convertRectangleToPolygon(wallRectangle);

            // If hitting a wall, don't allow movement at all
            if (Intersector.overlapConvexPolygons(spriteAsPoly, polyBound, mtv)) {
                newPosition.x += mtv.depth * mtv.normal.x;
                newPosition.y += mtv.depth * mtv.normal.y;
            }
        }
    }

    return newPosition;
}

From source file:us.thirdmillenium.strategicassaultsimulator.environment.Environment.java

License:Apache License

/**
 * Generates a set of all lines describing collisions in the game map.
 *
 * @param gameMap/* www  . j  a  v  a2 s  .  co  m*/
 * @return
 */
public static Set<Line> createCollisionLineSet(TiledMap gameMap) {
    Set<Line> collisionSet = Collections.newSetFromMap(new ConcurrentHashMap<Line, Boolean>());

    // Add World boundaries - HARD CODED!!!
    collisionSet.add(new Line(new Vector2(0, 0), new Vector2(0, 1216)));
    collisionSet.add(new Line(new Vector2(0, 0), new Vector2(800, 0)));
    collisionSet.add(new Line(new Vector2(800, 1216), new Vector2(800, 0)));
    collisionSet.add(new Line(new Vector2(800, 1216), new Vector2(0, 1216)));

    // Add Collision Box Lines that surround walls
    MapObjects wallMapObjects = gameMap.getLayers().get(2).getObjects();
    Object rectangleMapObject;
    Rectangle collisionRect;

    for (int i = 0; i < wallMapObjects.getCount(); i++) {
        rectangleMapObject = wallMapObjects.get(i);

        // Make sure this is a Rectangle from Tiled describing a wall.
        if (rectangleMapObject.getClass() == RectangleMapObject.class) {
            collisionRect = ((RectangleMapObject) rectangleMapObject).getRectangle();

            collisionSet.add(new Line(new Vector2(collisionRect.x, collisionRect.y),
                    new Vector2(collisionRect.x, collisionRect.y + collisionRect.height)));

            collisionSet.add(new Line(new Vector2(collisionRect.x, collisionRect.y),
                    new Vector2(collisionRect.x + collisionRect.width, collisionRect.y)));

            collisionSet.add(new Line(
                    new Vector2(collisionRect.x + collisionRect.width, collisionRect.y + collisionRect.height),
                    new Vector2(collisionRect.x, collisionRect.y + collisionRect.height)));

            collisionSet.add(new Line(
                    new Vector2(collisionRect.x + collisionRect.width, collisionRect.y + collisionRect.height),
                    new Vector2(collisionRect.x + collisionRect.width, collisionRect.y)));
        }
    }

    return collisionSet;
}

From source file:us.thirdmillenium.strategicassaultsimulator.environment.GameEnvironment.java

License:Apache License

@Override
public void simulate(float deltaTime) {
    // Compute time delta (max of frame speed)
    deltaTime = (float) Math.min(deltaTime, 1 / Params.FramesPerSecond);

    if (DRAW) {/*  w ww.j  a v  a 2 s.c o m*/
        // Clear Background
        Gdx.gl.glClearColor(1, 0, 0, 1);
        Gdx.gl.glBlendFunc(GL20.GL_SRC_ALPHA, GL20.GL_ONE_MINUS_SRC_ALPHA);
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

        // Draw Map
        this.Camera.update();
        this.TiledMapRenderer.setView(this.Camera);
        this.TiledMapRenderer.render();
    }

    // Test if Bullets Intersected with Anything
    MapObjects wallMapObjects = this.TiledMap.getLayers().get(2).getObjects();
    Iterator<GreenBullet> bullets = this.BulletTracker.iterator();
    this.SpriteBatchRenderer.setProjectionMatrix(this.Camera.combined);
    this.SpriteBatchRenderer.begin();

    while (bullets.hasNext()) {
        // Collect a Bullet to consider
        GreenBullet currentBullet = bullets.next();

        if (DRAW) {
            currentBullet.drawSprite(this.SpriteBatchRenderer);
        }

        currentBullet.updateBullet(deltaTime);

        // If bullet is off-screen, remove it.
        if (currentBullet.getBulletVector().x < 0 || currentBullet.getBulletVector().x > this.width
                || currentBullet.getBulletVector().y < 0 || currentBullet.getBulletVector().y > this.height) {
            this.BulletTracker.remove(currentBullet);
        } else {
            // Compare with all Agents
            Rectangle agentBound;

            Iterator<AgentModel> shootItr = this.shooters.iterator();

            while (shootItr.hasNext()) {
                AgentModel currShooter = shootItr.next();

                if (!currentBullet.thisAgentShotMe(currShooter) && Intersector.overlapConvexPolygons(
                        GraphicsHelpers.convertRectangleToPolygon(currShooter.getBoundingRectangle()),
                        currentBullet.getBulletPath())) {
                    currShooter.agentHit();
                    this.BulletTracker.remove(currentBullet);
                }
            }

            Iterator<AgentModel> agentItr = this.trainees.iterator();

            while (agentItr.hasNext()) {
                AgentModel currAgent = agentItr.next();

                if (!currentBullet.thisAgentShotMe(currAgent) && Intersector.overlapConvexPolygons(
                        GraphicsHelpers.convertRectangleToPolygon(currAgent.getBoundingRectangle()),
                        currentBullet.getBulletPath())) {
                    currAgent.agentHit();
                    this.BulletTracker.remove(currentBullet);
                }
            }

            // Compare with all Wall Boundaries
            for (int i = 0; i < wallMapObjects.getCount(); i++) {
                Object rectangleMapObject = wallMapObjects.get(i);

                // Make sure this is a Rectangle from Tiled describing a wall.
                if (rectangleMapObject.getClass() == RectangleMapObject.class) {
                    Rectangle wallRectangle = ((RectangleMapObject) rectangleMapObject).getRectangle();
                    Polygon polyBound = GraphicsHelpers.convertRectangleToPolygon(wallRectangle);

                    // Terminate when hitting a wall
                    if (Intersector.overlapConvexPolygons(polyBound, currentBullet.getBulletPath())) {
                        this.BulletTracker.remove(currentBullet);
                    }
                }
            }
        }
    }

    this.SpriteBatchRenderer.end();

    // Draw DEBUG information
    if (DEBUG && DRAW) {
        // Draw Map Nodes
        /*this.MapNodeSR.setProjectionMatrix(this.Camera.combined);
        this.MapNodeSR.setColor(Color.OLIVE);
        this.MapNodeSR.begin(ShapeRenderer.ShapeType.Filled);
                
        if (this.TraverseNodes != null) {
        for (Integer key : this.TraverseNodes.keySet()) {
            this.MapNodeSR.circle(this.TraverseNodes.get(key).getPixelX(), this.TraverseNodes.get(key).getPixelY(), 10);
        }
        }
                
        this.MapNodeSR.end();*/

        // Draw Overlay Lines
        this.LineRenderer.setProjectionMatrix(this.Camera.combined);
        this.LineRenderer.begin(ShapeRenderer.ShapeType.Filled);

        // For each Agent.  Different Colors?
        this.LineRenderer.setColor(Color.BLACK);

        /*if( PUPPET ) {
           this.puppet.drawPath(this.LineRenderer);
        }*/

        Iterator<AgentModel> agentItr = this.trainees.iterator();

        while (agentItr.hasNext()) {
            AgentModel currAgent = agentItr.next();
            currAgent.drawPath(this.LineRenderer);
        }

        this.LineRenderer.end();
    }

    // Draw Agent Sprites

    this.SpriteBatchRenderer.begin();

    /*if( PUPPET ) {
       this.puppet.updateAgent(deltaTime);
       this.puppet.drawAgent(this.SpriteBatchRenderer);
    }*/

    Iterator<AgentModel> shootItr = this.shooters.iterator();

    while (shootItr.hasNext()) {
        AgentModel currShooter = shootItr.next();

        currShooter.updateAgent(deltaTime);
        if (DRAW) {
            currShooter.drawAgent(this.SpriteBatchRenderer);
        }
    }

    Iterator<AgentModel> agentItr = this.trainees.iterator();

    while (agentItr.hasNext()) {
        AgentModel currAgent = agentItr.next();

        currAgent.updateAgent(deltaTime);

        if (DRAW) {
            currAgent.drawAgent(this.SpriteBatchRenderer);
        }
    }

    this.SpriteBatchRenderer.end();

    if (DEBUG) {
        Iterator<AgentModel> agentItr2 = this.trainees.iterator();

        ShapeRenderer visionCone = new ShapeRenderer();
        visionCone.setProjectionMatrix(this.Camera.combined);
        visionCone.begin(ShapeRenderer.ShapeType.Line);
        visionCone.setColor(Color.YELLOW);

        while (agentItr2.hasNext()) {
            AgentModel currAgent = agentItr2.next();

            currAgent.drawVision(visionCone);
        }

        visionCone.end();
    }

    /*// Test Draw the Collision Boxes
    if( DEBUG && DRAW ) {
    ShapeRenderer anotherShapeRenderer = new ShapeRenderer();
            
       anotherShapeRenderer.setProjectionMatrix(this.Camera.combined);
       anotherShapeRenderer.begin(ShapeRenderer.ShapeType.Line);
            
       bullets = this.BulletTracker.iterator();
            
       while(bullets.hasNext()) {
      GreenBullet currentBullet = bullets.next();
      anotherShapeRenderer.polygon(currentBullet.getBulletPath().getTransformedVertices());
       }
            
       for(int i = 0; i < wallMapObjects.getCount(); i++ ){
      Object obj = wallMapObjects.get(i);
            
      if( obj.getClass() == RectangleMapObject.class ) {
         Rectangle boundary = ((RectangleMapObject)obj).getRectangle();
         anotherShapeRenderer.rect(boundary.x, boundary.y, boundary.width, boundary.height);
            
         float[] vertices = {
            boundary.x, boundary.y,
            boundary.x + boundary.width, boundary.y,
            boundary.x + boundary.width, boundary.y + boundary.height,
            boundary.x, boundary.y + boundary.height
         };
            
         //Polygon polyBound = new Polygon(vertices);
         anotherShapeRenderer.setColor(Color.BLUE);
         anotherShapeRenderer.polygon(vertices);
      }
       }
            
       anotherShapeRenderer.end();
    }*/

}