Source

HI-2DRTSSGMDDPP1 / GameProgram / GameProgram / PlanetarySystem.cs

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

namespace GameProgram
{
  public class PlanetarySystem : GameEntity, Location
  {
    private StarSystem starSystem;
    private Planet planet;
    private List<Moon> moons = new List<Moon>();
    private Vector2 coordinatesWrtStarSystemOrigin = Vector2.Zero;
    //private Vector2 velocityWrtStarSystemOrigin = Vector2.Zero;
    //private Vector2 accelerationWrtStarSystemOrigin = Vector2.Zero;
    private float parentOrbitRadius = 0.0F;
    private float longitude = 0.0F;
    private float angularSpeed = ( ( 2F * (float)Math.PI ) / 60.0F );

    public PlanetarySystem( StarSystem starSystem, float parentOrbitRadius, float initialLongitude )
    {
      this.starSystem = starSystem;
      this.parentOrbitRadius = parentOrbitRadius;
      this.longitude = initialLongitude;

      coordinatesWrtStarSystemOrigin.X = parentOrbitRadius * (float)Math.Cos( longitude );
      coordinatesWrtStarSystemOrigin.Y = parentOrbitRadius * (float)Math.Sin( longitude );

      Random randomNumber = new Random( System.DateTime.Now.Millisecond );

      angularSpeed = ( randomNumber.Next( -100, 100 ) % 4 )+1 * ( ( 2F * (float)Math.PI ) / 60.0F );
    }


    public Planet Planet
    {
      get
      {
        return planet;
      }
      set
      {
        planet = value;
      }
    }

    public Moon Moon
    {
      get
      {
        throw new System.NotImplementedException();
      }
      set
      {
      }
    }

    //internal void addPlanet( GameProgram.Planet newPlanet )
    //{
    //  throw new NotImplementedException();
    //}

    internal void addMoon( GameProgram.Moon newMoon )
    {
      moons.Add( newMoon );
    }


    public Vector2 getAbsolutePosition()
    {
      return coordinatesWrtStarSystemOrigin;
    }

    public override List<VisableGameEntity> getChildVisableGameEntities()
    {
      List<VisableGameEntity> visableGameEntities = new List<VisableGameEntity>();

      visableGameEntities.Add( planet );

      foreach (Moon moon in moons)
      {
        visableGameEntities.Add( moon );
      }

      return visableGameEntities;
    }

    public override void update( GameTime gameTime )
    {

      //float distanceTravelled = angularSpeed 
      //                          * (float)gameTime.ElapsedGameTime.TotalSeconds
      //                          * parentOrbitRadius;

      longitude += angularSpeed * (float)gameTime.ElapsedGameTime.TotalSeconds;

      //coordinatesWrtStarSystemOrigin += velocityWrtStarSystemOrigin * (float)gameTime.ElapsedGameTime.TotalSeconds;

      coordinatesWrtStarSystemOrigin.X = parentOrbitRadius * (float)Math.Cos( longitude );
      coordinatesWrtStarSystemOrigin.Y = parentOrbitRadius * (float)Math.Sin( longitude );      

      planet.update( gameTime );

      foreach (Moon moon in moons)
      {
        moon.update( gameTime );
      }
    }
    
  }
}