示例#1
0
        /// <summary>
        /// The method takes the current JsonMessage from ProcessMessage and
        /// updates the models within the world.
        /// </summary>
        /// <param name="JsonMessage">Message passed from ProccessMessage</param>
        /// <param name="objectType">Int number representing the object's type</param>
        private void UpdateWorldModel(string JsonMessage, int objectType)
        {
            // Enter switch case based on what object has identified as and
            // update the models in the world
            switch (objectType)
            {
            case 0:
                // Convert Json message into Tank object
                Tank curTank = JsonConvert.DeserializeObject <Tank>(JsonMessage);
                //The tank object is us
                if (curTank.GetID() == playerID)
                {
                    selfTank = curTank;
                }
                lock (theWorld.Tanks)
                {
                    // Check if the world contains the object already
                    if (theWorld.Tanks.ContainsKey(curTank.GetID()))
                    {
                        // Remove the tank so that it can be updated
                        theWorld.Tanks.Remove(curTank.GetID());
                    }
                    //Add the tank to the world
                    theWorld.Tanks.Add(curTank.GetID(), curTank);
                }
                break;

            case 1:
                // Convert Json message into Wall object
                Wall curWall = JsonConvert.DeserializeObject <Wall>(JsonMessage);
                // No check needed to see if it already exists since walls will only be added once
                // Add the wall into the world
                lock (theWorld.Walls)
                {
                    theWorld.Walls.Add(curWall.GetID(), curWall);
                }
                break;

            case 2:
                // Convert Json message into Projectile object
                Projectile curProjectile = JsonConvert.DeserializeObject <Projectile>(JsonMessage);
                // Check if the world contains the object already
                lock (theWorld.Projectiles)
                {
                    if (theWorld.Projectiles.ContainsKey(curProjectile.getID()))
                    {
                        // Remove the projectile so that it can be updated
                        theWorld.Projectiles.Remove(curProjectile.getID());
                    }
                    //Check if projectile is still active
                    if (!curProjectile.Died)
                    {
                        // Re-add the projectile back into the world
                        theWorld.Projectiles.Add(curProjectile.getID(), curProjectile);
                    }
                }
                break;

            case 3:
                // Convert Json message into PowerUp object
                PowerUp curpowerUp = JsonConvert.DeserializeObject <PowerUp>(JsonMessage);
                // Check if the world contains the object already
                lock (theWorld.PowerUps)
                {
                    if (theWorld.PowerUps.ContainsKey(curpowerUp.getID()))
                    {
                        // Remove the PowerUp so that it can be updated
                        theWorld.PowerUps.Remove(curpowerUp.getID());
                    }
                    //Check if the powerup was collected or not
                    if (!curpowerUp.collected)
                    {
                        // Re-add the PowerUp back into the world
                        theWorld.PowerUps.Add(curpowerUp.getID(), curpowerUp);
                    }
                }
                break;

            case 4:
                // Convert Json message into Beam object
                Beam curBeam = JsonConvert.DeserializeObject <Beam>(JsonMessage);
                // Check if the world contains the object already
                lock (theWorld.Beams)
                {
                    // add the Beam into the world
                    theWorld.Beams.Add(curBeam.GetID(), curBeam);
                    //Trigger a timer with the beam so that it is removed from the world after a certain time
                    beamTimer = new Timer(new TimerCallback(RemoveBeam), curBeam, 500, -1);
                }
                break;
            }
            UpdateWorld();
        }
