如何在玩家位置创建纹理然后将其留在XNA?

时间:2013-04-11 04:43:27

标签: c# xna-4.0

我有一个几乎完整的玩家类游戏,但这是我的问题。当我按下控制+ K时,玩家自杀(意图)。现在,当他去世时,它会在玩家位置产生血腥飞溅(也是有意的)现在这就是问题,一旦玩家重新生成,血溅就会保持可见,但现在它跟随着玩家,这是一个如果你问我,小怪人

所以这是主要代码:

  using System;
  using System.Collections.Generic;
  using System.Linq;
  using Microsoft.Xna.Framework;
  using Microsoft.Xna.Framework.Audio;
  using Microsoft.Xna.Framework.Content;
  using Microsoft.Xna.Framework.GamerServices;
  using Microsoft.Xna.Framework.Graphics;
  using Microsoft.Xna.Framework.Input;
  using Microsoft.Xna.Framework.Media;
  using Microsoft.Xna.Framework.Net;
  using Microsoft.Xna.Framework.Storage;

  namespace Teir_Tactical_2A
  {
         public class Player
     {
    Random random = new Random();
    public Texture2D playerunarmed;
    public Texture2D playerM1911;
    public Texture2D bloodspatter2;
    public Texture2D playerM4;
    public KeyboardState keyState;
    public SpriteFont Font1;
    public KeyboardState oldKeyboardState;
    public Vector2 playerPosition;
    public SpriteBatch spriteBatch;
    public float Angle { get; set; }
    public float AngularVelocity { get; set; }
    public Vector2 playerVelocity = Vector2.One;
    public bool M1911;
    public bool M4;
    String[] strs = new String[] { "You suicided. Way to go Genius.", "I saw your limbs blow off with that one.", "Click to Respawn", "HAHAHAHAHAHA-Sorry Click to Respawn" };
    String suicideStr;
    public bool player;
    public bool suicide;
    public bool LMBpressed;
    public bool RMBpressed;
    public bool isplayeralive;
    public bool isplayerdead;
    public bool respawnscreen;
    public bool bloodspatter;
    float angle;





    public Player(ContentManager content, Vector2 location)
    {
        this.playerunarmed = content.Load<Texture2D>("PLAYER");
        this.playerM1911 = content.Load<Texture2D>("PLAYERM1911");
        this.playerM4 = content.Load<Texture2D>("PLAYERM4");
        this.bloodspatter2 = content.Load<Texture2D>("blood");
        playerPosition = location;
        suicideStr = strs[random.Next(strs.Length)];
        M1911 = true;
        M4 = true;
        player = true;
        LMBpressed = false;
        suicide = false; 
        RMBpressed = false;
        isplayeralive = true;
        isplayerdead = false;
        respawnscreen = false;
        bloodspatter = false;
        Font1 = content.Load<SpriteFont>("Font1");
    }


    public void Update()
    {
        MouseState curMouse = Mouse.GetState();

        if (bloodspatter == true)
        {
            blood();
        }

        if (suicide == true)
        {
            suicided();
        }

        if (isplayeralive == true)
        {
            alive();
        }

        if (isplayerdead == true)
        {
            dead();
        }

        if (M1911 == true)
        {
            armedM1911();
        }

        if (M4 == true)
        {
            armedM4();
        }
        if (player == true)
        {
            unarmed();
        }
        if (LMBpressed == true)
        {
            LMBpressedA();
        }
        if (RMBpressed == true)
        {
            RMBpressedA();
        }

        Vector2 mouseLoc = new Vector2(curMouse.X, curMouse.Y);

        Vector2 direction = mouseLoc - playerPosition;

        angle = (float)(Math.Atan2(direction.Y, direction.X));

        if (curMouse.LeftButton == ButtonState.Pressed)
        {
            LMBpressed = true;

        }
        if (curMouse.RightButton == ButtonState.Pressed)
        {
            RMBpressed = true;
        }

        keyState = Keyboard.GetState();

        if (isplayeralive == false && LMBpressed == true)
        {
            isplayeralive = true;
            isplayerdead = false;
            suicide = false;
            suicideStr = strs[random.Next(strs.Length)];
        }


        if (keyState.IsKeyDown(Keys.LeftControl) && keyState.IsKeyDown(Keys.K))
        {
            isplayeralive = false;
            isplayerdead = true;
            bloodspatter = true;
            suicide = true;

        }

        if (keyState.IsKeyDown(Keys.D1))
        {

            M1911 = false;
            M4 = false;
            player = true;

        }
        if (keyState.IsKeyDown(Keys.D2))
        {
            M1911 = true;
            player = false;

        }
        if (keyState.IsKeyDown(Keys.D3))
        {
            M4 = true;
            M1911 = false;
            player = false;

        }





        if (keyState.IsKeyDown(Keys.D))
            playerPosition.X += playerVelocity.X + 1;
        if (keyState.IsKeyDown(Keys.A))
            playerPosition.X -= playerVelocity.X + 1;

        if (keyState.IsKeyDown(Keys.W))
            playerPosition.Y -= playerVelocity.Y + 1;
        if (keyState.IsKeyDown(Keys.S))
            playerPosition.Y += playerVelocity.Y + 1;
    }


    public void Draw(SpriteBatch spriteBatch)
    {
        spriteBatch.Begin();
        {

            Rectangle sourceRectangle = new Rectangle(0, 0, playerunarmed.Width, playerunarmed.Height);
            Vector2 origin = new Vector2(playerunarmed.Width / 2, playerunarmed.Height / 2);

            if (M1911 == true)
            {
                spriteBatch.Draw(playerM1911, new Rectangle((int)playerPosition.X, (int)playerPosition.Y, playerunarmed.Width, playerunarmed.Height), null, Color.White, angle + 90, new Vector2(86, 88), SpriteEffects.None, 0f);
            }

            if (suicide == true)
            {
                spriteBatch.DrawString(Font1, suicideStr, new Vector2(10, 670), Color.Red);  
            }


            }
            if (bloodspatter == true)
            {
                spriteBatch.Draw(bloodspatter2, new Rectangle((int)playerPosition.X, (int)playerPosition.Y, bloodspatter2.Width, bloodspatter2.Height), Color.White);
            }



            if (player == true)
            {
                spriteBatch.Draw(playerunarmed, new Rectangle((int)playerPosition.X, (int)playerPosition.Y, playerunarmed.Width, playerunarmed.Height), null, Color.White, angle + 90, new Vector2(86, 88), SpriteEffects.None, 0f);
            }
            if (M4 == true)
            {
                spriteBatch.Draw(playerM4, new Rectangle((int)playerPosition.X, (int)playerPosition.Y, playerunarmed.Width, playerunarmed.Height), null, Color.White, angle + 90, new Vector2(86, 88), SpriteEffects.None, 0f);

            }
            if (LMBpressed == true)
            {
                spriteBatch.DrawString(Font1, "LMB PRESSED (shot taken prototype)", new Vector2(1000, 22), Color.GreenYellow);
            }
            if (RMBpressed == true)
            {
                spriteBatch.DrawString(Font1, "RMB PRESSED (grenade thrown prototype)", new Vector2(968, 34), Color.Red);
            }
            if (RMBpressed == true && LMBpressed == true)
            {
                spriteBatch.DrawString(Font1, "If you are seeing this, the mouse is functioning correctly", new Vector2(810, 45), Color.Black);
            }
        spriteBatch.End();
    }




    public void armedM1911()
    {
        M1911 = true;
        M4 = false;
        player = false;

    }
    public void armedM4()
    {
        M4 = true;
        M1911 = false;
        player = false;
    }
    public void unarmed()
    {
        M1911 = false;
        M4 = false;
    }
    public void LMBpressedA()
    {
        LMBpressed = false;
    }
    public void RMBpressedA()
    {
        RMBpressed = false;
    }
    public void alive()
    {
        M1911 = false;
        player = true;
        M4 = false;
        respawnscreen = false;
    }
    public void dead()
    {
        M1911 = false;
        player = false;
        M4 = false;
        respawnscreen = true;
    }

    public void blood()
    {
        bloodspatter = true;
    }

    public void suicided()
    {
        suicide = true;
    }

}

}

