Example #1
0
        /// <summary>
        /// If Ship s does not exist in ships adds it. If the id already exists
        /// in ships, updates reference in ships to s.
        /// </summary>
        public void AddShip(Ship ship)
        {
            // if the ship is null then do nothing
            if (ship == null)
            {
                return;
            }

            // if the ship is dead then remove it from the world
            if (!ship.IsAlive())
            {
                aliveShips.Remove(ship.GetID());
            }
            // if the ship is in the world then replace the old ship with the passed in ship
            else if (aliveShips.ContainsKey(ship.GetID()))
            {
                aliveShips[ship.GetID()] = ship;
            }
            // if the ship is not in the world then add it
            else
            {
                aliveShips.Add(ship.GetID(), ship);
            }

            // we have taken care of the alive ships, now lets keep track of all ships
            AddAllShips(ship);
        }
        /// <summary>
        /// Acts as a drawing delegate for DrawObjectWithTransform
        /// After performing the necessary transformation (translate/rotate)
        /// DrawObjectWithTransform will invoke this method
        /// </summary>
        /// <param name="o">The object to draw</param>
        /// <param name="e">The PaintEventArgs to access the graphics</param>
        private void ShipDrawer(object o, PaintEventArgs e)
        {
            Ship   ship = o as Ship;
            Bitmap shipSprite;
            int    x, y;
            Point  p;

            x = WorldSpaceToImageSpace(this.Size.Width, (int)ship.GetLocation().GetX() - (SHIP_SIZE.Width / 2));
            y = WorldSpaceToImageSpace(this.Size.Width, (int)ship.GetLocation().GetY() - (SHIP_SIZE.Height / 2));
            p = new Point(x, y);

            if (theWorld.GetMarioMode() && ship.GetID() == theWorld.GetCurrentPlayer())
            {
                shipSprite = marioShip;
            }
            else
            {
                if (ship.GetThrust())
                {
                    shipSprite = shipSpritesThrust[ship.GetID() % shipSprites.Count];
                }
                else
                {
                    shipSprite = shipSprites[ship.GetID() % shipSprites.Count];
                }
            }

            e.Graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
            e.Graphics.DrawImage(shipSprite, p);
        }
Example #3
0
 /// <summary>
 /// This method "kills" a ship and adds it to a list to respawn.
 /// </summary>
 /// <param name="ship"></param>
 public void KillShip(Ship ship)
 {
     lock (shipDictionary)
     {
         this.RemoveShip(ship.GetID());
         ship.Died();
         deadShipDictionary.Add(ship.GetID(), ship);
     }
 }
Example #4
0
 /// <summary>
 /// When a ship is added to the game we update the info or add it
 /// to the world
 /// </summary>
 private void AddAllShips(Ship ship)
 {
     // if the ship is in the world, update its reference
     if (allShips.ContainsKey(ship.GetID()))
     {
         allShips[ship.GetID()] = ship;
     }
     // if the ship is not in the world then add it
     else
     {
         allShips.Add(ship.GetID(), ship);
     }
 }
Example #5
0
 /// <summary>
 /// Helper method that helps to draw the ship. Picks the correct sprite for current ship.
 /// </summary>
 /// <param name="s">a ship object</param>
 /// <returns>the corresponding Image sprite</returns>
 private Image DrawShip(Ship s)
 {
     // if coasting, return coasting
     if (!s.IsThrusting())
     {
         // retrieve image from CoastingShips image dictionary
         return(CoastingShips[s.GetID() % 8]);
     }
     // else, return thrusting
     else
     {
         // retrieve image from CoastingShips image dictionary
         return(ThrustingShips[s.GetID() % 8]);
     }
 }
Example #6
0
        private void hpDrawer(object o, PaintEventArgs e)
        {
            Ship s       = o as Ship;
            int  hpWidth = 8;

            DrawHP(s.GetID() % 8, hpWidth, e);
        }
        /// <summary>
        /// Acts as a drawing delegate for DrawObjectWithTransform
        /// After performing the necessary transformation (translate/rotate)
        /// DrawObjectWithTransform will invoke this method
        /// </summary>
        /// <param name="o">The object to draw</param>
        /// <param name="e">The PaintEventArgs to access the graphics</param>
        private void ShipDrawer(object o, PaintEventArgs e)
        {
            Ship p = o as Ship;

            int shipWidth = 35;

            if (p.GetHealth() == 0)
            {
                e.Graphics.DrawImage(dead_ship, -(shipWidth / 2), -(shipWidth / 2), shipWidth, shipWidth); return;
            }
            if (p.isThrustOn())
            {
                e.Graphics.DrawImage(shipImagesThrust[p.GetID() % 8], -(shipWidth / 2), -(shipWidth / 2), shipWidth, shipWidth);
            }
            else
            {
                e.Graphics.DrawImage(shipImagesCoast[p.GetID() % 8], -(shipWidth / 2), -(shipWidth / 2), shipWidth, shipWidth);
            }
        }
