Game-Design / Sprint3_actual / Text / ScrollingText.cs
ScrollingText.cs
Raw
using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Content;
using Sprint3_actual.SoundManagement;

namespace Sprint3_actual.Text
{
    class ScrollingText : IText
    {
        private Vector2 posVector;
        private bool removed;
        private String fullText;
        private String displayedText;
        private Color color;
        private int msPerChar;
        private int charTimer;
        private int stringLength;
        private int currentChar;
        private int nextChar;

        private int msOnScreen;
        private int currentTime;
        private bool expires;

        public ScrollingText(String text, int xPos, int yPos, double secondsOnScreen, Color color)
        {
            this.removed = false;
            this.posVector = new Vector2(xPos, yPos);
            this.fullText = text;
            this.displayedText = "";
            this.color = color;
            this.msPerChar = 80;
            this.charTimer = 0;
            this.stringLength = text.Length;
            this.currentChar = 0;
            this.expires = false;

            if(secondsOnScreen > 0)
            {
                this.currentTime = 0;
                this.msOnScreen = (int) (secondsOnScreen * 1000);
                this.expires = true;
            }

        }

        public void DrawText()
        {

            GlobalVars.spriteBatch.DrawString(GlobalVars.font, displayedText, posVector, color);
        }

        public void UpdateText(GameTime gameTime)
        {
            if(expires)
            {
                this.currentTime += gameTime.ElapsedGameTime.Milliseconds;
                if(currentTime > msOnScreen)
                {
                    removed = true;
                }
            }

            if (currentChar < stringLength)
            {

                this.charTimer += gameTime.ElapsedGameTime.Milliseconds;
                if (this.charTimer > this.msPerChar)
                {
                    this.charTimer = 0;

                    nextChar = currentChar + 1;

                    for(int index = currentChar+1; index < stringLength; index++)
                    {
                        if(!char.IsWhiteSpace(fullText[index]))
                        {
                            nextChar = index;
                            break;
                        }
                    }

                    displayedText += fullText.Substring(currentChar, nextChar-currentChar);
                    currentChar += (nextChar-currentChar);

                    SoundEngine.Instance.playTextVoice();
                }
            }

        }

        public bool IsRemoved()
        {
            return removed;
        }
    }
}