Example #1
0
 /// <summary>
 /// updates health of buildings marked for construction
 /// </summary>
 private void Construction()
 {
     for (int i = 0; i < toBuild.Count; i++)
     {
         toBuild[i].healthBar.UpdateHealth(toBuild[i], Game.GraphicsDevice);
         toBuild[i].Construct();
         if (toBuild[i].CurrentHealth >= toBuild[i].TotalHealth)
         {
             if (toBuild[i].HasTag("Wood Collector"))
             {
                 for (int j = 0; j < toBuild[i].GarrisonedUnits.Count; j++)
                 {
                     ((Civilian)toBuild[i].GarrisonedUnits[j]).Harvest(world.FindNearest("Wood", toBuild[i].GarrisonedUnits[j].Position), world);
                 }
             }
             else if (toBuild[i].HasTag("Iron Collector"))
             {
                 for (int j = 0; j < toBuild[i].GarrisonedUnits.Count; j++)
                 {
                     ((Civilian)toBuild[i].GarrisonedUnits[j]).Harvest(world.FindNearest("Iron", toBuild[i].GarrisonedUnits[j].Position), world);
                 }
             }
             toBuild[i].Subscribe((ITechObserver)this);
             toBuild.RemoveAt(i);
         }
     }
 }
        public void FindTarget(WorldHandler world)
        {
            IEntity entity = world.FindNearest(this.TeamAssociation - 1, this.Position);

            if (entity != null)
            {
                if (entity is ModifiableTile)
                {
                    Attack(entity, world);
                }
            }
        }
        /// <summary>
        /// Interact with each tile based on what type of tile it is if the unit is within range of their target.
        /// </summary>
        private void UnitInteraction(WorldHandler world)
        {
            IEntity entity = Vector2.Distance(world.FindNearest(TeamAssociation + 1, Position).Position, Position) < stats[typeof(Range)].Value * 2 + teamStats[typeof(Range)].Value * 2 ? world.FindNearest(TeamAssociation + 1, Position) : null;

            if (entity != null)//HACK this won't always work
            {
                Attack(entity, world);
            }
            float dist = Vector2.Distance(TargetPosition + DistanceFromPosition, nextPoint);

            if (Direction == zero && Target != null && dist <= stats[typeof(Range)].Value)
            {
                if (((ModifiableTile)Target).TeamAssociation != this.TeamAssociation)
                {
                    attack.Attack(Target, this, stats[typeof(MeleeDamage)].Value + teamStats[typeof(MeleeDamage)].Value);
                    if (((ModifiableTile)Target).State == tileState.dead)
                    {
                        Target = null;
                    }
                }
            }
        }
Example #4
0
        /// <summary>
        /// Interact with each tile based on what type of tile it is if the unit is within range of their target.
        /// </summary>
        private void UnitInteraction(WorldHandler world)
        {
            float dist = Vector2.Distance(TargetPosition + DistanceFromPosition, Position);

            if (Direction == zero && Target != null && !arrived && dist <= stats[typeof(Range)].Value * 1.5)
            {
                if (Target is Building && ((ModifiableTile)Target).TeamAssociation == this.TeamAssociation)
                {
                    ((Building)Target).GarrisonedUnits.Add(this);
                    if (this.UnitState == BaseUnitState.harvest)
                    {
                        if (((Building)Target).HasTag("Wood Collector") && ((Building)Target).HasTag("Iron Collector"))
                        {
                            ((Building)Target).Collect(this.unitWallet.Withdraw());
                        }
                        else if (((Building)Target).HasTag("Iron Collector"))
                        {
                            ((Building)Target).Collect(this.unitWallet.Withdraw(new Iron()));
                        }
                    }
                    arrived = true;
                    if (returnTarget != null)
                    {
                        Harvest(returnTarget, world);
                    }
                    else if (toBuild.Count > 0)
                    {
                        Build(toBuild.Pop(), world);
                    }
                    returnTarget = null;//Set to null after setting target...keeps the unit from going random places the player wouldn't expect.
                }
                else if (Target is IHarvestable)
                {
                    Type   type = ((HarvestableUnit)Target).type.GetType();
                    Wallet wal  = ((HarvestableUnit)Target).Harvest(stats[typeof(HarvestPower)].Value + teamStats[typeof(HarvestPower)].Value);
                    if (((HarvestableUnit)Target).State == tileState.dead) //When the source dies find a new thing to harvest
                    {
                        Harvest(world.FindNearest(((HarvestableUnit)Target).type.GetType().Name.ToString(), this.Position), world);
                    }
                    bool returnResources = unitWallet.Deposit(wal);
                    if (returnResources)//If the unit wallet is full return it to the nearest building that collects the type of the harvestable resource
                    {
                        ((HarvestableUnit)Target).Return(wal);
                        returnTarget = Target;
                        try
                        {
                            Garrison(world.FindNearest(type.Name.ToString() + " Collector", this.Position), world);
                        }
                        catch (NullReferenceException) { }
                    }
                }
                //Attack the unit if it isn't part of their team
                else if (((ModifiableTile)Target).TeamAssociation != this.TeamAssociation)
                {
                    Target.Damage(stats[typeof(MeleeDamage)].Value + teamStats[typeof(MeleeDamage)].Value);
                    if (((ModifiableTile)Target).State == tileState.dead)
                    {
                        Target = null;
                    }
                }
                timer = 0;// Only reset the timer if the unit does something that way the player instantly acts when they get to the position
            }
        }