Example #8
0
 /// <summary>
 /// Detects whether or not a ship is hit by a projectile
 /// </summary>
 private void CollisionWithAProjectile(Ship ship, Projectile projectile)
 {
     if (ship.GetID() != projectile.GetOwner() && ship.IsAlive())
     {
         if (WithinARadius(ship.GetLocation(), projectile.GetLocation(), shipSize))
         {
             // the passed in ship was hit by a projectile so we update health and remove
             // the projectile from the world
             HitAProjectile(ship, projectile);
         }
     }
 }
Example #9
0
        /// <summary>
        /// Returns true if ship and projectile are within collision distance, otherwise false.
        /// </summary>
        /// <param name="ship"></param>
        /// <param name="proj"></param>
        /// <returns></returns>
        public bool HasCollidedShipProj(Ship ship, Projectile proj)
        {
            Vector2D shipLoc        = ship.GetLocation();
            Vector2D projLoc        = proj.GetLocation();
            Vector2D distanceVector = shipLoc - projLoc;
            double   distanceLength = distanceVector.Length();

            if (distanceLength <= ShipSize && (ship.GetID() != proj.GetOwner()))
            {
                return(true);
            }
            return(false);
        }
Example #10
0
        /// <summary>
        /// Spawns a ship using the properties of an existing ship. Used for respawning existing ships which have been killed. Spawn location is randomized.
        /// </summary>
        /// <param name="ship">ship to be respawned</param>
        public void RespawnShip(Ship ship)
        {
            Vector2D newLoc    = new Vector2D(rand.Next(-worldSize / 2, worldSize / 2), rand.Next(-worldSize / 2, worldSize / 2));
            Vector2D newOrient = new Vector2D(0, -1);
            Ship     newShip   = new Ship(ship.GetID(), newLoc, newOrient, false, ship.GetName(), ShipHealth, ship.GetScore());

            // Set ship's modifiable variables
            newShip.setAccelRate(ShipAccel);
            newShip.SetRespawnDelay(ShipRespawnRate);
            newShip.SetFireDelay(FireDelay);
            newShip.setWorldSize(this.worldSize);

            this.addShip(newShip);
        }
Example #11
0
        /// <summary>
        /// This method will either add a new ship to the world, or update
        /// a current ship with a new ship depending on the ship's ID.
        /// </summary>
        /// <param name="newShip"></param>
        public void addShip(Ship newShip)
        {
            int shipId = newShip.GetID();

            lock (shipDictionary)
            {
                if (shipDictionary.ContainsKey(shipId))
                {
                    shipDictionary.Remove(shipId);
                    shipDictionary.Add(shipId, newShip);
                }
                else
                {
                    shipDictionary.Add(shipId, newShip);
                }
            }
        }
Example #12
0
        /// <summary>
        /// Creates a projectile with the same location and orientation of the ship which fired it, and adds it to the world. Then tells the ship that is projectile has been fired.
        /// </summary>
        /// <param name="currentShip">ship which fired this projectile</param>
        public void SpawnProjectile(Ship currentShip)
        {
            if (currentShip.ReadyToFire())
            {
                // Need to adjust spawn location Y-axis
                Vector2D projLocation = new Vector2D(currentShip.GetLocation().GetX(), currentShip.GetLocation().GetY());
                Vector2D projDir      = new Vector2D(currentShip.GetOrientation());
                Vector2D projVeloc    = new Vector2D(currentShip.GetOrientation().GetX() * ProjVelocity, currentShip.GetOrientation().GetY() * ProjVelocity);

                Projectile newProj = new Projectile(projIDs, projLocation, projDir, true, currentShip.GetID());   // May want to shift projectile
                newProj.SetVelocity(projVeloc);
                this.addProjectile(newProj);
                projIDs++;

                lock (shipDictionary)
                {
                    this.shipDictionary[currentShip.GetID()].FireProjectile();
                }
            }
        }
