Esempio n. 1
0
        /// <summary>
        /// Does collision detection between the shooter and the player
        /// </summary>
        /// <param name="shooter"></param>
        static void BulletToPlayer(ServerPlayer shooter)
        {
            // Get the distance between the player and the shooter
            foreach (ServerPlayer player in players)
            {
                // The shooter can't shoot themself, obviously.
                if (player != shooter)
                {
                    // If friendly fire is disabled and the player is
                    // on the same team, don't do collision detection
                    if (!friendlyFire && player.CurrentTeam == shooter.CurrentTeam)
                    {
                        continue;
                    }

                    // Get distance between the player and the enemy
                    Vector2 delta = player.Position - shooter.Position;

                    // Get -2Pi - 2Pi version of the shooter's angle
                    float angle = shooter.Rotation < 0 ? (float)(shooter.Rotation + (2 * Math.PI)) : shooter.Rotation;

                    // Get angle between shooter and player
                    float shooterToPlayerAngle = (float)Math.Atan2(delta.Y, delta.X);

                    // If the angle between the shooter and player is less than 0 radians
                    // add 2 Pi to convert it from -Pi - Pi domain to -2Pi - 2Pi domain
                    shooterToPlayerAngle = shooterToPlayerAngle < 0
                        ? (float)(shooterToPlayerAngle + (2 * Math.PI))
                        : shooterToPlayerAngle;

                    // If the angle of the shooter is within 0.2 radians of the
                    // angle between the shooter and the player, it means they are
                    // not aiming in the opposite direction of the player which would
                    // result in the collision detection returning true
                    if (angle > shooterToPlayerAngle - 0.2f &&
                        angle < shooterToPlayerAngle + 0.2f)
                    {
                        // Get the direction of the shooter
                        Vector2 direction = new Vector2((float)Math.Cos(angle), (float)Math.Sin(angle));

                        // Get distance between the player and any possible obstacles in between the
                        // player and the enemy
                        RayCastResult result = raycaster.RayCastMethod(shooter.Position, direction, 1280,
                                                                       MapData.TileMap, MapData.MapArea, angle);

                        // Get the delta between the collision point and the shooter
                        Vector2 raycastDistance = result.CollisionPos - shooter.Position;

                        // If the raycast had collided with an object in between two players
                        // the distance of the raycast would be shorter, therefore, the player
                        // has no direct line of sight with the other player
                        if (raycastDistance.Length() > delta.Length())
                        {
                            // If the shot passes through the player and they are alive
                            if (Collision.NonAACollision(shooter.Position,
                                                         shooter.Rotation, new Rectangle(
                                                             (int)player.Position.X - 16, (int)player.Position.Y + 16,
                                                             32, 32), player.Rotation) && player.State == ServerClientInterface.PlayerState.Alive)
                            {
                                // Deal the correct amount of damage depending on the weapon
                                switch (shooter.CurrentWeapon.Weapon)
                                {
                                case WeaponData.Weapon.Knife:
                                    break;

                                case WeaponData.Weapon.Ak47:
                                    player.Damage(12, 0);
                                    break;

                                case WeaponData.Weapon.Glock:
                                    break;

                                case WeaponData.Weapon.Awp:
                                    break;

                                case WeaponData.Weapon.Usp:
                                    break;

                                case WeaponData.Weapon.M4A1:
                                    player.Damage(12, 0);
                                    break;
                                }

                                Console.WriteLine("\"" + shooter.UserName + "\" shot \"" + player.UserName + " with " +
                                                  shooter.CurrentWeapon.Weapon);

                                // If the player's health is less than zero, they died. Let everyone know.
                                if (player.Health <= 0)
                                {
                                    player.SetHealth(0);
                                    player.SetArmor(0);
                                    player.SetState(ServerClientInterface.PlayerState.Dead);
                                    Console.WriteLine(shooter.UserName + " killed " + player.UserName +
                                                      " with " + shooter.CurrentWeapon.Weapon);
                                }

                                // Send data to all players
                                outMsg = server.CreateMessage();
                                outMsg.Write(ServerClientInterface.DAMAGE);
                                outMsg.Write(player.Identifier);
                                outMsg.Write(player.Health);
                                outMsg.Write(player.Armor);
                                server.SendToAll(outMsg, NetDeliveryMethod.UnreliableSequenced);
                            }
                        }
                    }
                }
            }
        }