靠近地图边缘时相对于玩家绘制的物品



我有一个磁贴引擎,一切都在膨胀,我的玩家四处走动都很好,我正在努力添加物品,玩家总是在屏幕的中心,直到他接近世界的边缘,然后他开始接近边缘。

当我在世界中绘制物品时,它们画得很好,除非玩家离开中心(在世界的边缘)。我只是无法解决这个问题。

public static void Draw(SpriteBatch spriteBatch, World w, Item i, Player p, Point screenDimensions)
    {
        bool IsOnScreen = true;
        float leftX = p.X - ((screenDimensions.X / 32) / 2);
        float rightX = leftX + (screenDimensions.X / 32);
        float topY = p.Y - ((screenDimensions.Y / 32) / 2);
        float bottomY = topY + (screenDimensions.Y / 32);
        if (i.x < leftX || i.x > rightX || i.y < topY || i.y > bottomY)
            IsOnScreen = false;
        if (IsOnScreen)
            i.animation.Draw(spriteBatch, (int)Math.Floor((i.x - leftX) * 32), (int)Math.Floor((i.y - topY) * 32));
    }

它漂亮的自我解释,世界被传入以获取尺寸(w.worldDimensions.x表示宽度,.y表示高度),该项目用于获取i.xi.y(在游戏世界中的位置,而不是在屏幕上),玩家用于绘制它相对(包含.x.y用于位置),然后是屏幕尺寸。

好吧,

对我来说看起来不是很清楚。您使用的是相机类吗?如果您使用相机类并使用它来导航您的世界,则这永远不会发生。

这是我目前用于我的项目的基本方法。

class Camera
{
    float zoom;
    public float Rotation { get; private set; }
    public Vector2 Position { get; private set; }
    Matrix transform;
    int velocity = 60;
    UserInput input;
    public float Zoom
    {
        get { return zoom; }
        set { zoom = value; if (zoom < 0.1f) zoom = 0.1f; } // Negative zoom will flip image
    }
    public Camera(UserInput input)
    {
        zoom = 1.0f;
        Rotation = 0f;
        Position = new Vector2(0, 0);
        this.input = input;
    }
    public void MoveCam()
    {
        if (input.IsKeyHold(Keys.Up))
        {
            Position += new Vector2(0, -velocity);
        }
        if (input.IsKeyHold(Keys.Left))
        {
            Position += new Vector2(-velocity, 0);
        }
        if (input.IsKeyHold(Keys.Down))
        {
            Position += new Vector2(0, velocity);
        }
        if (input.IsKeyHold(Keys.Right))
        {
            Position += new Vector2(velocity, 0);
        }
        if (input.IsKeyHold(Keys.OemMinus))
        {
            Zoom -= .01f * Zoom;
        }
        else if (input.IsKeyHold(Keys.OemPlus))
        {
            Zoom += .01f * Zoom;
        }
    }
    public void FollowCam(int xPos, int yPos)
    {
        Position = new Vector2(xPos * TileData.Width, yPos * TileData.Height);
    }

    public Matrix TransformMatrix(GraphicsDevice device)
    {
        transform = Matrix.CreateTranslation(new Vector3(-Position.X, -Position.Y, 0)) *
            Matrix.CreateRotationX(MathHelper.ToRadians(Rotation)) *
            Matrix.CreateRotationY(MathHelper.ToRadians(Rotation)) *
            Matrix.CreateRotationZ(MathHelper.ToRadians(Rotation)) *
            Matrix.CreateScale(new Vector3(zoom, zoom, 0)) *
            Matrix.CreateTranslation(new Vector3(device.Viewport.Width * 0.5f, device.Viewport.Height * 0.5f, 0));
        return transform;
    }
}

只需像在 main 中一样实例化类并在您的 draw 方法中使用它。

batch.Begin(SpriteSortMode.Immediate, BlendState.AlphaBlend, null, null, null, null, camera.TransformMatrix(graphicsDevice));
batch.End()

在这个精灵批次中绘制你世界中的所有内容,并使用新的基本元素来绘制屏幕坐标,如 gui/hud。您可以使用相机移动方法手动移动它,并使用锁定将其锁定在任何位置(如果更新,则会跟随)。

如果您有大型地图,则可能只想渲染必要的切片。我在地图类中是这样做的:

public void Draw(SpriteBatch batch, Vector2 camPosition, float camZoom, GraphicsDevice device)
    {
        float top = (camPosition.Y / TileData.Height) - ((device.Viewport.Height / 2) / TileData.Height + 1) / camZoom;
        float bottom = (camPosition.Y / TileData.Height) + ((device.Viewport.Height / 2) / TileData.Height + 2) / camZoom;
        float left = (camPosition.X / TileData.Width) - ((device.Viewport.Width / 2) / TileData.Width + 1) / camZoom;
        float right = (camPosition.X / TileData.Width) + ((device.Viewport.Width / 2) / TileData.Width + 2) / camZoom;
        for (int y = (int)top; y < (int)bottom; y++)
        {
            for (int x = (int)left; x < (int)right; x++)
            {
                if (y >= 0 && y < map.GetLength(1) && x >= 0 && x < map.GetLength(0))
                {
                    batch.Draw(map[x, y].texture, new Rectangle(x * TileData.Width, y * TileData.Height, TileData.Width, TileData.Height), Color.White);
                }
            }
        }
    }

在这里,我首先弄清楚从每个方向绘制哪些图块。请注意 camZoom,您希望在缩小时绘制更多图块。然后我在 for 循环中使用这些"边界",if 语句确保我没有访问不存在的磁贴(越界)。

最新更新