Libgdx碰撞系统的方式不同

时间:2014-07-22 12:34:47

标签: java libgdx collision

我正在利用LibGdx进行我的第一场比赛并且进展顺利。至少我认为是。但我想实现一个更好的碰撞系统......我目前的碰撞看起来像这样:

       List<Rectangle> bounds= new ArrayList<Rectangle>();

       for (int i = 0; i < 250; i++) 
       {
            for (int j = 0; j < 250; j++) 
            {
                TiledMapTileLayer cur = (TiledMapTileLayer) map.getLayers().get(1);
                Cell cell3 = new Cell();

                if (cur.getCell(i, j) != null) 
                {
                    cell3 = cur.getCell(i, j);
                    System.out.println("Found a chest detector at: " + i + ", " + j
                            + ", " + cell3.getTile().getId());
                    bounds.add(new Rectangle(i * 64, j * 64, 64, 64));
                }
            }
        }


// CHECKING FOR TREES
        for (int i = 0; i < bounds.size(); i++) 
        {
            if (bounds.get(i).overlaps(player.getBounds())) 
            {
                int x = (int) bounds.get(i).x / 64;
                int y = (int) bounds.get(i).y / 64;

                TiledMapTileLayer cur = (TiledMapTileLayer) map.getLayers()
                        .get(1);
                Cell cell = cur.getCell(x, y);

                if (cell.getTile().getProperties().containsKey("Green")) 
                {
                    if (Gdx.input.isKeyPressed(Keys.SPACE)) 
                    {
                        System.out.println("You've cut down a tree!");
                    }
                }
                if (cell.getTile().getProperties().containsKey("Gray")) 
                {
                    player.reAdjust();
                }
            }
        }

正如你所看到的那样,当玩家OVERLAPS的树有一个属性为&#34; Gray&#34;调用名为reAdjust的方法;该方法在播放器类中:

首先我定义一个String移动并将其设置为&#34;&#34 ;;

String movement = "";

之后我添加了移动代码:

        if (Gdx.input.isKeyPressed(Keys.W)) 
        {
            if (curEnergy > 0) 
            {
                if (Gdx.input.isKeyPressed(Keys.SHIFT_LEFT)) 
                {
                    position.y += 2.5f;
                    movement = "up";
                    curEnergy--;

                    currentFrame = animation.getKeyFrame(12 + stateTime);
                }
            }
            else 
            {
                allowRegen = true;
                checkIfEnergyIsZero();
            }
            position.y += 2f;
            movement = "up";
            currentFrame = animation.getKeyFrame(12 + stateTime);
        }

        if (Gdx.input.isKeyPressed(Keys.A)) 
        {
            if (curEnergy > 0) 
            {
                if (Gdx.input.isKeyPressed(Keys.SHIFT_LEFT)) 
                {
                    position.x -= 2.5f;
                    movement = "left";
                    curEnergy--;
                    currentFrame = animation.getKeyFrame(4 + stateTime);
                }
            } 
            else 
            {
                allowRegen = true;
                checkIfEnergyIsZero();
            }
            position.x -= 2f;
            movement = "left";
            currentFrame = animation.getKeyFrame(4 + stateTime);
        }

        if (Gdx.input.isKeyPressed(Keys.D)) 
        {
            if (curEnergy > 0) 
            {
                if (Gdx.input.isKeyPressed(Keys.SHIFT_LEFT)) 
                {
                    position.x += 2.5f;
                    movement = "right";
                    curEnergy--;
                    currentFrame = animation.getKeyFrame(8 + stateTime);
                }
            } 
            else 
            {
                allowRegen = true;
                checkIfEnergyIsZero();
            }
            position.x += 2f;
            movement = "right";
            currentFrame = animation.getKeyFrame(8 + stateTime);
        }

        if (Gdx.input.isKeyPressed(Keys.S)) 
        {
            if (curEnergy > 0) 
            {
                if (Gdx.input.isKeyPressed(Keys.SHIFT_LEFT)) 
                {
                    position.y -= 2.5f;
                    movement = "down";
                    curEnergy--;
                    currentFrame = animation.getKeyFrame(0 + stateTime);
                }
            } 
            else 
            {
                allowRegen = true;
                checkIfEnergyIsZero();
            }
            position.y -= 2f;
            movement = "down";
            currentFrame = animation.getKeyFrame(0 + stateTime);
        }
    }

请忽略curEnergy的行,这只是我正在做的事情...... 最后,我制作了reAdjust方法:

public void reAdjust() 
{
    if (movement == "up") {
        position.y -= 2f;
    }
    if (movement == "down") {
        position.y += 2f;
    }
    if (movement == "right") {
        position.x -= 2f;
    }
    if (movement == "left") {
        position.x += 2f;
    }
}

现在,我的问题是,是否有更好的碰撞方式?我真的不喜欢这个...... 如果您需要代码的任何其他部分或者我忘记了什么,请告诉我。 非常感谢你!

编辑: 这是我按下2个移动按钮时移动到墙上的图片: http://i1252.photobucket.com/albums/hh576/mateo72354/bugExample_zps72892da0.png

0 个答案:

没有答案