Vector2Int GetNewLocationInMoveRadius()
        {
            int        moveRadius = TraitsUtil.GetMoveRadius(agent);
            Vector2Int location   = Vector2Int.zero;

            bool isFoundLocation = false;
            int  tryCount        = 0;

            while (isFoundLocation == false && tryCount++ < maxTryCount)
            {
                Vector2Int offset = Vector2Int.RoundToInt(Random.insideUnitCircle * moveRadius);
                location    = agent.Location;
                location.x += offset.x;
                location.y += offset.y;

                isFoundLocation = agent.Map.InBounds(location);
            }

            if (tryCount > maxTryCount)
            {
                return(agent.Location);
            }

            return(location);
        }
Beispiel #2
0
        void HandleAttack(IAgent attackingAgent)
        {
            attackingAgent.Description = "(HP: " + TraitsUtil.GetHealth(attackingAgent) + ") Attacking " + agent.DisplayName;

            agent.TargetMapElement = attackingAgent;

            int health = TraitsUtil.GetHealth(agent);

            int attackStrength  = TraitsUtil.GetRandomAttackStrength(attackingAgent);
            int defenseStrength = TraitsUtil.GetRandomDefenseStrength(agent);

            int healthDecrement = attackStrength - defenseStrength;

            if (healthDecrement > 0)
            {
                health -= healthDecrement;
                TraitsUtil.SetHealth(agent, health);
            }


            if (health <= 0)
            {
                agent.Description = "Killed by " + attackingAgent.DisplayName;
                agent.HandleTransition(onDeathTransition);
            }
            else
            {
                agent.Description = "(HP: " + health + ") Attacked by " + attackingAgent.DisplayName;// + "\nattackStrength = "+attackStrength+", defenseStrength = "+defenseStrength+", remaining health = " + health;
                agent.HandleTransition(onAttackedTransition);
            }

            //Debug.Log("HandleAttack health = " + health+", "+ agent.Description);
        }
Beispiel #3
0
        IEnumerator Move()
        {
            float moveSpeed = TraitsUtil.GetMoveSpeed(agent);

            YieldInstruction yieldInstruction = new WaitForEndOfFrame();

            bool isLocationReached = false;

            while (isLocationReached == false)
            {
                yield return(yieldInstruction);

                Vector3 targetPosition = agent.Map.CellToLocal(agent.TargetLocation);
                float   targetDistance = Vector2.Distance(agent.Position, targetPosition);

                isLocationReached = targetDistance < 0.001f;
                if (isLocationReached == false)
                {
                    agent.Position = Vector2.MoveTowards(agent.Position, targetPosition, moveSpeed);
                }

                UpdateSortingOrder();
            }

            Complete();
        }
        void AttackTarget()
        {
            if (agent.TargetMapElement == null)
            {
                CallTargetKilledTransition();
                return;
            }

            IAttackReceiver attackReceiver = agent.TargetMapElement as IAttackReceiver;

            if (attackReceiver == null)
            {
                CallTargetKilledTransition();
                return;
            }

            int targetHealth = TraitsUtil.GetHealth(agent.TargetMapElement);

            if (targetHealth <= 0)
            {
                CallTargetKilledTransition();
            }
            else
            {
                attackReceiver.ReceiveAttack(agent);
            }
        }