コード例 #1
0
        protected override void OnInitialize()
        {
            if (!Engine.GetEntities().Contains(LaserEnemyEntity))
            {
                SetNext(null);
                Kill();
                return;
            }

            // Create laser beam if the laser enemy doesn't currently have one
            LaserEnemyComponent laserEnemyComp = LaserEnemyEntity.GetComponent <LaserEnemyComponent>();
            TransformComponent  transformComp  = LaserEnemyEntity.GetComponent <TransformComponent>();

            if (laserEnemyComp.LaserBeamEntity == null)
            {
                laserEnemyComp.LaserBeamEntity = LaserBeamEntity.Create(Engine, transformComp.Position, false);
            }

            LaserBeamComponent laserBeamComp = laserEnemyComp.LaserBeamEntity.GetComponent <LaserBeamComponent>();

            laserBeamComp.Thickness          = 0;
            laserBeamComp.InteractWithShield = true;
            laserBeamComp.ComputeReflection  = false;

            // Remove CollisionComponent if the laser beam has one
            if (laserEnemyComp.LaserBeamEntity.HasComponent <CollisionComponent>())
            {
                laserEnemyComp.LaserBeamEntity.RemoveComponent <CollisionComponent>();
            }

            EventManager.Instance.QueueEvent(new LaserBeamWarmUpStart(laserEnemyComp.LaserBeamEntity));
        }
コード例 #2
0
        private void UpdateLaserBeam(float alpha)
        {
            LaserEnemyComponent laserEnemyComp = LaserEnemyEntity.GetComponent <LaserEnemyComponent>();
            LaserBeamComponent  laserBeamComp  = laserEnemyComp.LaserBeamEntity.GetComponent <LaserBeamComponent>();

            laserBeamComp.Thickness = MathHelper.Lerp(0, CVars.Get <float>("laser_enemy_warm_up_thickness"), Easings.QuadraticEaseIn(alpha));
        }
コード例 #3
0
        private Entity FindLaserBeamOwner(Entity laserBeamEntity)
        {
            Family laserEnemyFamily = Family.All(typeof(LaserEnemyComponent)).Get();

            foreach (Entity entity in Engine.GetEntitiesFor(laserEnemyFamily))
            {
                LaserEnemyComponent laserEnemyComponent = entity.GetComponent <LaserEnemyComponent>();
                if (laserEnemyComponent.LaserBeamEntity == laserBeamEntity)
                {
                    return(entity);
                }
            }
            return(null);
        }
コード例 #4
0
        private void UpdateLaserBeam(float alpha)
        {
            LaserEnemyComponent laserEnemyComp = LaserEnemyEntity.GetComponent <LaserEnemyComponent>();
            LaserBeamComponent  laserBeamComp  = laserEnemyComp.LaserBeamEntity.GetComponent <LaserBeamComponent>();

            float frequency  = CVars.Get <float>("laser_enemy_fire_frequency");
            float offset     = CVars.Get <float>("laser_enemy_fire_thickness");
            float zeroToPeak = CVars.Get <float>("laser_enemy_fire_thickness_variability");
            float initialThicknessDecaySpeed = CVars.Get <float>("laser_enemy_fire_initial_thickness_decay_factor");
            float closingEnvelopeDecaySpeed  = CVars.Get <float>("laser_enemy_fire_closing_envelope_decay_factor");

            float thickness = (float)(-zeroToPeak * Math.Sin(2 * MathHelper.Pi * frequency * Timer.Elapsed)) + MathHelper.Lerp(_initialBeamThickness, offset, 1 - (float)Math.Exp(-initialThicknessDecaySpeed * 5 * alpha));

            thickness *= (float)(1 - Math.Exp(-closingEnvelopeDecaySpeed * 5 * (1 - alpha))); // Closing envelope
            laserBeamComp.Thickness = thickness;
        }
コード例 #5
0
 protected override void OnKill()
 {
     if (LaserEnemyEntity != null)
     {
         // Destroy laser beam
         LaserEnemyComponent laserEnemyComp = LaserEnemyEntity.GetComponent <LaserEnemyComponent>();
         if (laserEnemyComp != null)
         {
             if (laserEnemyComp.LaserBeamEntity != null)
             {
                 Engine.DestroyEntity(laserEnemyComp.LaserBeamEntity);
                 laserEnemyComp.LaserBeamEntity = null;
                 EventManager.Instance.QueueEvent(new LaserBeamFireEnd(laserEnemyComp.LaserBeamEntity));
             }
         }
     }
 }
