Ejemplo n.º 1
0
 public void RemoveUnit(AttackableObject a)
 {
     units[a.TeamCode].Remove(a);
     if (!a.isMovable)
     {
         staticObjects.RemoveAt(a.GetFloatArray());
     }
 }
Ejemplo n.º 2
0
 public void DestroyObject(AttackableObject a)
 {
     toDestroy.Add(a);
     if (!a.isMovable)
     {
         staticObjects.RemoveAt(a.GetFloatArray());
     }
 }
Ejemplo n.º 3
0
    public bool CanPlace(Vector2 pos, float dist)
    {
        AttackableObject temp = staticObjects.GetNearestNeighbours(new float[] { pos.x, pos.y }, 1)[0].Value;

        if (temp == null)
        {
            return(false);
        }
        Vector2 t = new Vector2(temp.transform.position.x, temp.transform.position.z);

        return(Vector2.Distance(pos, t) >= dist);
    }
Ejemplo n.º 4
0
    private AttackableObject PickNewTarget()
    {
        // Retrieve all components in the area that represent eatable objects
        var attackableObjects = FindObjectsOfType <AttackableObject>();
        List <AttackableObject> attackableObjectsInRange = new List <AttackableObject>();

        // Check which components are in range
        foreach (var attackableObject in attackableObjects)
        {
            if (!(attackableObject.isAttackable))
            {
                continue;
            }

            var targetPos = attackableObject.transform.position;

            if (Vector3.Distance(targetPos, transform.position) < eatRadius)
            {
                attackableObjectsInRange.Add(attackableObject);
            }
        }

        if (attackableObjectsInRange.Count <= 0)
        {
            return(null);
        }

        // Get a list of all unsatisfied stats
        List <StatTypeClass> unsatisfiedStats = GetUnsatisfiedStats();

        // Order unsatisfied stats based on which one is the lowest
        unsatisfiedStats = unsatisfiedStats.OrderBy(t => t.value).ToList();

        AttackableObject mostValueableObject = null;
        float            highestStatValue    = 0;

        // Based on the order of the lowest stats, look for item which provides
        // most of that stat
        foreach (var stat in unsatisfiedStats)
        {
            foreach (var objInRange in attackableObjectsInRange)
            {
                if (objInRange.statFillValues.ContainsKey(stat.statName) &&
                    objInRange.statFillValues[stat.statName] > highestStatValue)
                {
                    mostValueableObject = objInRange;
                    highestStatValue    = objInRange.statFillValues[stat.statName];
                }
            }
        }

        return(mostValueableObject);
    }
Ejemplo n.º 5
0
    public static void SuppressHiddenEnemy(EnemyMarker enemy)
    {
        AttackableObject <EnemyMarker> info = instance.markersForAttack.Find(m => {
            return(m.GetObject() == enemy);
        });

        if (info == null)
        {
            Debug.LogError("No enemy marker found!");
        }
        else
        {
            info.AddSuppressor();
        }
    }
Ejemplo n.º 6
0
    public static void NoLongerSuppressVisibleEnemy(EnemyTarget enemy)
    {
        AttackableObject <EnemyTarget> info = instance.targetsForAttack.Find(m => {
            return(m.GetObject() == enemy);
        });

        if (info == null)
        {
            Debug.LogError("No enemy target found!");
        }
        else
        {
            info.RemoveSuppressor();
        }
    }
Ejemplo n.º 7
0
    public static void NoLongerFlankLeftHiddenEnemy(EnemyMarker enemy)
    {
        AttackableObject <EnemyMarker> info = instance.markersForAttack.Find(m => {
            return(m.GetObject() == enemy);
        });

        if (info == null)
        {
            Debug.LogError("No enemy marker found!");
        }
        else
        {
            info.RemoveLeftFlanker();
        }
    }
Ejemplo n.º 8
0
    /// <summary>
    /// Finds Targets with the lowest Health.
    /// </summary>
    /// <param name="enemies">list of enemies in sight</param>
    private void FindEnemyWithLowestHealth(GameObject[] enemies)
    {
        target = null;
        float _lowestHealth = Mathf.Infinity;

        foreach (var enemy in enemies)
        {
            AttackableObject enemyObject = enemy.gameObject.GetComponentInParent <AttackableObject>();
            if (enemyObject.health < _lowestHealth)
            {
                SetTarget(enemy);
                _lowestHealth = enemyObject.health;
            }
        }
    }
