I am making an xna game which contains turn-based RPG battle elements. Any good game that features turn-based battle has to have a good feedback to the user.
For example, When my guy attacks, there should be a text popup that says something like "HERO DOES 15 DMG!". And this text should disappear several seconds after.
I am currently able to put text on the screen via standard spritefont drawing. However, in order to make it disappear I am currently using bools and a timer. I have been doing research on timing related stuff but I seem to be very bad at it.
Using some of the suggestions I have received, I have made a messagesystem class. But I am still confused about the implementation. Here is the new code:
public class MessageSystem
{
public static readonly MessageSystem Instance = new MessageSystem();
public float timenow;
struct Message
{
public SpriteFont Font;
public string Data;
public Vector2 Position;
public float Duration;
public bool isActive { get; set; }
}
List<Message> Messages = new List<Message>();
public MessageSystem()
{
}
public void Show(string data, Vector2 pos, SpriteFont font, float duration)
{
Messages.Add(new Message() { Data = data, Font = font, Position = pos, Duration = duration });
}
public void Update(GameTime gameTime)
{
timenow += (float)gameTime.ElapsedGameTime.TotalSeconds;
for (int i = 0; i < Messages.Count; )
{
if (Messages[i].Duration < timenow)
{
Messages.RemoveAt(i);
continue;
}
i++;
}
}
public void Draw(SpriteBatch batch)
{
foreach (Message msg in Messages)
{
batch.DrawString(msg.Font, msg.Data, msg.Position, Color.Lime);
}
}
}
Here is the implementation part:
if (p1atktimer >= 10)
{
AttackMsg.Show(playerBattler.Name + " attacks furiously!", battlemsgs, battlefont, 2.0f);
DealDmgFromLeft(gameTime);
p1atktimer = 0;
timenow = 0;
}
In my update method I just have:
Attackmsg.Update(gameTime);
And similarly, in my Draw method I have:
Attackmsg.Draw(spriteBatch);
So, it goes away when it is supposed to, but it does not appear again when the p1atktimer condition becomes true again. I guess i have to surround the attackmsg.Draw(spriteBatch) command with conditionals instead of the show? But this brings me back to my previous question: Do I have to make bools for everytime I want to show this kind of text.
Let's continue with this example, I am going to make a bool atkmsgshowing = false. And make it true when someone's attackguage reaches 10. Because, if I do not do this, the msg works only once. So then, the attacker attacks, and I will make the bool true. When the attack ends, atkmsgshowing will go back to false. Doesn't this defeat the purpose of making a MessageSystem class?