using System; using Windows.Foundation; using DodgeEm.View.Sprites; namespace DodgeEm.Model { /// /// Defines basics of every game object. /// public abstract class GameObject { #region Data members private Point location; #endregion #region Properties /// /// Gets or sets the x location of the game object. /// /// /// The x. /// public double X { get => this.location.X; set { this.location.X = value; this.render(); } } /// /// Gets or sets the y location of the game object. /// /// /// The y. /// public double Y { get => this.location.Y; set { this.location.Y = value; this.render(); } } /// /// Gets the x speed of the game object. /// /// /// The speed x. /// public int SpeedX { get; private set; } /// /// Gets the y speed of the game object. /// /// /// The speed y. /// public int SpeedY { get; private set; } /// /// Gets the width of the game object. /// /// /// The width. /// public double Width => this.Sprite.Width; /// /// Gets the height of the game object. /// /// /// The height. /// public double Height => this.Sprite.Height; /// /// Gets or sets the sprite associated with the game object. /// /// /// The sprite. /// public BaseSprite Sprite { get; protected set; } #endregion #region Methods /// /// Moves the game object right. /// Precondition: None /// Postcondition: X == X@prev + SpeedX /// public void MoveRight() { this.moveX(this.SpeedX); } /// /// Moves the game object left. /// Precondition: None /// Postcondition: X == X@prev + SpeedX /// public void MoveLeft() { this.moveX(-this.SpeedX); } /// /// Moves the game object up. /// Precondition: None /// Postcondition: Y == Y@prev - SpeedY /// public void MoveUp() { this.moveY(-this.SpeedY); } /// /// Moves the game object down. /// Precondition: None /// Postcondition: Y == Y@prev + SpeedY /// public void MoveDown() { this.moveY(this.SpeedY); } private void moveX(int x) { this.X += x; } private void moveY(int y) { this.Y += y; } private void render() { var render = this.Sprite as ISpriteRenderer; render?.RenderAt(this.X, this.Y); } /// /// Sets the speed of the game object. /// Precondition: speedX >= 0 AND speedY >=0 /// Postcondition: SpeedX == speedX AND SpeedY == speedY /// /// The speed x. /// The speed y. protected void SetSpeed(int speedX, int speedY) { if (speedX < 0) { throw new ArgumentOutOfRangeException(nameof(speedX)); } if (speedY < 0) { throw new ArgumentOutOfRangeException(nameof(speedY)); } this.SpeedX = speedX; this.SpeedY = speedY; } #endregion } }