Example #1
0
        public void TakeDamage(int damage, IGameUnit attackingUnit, MoveUnitChangeContainer moveUnitChangeContainer)
        {
            // Can't inflict damage to non-destructable tiles so check that first
            if (!MapTileData.Destructable || TileHealth <= 0)
            {
                return;
            }

            // For now only handle dealing positive damage
            if (damage <= 0)
            {
                return;
            }

            // Change tile health to inflict damage
            var startingHealth = TileHealth;

            TileHealth = Mathf.Clamp(TileHealth - damage, 0, MapTileData.TileMaxHealth);

            // Check if this tile has been destroyed aka HP = 0
            if (tileHealth <= 0 && !IsDestroyed)
            {
                // Create a container for our state change
                var tileChange = new TileStateChangeContainer(this, IsDestroyed, startingHealth, MapTileData);
                moveUnitChangeContainer.TileChanges.Add(tileChange);

                // If we have an attacking unit then count our destruction value and body count towards them
                if (attackingUnit != null)
                {
                    attackingUnit.IncrementStatistic(UnitStatistic.PropertyDamage, DestructionValue);
                    moveUnitChangeContainer.ApplyStatisticOffset(UnitStatistic.PropertyDamage, DestructionValue);

                    attackingUnit.IncrementStatistic(UnitStatistic.BodyCount, DestructionBodyCount);
                    moveUnitChangeContainer.ApplyStatisticOffset(UnitStatistic.BodyCount, DestructionBodyCount);
                }

                // If we have a tile to become when we're destroyed then we'll change to that
                if (MapTileData.OnDestroyedTile != null)
                {
                    // Update the backing map tile data for this tile, this should trigger a refresh of this tile
                    ChangeMapTileData(MapTileData.OnDestroyedTile);
                }
                else
                {
                    // Otherwise just flag this tile as destroyed
                    IsDestroyed = true;
                }
            }
        }