示例#1
0
        public void Attack(Unit target)
        {
            bool debug_attBack = false;
            int  debug_att1    = 0;
            int  debug_att2    = 0;

            if (canAttack && target.owner != owner && (!App.useFog || !(Game.game.Map[target.x, target.y, 0] as Terrain).isFogForPlayers[Game.game.playerState]))
            {
                //App.Anim(x, y, (x + target.x) / 2, (y + target.y) / 2, this, () => { App.Anim((x + target.x) / 2, (y + target.y) / 2, x, y, this, () => { }); });

                canAttack = false;
                if (!ability.HasFlag(Ability.Dash))
                {
                    turns = 0;
                }

                int attackStrength = attack * 2;

                /*if (hp < ((double)maxhp / 100) * 40)
                 *  attackStrength = attack;*/
                attackStrength -= target.defense;

                // If defending unit stands in city with walls, substract another one attack point
                if (Game.game.Map[target.x, target.y, 1] is City && (Game.game.Map[target.x, target.y, 1] as City).buildings[(int)Building.EBuildings.Walls].state == Building.States.Builded)
                {
                    attackStrength--;
                }

                // If attacking unit is in water, substract another attack point
                if ((Game.game.Map[x, y, 0] as Terrain).type == (int)Terrain.Types.Sea)
                {
                    attackStrength--;
                }
                // If defeding unit is in water, add one attack point
                if ((Game.game.Map[target.x, target.y, 0] as Terrain).type == (int)Terrain.Types.Sea)
                {
                    attackStrength++;
                }

                if (attackStrength < 1)
                {
                    attackStrength = 1;
                }

                debug_att1 = attackStrength;

                target.hp -= attackStrength;

                if (target.hp <= 0)
                {
                    target.alive = false;
                    Game.game.Map[target.x, target.y, target.z] = null;
                    if (ability.HasFlag(Ability.Combo))
                    {
                        canAttack = true;
                        turns     = maxturns;
                    }
                    if (owner == Game.game.players[0])
                    {
                        Achievement.enemiesKilled++;
                        Achievement.Check();
                    }
                    else if (target.owner == Game.game.players[0])
                    {
                        Achievement.unitsLost++;
                        Achievement.Check();
                    }
                }
                // If enemy is alive AND (you attack melee OR you both attack ranged), attack back
                else if (!ability.HasFlag(Ability.RangedAttack) || (ability.HasFlag(Ability.RangedAttack) && target.ability.HasFlag(Ability.RangedAttack)))
                {
                    debug_attBack = true;

                    attackStrength = target.attack * 2;

                    /*if (target.hp < ((double)target.maxhp / 100) * 80)
                     *  attackStrength = target.attack;*/
                    attackStrength -= defense;
                    if (attackStrength < 1)
                    {
                        attackStrength = 1;
                    }

                    debug_att2 = attackStrength;

                    hp -= attackStrength;

                    if (hp <= 0)
                    {
                        alive = false;
                        Game.game.Map[x, y, z] = null;
                        if (target.owner == Game.game.players[0])
                        {
                            Achievement.enemiesKilled++;
                            App.KilledInDefense++;
                        }
                        else if (owner == Game.game.players[0])
                        {
                            Achievement.unitsLost++;
                            Achievement.Check();
                        }
                    }
                }
            }

            if (App.debug)
            {
                Bridge.Script.Call("console.log", $"Unit {name} (player: {owner.color}) attacked {target.name} (player: {target.owner.color}). Attack back: {debug_attBack}. Attack 1: {debug_att1}. Attack 2: {debug_att2}");
            }
        }