コード例 #6
0
        protected override void OnInitialize()
        {
            if (!Engine.GetEntities().Contains(LaserEnemyEntity) ||
                !LaserEnemyEntity.HasComponent <LaserEnemyComponent>())
            {
                SetNext(null);
                Kill();
                return;
            }

            // Create laser beam if the laser enemy doesn't currently have one
            LaserEnemyComponent laserEnemyComp = LaserEnemyEntity.GetComponent <LaserEnemyComponent>();
            TransformComponent  transformComp  = LaserEnemyEntity.GetComponent <TransformComponent>();

            if (laserEnemyComp.LaserBeamEntity == null)
            {
                laserEnemyComp.LaserBeamEntity = LaserBeamEntity.Create(Engine, transformComp.Position, true);
            }

            LaserBeamComponent laserBeamComp = laserEnemyComp.LaserBeamEntity.GetComponent <LaserBeamComponent>();

            _initialBeamThickness            = laserBeamComp.Thickness;
            laserBeamComp.InteractWithShield = true;
            laserBeamComp.ComputeReflection  = true;

            // Add CollisionComponent if the laser beam doesn't have one
            if (!laserEnemyComp.LaserBeamEntity.HasComponent <CollisionComponent>())
            {
                laserEnemyComp.LaserBeamEntity.AddComponent(new CollisionComponent(new PolygonCollisionShape(new Vector2[] {
                    new Vector2(10, -10),
                    new Vector2(10, 10),
                    new Vector2(-10, 10),
                    new Vector2(-10, -10)
                })));
            }

            EventManager.Instance.QueueEvent(new LaserBeamFireStart(laserEnemyComp.LaserBeamEntity));
        }
コード例 #7
0
        public override bool Handle(IEvent evt)
        {
            ComponentRemovedEvent <LaserBeamComponent> laserBeamComponentRemovedEvent = evt as ComponentRemovedEvent <LaserBeamComponent>;

            if (laserBeamComponentRemovedEvent != null)
            {
                LaserBeamComponent laserBeamComp = laserBeamComponentRemovedEvent.Component;

                // Laser beam destruction
                Entity reflectionBeamEntity = laserBeamComp.ReflectionBeamEntity;
                while (reflectionBeamEntity != null)
                {
                    Entity entityToDestroy = reflectionBeamEntity;

                    LaserBeamComponent reflectionBeamComp = entityToDestroy.GetComponent <LaserBeamComponent>();
                    if (reflectionBeamComp != null)
                    {
                        reflectionBeamEntity = reflectionBeamComp.ReflectionBeamEntity;
                    }

                    Engine.DestroyEntity(entityToDestroy);
                }
            }

            ComponentRemovedEvent <LaserEnemyComponent> laserEnemyComponentRemovedEvent = evt as ComponentRemovedEvent <LaserEnemyComponent>;

            if (laserEnemyComponentRemovedEvent != null)
            {
                LaserEnemyComponent laserEnemyComp = laserEnemyComponentRemovedEvent.Component;
                if (laserEnemyComp.LaserBeamEntity != null)
                {
                    Engine.DestroyEntity(laserEnemyComp.LaserBeamEntity);
                }
            }

            return(false);
        }
コード例 #8
0
        private void EnemyHazardCollision(Entity enemy, Entity hazard)
        {
            if (enemy.HasComponent <LaserEnemyComponent>())
            {
                LaserEnemyComponent laserEnemyComp = enemy.GetComponent <LaserEnemyComponent>();
                if (laserEnemyComp.LaserBeamEntity == hazard)
                {
                    return;
                }
            }
            Color color = Color.White;

            if (enemy.HasComponent <ColoredExplosionComponent>())
            {
                color = enemy.GetComponent <ColoredExplosionComponent>().Color;
            }
            EventManager.Instance.QueueEvent(new CreateExplosionEvent(enemy.GetComponent <TransformComponent>().Position, color));

            Engine.DestroyEntity(enemy);
            if (hazard.HasComponent <ProjectileComponent>())
            {
                ProjectileComponent projectileComp = hazard.GetComponent <ProjectileComponent>();
                if (projectileComp.LastBouncedBy != null)
                {
                    EventManager.Instance.QueueEvent(new IncreasePlayerScoreEvent(projectileComp.LastBouncedBy, CVars.Get <int>("score_base_destroy_enemy_with_projectile")));
                }
                Engine.DestroyEntity(hazard);
            }
            if (hazard.HasComponent <LaserBeamReflectionComponent>())
            {
                LaserBeamReflectionComponent laserBeamReflectionComp = hazard.GetComponent <LaserBeamReflectionComponent>();
                if (laserBeamReflectionComp.ReflectedBy != null)
                {
                    EventManager.Instance.QueueEvent(new IncreasePlayerScoreEvent(laserBeamReflectionComp.ReflectedBy, CVars.Get <int>("score_base_destroy_enemy_with_laser")));
                }
            }
        }