Ejemplo n.º 9
0
    public static void FlankLeftVisibleEnemy(EnemyTarget enemy)
    {
        AttackableObject <EnemyTarget> info = instance.targetsForAttack.Find(m => {
            return(m.GetObject() == enemy);
        });

        if (info == null)
        {
            Debug.LogError("No enemy target found!");
        }
        else
        {
            info.AddLeftFlanker();
        }
    }
Ejemplo n.º 10
0
    private bool targetWithinAttackAngle(AttackableObject target)
    {
        Vector3 lookDirection = transform.forward;

        lookDirection.y = 0;

        // Determine which direction to rotate towards
        Vector3 targetDirection = target.transform.position - transform.position;

        targetDirection.y = 0;

        float angleToTarget = Vector3.Angle(lookDirection, targetDirection);

        return(angleToTarget < angleOfAttack);
    }
Ejemplo n.º 11
0
    public override void DropItem(PlaceableSurface surface)
    {
        base.DropItem(surface);
        AttackableObject attackable = GetComponent <AttackableObject>();

        if (attackable)
        {
            if (canBeUsedFor == UseType.CookedFood)
            {
                attackable.statFillValuesList.Clear();
                attackable.statFillValuesList.Add(new StatTypeClass(StatType.Hunger, percentageFull, 0f));
                attackable.statFillValuesList.Add(new StatTypeClass(StatType.Happiness, (percentageFull > 0.2f) ? 0.3f : -0.1f, 0f));
            }
            attackable.RefreshAttackableObject();
        }
    }
Ejemplo n.º 12
0
    public static int GetSuppressorsOnHiddenEnemy(
        EnemyMarker target
        )
    {
        AttackableObject <EnemyMarker> info = instance.markersForAttack.Find(m => {
            return(m.GetObject() == target);
        });

        if (info == null)
        {
            Debug.LogError("No enemy marker found!");
            return(0);
        }
        else
        {
            return(info.GetCoveringFireAttackers());
        }
    }
Ejemplo n.º 13
0
    public static int GetSuppressorsOnVisibleEnemy(
        EnemyTarget target
        )
    {
        AttackableObject <EnemyTarget> info = instance.targetsForAttack.Find(m => {
            return(m.GetObject() == target);
        });

        if (info == null)
        {
            Debug.LogError("No enemy target found!");
            return(0);
        }
        else
        {
            return(info.GetCoveringFireAttackers());
        }
    }
Ejemplo n.º 14
0
    private void FindAndAttackTarget(AttackableObject newTarget)
    {
        if (newTarget)
        {
            rotateScript.SetTarget(newTarget.transform);

            // If target is within the angle of attack, then attack
            if (targetWithinAttackAngle(newTarget))
            {
                IsAttacking = true;
            }
            else
            {
                IsAttacking = false;
            }
        }
        else
        {
            rotateScript.SetTarget(null);
            IsAttacking = false;
        }
    }
Ejemplo n.º 15
0
    public void AddUnit(AttackableObject a)
    {
        if (!unitCodes.Contains(a.TeamCode))
        {
            unitCodes.Add(a.TeamCode);
            units[a.TeamCode] = new List <AttackableObject>();
            units[a.TeamCode].Add(a);
        }
        else
        {
            units[a.TeamCode].Add(a);
        }

        if (!a.isMovable)
        {
            staticObjects.Add(a.GetFloatArray(), a);
            GameManager.instance.players[a.TeamCode].buildings.Add(a);
        }
        else
        {
            GameManager.instance.players[a.TeamCode].units.Add(a);
        }
    }
Ejemplo n.º 16
0
 private static AttackObject WhenAttackObject(Character sourceCharacter, AttackableObject attackableObject, int points)
 {
     return(new AttackObject(sourceCharacter, attackableObject, points));
 }
