GameObject.cs :  » Game » SDL » SdlDotNetExamples » Triad » C# / CSharp Open Source

Home
C# / CSharp Open Source
1.2.6.4 mono .net core
2.2.6.4 mono core
3.Aspect Oriented Frameworks
4.Bloggers
5.Build Systems
6.Business Application
7.Charting Reporting Tools
8.Chat Servers
9.Code Coverage Tools
10.Content Management Systems CMS
11.CRM ERP
12.Database
13.Development
14.Email
15.Forum
16.Game
17.GIS
18.GUI
19.IDEs
20.Installers Generators
21.Inversion of Control Dependency Injection
22.Issue Tracking
23.Logging Tools
24.Message
25.Mobile
26.Network Clients
27.Network Servers
28.Office
29.PDF
30.Persistence Frameworks
31.Portals
32.Profilers
33.Project Management
34.RSS RDF
35.Rule Engines
36.Script
37.Search Engines
38.Sound Audio
39.Source Control
40.SQL Clients
41.Template Engines
42.Testing
43.UML
44.Web Frameworks
45.Web Service
46.Web Testing
47.Wiki Engines
48.Windows Presentation Foundation
49.Workflows
50.XML Parsers
C# / C Sharp
C# / C Sharp by API
C# / CSharp Tutorial
C# / CSharp Open Source » Game » SDL 
SDL » SdlDotNetExamples » Triad » GameObject.cs
#region LICENSE
//*****************************************************************************
//  This program is free software; you can redistribute it and/or
//  modify it under the terms of the GNU General Public License
//  as published by the Free Software Foundation; either version 2
//  of the License, or (at your option) any later version.
//  This program is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//  GNU General Public License for more details.
//  You should have received a copy of the GNU General Public License
//  along with this program; if not, write to the Free Software
//  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
//  
//  Created by Michael Rosario
//  July 29th,2003
//  Contact me at mrosario@scrypt.net  
//*****************************************************************************
#endregion LICENSE

using System;
using System.Globalization;
using System.Drawing;
using System.Diagnostics.CodeAnalysis;

using SdlDotNet.Graphics;

namespace SdlDotNetExamples.Triad{
    /// <summary>
    /// 
    /// </summary>
    public abstract class GameObject
    {
        private GameObject parent;
        /// <summary>
        /// 
        /// </summary>
        public GameObject Parent
        {
            get
            {
                return parent;
            }
            set
            {
                parent = value;
            }
        }

        private int x;
        /// <summary>
        /// 
        /// </summary>
        [SuppressMessage("Microsoft.Naming", "CA1704:IdentifiersShouldBeSpelledCorrectly", Justification = "Correct Spelling")]
        public int X
        {
            get
            {
                return x;
            }
            set
            {
                x = value;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        public int ScreenX
        {
            get
            {
                int x = this.x;
                if (this.parent != null)
                {
                    x += this.parent.ScreenX;
                }
                return x;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        public int ScreenY
        {
            get
            {
                int y = this.y;
                if (this.parent != null)
                {
                    y += this.parent.ScreenY;
                }
                return y;
            }
        }

        private int y;
        /// <summary>
        /// 
        /// </summary>
        [SuppressMessage("Microsoft.Naming", "CA1704:IdentifiersShouldBeSpelledCorrectly", Justification = "Correct Spelling")]
        public int Y
        {
            get
            {
                return y;
            }
            set
            {
                y = value;
            }
        }

        private int width;
        /// <summary>
        /// 
        /// </summary>
        public int Width
        {
            get
            {
                return width;
            }
            set
            {
                if (width <= 0)
                {
                    throw new GameException(SdlDotNetExamplesBrowser.StringManager.GetString(
                        "WidthSetZero", CultureInfo.CurrentUICulture));
                }

                width = value;
            }
        }

        private int height;
        /// <summary>
        /// 
        /// </summary>
        public int Height
        {
            get
            {
                return height;
            }
            set
            {
                if (value <= 0)
                {
                    throw new GameException(SdlDotNetExamplesBrowser.StringManager.GetString(
                        "HeightSetZero", CultureInfo.CurrentUICulture));
                }

                height = value;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        public int X2
        {
            get
            {
                return x + width;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        public int Y2
        {
            get
            {
                return y + height;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        public int ScreenX2
        {
            get
            {
                int offSetX = 0;
                if (this.parent != null)
                {
                    offSetX = this.parent.ScreenX;
                }
                return this.X2 + offSetX;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        public int ScreenY2
        {
            get
            {
                int offSetY = 0;
                if (this.parent != null)
                {
                    offSetY = this.parent.ScreenY;
                }
                return y + height + offSetY;
            }
        }

        int previousWidth;
        int previousHeight;
        Size currentSize;
        /// <summary>
        /// 
        /// </summary>
        public Size Size
        {
            get
            {
                if ((previousWidth != width) || (previousHeight != height))
                {
                    currentSize = new Size(width, height);
                }

                previousWidth = width;
                previousHeight = height;

                return currentSize;
            }
            set
            {
                this.width = value.Width;
                this.height = value.Height;
            }
        }

        System.Drawing.Rectangle currentRectangle;
        Point previousLocation;
        /// <summary>
        /// 
        /// </summary>
        public Rectangle Rectangle
        {
            get
            {
                if (previousLocation != Location)
                {
                    currentRectangle = new System.Drawing.Rectangle(Location, this.Size);
                }

                previousLocation = Location;
                return currentRectangle;
            }
        }

        Point previousScreenLocation;
        System.Drawing.Rectangle currentScreenRectangle;
        /// <summary>
        /// 
        /// </summary>
        public Rectangle ScreenRectangle
        {
            get
            {
                if (previousScreenLocation != ScreenLocation)
                {
                    currentScreenRectangle = new System.Drawing.Rectangle(ScreenLocation, this.Size);
                }

                previousScreenLocation = ScreenLocation;
                return currentScreenRectangle;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        public Point Location
        {
            get
            {
                return new Point(x, y);
            }
            set
            {
                this.x = value.X;
                this.y = value.Y;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        public Point ScreenLocation
        {
            get
            {
                return new Point(this.ScreenX, this.ScreenY);
            }
        }

        /// <summary>
        /// 
        /// </summary>
        public static Point BottomRightCorner
        {
            get
            {
                return new Point(0, 0);
            }
        }

        /// <summary>
        /// 
        /// </summary>
        public abstract void Update();
        /// <summary>
        /// 
        /// </summary>
        /// <param name="surface"></param>
        protected abstract void DrawGameObject(Surface surface);
        /// <summary>
        /// 
        /// </summary>
        /// <param name="surface"></param>
        public void Draw(Surface surface)
        {
            if (surface == null)
            {
                throw new ArgumentNullException("surface");
            }

            DrawGameObject(surface);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="point"></param>
        /// <returns></returns>
        public bool Contains(Point point)
        {
            bool inSideX = (ScreenX <= point.X) && (point.X <= ScreenX2);
            bool inSideY = (ScreenY <= point.Y) && (point.Y <= ScreenY2);
            return inSideX && inSideY;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public bool Hits(GameObject obj)
        {
            if (obj == null)
            {
                return false;
            }

            return Contains(obj.Location) || Contains(GameObject.BottomRightCorner);
        }
    }
}
www.java2v.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.