XNA Reusable Class(es) Challenge! Open Ended

  • (2 Pages)
  • +
  • 1
  • 2

19 Replies - 2707 Views - Last Post: 15 December 2011 - 01:22 AM

#16 SixOfEleven  Icon User is offline

  • using Caffeine;
  • member icon

Reputation: 945
  • View blog
  • Posts: 6,342
  • Joined: 18-October 08

Re: XNA Reusable Class(es) Challenge! Open Ended

Posted 07 December 2011 - 03:32 PM

I've been working a little on my audio managing component and it is quite nice. It can be used to manage SoundEffect and SoundEffectInstance objects. Usage is simple, just create an instance and added it to the list of components in your game. If you want to use it in a GameComponent outside of your game class you can retrieve it from the services for the game. Add SoundEffect objects to the manager with a unique name.

Spoiler

Was This Post Helpful? 2
  • +
  • -

#17 AdoTheLimey  Icon User is offline

  • D.I.C Head

Reputation: 23
  • View blog
  • Posts: 99
  • Joined: 28-June 10

Re: XNA Reusable Class(es) Challenge! Open Ended

Posted 07 December 2011 - 08:38 PM

This is a message window, similar to the chat/combat windows used in World of Warcraft.

To use, call the Initialise method and pass the following variables into it:

spritefont : which spritefont to use,
location: bottomleft corner of the window,
time: how long to display a message before fading out,
lineheight: spacing between each line
defaultColour: colour used when displaying messages.

Then just call the Messagewindow.Update() and Messagewindow.Draw() functions.

To send a message, use Messagewindow.AddMessage(string). You can also specify a colour to use with Messagewindow.AddMessage(string, colour)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace YourNameSpace
{
    public static class MessageWindow
    {
        //Message Window to display updates to the player.  Window Origin is bottom left and message will scroll upwards and slowly fade out

        private static SpriteFont font; // font to use
        private static Vector2 origin; //bottomleft corner of window
        private static float timeToDisplay; //length of time that message will be displayed before fading out.
        private static int offset; // height of messageline;
        private static int numberOfLines = 8; // controls how many lines to display at once
        private static Color fontColour; // font Colour
        private static Color defaultFontColour; //default font colour

        public static List<Message> MessageBuffer = new List<Message>(); // Holds messages to be displayed
        
        public static void Initialise(SpriteFont spriteFont, Vector2 location, float time, int lineHeight, Color defaultColour)
        {
            MessageBuffer.Clear();

            font = spriteFont;
            origin = location;
            timeToDisplay = time;
            offset = lineHeight;
            defaultFontColour = defaultColour;
        }

        public static void AddMessage(string message)
        {
            fontColour = defaultFontColour;
            Message messageToDisplay = new Message(message, fontColour);
            messageToDisplay.aliveTimer = timeToDisplay;
            MessageBuffer.Add(messageToDisplay);

            if (MessageBuffer.Count > numberOfLines) // If message bugger is full, removes the oldest item from MessageBuffer
            {
                MessageBuffer.RemoveAt(0);
            }
        }

        public static void AddMessage(string message, Color colour)
        {
            fontColour = colour;
            Message messageToDisplay = new Message(message, fontColour);
            messageToDisplay.aliveTimer = timeToDisplay;
            MessageBuffer.Add(messageToDisplay);

            if (MessageBuffer.Count > numberOfLines) // If message bugger is full, removes the oldest item from MessageBuffer
            {
                MessageBuffer.RemoveAt(0);
            }
        }

        public static void RemoveMessage(int index)
        {
            MessageBuffer.RemoveAt(index);
        }

        public static void Update(GameTime gameTime)
        {
            for (int x = MessageBuffer.Count - 1; x >= 0; x--)
            {
                MessageBuffer[x].Update(gameTime);
                if (!MessageBuffer[x].active)
                {
                    MessageBuffer.RemoveAt(x);
                }
            }        
        }

        public static void Draw(SpriteBatch spriteBatch)
        {
            for (int x = 0 ; x < MessageBuffer.Count; x++)
            {
                spriteBatch.DrawString(font, MessageBuffer[x].message, new Vector2(origin.X, origin.Y - ((MessageBuffer.Count - x) * offset)), MessageBuffer[x].colour);
            }
        }
    }

public class Message
    {
        public string message;
        public float messageAge;
        public bool active;
        public float aliveTimer = 0.0f;
        public Color colour;             

        public Message(string message, Color fontColour)
        {
            this.message = message;
            colour = fontColour;
            messageAge = 0.0f;
            active = true;            
        }

        public void Update(GameTime gameTime)
        {
            float elapsed = (float)gameTime.ElapsedGameTime.TotalSeconds;
            messageAge += elapsed;

            if (active)
            {
                if (messageAge > aliveTimer)
                {
                    active = false;
                }
            }
        }
    }
}



Was This Post Helpful? 2
  • +
  • -

#18 Fib  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 161
  • View blog
  • Posts: 554
  • Joined: 12-March 09

Re: XNA Reusable Class(es) Challenge! Open Ended

Posted 09 December 2011 - 07:57 AM

Fantastic class AdoTheLimey!!

There's some great stuff being posted in here.
Was This Post Helpful? 0
  • +
  • -

#19 AdoTheLimey  Icon User is offline

  • D.I.C Head

Reputation: 23
  • View blog
  • Posts: 99
  • Joined: 28-June 10

Re: XNA Reusable Class(es) Challenge! Open Ended

Posted 10 December 2011 - 02:29 PM

View PostFib, on 09 December 2011 - 07:57 AM, said:

Fantastic class AdoTheLimey!!

There's some great stuff being posted in here.



Thanks Fib, I'm just happy to give something back :)
Was This Post Helpful? 0
  • +
  • -

#20 racidon  Icon User is offline

  • D.I.C Head

Reputation: 59
  • View blog
  • Posts: 172
  • Joined: 27-August 11

Re: XNA Reusable Class(es) Challenge! Open Ended

Posted 15 December 2011 - 01:22 AM

Thought I'd post this "INIFile" class I made for an application. Although it uses no XNA components at all it can be very helpful for settings files.
It's not complete though I've still got a few methods to write, just simple set/save and some other load methods.

Here is the pastebin link: INIFile

Spoiler

Was This Post Helpful? 1
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2