예제 #1
0
파일: Creature.cs 프로젝트: OCox1991/Genome
        /// <summary>
        /// Attacks another creature, comparing strength to defence and moving towards the other creature if it is not adjacent
        /// </summary>
        /// <param name="otherCreature">The creature to attack</param>
        public void attack(Creature otherCreature)
        {
            if (isAdjacent(otherCreature))
            {
                int winnerEnergy = 10;
                int loserEnergy = 20;

                otherCreature.attacked(this);
                inCombat = true;
                inCombatWith = otherCreature;
                int attackVal = this.getStrength() + rand(10);
                int defVal = otherCreature.getDefence() + rand(10);

                if (attackVal > defVal)
                {
                    otherCreature.damage(attackVal - defVal);
                    this.drainEnergy(winnerEnergy);
                    otherCreature.drainEnergy(loserEnergy);
                }
                else if (attackVal < defVal)
                {
                    this.damage((defVal - attackVal) / 2);
                    this.drainEnergy(loserEnergy);
                    otherCreature.drainEnergy(winnerEnergy);
                }
                else
                {
                    this.drainEnergy(loserEnergy);
                    otherCreature.drainEnergy(loserEnergy);
                }
            }
            else
            {
                moveTowards(otherCreature);
            }
        }