Как остановить движение курсора мыши с камерой - PullRequest
0 голосов
/ 02 января 2019

когда я щелкаю по экрану мышью, мой спрайт перемещается в это положение, но когда камера перемещается, курсор мыши перемещается вместе с ней, и спрайт перемещается в неправильное положение.Как я могу это исправить, когда моя камера перемещается, моя мышь остается в правильном положении?Я попытался соответствующим образом изменить положение мыши и изменить местоположение камеры, но я все еще не могу выполнить ее

благодарности

using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;

using XELibrary;
using Comora;


namespace gameTest
{

public class PlayingState
{
    public SpriteBatch SpriteBatch { get; private set; }
    public Vector2 playerPosition = new Vector2(100, 100);
    private Vector2 velocity = new Vector2(200.0f, 0.0f);
    private DirectionType direction = DirectionType.Right;
    public Rectangle spriteRectangle;
    private int health;
    private int radius;
    private string playerKey;


    MouseState mState;
    MouseState oldmState;
    Vector2 current_mouse_pos = new Vector2(0, 0);
    Vector2 cursor_mouse_pos;
    private Camera camera;

    // BACKGROUND
    Texture2D backgroundtexture;
    Vector2 backgroundPosition;

    Texture2D mouse_Sprite;

    SpriteFont gameFont;

    IScrollingBackgroundManager scrollingBackgroundManager;
    ICelAnimationManager celAnimationManager;
    ISoundManager soundManager;

    // ---------- GETTERS AND SETTERS ----------
    public float HealthTimer { get { return healthTimer; } set { 
    healthTimer = value; } }
    public int Radius { get { return radius; } }
    public int Health { get { return health; } set { health = value; } }
    public Vector2 PlayerPosition { get { return playerPosition; } set { 
    playerPosition = value; } }
    public void SetX(float newX) { playerPosition.X = newX; }
    public void SetY(float newY) { playerPosition.Y = newY; }

    // ---------- C O N S T R U C T O R ----------
    public PlayingState(Game game) : base(game)
    {
        game.Services.AddService(typeof(IPlayingState), this);

        scrollingBackgroundManager = (IScrollingBackgroundManager)game.Services.GetService(typeof(IScrollingBackgroundManager));
        celAnimationManager = (ICelAnimationManager)game.Services.GetService(typeof(ICelAnimationManager));
        soundManager = (ISoundManager)game.Services.GetService(typeof(ISoundManager));

        playerKey = "playerPauseDown";
    }

    // ---------- I N I T I A L I Z E ----------
    public override void Initialize()
    {
        this.camera = new Camera(this.GraphicsDevice);
        base.Initialize();
    }

    // ---------- L O A D - C O N T E N T ----------
    protected override void LoadContent()
    {
        **here i load my animation sprites** 
    }

    // ---------- U P D A T E ----------
    public override void Update(GameTime gameTime)
    {
        KeyboardState kState = Keyboard.GetState();
        float dt = (float)gameTime.ElapsedGameTime.TotalSeconds;

        if (healthTimer > 0) { HealthTimer -= dt; }

        foreach (Enemy en in Enemy.enemies)
        {
            en.Update(gameTime, PlayerPosition);
        }
        Enemy.enemies.RemoveAll(e => e.Health <= 0);

        // handleInput(gameTime);
        handleInputMouse(gameTime);
        playerWalkToMouse();

        this.camera.Update(gameTime);
        this.camera.Position = playerPosition;



        base.Update(gameTime);
    }

    // ---------- M O U S E ----------
    private void handleInputMouse(GameTime gameTime)
    {
        /*
        MouseState mStateHandle = Mouse.GetState(); ;
        if (mStateHandle.LeftButton == ButtonState.Pressed)
            current_mouse_pos = new Vector2(mStateHandle.X, mStateHandle.Y);

        if (current_mouse_pos.X > PlayerPosition.X)
            playerKey = "playerWalkRight";
        else if (current_mouse_pos.X < PlayerPosition.X)
            playerKey = "playerWalkLeft";
        else if (current_mouse_pos.Y < PlayerPosition.Y)
            playerKey = "playerWalkUp";
        else if (current_mouse_pos.Y > PlayerPosition.Y)
            playerKey = "playerWalkDown";
        else
            playerKey = "playerPauseDown";
        */


        if (mState.LeftButton == ButtonState.Pressed)
            current_mouse_pos = new Vector2(mState.X, mState.Y);

        if (current_mouse_pos.X > PlayerPosition.X)
            playerKey = "playerWalkRight";
        else if (current_mouse_pos.X < PlayerPosition.X)
            playerKey = "playerWalkLeft";
        else if (current_mouse_pos.Y < PlayerPosition.Y)
            playerKey = "playerWalkUp";
        else if (current_mouse_pos.Y > PlayerPosition.Y)
            playerKey = "playerWalkDown";
        else
            playerKey = "playerPauseDown";
    }


    // ---------- D R A W ----------
    public override void Draw(GameTime gameTime)
    {
        SpriteBatch.Begin(this.camera);


        SpriteBatch.Draw(backgroundtexture, backgroundPosition, Color.White);

        celAnimationManager.Draw(gameTime, playerKey, OurGame.SpriteBatch, PlayerPosition, SpriteEffects.None);

        Vector2 cursorPos = new Vector2(Mouse.GetState().X, Mouse.GetState().Y);
        SpriteBatch.Draw(mouse_Sprite, cursorPos, Color.White);

        SpriteBatch.End();
        base.Draw(gameTime);

    }

1 Ответ

0 голосов
/ 03 января 2019

Положение мыши, полученное с помощью GetState (), всегда соответствует Windows, в которой отображается ваша игра.Она никогда не относится к вашей камере.

Если я правильно понимаю вашу логику, вам просто нужно вычесть положение камеры по осям x и y в положение мыши, полученное с помощью функции GetState ().

Таким образом, вы свяжете свою «мировую» позицию с положением камеры.

...