Ejemplo n.º 17
0
    private IEnumerator HandleAttacking()
    {
        float timeBeg = 0f;
        //Intermediate time used when in loop
        float inter = Time.realtimeSinceStartup;

        while (true)
        {
            timeBeg = Time.realtimeSinceStartup;
            for (int i = 0; i < unitCodes.Count; i++)
            {
                for (int j = 0; j < units[unitCodes[i]].Count; j++)
                {
                    AttackableObject attacker = units[unitCodes[i]][j];

                    if (attacker.isAttacking && attacker.target != null)
                    {
                        AttackableObject target = attacker.target;

                        if (Vector3.Distance(attacker.transform.position, target.transform.position) > attacker.idealRange)
                        {
                            if (!attacker.overrideMovement && attacker.isMovable)
                            {
                                attacker.isApproaching = true;
                                attacker.isAttacking   = false;
                            }
                            else if (!attacker.overrideMovement)
                            {
                                attacker.startSearch = true;
                                attacker.isAttacking = false;
                            }
                        }
                        else
                        {
                            attacker.AttackTarget();
                        }
                    }
                    else if (attacker.isAttacking && attacker.isBuilder)
                    {
                        attacker.AttackTarget();
                    }
                    else if (attacker.isAttacking && attacker.target == null)
                    {
                        attacker.isAttacking = false;
                        attacker.startSearch = true;
                    }

                    if (Time.realtimeSinceStartup - inter > 0.005f)
                    {
                        //twaiter = twaiter + 0.1f * (Time.realtimeSinceStartup - t3) + 0.05f;
                        //yield return new WaitForSeconds(0.1f * (Time.realtimeSinceStartup - inter) + 0.05f);
                        yield return(null);

                        inter = Time.realtimeSinceStartup;
                    }
                }
            }
            RemoveDeadUnits();
            yield return(new WaitForSeconds(0.5f + 1.0f * (Time.realtimeSinceStartup - timeBeg)));
        }
    }
