Example #1
0
    public void Execute()
    {
        foreach (Entity e in _group.GetEntities())
        {
            LaserComponent component = e.laser;
            Entity         source    = component.source;
            if (source != null && source.hasLaserSpawner)               // todo make it child or sth
            {
                Vector2 sourcePosition = source.position.pos;
                LaserSpawnerComponent sourceSpawner = source.laserSpawner;

                GameObject go        = e.gameObject.gameObject;
                Transform  transform = go.transform;
                transform.localScale    = new Vector3(transform.localScale.x, sourceSpawner.height * pixelsPerUnit, transform.localScale.z);
                transform.localRotation = Quaternion.AngleAxis(sourceSpawner.angle, Vector3.forward);
                float xOffset = sourceSpawner.height / 2.0f * sourceSpawner.direction.x;
                float yOffset = sourceSpawner.height / 2.0f * sourceSpawner.direction.y;
                e.position.pos.Set(sourcePosition.x + xOffset, sourcePosition.y + yOffset);
            }
            else
            {
                e.isDestroyEntity = true;
            }
        }
    }
Example #2
0
    void OnTriggerEnter2D(Collider2D collision)
    {
        // ------ Calculate damage ------ //
        float          damageReceived = 0;
        LaserComponent projectile     = collision.gameObject.GetComponent <LaserComponent>();

        if (projectile != null)
        {
            damageReceived = projectile.LaserDamage;
        }
        else if (this.gameObject.tag == GameConstants.PLAYER_TAG)
        {
            damageReceived = 1;
        }
        GetHit(damageReceived);
    }
Example #3
0
    public void Update(Identity identity, LaserComponent laser, GameobjectComponent gameobjectComponent)
    {
        if (laser == null || laser.enable == false)
        {
            return;
        }

        if (Time.time < laser.exisingTime)
        {
            laser.attackTimer += Time.deltaTime;
            if (laser.attackTimer >= laser.attackInterval)
            {
                laser.attackTimer = 0;
                Transform    transform = gameobjectComponent.transform;
                RaycastHit2D hit       = Physics2D.Raycast(transform.position, transform.up);
                Debug.DrawLine(transform.position, transform.up * 700);
                if (hit.collider != null)
                {
                    //Debug.DrawLine(transform.position, hit.point);
                    EntityHolder entityHolder = hit.collider.gameObject.GetComponent <EntityHolder>();
                    if (entityHolder == null)
                    {
                        return;
                    }
                    Entity hitEntity = entityHolder.entity;
                    if (hitEntity != identity.master)
                    {
                        Debug.Log("LaserSystem, camps:" + hitEntity.identity.camp.ToString() + identity.camp.ToString());
                        if (hitEntity.identity.camp != identity.camp && hitEntity.lifeComponent.enable == true)
                        {
                            world.lifeSystem.ChangeHP(hitEntity.lifeComponent, -laser.damage);
                            world.effectSystem.CreateExplosion(hit.point, "small");
                        }
                    }
                }
            }
        }
        else
        {
            world.entitySystem.DestroyEntity(identity.entity);
        }
    }
