예제 #1
0
        /// <summary>
        /// Constructor for platform
        /// </summary>
        /// <param name="a_world"></param>World in which the platform is created and drawn
        /// <param name="a_leftPoint"></param>Left end point of the platform
        /// <param name="a_rightPoint"></param>Right end point of the platform
        /// <param name="a_texture"></param>Platform texture
        public Platform(World a_world, Vector2 a_leftPoint, Vector2 a_rightPoint, Texture2D a_texture)
        {
            m_world = a_world;

            m_leftPoint  = a_leftPoint;
            m_rightPoint = a_rightPoint;

            // Initializing platform position and dimensions
            float   width    = MathX.LineLength(a_leftPoint, a_rightPoint);
            Vector2 midPoint = MathX.Midpoint(a_leftPoint, a_rightPoint);

            m_sprite   = new Sprite(a_world, a_texture, new Vector2(width, 10), 0.0f, midPoint);
            m_rotation = MathX.GetAngleBetween(a_leftPoint, a_rightPoint) * (float)(Math.PI / 180);
            m_sprite.m_body.Rotation = m_rotation;
            m_sprite.m_body.BodyType = BodyType.Static;
            m_sprite.m_body.UserData = "platform";
            m_currentState           = PlatformState.ROOF;

            // Initializing normal vector
            Vector2 difference = a_leftPoint - a_rightPoint;

            m_normalVector = new Vector2(-difference.Y, difference.X);
            m_normalVector.Normalize();

            m_connection = new Sprite(a_world, ContentLibrary.circleTexture, 5, 0.0f, a_leftPoint);
            m_connection.m_body.BodyType = BodyType.Static;
            m_connection2 = new Sprite(a_world, ContentLibrary.circleTexture, 5, 0.0f, a_rightPoint);
            m_connection2.m_body.BodyType = BodyType.Static;
            //m_connection.m_body.UserData = "platform";
        }
예제 #2
0
        /// <summary>
        /// Updates the turret tracking and shooting
        /// </summary>
        /// <param name="a_gameTime"></param>
        public void Update(GameTime a_gameTime)
        {
            Vector2 projection;
            float   projectionLength = 0;

            projectionLength = MathX.LineLength(m_player.m_body.Position, m_top.m_body.Position) / 3.5f;

            projection = (Vector2.Normalize(-m_state.m_world.Gravity) * projectionLength) + m_player.m_body.Position;

            m_angle = projection - m_top.m_body.Position;

            float rot = (float)(Math.Atan2(m_angle.Y, m_angle.X) - (Math.PI / 2));

            if (rot > 2 * Math.PI)
            {
                rot = 0;
            }

            m_top.m_body.Rotation = rot;

            m_cooldown -= (float)a_gameTime.ElapsedGameTime.TotalSeconds;
            if (m_cooldown >= 0)
            {
                m_cooldown -= (float)a_gameTime.ElapsedGameTime.TotalSeconds;
            }
            else
            {
                //check distance between turret and player
                if (MathX.LineLength(m_player.m_body.Position, m_top.m_body.Position) < 3)
                {
                    //check if the target is within rotation range
                    float upperRange = m_base.m_body.Rotation + (float)(Math.PI / 3);
                    float lowerRange = m_base.m_body.Rotation - (float)(Math.PI / 3);

                    if (upperRange > (float)(Math.PI / 2) && m_top.m_body.Rotation < 0)
                    {
                        upperRange -= 2 * (float)Math.PI;
                        lowerRange -= 2 * (float)Math.PI;
                    }

                    if (lowerRange < (float)(-3 * (Math.PI / 2)) && m_top.m_body.Rotation > 0)
                    {
                        lowerRange += 2 * (float)Math.PI;
                        upperRange += 2 * (float)Math.PI;
                    }

                    if (m_top.m_body.Rotation > lowerRange && m_top.m_body.Rotation < upperRange)
                    {
                        List <Fixture> collision     = m_state.m_world.RayCast(ConvertUnits.ToSimUnits(m_top.Position), m_player.m_body.Position);
                        Fixture        platformFound = collision.Find(x => (string)x.Body.UserData == "platform");
                        if (platformFound == null)
                        {
                            Shoot(m_state.m_world);
                            m_cooldown = 2;
                        }
                    }
                }
            }
        }