以下是我认为问题所在(95%肯定)我需要获得玩家坐标并将其设置为血腥的位置(并在必要时产生更多)。

       if (bloodspatter == true)
            {
                spriteBatch.Draw(bloodspatter2, new Rectangle((int)playerPosition.X, (int)playerPosition.Y, bloodspatter2.Width, bloodspatter2.Height), Color.White);
            }

这可能是因为我使用playerPosition作为X/Y坐标的方式,但我不知道如何告诉它首先获取它们然后设置 bloodspatters 位置到玩家自杀的地方。

编辑:似乎是一个误会,我希望血液纹理消失,我希望它保持玩家最后死亡的地方(在此case,suiciding,直到我可以添加调用它的子弹)基本上,性能不是一个问题,因为它的纹理非常低,而且我只希望它们在游戏关闭或等级变化时消失(不是问题)

2 个答案:

答案 0 :(得分:0)

不要将播放器与血吸虫连接。让它独立。因此,当玩家死亡时,只会调用将在当前玩家位置创建血液的函数。

创建一个列表,您将存储所有血液制品,然后只需将新的血液制品添加到该列表中。然后在gamplay类中调用draw并更新此列表。

伪:

splater (position as vector)
splaterlist inherit list of splater
- sub add new splater(position)
- sub update each splater, add duration, fade out
- sub draw each splater

player class
if player die add new splater into splaterlist

gameplay class
update player, draw player
update splaterlist, drawsplaterlist

答案 1 :(得分:0)

如果您想在自杀后留下一个血斑,您可以使用向您的Player类添加一个Vector2并将其命名为PlaceOfDeath。将其默认为新的Vector2.Zero();当你死亡时,将你的英雄的位置设置为新的PlaceOfDeath,然后使用该Vector2作为你的精灵的绘制点。

如果你想画出你的玩家已经死亡的每个地方,你可以创建一个Vector2s列表(List)并称之为PlacesOfDeath。将其默认为空列表。当你死亡时,用PlacesOfDeath.Add({Vector2英雄位置})设置你的英雄的位置。

然后在您的绘图代码中,您可以迭代PlacesOfDeath列表中的每个Vector2并在每个地方绘制血溅。

现在到代码

单次血液放置 -

添加到您的类属性/成员声明:

  

private Vector2 placeOfDeath = new Vector2.Zero();

加入你的自杀方法,

  

placeOfDeath = playerPosition;

在Draw方法中更改if(bloodsplatter == true)条件代码块:

  

spriteBatch.Draw(bloodspatter2,new Rectangle((int)placeOfDeath.X,(int)placeOfDeath.Y,bloodspatter2.Width,bloodspatter2.Height),Color.White);

对于多个血斑

添加到您的类属性/成员声明:

  

private List placesOfDeath = new List();

加入你的自杀方法,

  

placesOfDeath.Add(playerPosition);

在Draw方法中更改if(bloodsplatter == true)条件代码块:

  

foreach(在PlacesOfDeath中的Vector2 placeOfDeath){

     

spriteBatch.Draw(bloodspatter2,new Rectangle((int)placeOfDeath.X,(int)placeOfDeath.Y,bloodspatter2.Width,bloodspatter2.Height),Color.White);

     

}