Example #4
0
        public override void Update(GameTime gameTime)
        {
            List <Entity> thingsThatDoDamage   = world.GetEntities(new[] { typeof(DealsDamageComponent), typeof(PositionComponent), typeof(RenderComponent) });
            List <Entity> thingsThatTakeDamage = world.GetEntities(new[] { typeof(TakesDamageComponent), typeof(PositionComponent), typeof(RenderComponent) });

            foreach (Entity damageDealer in thingsThatDoDamage)
            {
                DealsDamageComponent ddc   = (DealsDamageComponent)damageDealer.components[typeof(DealsDamageComponent)];
                PositionComponent    dd_pc = (PositionComponent)damageDealer.components[typeof(PositionComponent)];
                RenderComponent      dd_rc = (RenderComponent)damageDealer.components[typeof(RenderComponent)];

                Rectangle damageDealerRect = new Rectangle((int)dd_pc.position.X - (dd_rc.CurrentTexture.Width / 2), (int)dd_pc.position.Y - (dd_rc.CurrentTexture.Height / 2), dd_rc.CurrentTexture.Width, dd_rc.CurrentTexture.Height);

                foreach (Entity damageTaker in thingsThatTakeDamage)
                {
                    TakesDamageComponent tdc = (TakesDamageComponent)damageTaker.components[typeof(TakesDamageComponent)];

                    //Bitwise AND the collision masks, a value > 0 means we have objects that can be compared
                    if ((tdc.takesDamageFromMask & ddc.damageTypeMask) == 0)
                    {
                        continue;
                    }

                    PositionComponent td_pc = (PositionComponent)damageTaker.components[typeof(PositionComponent)];
                    if (!damageTaker.HasComponent(typeof(RenderComponent)))
                    {
                        continue;
                    }
                    RenderComponent td_rc           = (RenderComponent)damageTaker.components[typeof(RenderComponent)];
                    Rectangle       damageTakerRect = new Rectangle((int)td_pc.position.X - (td_rc.CurrentTexture.Width / 2), (int)td_pc.position.Y - (td_rc.CurrentTexture.Height / 2), td_rc.CurrentTexture.Width, td_rc.CurrentTexture.Height);
                    if (damageDealerRect.Intersects(damageTakerRect))
                    {
                        if (!damageTaker.HasComponent(typeof(ShieldedComponent)))
                        {
                            tdc.health -= ddc.strength;
                        }

                        //Check if the damage dealer was a laser
                        if (damageDealer.HasComponent(typeof(LaserComponent)))
                        {
                            LaserComponent dd_lc = (LaserComponent)damageDealer.components[typeof(LaserComponent)];

                            //Spawn an explosion at the location of collision
                            Entity explosion = new Entity();
                            explosion.AddComponent(new RenderComponent(dd_lc.explosionTexture));
                            explosion.AddComponent(new PositionComponent(dd_pc.position));
                            explosion.AddComponent(new ExplosionComponent());
                            world.AddEntity(explosion);
                        }

                        world.RemoveEntity(damageDealer);

                        if (tdc.health <= 0)
                        {
                            if (damageTaker.HasComponent(typeof(PlayerComponent)))
                            {
                                PlayerComponent playerComp = (PlayerComponent)damageTaker.components[typeof(PlayerComponent)];
                                playerComp.lives -= 1;
                                damageTaker.RemoveComponent(typeof(RenderComponent));
                            }
                            else
                            {
                                world.RemoveEntity(damageTaker);
                            }

                            if (damageDealer.HasComponent(typeof(LaserComponent)))
                            {
                                int credit = 1;
                                if (damageTaker.HasComponent(typeof(MeteorComponent)))
                                {
                                    MeteorComponent meteorComponent = (MeteorComponent)damageTaker.components[typeof(MeteorComponent)];
                                    if (meteorComponent.isBig)
                                    {
                                        credit = 2;
                                    }
                                }

                                Game1.instance.kills += 1;
                                double multiplier = 1 + Math.Log(GameScreen.timeStayedAlive / 1000);
                                double score      = (credit * multiplier) * 100;
                                Game1.instance.playerScore += score;

                                Entity e = new Entity();
                                e.AddComponent(new PositionComponent(td_pc.position));
                                e.AddComponent(new NotificationComponent("+" + Math.Truncate(score), 200, false));
                                Game1.instance.world.AddEntity(e);
                            }

                            if (damageTaker.HasComponent(typeof(MeteorComponent)))
                            {
                                MeteorComponent meteorComponent = (MeteorComponent)damageTaker.components[typeof(MeteorComponent)];
                                //Spawn small meteors when big ones break
                                if (meteorComponent.isBig)
                                {
                                    Random rand    = new Random();
                                    int    randAmt = rand.Next(2, 6);
                                    for (int i = 0; i < randAmt; i++)
                                    {
                                        Entity newMeteor = new Entity();
                                        newMeteor.AddComponent(new MeteorComponent(false));
                                        newMeteor.AddComponent(new TakesDamageComponent(5, LASER));
                                        newMeteor.AddComponent(new DealsDamageComponent(5, METEOR));
                                        newMeteor.AddComponent(new RenderComponent(Game1.instance.meteorSmall));
                                        newMeteor.AddComponent(new SpeedComponent(new Vector2(rand.Next(-3, 3), rand.Next(2, 5))));
                                        newMeteor.AddComponent(new PositionComponent(new Vector2(td_pc.position.X, td_pc.position.Y)));
                                        world.AddEntity(newMeteor);
                                    }
                                }
                            }
                        }

                        break;
                    }
                }
            }
        }