xDardas's Profile
Reputation: 0
Apprentice
- Group:
- New Members
- Active Posts:
- 4 (0.02 per day)
- Joined:
- 10-December 12
- Profile Views:
- 33
- Last Active:
Dec 12 2012 04:34 AM- Currently:
- Offline
Previous Fields
- Dream Kudos:
- 0
Posts I've Made
-
In Topic: Inheritance VS single class - whats better?
Posted 11 Dec 2012
Right then, heres the code:
Entity:
using System; using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Graphics; namespace Goblin_GameLib { /// <summary> /// Represents an object that can be drawn and updated. /// </summary> public class Entity { /// <summary> /// Gets or sets the texture of the entity. /// </summary> public Texture2D Texture { get; set; } /// <summary> /// Gets or sets the color at which the entity gets drawn. /// </summary> public Color Color { get; set; } /// <summary> /// Gets or sets the X axis coordinate of the entitys location. /// </summary> public Int32 X { get; set; } /// <summary> /// Gets or sets the Y axis coordinate of the entitys location. /// </summary> public Int32 Y { get; set; } /// <summary> /// Gets or sets the width of the entity when drawn. /// </summary> public Int32 Width { get; set; } /// <summary> /// Gets or sets the height of the entity when drawn. /// </summary> public Int32 Height { get; set; } /// <summary> /// Gets or sets the offsets x axis coordinate of the entity. /// </summary> public Int32 OffsetX { get; set; } /// <summary> /// Gets or sets the offsets y axis coordinate of the entity. /// </summary> public Int32 OffsetY { get; set; } /// <summary> /// Gets or sets the source rectangle used to draw the entity. /// </summary> public Rectangle? SourceRectangle { get; set; } /// <summary> /// Initializes a new entity instance. /// </summary> /// <param name="Texture">The texture to use to draw the entity.</param> /// <param name="Color">The color to use to draw the entity.</param> public Entity(Texture2D Texture, Color Color) { this.Texture = Texture; this.Color = Color; this.Width = this.Texture.Width; this.Height = this.Texture.Height; } /// <summary> /// Initializes a new entity instance. /// </summary> /// <param name="Texture">The texture to use to draw the entity.</param> /// <param name="Color">The color to use to draw the entity.</param> /// <param name="X">The x coordinate to move the entitys location to.</param> /// <param name="Y">The y coordinate to move the entitys location to.</param> public Entity(Texture2D Texture, Color Color, Int32 X, Int32 Y) { this.Texture = Texture; this.Color = Color; this.Width = this.Texture.Width; this.Height = this.Texture.Height; this.X = X; this.Y = Y; } /// <summary> /// A method that updates the entity. /// </summary> /// <param name="gameTime">Provides a snapshot of the game timing state.</param> public virtual void Update(GameTime gameTime) { //TODO: Insert update logic here. //Not much to update though.. } /// <summary> /// A method that draws the entity. /// </summary> /// <param name="spriteBatch">The SpriteBatch instance to draw the entity with.</param> /// <param name="gameTime">Provides a snapshot of the game timing state.</param> public virtual void Draw(SpriteBatch spriteBatch, GameTime gameTime) { spriteBatch.Draw(this.Texture, this.GetDrawRectangle(), this.SourceRectangle, this.Color); } public virtual Rectangle GetDrawRectangle() { return new Rectangle(this.X - this.OffsetX, this.Y - this.OffsetY, this.Width, this.Height); } } }
Interactable Entity:
using System; using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Graphics; namespace Goblin_GameLib { /// <summary> /// Represents the collision type for an interactable entity. /// </summary> public enum CollisionType { RectangleCollision, PerPixelCollision, } /// <summary> /// Contains event data for interaction between 2 entities. /// </summary> public class InteractionEventArgs : EventArgs { /// <summary> /// Gets the entity that was interacted with. /// </summary> public Entity Interacted { get; private set; } /// <summary> /// Gets the collision type used to check the interaction. /// </summary> public CollisionType CollisionType { get; private set; } /// <summary> /// Initializes a new interaction event data instance. /// </summary> /// <param name="Interacted">The entity that was interacted with.</param> /// <param name="CollisionType">The collision type used to check the interaction.</param> public InteractionEventArgs(Entity Interacted, CollisionType CollisionType) { this.Interacted = Interacted; this.CollisionType = CollisionType; } } /// <summary> /// Represents an entity that can interact with other entities. /// </summary> public class InteractableEntity : Entity { //I know it shouldn't be public, still WIP. public event EventHandler<InteractionEventArgs> InteractionNotifier; /// <summary> /// Gets or sets the collision type of the entity. /// </summary> public CollisionType CollisionType { get; set; } /// <summary> /// Initializes a new entity. /// </summary> /// <param name="Texture">The texture to use to draw the entity.</param> /// <param name="Color">The color to use to draw the entity.</param> /// <param name="CollisionType">The collision type to check the entitys interaction with.</param> public InteractableEntity(Texture2D Texture, Color Color, CollisionType CollisionType) : base(Texture, Color) { this.CollisionType = CollisionType; } /// <summary> /// Initializes a new entity instance. /// </summary> /// <param name="Texture">The texture to use to draw the entity.</param> /// <param name="Color">The color to use to draw the entity.</param> /// <param name="X">The x coordinate to move the entitys location to.</param> /// <param name="Y">The y coordinate to move the entitys location to.</param> /// <param name="CollisionType">The collision type to check the entitys interaction with.</param> public InteractableEntity(Texture2D Texture, Color Color, Int32 X, Int32 Y, CollisionType CollisionType) : base(Texture, Color, X, Y) { this.CollisionType = CollisionType; } /// <summary> /// A method that checks if this entity collides with the given entity. /// </summary> /// <param name="Entity">The entity to check collision with.</param> /// <returns>True if the entities collide, false otherwise.</returns> public virtual bool Interacts(Entity Entity) { EventHandler<InteractionEventArgs> handler = this.InteractionNotifier; bool ReturnValue = false; Rectangle a = this.GetDrawRectangle(); Rectangle b = Entity.GetDrawRectangle(); if (this.CollisionType == CollisionType.RectangleCollision) { ReturnValue = a.Intersects(B)/>/>; } else if (this.CollisionType == CollisionType.PerPixelCollision) { Int32 Left = Math.Max(a.Left, b.Left); Int32 Right = Math.Max(a.Right, b.Right); Int32 Top = Math.Max(a.Top, b.Top); Int32 Bottom = Math.Max(a.Bottom, b.Bottom); Color[] c = new Color[1]; Color[] d = new Color[1]; for (int y = Top; y < Bottom && !ReturnValue; y++) { for (int x = Left; x < Right && !ReturnValue; x++) { this.Texture.GetData<Color>(c, (x - a.Left) - (y - a.Top) * a.Width, 1); this.Texture.GetData<Color>(d, (x - b.Left) - (y - b.Top) * b.Width, 1); if (c[0].A != 0 && d[0].A != 0) ReturnValue = true; } } } if (ReturnValue && handler != null) handler.Invoke((object)this, new InteractionEventArgs(Entity, this.CollisionType)); return ReturnValue; } } }
Sorry about posting it in a website, seemed better at the time. -
In Topic: Inheritance VS single class - whats better?
Posted 10 Dec 2012
Alright, well I've read some of your tutorials, and decided to re-try the code.
Heres my new version:
{link to unknown server removed}
I've also decided to add an interactable entity, that can collide with other entities, as a friend sugguested.
{link to unknown server removed}
Is this the correct way to do this?
P.S. If this still isn't correct, I guess I'll trully give up on this code, and read more tutorials before I try again..
Thanks in advance, once again ~Dardas. -
In Topic: Inheritance VS single class - whats better?
Posted 10 Dec 2012
tlhIn'toq, thanks for your answer, feels like I understand a little bit better how inheritance works, and when I should use it. Anyhow, I'll start by looking into the classes and objects tutorial, thanks once again.
My Information
- Member Title:
- New D.I.C Head
- Age:
- Age Unknown
- Birthday:
- Birthday Unknown
- Gender:
Contact Information
- E-mail:
- Private
Friends
xDardas hasn't added any friends yet.
|
|


Find Topics
Find Posts
View Reputation Given
|
Comments
xDardas has no profile comments yet. Why not say hello?