示例#2
0
        /// <summary>
        /// General purpose helper method that checks if an object collides with
        /// anything else in the world.
        /// </summary>
        /// <param name="o"></param> The object we want to check against everything else
        /// <param name="objectType"></param> A integer code that tells the method what kind of object o is
        /// <returns></returns>
        private bool CheckForCollision(Object o, int objectType)
        {
            //Collision flags
            bool collisionStatusX = false;
            bool collisionStatusY = false;

            //If o is a projectile
            if (objectType == 0)
            {
                Projectile proj = o as Projectile;

                //returns true if projectile if it leaves world
                if ((proj.Location.GetX() > serverWorld.Size / 2) || (proj.Location.GetX() < -(serverWorld.Size / 2)) || (proj.Location.GetY() > serverWorld.Size / 2) || (proj.Location.GetY() < -serverWorld.Size / 2))
                {
                    return(true);
                }

                //Check collsion against walls
                foreach (Wall curWall in serverWorld.Walls.Values)
                {
                    //Get range of values for x
                    double high = Math.Max(curWall.GetP2().GetX(), curWall.GetP1().GetX()) + 25;
                    double low  = Math.Min(curWall.GetP2().GetX(), curWall.GetP1().GetX()) - 25;

                    //Check if projectile is in between range of x values
                    if (proj.Location.GetX() < high && proj.Location.GetX() > low)
                    {
                        collisionStatusX = true;
                    }

                    //Get range of values for y
                    high = Math.Max(curWall.GetP2().GetY(), curWall.GetP1().GetY()) + 25;
                    low  = Math.Min(curWall.GetP2().GetY(), curWall.GetP1().GetY()) - 25;

                    //Check if projectile is in between range of y values
                    if (proj.Location.GetY() < high && proj.Location.GetY() > low)
                    {
                        collisionStatusY = true;
                    }

                    //If both are within range, projectile collided with wall so return true
                    if (collisionStatusX && collisionStatusY)
                    {
                        return(true);
                    }
                    else //Projectile did not collide with this wall, reset flags and move on to next segment
                    {
                        collisionStatusY = false;
                        collisionStatusX = false;
                        continue;
                    }
                }

                //Check collision against tanks
                lock (serverWorld.Tanks)
                {
                    foreach (Tank t in serverWorld.Tanks.Values)
                    {
                        //Create radius around tank
                        Vector2D radius = proj.Location - t.Location;

                        //If distance between projectile and tank is less than 30, its a hit
                        if (radius.Length() < 30 && proj.getOwner() != t.GetID())
                        {
                            //Can't hit a dead tank
                            if (t.HealthLevel == 0)
                            {
                                return(false);
                            }

                            //It's a hit if tank has health
                            if (t.HealthLevel > 0)
                            {
                                //decrement tank health
                                t.HealthLevel -= 1;
                            }

                            //if health is 0, then it has died
                            if (t.HealthLevel == 0)
                            {
                                t.HasDied = true;
                                //increment player score
                                serverWorld.Tanks[proj.getOwner()].Score++;
                            }
                            return(true);
                        }
                    }
                }
            }

            //object is a tank
            else if (objectType == 1)
            {
                Tank t = o as Tank;

                //Check for powerup collision and wall collision
                foreach (Wall curWall in serverWorld.Walls.Values)
                {
                    //Get range of values for x
                    double high = Math.Max(curWall.GetP2().GetX(), curWall.GetP1().GetX()) + 55;
                    double low  = Math.Min(curWall.GetP2().GetX(), curWall.GetP1().GetX()) - 55;

                    //Check if projectile is in between range of x values
                    if (t.Location.GetX() < high && t.Location.GetX() > low)
                    {
                        collisionStatusX = true;
                    }

                    //Get range of values for y
                    high = Math.Max(curWall.GetP2().GetY(), curWall.GetP1().GetY()) + 55;
                    low  = Math.Min(curWall.GetP2().GetY(), curWall.GetP1().GetY()) - 55;

                    //Check if projectile is in between range of y values
                    if (t.Location.GetY() < high && t.Location.GetY() > low)
                    {
                        collisionStatusY = true;
                    }

                    //If both are within range, projectile collided with wall so return true
                    if (collisionStatusX && collisionStatusY)
                    {
                        return(true);
                    }
                    else //Projectile did not collide with this wall, reset flags and move on to next segment
                    {
                        collisionStatusY = false;
                        collisionStatusX = false;
                        continue;
                    }
                }

                lock (serverWorld.PowerUps)
                {
                    foreach (PowerUp curPower in serverWorld.PowerUps.Values)
                    {
                        //Create radius around tank
                        Vector2D radius = curPower.position - t.Location;

                        //If distance between powerup and tank is less than 30, its a hit
                        if (radius.Length() < 30)
                        {
                            // Increment powerUp number if powerup is not collected
                            if (!curPower.collected)
                            {
                                t.CollectPowerup();
                            }

                            //Powerup has been collected
                            curPower.collected = true;
                            return(false);
                        }
                    }
                }
            }

            //object is a powerup
            else if (objectType == 2)
            {
                PowerUp power = o as PowerUp;
                foreach (Wall curWall in serverWorld.Walls.Values)
                {
                    //Get range of values for x
                    double high = Math.Max(curWall.GetP2().GetX(), curWall.GetP1().GetX()) + 25;
                    double low  = Math.Min(curWall.GetP2().GetX(), curWall.GetP1().GetX()) - 25;

                    //Check if powerUp is in between range of x values
                    if (power.position.GetX() < high && power.position.GetX() > low)
                    {
                        collisionStatusX = true;
                    }

                    //Get range of values for y
                    high = Math.Max(curWall.GetP2().GetY(), curWall.GetP1().GetY()) + 25;
                    low  = Math.Min(curWall.GetP2().GetY(), curWall.GetP1().GetY()) - 25;

                    //Check if powerUp is in between range of y values
                    if (power.position.GetY() < high && power.position.GetY() > low)
                    {
                        collisionStatusY = true;
                    }

                    //If both are within range, powerUp collided with wall so return true
                    if (collisionStatusX && collisionStatusY)
                    {
                        return(true);
                    }
                    else //PowerUp did not collide with this wall, reset flags and move on to next segment
                    {
                        collisionStatusY = false;
                        collisionStatusX = false;
                        continue;
                    }
                }

                lock (serverWorld.Tanks)
                {
                    foreach (Tank t in serverWorld.Tanks.Values)
                    {
                        //Create radius around tank
                        Vector2D radius = power.position - t.Location;

                        //If distance between powerUp and tank is less than 30, it can't spawn
                        if (radius.Length() < 30)
                        {
                            return(true);
                        }
                    }
                }
            }
            return(false);
        }
