Ejemplo n.º 1
0
        private void UpdateTransform(Entity playerShield)
        {
            PlayerShieldComponent shieldComp = playerShield.GetComponent <PlayerShieldComponent>();

            if (shieldComp.ShipEntity == null)
            {
                return;
            }
            if (!playerShipFamily.Matches(shieldComp.ShipEntity))
            {
                throw new Exception("Player shield does not have a valid ship entity within PlayerShieldComponent.");
            }
            Entity playerShip = shieldComp.ShipEntity;

            TransformComponent shieldTransformComp = playerShield.GetComponent <TransformComponent>();
            TransformComponent shipTransformComp   = playerShip.GetComponent <TransformComponent>();

            Vector2 shieldTargetPosition = shipTransformComp.Position
                                           + new Vector2((float)(shieldComp.Radius * Math.Cos(shieldComp.Angle)),
                                                         (float)(shieldComp.Radius * Math.Sin(shieldComp.Angle)));

            shieldTransformComp.Move(shieldTargetPosition - shieldTransformComp.Position);

            Vector2 shieldToShip   = shieldTargetPosition - shipTransformComp.Position;
            float   targetRotation = (float)Math.Atan2(shieldToShip.Y, shieldToShip.X) + (float)(Math.PI / 2); // PI/2 because sprite is rotated 90deg wrong

            shieldTransformComp.Rotate(targetRotation - shieldTransformComp.Rotation);
        }
Ejemplo n.º 2
0
        private void UpdateAngleFromInput(Entity playerShield)
        {
            PlayerShieldComponent shieldComp = playerShield.GetComponent <PlayerShieldComponent>();
            PlayerComponent       playerComp = playerShield.GetComponent <PlayerShieldComponent>().ShipEntity.GetComponent <PlayerComponent>();

            if (playerComp != null)
            {
                shieldComp.Angle = playerComp.Player.InputMethod.GetSnapshot().Angle - shieldComp.Offset;
            }
        }
        public override void Update(float dt)
        {
            foreach (Entity playerShieldEntity in playerShieldEntities)
            {
                TransformComponent    transformComp = playerShieldEntity.GetComponent <TransformComponent>();
                PlayerShieldComponent shieldComp    = playerShieldEntity.GetComponent <PlayerShieldComponent>();

                if (shieldComp.ShipEntity == null)
                {
                    return;
                }
                if (!playerShipFamily.Matches(shieldComp.ShipEntity))
                {
                    throw new Exception("Player shield does not have a valid ship entity within PlayerShieldComponent.");
                }
                Entity             playerShip        = shieldComp.ShipEntity;
                TransformComponent shipTransformComp = playerShip.GetComponent <TransformComponent>();

                Vector2 shieldNormal = transformComp.Position - shipTransformComp.Position;
                shieldNormal.Normalize();
                Vector2 shieldTangent = new Vector2(-shieldNormal.Y, shieldNormal.X);

                foreach (Entity collisionEntity in collisionEntities)
                {
                    TransformComponent collisionTransformComp = collisionEntity.GetComponent <TransformComponent>();
                    CollisionComponent collisionComp          = collisionEntity.GetComponent <CollisionComponent>();

                    Vector2[] collisionEntityCorners =
                    {
                        new Vector2(-collisionComp.BoundingBoxComponent.Width / 2, -collisionComp.BoundingBoxComponent.Height / 2),
                        new Vector2(-collisionComp.BoundingBoxComponent.Width / 2, collisionComp.BoundingBoxComponent.Height / 2),
                        new Vector2(collisionComp.BoundingBoxComponent.Width / 2,  collisionComp.BoundingBoxComponent.Height / 2),
                        new Vector2(collisionComp.BoundingBoxComponent.Width / 2, -collisionComp.BoundingBoxComponent.Height / 2)
                    };
                    bool colliding = false;
                    foreach (Vector2 collisionCorner in collisionEntityCorners)
                    {
                        Vector2 shieldCenterToCorner = transformComp.Position - (collisionCorner + collisionTransformComp.Position);
                        // Cast center-to-corner onto normal and tangent
                        float normalCasted  = Vector2.Dot(shieldCenterToCorner, shieldNormal);
                        float tangentCasted = Vector2.Dot(shieldCenterToCorner, shieldTangent);

                        colliding = colliding || Math.Abs(normalCasted) <= shieldComp.Bounds.Y && Math.Abs(tangentCasted) <= shieldComp.Bounds.X;
                    }
                    if (colliding)
                    {
                        if (!collisionComp.collidingWith.Contains(playerShieldEntity))
                        {
                            collisionComp.collidingWith.Add(playerShieldEntity);
                            EventManager.Instance.QueueEvent(new CollisionStartEvent(playerShieldEntity, collisionEntity));
                        }
                    }
                    else
                    {
                        if (collisionComp.collidingWith.Contains(playerShieldEntity))
                        {
                            collisionComp.collidingWith.Remove(playerShieldEntity);
                            EventManager.Instance.QueueEvent(new CollisionEndEvent(playerShieldEntity, collisionEntity));
                        }
                    }
                }
            }
        }