Example #13
0
        public void ReceiveData(SocketState state)
        {
            StringBuilder          sb       = state.sb;
            JsonSerializerSettings settings = new JsonSerializerSettings
            {
                MissingMemberHandling = MissingMemberHandling.Error
            };

            try
            {
                char[]   separator = new char[] { '\n' };
                string[] strArray  = sb.ToString().Split(separator, StringSplitOptions.RemoveEmptyEntries);
                int      length    = strArray.Length;
                if (sb.ToString()[sb.Length - 1] != '\n')
                {
                    length--;
                }
                List <Ship> list  = new List <Ship>();
                List <Ship> list2 = new List <Ship>();
                for (int i = 0; i < length; i++)
                {
                    string json = strArray[i];
                    if ((json[0] == '{') && (json[json.Length - 1] == '}'))
                    {
                        Ship       item       = null;
                        Projectile projectile = null;
                        Star       star       = null;
                        JObject    obj1       = JObject.Parse(json);
                        JToken     token      = obj1["ship"];
                        JToken     token2     = obj1["proj"];
                        if (token != null)
                        {
                            item = JsonConvert.DeserializeObject <Ship>(json, settings);
                        }
                        if (token2 != null)
                        {
                            projectile = JsonConvert.DeserializeObject <Projectile>(json, settings);
                        }
                        if (obj1["star"] != null)
                        {
                            star = JsonConvert.DeserializeObject <Star>(json, settings);
                        }
                        World world = this.world;
                        lock (world)
                        {
                            if (item != null)
                            {
                                if (this.world.GetShips().ContainsKey(item.GetID()))
                                {
                                    if (this.world.GetShips()[item.GetID()].Alive && !item.Alive)
                                    {
                                        list2.Add(item);
                                    }
                                }
                                else
                                {
                                    list.Add(item);
                                }
                                this.world.GetShips()[item.GetID()] = item;
                            }
                            if (projectile != null)
                            {
                                if (projectile.IsAlive())
                                {
                                    this.world.GetProjectiles()[projectile.GetID()] = projectile;
                                }
                                else if (this.world.GetProjectiles().ContainsKey(projectile.GetID()))
                                {
                                    this.world.GetProjectiles().Remove(projectile.GetID());
                                }
                            }
                            if (star != null)
                            {
                                this.world.GetStars()[star.GetID()] = star;
                            }
                        }
                        sb.Remove(0, json.Length + 1);
                    }
                }
                foreach (Ship ship2 in list2)
                {
                    this.ShipDied(ship2);
                }
                foreach (Ship ship3 in list)
                {
                    this.NewShip(ship3);
                }
                this.FrameTick();
            }
            catch (JsonReaderException)
            {
            }
            catch (Exception)
            {
            }
            state.call_me = new Action <SocketState>(this.ReceiveData);
            Networking.RequestMoreData(state);
        }
Example #14
0
        /// <summary>
        /// Computes the acceleration, velocity, and position of the passed in ship
        /// </summary>
        public void MotionForShips(Ship ship)
        {
            // if the ship isn't alive, just skip it
            if (!ship.IsAlive())
            {
                return;
            }

            // handle left turn command
            if (ship.TurnLeft)
            {
                ship.GetDirection().Rotate(-turningRate);
                ship.TurnLeft = false;
            }
            // handle right turn command
            else if (ship.TurnRight)
            {
                ship.GetDirection().Rotate(turningRate);
                ship.TurnRight = false;
            }

            // spawn a projectile if projectile command has been given
            if (ship.FireProjectile)
            {
                Projectile p = new Projectile(ship.GetID(), projectileID++, ship.GetLocation(), ship.GetDirection());
                AddProjectile(p);
                ship.FireProjectile = false;
                ship.ResetFireTimer();

                // we just fired a projectile, increment total number of fired projectiles
                ship.ShotsFired++;
            }

            //get a zero vector
            Vector2D acceleration = new Vector2D(0.0, 0.0);

            //compute the acceleration caused by the star
            foreach (Star star in stars.Values)
            {
                Vector2D g = star.GetLocation() - ship.GetLocation();
                g.Normalize();
                acceleration = acceleration + g * star.GetMass();
            }

            if (ship.HasThrust())
            {
                //compute the acceleration due to thrust
                Vector2D t = new Vector2D(ship.GetDirection());
                t            = t * engineStrength;
                acceleration = acceleration + t;
            }

            // recalculate velocity and location
            ship.SetVelocity(ship.GetVelocity() + acceleration);
            ship.SetLocation(ship.GetVelocity() + ship.GetLocation());
            // check to see if ship is off screen
            Wraparound(ship);

            // check for collisions with any star
            foreach (Star star in stars.Values)
            {
                CollisionWithAStar(ship, star);
            }

            // check for collisions with any projectiles
            foreach (Projectile proj in projectiles.Values)
            {
                CollisionWithAProjectile(ship, proj);
            }

            ship.IncrementFireTimer();
        }