示例#3
0
        /// <summary>
        /// Deserializes an object from an inputted string. This is handled differently depending on which
        /// kind of object it is.
        /// </summary>
        /// <param name="serializedObject">Serialized object string</param>
        private void UpdateObject(string serializedObject)
        {
            JObject obj = JObject.Parse(serializedObject);

            //Tank
            JToken token = obj["tank"];

            if (token != null)
            {
                Tank tank = JsonConvert.DeserializeObject <Tank>(serializedObject);
                TheWorld.Tanks[tank.ID] = tank;

                //Assigns a color to the tank and increments the seen players
                if (!TankColorRecord.ContainsKey(tank.ID))
                {
                    TankColorRecord.Add(tank.ID, SeenPlayers % 8);
                    SeenPlayers++;
                }

                //If it disconnected the tank is removed
                if (tank.Disconnected)
                {
                    TheWorld.Tanks.Remove(tank.ID);
                }

                //It also notes that the walls must be done if it's importing a tank
                wallsDone = true;
                return;
            }

            //Projectile
            token = obj["proj"];
            if (token != null)
            {
                Projectile proj = JsonConvert.DeserializeObject <Projectile>(serializedObject);
                TheWorld.Projectiles[proj.ID] = proj;
                //Removes projectiles when they die
                if (proj.Died)
                {
                    TheWorld.Projectiles.Remove(proj.ID);
                }
                return;
            }

            //Powerup
            token = obj["power"];
            if (token != null)
            {
                PowerUp power = JsonConvert.DeserializeObject <PowerUp>(serializedObject);
                TheWorld.PowerUps[power.ID] = power;
                //Removes powerups when they die
                if (power.Died)
                {
                    TheWorld.PowerUps.Remove(power.ID);
                }
                return;
            }

            //Beam
            token = obj["beam"];
            if (token != null)
            {
                Beam beam = JsonConvert.DeserializeObject <Beam>(serializedObject);
                TheWorld.Beams[beam.ID] = beam;
                return;
            }

            //Wall
            token = obj["wall"];
            if (token != null)
            {
                Wall wall = JsonConvert.DeserializeObject <Wall>(serializedObject);
                TheWorld.Walls[wall.ID] = wall;
                return;
            }
        }
示例#4
0
 /// <summary>
 /// Updates all powerups currently present in The World
 /// </summary>
 /// <param name="t"> the powerup thats being updated </param>
 public void UpdatePowerUp(PowerUp p)
 {
     PowerUps[p.ID] = p;
 }
        /// <summary>
        /// Draws Powerup
        /// </summary>
        /// <param name="o"></param>
        /// <param name="e"></param>
        private void PowerUpDrawer(object o, PaintEventArgs e)
        {
            PowerUp p = o as PowerUp;

            e.Graphics.DrawImage(powerup, -8, -8);
        }