AbstractedSheep.ShuttleTrackerWorld.World.AddOrUpdateShuttle C# (CSharp) Method

AddOrUpdateShuttle() public method

Adds a shuttle to the world or updates the position of an existing shuttle.
public AddOrUpdateShuttle ( int shuttleId, Coordinate location, string name, int bearing, string cardinalPoint, int speed, int route = -1 ) : void
shuttleId int The ID number of the shuttle.
location Coordinate The shuttle's current location.
name string The name of the shuttle.
bearing int The heading of the shuttle in degrees from north.
cardinalPoint string The heading of the shuttle as a cardinal direcation (e.g. Northwest).
speed int The speed of the shuttle in miles per hour.
route int The id of the shuttle route. -1 indicates that the shuttle is not on a route.
return void
        public void AddOrUpdateShuttle(int shuttleId, Coordinate location, string name, int bearing, string cardinalPoint, int speed, int route = -1)
        {
            Shuttle s = this.shuttles[shuttleId];
            Route r = this.routes[route];

            if (s == null)
            {
                s = new Shuttle();
                s.Id = shuttleId;
                s.Location = location;
                s.Name = name;
                s.Bearing = bearing;
                s.CardinalPoint = cardinalPoint;
                s.Speed = speed;
                s.LastUpdateTime = CurrentTimeMillis();

                this.shuttles.Add(s.Id, s);

                if (r != null)
                {
                    s.CurrentRoute = r;
                    r.shuttles.Add(s.Id, s);
                }

                s.SnapToRoute();
            }
            else
            {
                s.LastUpdateTime = CurrentTimeMillis();
                s.Location = location;
                s.Speed = speed;
                s.Bearing = bearing;
                s.CardinalPoint = cardinalPoint;
                s.Name = name;

                if (r == null && s.CurrentRoute != null)
                {
                    s.CurrentRoute.shuttles.Remove(s.Id);
                    s.CurrentRoute = null;
                }
                else if (r != null && s.CurrentRoute != null && s.CurrentRoute != r)
                {
                    s.CurrentRoute.shuttles.Remove(s.Id);
                    s.CurrentRoute = r;
                    r.shuttles.Add(s.Id, s);
                }
                else if (r != null && s.CurrentRoute == null)
                {
                    s.CurrentRoute = r;
                    r.shuttles.Add(s.Id, s);
                }

                s.SnapToRoute();
            }
        }