Ejemplo n.º 1
0
        public void Update()
        {
            bool deadFlag = false;

            lock (this)
            {
                // use ship.Update and projectile.Update to calculate state of the world
                foreach (Ship s in ship.Values)
                {
                    if (s.GetActive())
                    {
                        //changed update ship to a bool so that we can implement a respawn mechanism
                        //if the ship is alive update the ship
                        //if the ship should technically be dead then run respawn method
                        if (s.Update(star, s))
                        {
                            if (s.IsFiring())
                            {
                                if (s.Pew(time))
                                {
                                    Proj pro = new Proj(AssignIDProj(), s.GetLocation(), s.GetOrientation(), s.GetID());
                                    proj.Add(pro.GetID(), pro);
                                }
                            }
                            //implement wrap around.
                            if (s.GetLocation().GetX() > WorldSize / 2 || s.GetLocation().GetX() < -WorldSize / 2)
                            {
                                s.SetShipLocation(-s.GetLocation().GetX(), s.GetLocation().GetY());
                            }
                            if (s.GetLocation().GetY() > WorldSize / 2 || s.GetLocation().GetY() < -WorldSize / 2)
                            {
                                s.SetShipLocation(s.GetLocation().GetX(), -s.GetLocation().GetY());
                            }
                        }
                        else
                        {
                            purgatory.Add(s);
                            deadFlag = true;
                        }
                    }
                }
                if (deadFlag)
                {
                    lock (purgatory)
                    {
                        Die(purgatory);
                    }
                }


                foreach (Proj p in proj.Values)
                {
                    if (p.GetActive())
                    {
                        //checks to see if the projectile hits the edge of the world
                        if (p.Update(star, ship))
                        {
                            if (p.GetLocation().Length() > Math.Sqrt(2.0 * (WorldSize / 2.0) * (WorldSize / 2.0)))
                            {
                                p.SetActive(false);
                            }
                            //loop through and checks to see if the projectiles are hitting
                            foreach (Ship ships in ship.Values)
                            {
                                if (ships.Alive() && p.GetOwner() != ships.GetID())
                                {
                                    double collision = (p.GetLocation() - ships.GetLocation()).Length();

                                    //detects collosion
                                    if (collision < 25)
                                    {
                                        p.SetActive(false);

                                        ships.Hit();
                                        ship[p.GetOwner()].HitCount();
                                        //kills the ship and increments points
                                        if (ships.Alive() == false)
                                        {
                                            if (this.ship.ContainsKey(p.GetOwner()))
                                            {
                                                ship[p.GetOwner()].Point();
                                            }
                                            ships.SetShipActive(false);
                                            deadFlag = true;
                                            purgatory.Add(ships);
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }

            time++;
        }