Ejemplo n.º 18
0
    IEnumerator ApproachTargets()
    {
        float timeBeg = 0;
        float timeEnd = 0;
        float time3   = 0;

        float dist         = 0;
        float stoppingDist = 0;

        float ax, ay, az;
        float tx, ty, tz;

        while (true)
        {
            if (unitCodes.Count == 0)
            {
                yield return(null);

                continue;
            }

            timeBeg = Time.realtimeSinceStartup;
            for (int i = 0; i < unitCodes.Count; i++)
            {
                for (int j = 0; j < units[unitCodes[i]].Count; j++)
                {
                    AttackableObject appr = units[unitCodes[i]][j];

                    if (appr.startApproach)
                    {
                        if (appr.overrideMovement)
                        {
                            //Set the unit to look for targets within range
                            appr.onTargetSearch = true;

                            appr.startApproach = false;
                            appr.isApproaching = true;
                            appr.lastMoveDist  = float.MaxValue;
                            //appr.navAgent.SetDestination(appr.overrideMovePos);
                            appr.StartWaypoints();
                        }
                        else
                        {
                            if (appr.target == null)
                            {
                                appr.startApproach = false;
                                appr.isApproaching = false;
                                appr.startSearch   = false;
                                continue;
                            }
                            else if (appr.isMovable)
                            {
                                appr.startApproach = false;
                                appr.isApproaching = true;
                                appr.navAgent.SetDestination(appr.target.transform.position);
                                appr.navAgent.speed = appr.moveSpeed;
                            }
                        }
                    }

                    if (appr.isApproaching)
                    {
                        if (appr.overrideMovement)
                        {
                            float newDist = Vector2.Distance(new Vector2(appr.overrideMovePos.x, appr.overrideMovePos.z), new Vector2(appr.transform.position.x, appr.transform.position.z));
                            if (newDist < 1)
                            {
                                if (!appr.MoveToNextLoc())
                                {
                                    Debug.Log("Reached target pos");
                                    appr.navAgent.SetDestination(appr.transform.position);
                                    appr.isApproaching    = false;
                                    appr.overrideMovement = false;
                                }
                            }
                            else
                            {
                                appr.lastMoveDist = newDist;
                            }
                        }
                        else
                        {
                            AttackableObject targ = appr.target;
                            if (targ == null)
                            {
                                appr.isApproaching = false;
                                appr.startSearch   = true;
                                continue;
                            }

                            ax = appr.transform.position.x;
                            ay = appr.transform.position.y;
                            az = appr.transform.position.z;

                            tx = targ.transform.position.x;
                            ty = targ.transform.position.y;
                            tz = targ.transform.position.z;

                            NavMeshAgent apprNav = appr.navAgent;
                            NavMeshAgent targNav = targ.navAgent;

                            //Calculate stopping condition for NavMeshAgent
                            //apprNav.stoppingDistance = apprNav.radius / appr.transform.localScale.x + targNav.radius / targ.transform.localScale.x;
                            apprNav.stoppingDistance = appr.idealRange;

                            //Distance between approacher and target
                            dist = Mathf.Sqrt((tx - ax) * (tx - ax) + (ty - ay) * (ty - ay) + (tz - az) * (tz - az));

                            //stoppingDist = appr.idealRange + appr.transform.localScale.x * targ.transform.localScale.x*apprNav.stoppingDistance;
                            stoppingDist = appr.idealRange;

                            // counting increased distances (failure to approach) between attacker and target;
                            // if counter failedR becomes bigger than critFailedR, preparing for new target search.
                            if (appr.prevDist < dist)
                            {
                                appr.apprFailed += 1;
                                if (appr.apprFailed > appr.critApprFailed)
                                {
                                    appr.isApproaching = false;
                                    appr.startSearch   = true;
                                    appr.apprFailed    = 0;
//                                    Debug.Log("Approach failed");
                                }
                            }
                            else
                            {
                                if (dist < stoppingDist)
                                {
                                    //Debug.Log("Reached target");
                                    apprNav.SetDestination(appr.transform.position);
                                    //Debug.Log("Starting to attack");
                                    //Set unit up for attacking
                                    appr.isApproaching = false;
                                    appr.isAttacking   = true;
                                    if (appr.isCollector)
                                    {
                                        appr.OnReachTarget();
                                    }
                                }
                                else
                                {
                                    //Starting to move
                                    if (appr.isMovable)
                                    {
                                        apprNav.SetDestination(targ.transform.position);
                                        apprNav.speed = appr.moveSpeed;
                                    }
                                }
                            }
                            appr.prevDist = dist;
                        }
                        if (Time.realtimeSinceStartup - time3 > 0.005f)
                        {
                            //twaiter = twaiter + 0.1f * (Time.realtimeSinceStartup - t3) + 0.05f;
                            //yield return new WaitForSeconds(0.1f * (Time.realtimeSinceStartup - time3) + 0.05f);
                            yield return(null);

                            time3 = Time.realtimeSinceStartup;
                        }
                    }
                }
            }
            yield return(new WaitForSeconds(1.0f * (timeEnd - timeBeg) + 1.0f));
        }
    }