コード例 #9
0
        public override void Update(float dt)
        {
            foreach (Entity laserEnemyEntity in _laserEnemyEntities)
            {
                LaserEnemyComponent laserEnemyComp = laserEnemyEntity.GetComponent <LaserEnemyComponent>();
                Entity laserBeamEntity             = laserEnemyComp.LaserBeamEntity;

                if (laserBeamEntity != null)
                {
                    LaserBeamComponent laserBeamComp = laserBeamEntity.GetComponent <LaserBeamComponent>();
                    if (laserBeamComp == null)
                    {
                        throw new Exception("Laser beam does not have a `LaserBeamComponent`.");
                    }

                    TransformComponent transformComp = laserEnemyEntity.GetComponent <TransformComponent>();
                    float cos             = (float)Math.Cos(transformComp.Rotation),
                          sin             = (float)Math.Sin(transformComp.Rotation);
                    Vector2 laserEnemyTip = new Vector2(4 + 0.35f / 1.5f, 0); // Right-most extent of laser enemy
                    laserEnemyTip = new Vector2(cos * laserEnemyTip.X - sin * laserEnemyTip.Y,
                                                sin * laserEnemyTip.X + cos * laserEnemyTip.Y);
                    laserEnemyTip *= transformComp.Scale;
                    laserEnemyTip += transformComp.Position;

                    Vector2 laserEnemyDirection = new Vector2((float)Math.Cos(transformComp.Rotation), (float)Math.Sin(transformComp.Rotation)); // laserEnemyTip - transformComp.Position;
                    laserEnemyDirection.Normalize();

                    // Simple raycast to find edge/shield this laser touches
                    RaycastHit laserHit = Raycast(laserBeamComp.InteractWithShield ? _raycastWithShieldEntities : _raycastEntities, laserEnemyTip, laserEnemyDirection);

                    if (laserHit.Other.HasComponent <PlayerShieldComponent>() /*&& laserHit.Other.GetComponent<PlayerShieldComponent>().LaserReflectionActive == true*/ && laserBeamComp.ComputeReflection)
                    {
                        Vector2 shieldNormal = laserHit.Position - laserHit.Other.GetComponent <PlayerShieldComponent>().ShipEntity.GetComponent <TransformComponent>().Position;
                        if (Vector2.Dot(shieldNormal, laserHit.Normal) > 0)
                        {
                            if (laserBeamComp.ReflectionBeamEntity == null)
                            {
                                laserBeamComp.ReflectionBeamEntity = LaserBeamEntity.Create(Engine, Vector2.Zero, laserBeamEntity.HasComponent <CollisionComponent>());
                                laserBeamComp.ReflectionBeamEntity.AddComponent(new LaserBeamReflectionComponent());
                                if (laserHit.Other.HasComponent <PlayerComponent>())
                                {
                                    laserBeamComp.ReflectionBeamEntity.GetComponent <LaserBeamReflectionComponent>().ReflectedBy
                                        = laserHit.Other.GetComponent <PlayerComponent>().Player;
                                }
                            }

                            Entity reflectionBeamEntity = laserBeamComp.ReflectionBeamEntity;
                            reflectionBeamEntity.GetComponent <LaserBeamComponent>().Thickness = laserBeamComp.Thickness;
                            Vector2 laserDirection   = laserHit.Position - laserEnemyTip;
                            Vector2 beamOutDirection = GetReflectionVector(laserDirection, laserHit.Normal);
                            // Simple raycast to find edge this laser touches
                            RaycastHit reflectionHit = Raycast(_raycastEntities, laserHit.Position, beamOutDirection);
                            SetLaserBeamProperties(reflectionBeamEntity,
                                                   laserHit.Position,
                                                   reflectionHit.Position,
                                                   (float)Math.Atan2(beamOutDirection.Y, beamOutDirection.X),
                                                   reflectionBeamEntity.GetComponent <LaserBeamComponent>().Thickness);
                        }
                    }
                    else
                    {
                        if (laserBeamComp.ReflectionBeamEntity != null)
                        {
                            Engine.DestroyEntity(laserBeamComp.ReflectionBeamEntity);
                            laserBeamComp.ReflectionBeamEntity = null;
                        }
                    }

                    SetLaserBeamProperties(laserBeamEntity, laserEnemyTip, laserHit.Position, transformComp.Rotation, laserBeamComp.Thickness);
                }
            }
        }