Click here to Skip to main content
15,897,032 members
Articles / Game Development

Journey of Windows 8 Game (SkyWar) for Intel App Up Using MonoGame Framework

Rate me:
Please Sign up or sign in to vote.
5.00/5 (2 votes)
8 Jan 2013CPOL15 min read 23.5K   226   12  
Journey of Windows 8 game SkyWar for Intel App Up competition
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SkyWar
{
    class AnimatedSprite
    {
        Texture2D t2dTexture;

        float fFrameRate = 0.02f;
        float fElapsed = 0.0f;

        int iFrameOffsetX = 0;
        int iFrameOffsetY = 0;
        int iFrameWidth = 32;
        int iFrameHeight = 32;

        int iFrameCount = 1;
        int iCurrentFrame = 0;
        int iScreenX = 0;
        int iScreenY = 0;
        public bool IsActive { get; set; }
        public bool OneShotAnimation { get; set; }
        public bool DeactivateOnAnimationOver { get; set; }

        Color cTinting = Color.White;
        public Color Tint
        {
            get { return cTinting; }
            set { cTinting = value; }
        }

        bool bAnimating = true;

        public int X
        {
            get { return iScreenX; }
            set { iScreenX = value; }
        }

        public int Y
        {
            get { return iScreenY; }
            set { iScreenY = value; }
        }

        public int Frame
        {
            get { return iCurrentFrame; }
            set { iCurrentFrame = (int)MathHelper.Clamp(value, 0, iFrameCount); }
        }

        public float FrameLength
        {
            get { return fFrameRate; }
            set { fFrameRate = (float)Math.Max(value, 0f); }
        }

        public bool IsAnimating
        {
            get { return bAnimating; }
            set { bAnimating = value; }
        }

        public AnimatedSprite(Texture2D texture, int FrameOffsetX, int FrameOffsetY, int FrameWidth, int FrameHeight, int FrameCount)
        {
            t2dTexture = texture;
            iFrameOffsetX = FrameOffsetX;
            iFrameOffsetY = FrameOffsetY;
            iFrameWidth = FrameWidth;
            iFrameHeight = FrameHeight;
            iFrameCount = FrameCount;
            IsActive = true;
        }

        public Rectangle GetSourceRect()
        {
            return new Rectangle(iFrameOffsetX + (iFrameWidth * iCurrentFrame), iFrameOffsetY, iFrameWidth, iFrameHeight);
        }

        public void Update(GameTime gameTime)
        {
            if (IsActive)
            {
                if (bAnimating)
                {
                    // Accumulate elapsed time...
                    fElapsed += (float)gameTime.ElapsedGameTime.TotalSeconds;

                    // Until it passes our frame length
                    if (fElapsed > fFrameRate)
                    {
                        // Increment the current frame, wrapping back to 0 at iFrameCount
                        iCurrentFrame = (iCurrentFrame + 1) % iFrameCount;
                        if (iCurrentFrame >= iFrameCount - 1 && OneShotAnimation && DeactivateOnAnimationOver)
                            IsActive = false;
                        // Reset the elapsed frame time.
                        fElapsed = 0.0f;
                    }
                }
            }
        }

        public void Draw(SpriteBatch spriteBatch, int XOffset, int YOffset, bool NeedBeginEnd)
        {
            //if (NeedBeginEnd)
            //    spriteBatch.Begin();

            if (IsActive)
                spriteBatch.Draw(t2dTexture, new Rectangle(iScreenX + XOffset, iScreenY + YOffset, iFrameWidth, iFrameHeight), GetSourceRect(), cTinting);

            //if (NeedBeginEnd)
            //    spriteBatch.End();
        }

        public void Draw(SpriteBatch spriteBatch, int XOffset, int YOffset)
        {
            Draw(spriteBatch, XOffset, YOffset, true);
        }

        public void Draw(SpriteBatch spriteBatch, Rectangle boundingbox, bool NeedBeginEnd)
        {
            //if (NeedBeginEnd)
            //    spriteBatch.Begin();
            if (IsActive)
                spriteBatch.Draw(t2dTexture, boundingbox, GetSourceRect(), cTinting);
            //if (NeedBeginEnd)
            //    spriteBatch.End();
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer eMids Technologies Pvt Ltd
India India
2+ year of experience in Information Technology with the extensive exposure of Treasury & Capital, Health Care and ERP domain.

Area of interest - Multithreading, Files, WPF, WCF, Jquery, Mvc, Sql Server, Perceptual programming

Heavily Worked on desktop application ,Web application and WCF Services.

Also worked on small games application for learning purpose.

Latest interest - Windows 8 , Perceptual Programming, Windows Phone, WCF etc

Comments and Discussions