0

私はタイルエンジンを持っていて、それはすべてうねりが働いています、私のプレーヤーはうまく歩き回っています、私はアイテムを追加することに取り組んでいます、プレーヤーは常に画面の中央にいます。端に近づく。

ワールドでアイテムを描画すると、プレイヤーが中心 (ワールドの端) を離れる場合を除いて、アイテムは正常に描画されます。これを修正する方法に頭を悩ませることはできません。

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.x含むおよび位置) と、次に screenDimensions です。i.y.x.y

4

1 に答える 1

0

まあ、それは私にはあまり明確に見えません。カメラクラスを使用していますか?カメラクラスを使用し、それを使用して世界をナビゲートする場合、これは決して起こらないはずです。

これは、現在私のプロジェクトで使用している基本的なものです。

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 ステートメントは、存在しない (境界外) タイルにアクセスしていないことを確認します。

于 2012-10-11T20:23:16.713 に答える