Ejemplo n.º 19
0
    IEnumerator HandleSearch()
    {
        float timeBeg   = 0;
        float timeInter = 0;

        int hitUnits = 0;

        timeInter = Time.realtimeSinceStartup;
        while (true)
        {
            hitUnits = 0;
            timeBeg  = Time.realtimeSinceStartup;
            Dictionary <int, KdTree <float, AttackableObject> > unitTrees = new Dictionary <int, KdTree <float, AttackableObject> >();
            for (int i = 0; i < unitCodes.Count; i++)
            {
                unitTrees[i] = new KdTree <float, AttackableObject>(2, new FloatMath());
                for (int j = 0; j < units[unitCodes[i]].Count; j++)
                {
                    AttackableObject u = units[unitCodes[i]][j];
                    unitTrees[i].Add(u.GetFloatArray(), u);
                    if (u.startSearch)
                    {
                        if (!u.onTargetSearch)
                        {
                            u.onTargetSearch = true;
                        }
                        u.startSearch = false;
                    }

                    if (Time.realtimeSinceStartup - timeInter > 0.005f)
                    {
                        //yield return new WaitForSeconds(0.1f * (Time.realtimeSinceStartup - timeInter) + 0.05f);
                        yield return(null);

                        timeInter = Time.realtimeSinceStartup;
                    }
                }
            }

            for (int i = 0; i < unitCodes.Count; i++)
            {
                for (int j = 0; j < units[unitCodes[i]].Count; j++)
                {
                    AttackableObject searcher = units[unitCodes[i]][j];
                    if (!searcher.onTargetSearch)
                    {
                        continue;
                    }
                    AttackableObject closest = null;
                    if (searcher.canAttack)
                    {
//                        Debug.Log("Can attack: " + searcher.canAttack);
                        for (int k = 0; k < unitCodes.Count; k++)
                        {
                            if (k == i)
                            {
                                continue;
                            }

                            if (unitTrees[unitCodes[k]].Count == 0)
                            {
                                continue;
                            }

                            AttackableObject temp = unitTrees[unitCodes[k]].GetNearestNeighbours(searcher.GetFloatArray(), 1)[0].Value;
                            if (temp != null && (closest == null || Vector3.Distance(searcher.transform.position, closest.transform.position) > Vector3.Distance(searcher.transform.position, temp.transform.position)))
                            {
                                closest = temp;
                            }
                        }
                    }
                    else if (searcher.isCollector)
                    {
                        //                        Debug.Log("Searcher is collector");

                        /*int searchCount = 1;
                         * while (closest == null && searchCount < resources.Count)
                         * {
                         *
                         * }*/
                        //                        Debug.Log("Resource node is null: " + (resources.GetNearestNeighbours(searcher.GetFloatArray(), 1)[0].Value == null));
                        KdTreeNode <float, ResourceNode>[] temp = resources.GetNearestNeighbours(searcher.GetFloatArray(), 1);
                        if (temp.Length > 0 && temp[0] != null)
                        {
                            closest = resources.GetNearestNeighbours(searcher.GetFloatArray(), 1)[0].Value;
                        }
                    }
                    else
                    {
                        Debug.Log("Nothing");
                    }

                    if (closest != null)
                    {
//                        if (searcher.TeamCode == 0 && closest.TeamCode == searcher.TeamCode) Debug.Log("Closest is on the same team");

                        float distance = Vector3.Distance(searcher.transform.position, closest.transform.position);

                        if (searcher.isMovable && searcher.searchRange >= distance)
                        {
                            if (searcher.overrideMovement)
                            {
//                                Debug.Log("Unit found target while overriding movement");
                                searcher.target         = closest;
                                searcher.onTargetSearch = false;
                                searcher.isAttacking    = true;
                            }
                            else
                            {
                                searcher.target         = closest;
                                searcher.onTargetSearch = false;
                                searcher.startApproach  = true;
                            }
                        }
                        else if (searcher.idealRange >= distance)
                        {
                            searcher.target         = closest;
                            searcher.onTargetSearch = false;
                            searcher.isAttacking    = true;
                        }

                        /*else if(searcher.TeamCode == 0)
                         * {
                         *  Debug.Log(distance + " > " + searcher.searchRange + " : " + searcher.gameObject.name);
                         * }*/

                        /*if (searcher.isMovable && !searcher.overrideMovement)
                         * {
                         *  searcher.target = closest;
                         *  searcher.onTargetSearch = false;
                         *  searcher.startApproach = true;
                         * }
                         * else if(Vector3.Distance(searcher.transform.position, closest.transform.position) <= searcher.idealRange)
                         * {
                         *  searcher.target = closest;
                         *  searcher.onTargetSearch = false;
                         *  searcher.startAttack = true;
                         * }*/
                    }

                    if (Time.realtimeSinceStartup - timeInter > 0.005f)
                    {
                        //yield return new WaitForSeconds(0.1f * (Time.realtimeSinceStartup - timeInter) + 0.05f);
                        yield return(null);

                        timeInter = Time.realtimeSinceStartup;
                    }
                }
            }
            deltaSearchTime.text = "DST: " + ((Time.realtimeSinceStartup - timeBeg)) + " TU: " + hitUnits;
            yield return(new WaitForSeconds(0.5f + 1.0f * (Time.realtimeSinceStartup - timeBeg)));
        }
    }
Ejemplo n.º 20
0
 public Shoot(Rat rat, AttackableObject enemy) : base(rat)
 {
     this.enemy = enemy;
     OnStart();
 }
Ejemplo n.º 21
0
 public AttackObject(Character sourceCharacter, AttackableObject targetCharacter, int points)
 {
     SourceCharacter = sourceCharacter;
     TargetCharacter = targetCharacter;
     Points          = points;
 }