コード例 #1
0
    private void remember(ArrayList mem, ArrayList input)
    {
        if (input.Count > 0)
        {
            for (int i = 0; i < input.Count; i++)
            {
                GameObject    t          = (GameObject)input[i];
                bombFunctions func       = t.GetComponent("bombFunctions") as bombFunctions;
                bool          markedBomb = false;

                if (func != null)
                {
                    markedBomb = func.isMarked();
                }

                if (!mem.Contains(t) && !markedBomb && !occupiedMem.Contains(t))
                {
                    mem.Add(t);
                }
                else if (mem.Contains(t) && markedBomb)
                {
                    mem.Remove(t);
                }
            }
        }
    }
コード例 #2
0
    public void attackFinished()
    {
        //attackDone = true;
        Collider2D hitBomb = Physics2D.OverlapCircle(hitSpace.transform.position, 0.3f, 1 << LayerMask.NameToLayer("Bomb"));

        if (hitBomb != null)
        {
            bombFunctions bomb = hitBomb.gameObject.GetComponent("bombFunctions") as bombFunctions;
            bomb.explode();
        }


        Collider2D hit = Physics2D.OverlapCircle(hitSpace.transform.position, 0.40f, 1 << LayerMask.NameToLayer("Player"));

        if (hit != null)
        {
            if (blood != null)
            {
                GameObject b = (GameObject)Instantiate(blood);
                b.transform.position = new Vector2(hit.gameObject.transform.position.x, hit.gameObject.transform.position.y);


                Vector3 dir = transform.position - blood.transform.position;
                dir.z = 0; dir.Normalize();
                float angle = Mathf.Atan2(dir.y, dir.x) * Mathf.Rad2Deg;
                b.transform.rotation = Quaternion.Slerp(blood.transform.rotation, Quaternion.Euler(0, 0, angle), 1f);
            }


            stats tStats = hit.gameObject.GetComponent("stats") as stats;
            tStats.damage(gameObject);
        }

        anim.SetBool("IsAttacking", false);
    }
コード例 #3
0
    public void fiddle()
    {
        if (stat.hasBomb == true)
        {
            GameObject bomb = (GameObject)Instantiate(stat.bomb);
            bomb.transform.position = new Vector2(gameObject.transform.position.x, gameObject.transform.position.y);

            bombFunctions bF = bomb.GetComponent("bombFunctions") as bombFunctions;
            bF.explode();
            stat.bombDamage();
        }
    }
コード例 #4
0
    public void attackFinished()
    {
        attackDone = true;


        Collider2D hitBomb = Physics2D.OverlapCircle(hitSpace.transform.position, 0.20f, 1 << LayerMask.NameToLayer("Bomb"));

        if (hitBomb != null)
        {
            bombFunctions bomb = hitBomb.gameObject.GetComponent("bombFunctions") as bombFunctions;
            bomb.explode();
        }

        float hitRadius = 0.25f;

        if (stat.isMonster)
        {
            hitRadius = 0.4f;
        }
        Collider2D hit = Physics2D.OverlapCircle(hitSpace.transform.position, hitRadius, 1 << LayerMask.NameToLayer("Victim"));

        if (hit != null)
        {
            Brain      temp  = hit.gameObject.GetComponent("Brain") as Brain;
            GameObject blood = (GameObject)Instantiate(temp.blood);
            blood.transform.position = new Vector2(hit.gameObject.transform.position.x, hit.gameObject.transform.position.y);


            Vector3 dir = transform.position - blood.transform.position;
            dir.z = 0; dir.Normalize();
            float angle = Mathf.Atan2(dir.y, dir.x) * Mathf.Rad2Deg;
            blood.transform.rotation = Quaternion.Slerp(blood.transform.rotation, Quaternion.Euler(0, 0, angle), 1f);

            stats tStats = hit.gameObject.GetComponent("stats") as stats;
            tStats.damage(gameObject);
        }
    }
コード例 #5
0
    private void goodOlFashionAI()
    {
        if (wait == 0)
        {
            // Base Idle STATE
            if (Idle)
            {
                if (interrupt)
                {
                    Idle = false; if (!Monster)
                    {
                        runningAway = true;
                    }
                    else
                    {
                        ChasingPlayer = true;
                    } wait = (int)(3 * Time.deltaTime);
                }
                else
                {
                    Scanning = true; Idle = false; count = 0;
                }
            }


            // AI scans the area
            if (Scanning)
            {
                if (Monster)
                {
                    Collider2D p = Physics2D.OverlapCircle(brain.hitSpace.transform.position, 0.3f, 1 << LayerMask.NameToLayer("Player"));
                    if (p != null)
                    {
                        brain.attack();
                    }
                }

                if (interrupt)
                {
                    Scanning = false; if (!Monster)
                    {
                        runningAway = true;
                    }
                    else
                    {
                        ChasingPlayer = true;
                    } wait = (int)(3 * Time.deltaTime);
                }
                else
                {
                    if (count < 4)
                    {
                        check(count); wait = (int)(3 * Time.deltaTime); count++;
                    }
                    if (count == 4 && (bombMemory.Count == 0 || brain.hasBomb()))
                    {
                        wait = (int)(5 * Time.deltaTime); Scanning = false; Wandering = true;
                    }
                    else if (count == 4 && bombMemory.Count > 0 && !brain.hasBomb())
                    {
                        GameObject closestBomb = null;
                        for (int i = 0; i < bombMemory.Count; i++)
                        {
                            GameObject t = (GameObject)bombMemory[i];

                            if (closestBomb == null)
                            {
                                closestBomb = t;
                            }

                            else
                            {
                                if (t != null && Vector2.Distance(transform.position, t.transform.position) < Vector2.Distance(transform.position, closestBomb.transform.position))
                                {
                                    closestBomb = t;
                                }
                            }
                        }

                        if (closestBomb != null)
                        {
                            targetBomb = closestBomb;
                            bombMemory.Remove(closestBomb);

                            bombFunctions func = targetBomb.GetComponent("bombFunctions") as bombFunctions;
                            if (func.isMarked())
                            {
                                targetBomb = null; wait = (int)(3 * Time.deltaTime); Scanning = false; Wandering = true;
                            }
                            else
                            {
                                func.mark();

                                GameObject close1 = brain.closestNode();
                                GameObject close2 = func.closestNode();

                                //print ("Bomb Path from "+close1+" to "+close2);

                                brain.interruptPath();
                                brain.seek(close1, close2);
                                brain.printPath();
                                wait = (int)(3 * Time.deltaTime); Scanning = false; pickingUpBomb = true;
                            }
                        }

                        else
                        {
                            wait = (int)(3 * Time.deltaTime); Scanning = false; Wandering = true;
                        }
                    }
                }
            }



            // AI wanders aimlessly
            if (Wandering)
            {
                if (Monster)
                {
                    Collider2D p = Physics2D.OverlapCircle(brain.hitSpace.transform.position, 0.3f, 1 << LayerMask.NameToLayer("Player"));
                    if (p != null)
                    {
                        brain.attack();
                    }
                }


                if (interrupt)
                {
                    Wandering = false; if (!Monster)
                    {
                        runningAway = true;
                    }
                    else
                    {
                        ChasingPlayer = true;
                    } wait = (int)(3 * Time.deltaTime);
                }
                else
                {
                    int chance = Random.Range(1, 101);
                    if (chance > 20 && chance < 56)
                    {
                        Wandering = false; Idle = true; wait = (int)(5 * Time.deltaTime);
                    }
                    else
                    {
                        ArrayList n = brain.getNodesinSight();
                        if (n.Count == 0)
                        {
                            Wandering = false; Idle = true;
                        }
                        else if (n.Count > 0)
                        {
                            int randIndex = Random.Range(0, n.Count - 1);
                            //print ("Total is "+n.Count+" index chosen: "+randIndex);
                            if (randIndex > 0 && randIndex < n.Count - 1)
                            {
                                brain.interruptPath();
                                brain.seek(brain.closestNode(), (GameObject)n[randIndex]);
                                Wandering = false; Walking = true; wait = (int)(3 * Time.deltaTime);
                            }
                        }
                    }
                }
            }


            // Moving to a location
            if (Walking)
            {
                if (Monster)
                {
                    Collider2D p = Physics2D.OverlapCircle(brain.hitSpace.transform.position, 0.3f, 1 << LayerMask.NameToLayer("Player"));
                    if (p != null)
                    {
                        brain.attack();
                    }
                }

                Collider2D bomb = Physics2D.OverlapCircle(transform.position, 1f, 1 << LayerMask.NameToLayer("Bomb"));
                if (bomb != null && !brain.hasBomb())
                {
                    brain.pickUpBomb(bomb.gameObject);
                }

                if (!brain.pathing)
                {
                    Walking = false;

                    if (interrupt)
                    {
                        if (!Monster)
                        {
                            runningAway = true;
                        }
                        else
                        {
                            ChasingPlayer = true;
                        }
                    }
                    else
                    {
                        Wandering = true;
                    }

                    wait = (int)(3 * Time.deltaTime);
                }

                else
                {
                    if (interrupt)
                    {
                        brain.interruptPath();
                        Walking = false;
                        if (!Monster)
                        {
                            runningAway = true;
                            wait        = (int)(5 * Time.deltaTime);
                        }

                        else
                        {
                            ChasingPlayer = true;
                            wait          = (int)(1 * Time.deltaTime);
                        }
                    }
                }
            }


            // Moving to retrieve a bomb
            if (pickingUpBomb)
            {
                if (Monster)
                {
                    Collider2D p = Physics2D.OverlapCircle(brain.hitSpace.transform.position, 0.3f, 1 << LayerMask.NameToLayer("Player"));
                    if (p != null)
                    {
                        brain.attack();
                    }
                }

                if (!brain.pathing)
                {
                    if (targetBomb == null)
                    {
                        brain.interruptPath();
                    }
                    else
                    {
                        brain.pickUpBomb(targetBomb);
                    }

                    pickingUpBomb = false;
                    if (interrupt)
                    {
                        if (!Monster)
                        {
                            runningAway = true;
                        }
                        else
                        {
                            ChasingPlayer = true;
                        }
                    }
                    else
                    {
                        Wandering = true;
                    }

                    wait = (int)(5 * Time.deltaTime);
                }
            }



            // Feeling in fear
            if (runningAway)
            {
                brain.sprint();
                int chanceToForget = Random.Range(1, 101);
                if (chanceToForget < 31)
                {
                    hidingObjectMemory.Clear();
                }


                if (hidingObjectMemory.Count > 0)
                {
                    GameObject furthest = null;

                    for (int i = 0; i < hidingObjectMemory.Count; i++)
                    {
                        GameObject t = (GameObject)hidingObjectMemory[i];

                        if (furthest == null)
                        {
                            furthest = t;
                        }

                        else
                        {
                            if (Vector2.Distance(t.transform.position, lastKnownPlayerPos) > Vector2.Distance(furthest.transform.position, lastKnownPlayerPos))
                            {
                                furthest = t;
                            }
                        }
                    }


                    targetHiding = furthest;
                    HidingObject hide = targetHiding.GetComponent("HidingObject") as HidingObject;
                    brain.seek(brain.closestNode(), hide.node);
                    runningAway = false; Hiding = true; wait = (int)(10 * Time.deltaTime);
                }


                else
                {
                    int chance = Random.Range(1, 101);

                    runningAway       = false;
                    Panic             = true;
                    Flee              = true;
                    Fleeing           = false;
                    PanicHide         = false;
                    PanicHiding       = false;
                    DesperateWithBomb = false;
                    SuicideOnPlayer   = false;

                    randomCheck();
                    count = (int)(15 * Time.deltaTime);                  // frames to panic in
                    if (brain.hasBomb() && chance > bravery)
                    {
                        Flee = false; DesperateWithBomb = true;
                    }
                }
            }

            // Hiding in a place
            if (Hiding)
            {
                Collider2D bomb = Physics2D.OverlapCircle(transform.position, 1f, 1 << LayerMask.NameToLayer("Bomb"));
                if (bomb != null && !brain.hasBomb())
                {
                    brain.pickUpBomb(bomb.gameObject);
                }

                if (!brain.pathing)
                {
                    HidingObject h    = targetHiding.GetComponent("HidingObject") as HidingObject;
                    Vector2      hPos = h.node.transform.position;
                    gameObject.transform.position = new Vector2(hPos.x, hPos.y);

                    if (gameObject.renderer.material.color == Color.clear)
                    {
                        Hiding = false; Stay = true;
                        wait   = (int)(30 * Time.deltaTime);
                    }

                    else
                    {
                        Hiding = false;
                        occupiedMem.Add(targetHiding);
                        memCount     = 200;
                        targetHiding = null;

                        int rand = Random.Range(1, 101);
                        if (rand < 84 && rand > 56)
                        {
                            Hiding = false; Idle = true;
                        }

                        else
                        {
                            Hiding = false; runningAway = true;
                        }
                    }
                }
            }


            // Staying in a hiding spot
            if (Stay)
            {
                if (visiblePlayer == null)
                {
                    if (brain.hasBomb())
                    {
                        int chance = Random.Range(1, 101);
                        if (chance < 80)
                        {
                            brain.hideBomb(targetHiding);
                        }

                        Stay         = false; Wandering = true; wait = (int)(3 * Time.deltaTime);
                        targetHiding = null;
                    }

                    else
                    {
                        int chance = Random.Range(1, 101);
                        if (chance > 80)
                        {
                            Stay         = false; Wandering = true; wait = (int)(3 * Time.deltaTime);
                            targetHiding = null;
                        }
                    }
                }

                else
                {
                    wait = (int)(30 * Time.deltaTime);
                }
            }


            // Special Panicked state, prompts for more erratic behavior
            if (Panic)
            {
                bool hold = false;

                if (gameObject.renderer.material.color == Color.clear)
                {
                    hold = true;
                }

                if (count % 2 == 0)
                {
                    brain.sprint();
                }
                if (count > 0)
                {
                    count--;
                }

                if (visiblePlayer != null)
                {
                    count = (int)(20 * Time.deltaTime);                                 // Keep freaking out
                }
                if (Flee && !brain.pathing && !hold)
                {
                    GameObject furthest = null;
                    ArrayList  visible  = brain.getNodesinSight();

                    for (int i = 0; i < visible.Count; i++)
                    {
                        GameObject t = (GameObject)visible[i];
                        if (furthest == null)
                        {
                            furthest = t;
                        }
                        else
                        {
                            if (Vector2.Distance(t.transform.position, lastKnownPlayerPos) > Vector2.Distance(furthest.transform.position, lastKnownPlayerPos))
                            {
                                furthest = t;
                            }
                        }
                    }

                    if (furthest != null)
                    {
                        brain.interruptPath(); brain.seek(brain.closestNode(), furthest); Flee = false; Fleeing = true;
                    }
                    else
                    {
                        randomCheck();
                    }

                    wait = (int)(3 * Time.deltaTime);
                }


                if (Fleeing)
                {
                    Collider2D bomb = Physics2D.OverlapCircle(transform.position, 1f, 1 << LayerMask.NameToLayer("Bomb"));
                    if (bomb != null && !brain.hasBomb())
                    {
                        brain.pickUpBomb(bomb.gameObject);
                    }

                    if (!brain.pathing)
                    {
                        int chance = Random.Range(1, 101);
                        if (chance < 45 && visiblePlayer == null && hidingObjectMemory.Count > 0 && targetHiding == null && Vector2.Distance(transform.position, lastKnownPlayerPos) > 3f)
                        {
                            chance = Random.Range(1, 101);
                            if (chance > 5 && chance < 45)
                            {
                                Fleeing = false; PanicHide = true;
                            }
                            else
                            {
                                randomCheck(); Fleeing = false; Flee = true;
                            }
                        }

                        else
                        {
                            if (brain.hasBomb())
                            {
                                chance = Random.Range(1, 101);
                                if (chance > bravery)
                                {
                                    Fleeing = false; DesperateWithBomb = true;
                                }
                                else
                                {
                                    randomCheck(); Fleeing = false; Flee = true;
                                }
                            }
                        }

                        wait = (int)(3 * Time.deltaTime);
                    }
                }


                if (PanicHide)
                {
                    targetHiding = (GameObject)hidingObjectMemory[0];
                    HidingObject h = targetHiding.GetComponent("HidingObject") as HidingObject;

                    brain.seek(brain.closestNode(), h.node);
                    PanicHide = false; PanicHiding = true; wait = (int)(3 * Time.deltaTime);
                }


                if (PanicHiding)
                {
                    Collider2D bomb = Physics2D.OverlapCircle(transform.position, 1f, 1 << LayerMask.NameToLayer("Bomb"));
                    if (bomb != null && !brain.hasBomb())
                    {
                        brain.pickUpBomb(bomb.gameObject);
                    }


                    if (!brain.pathing)
                    {
                        HidingObject h    = targetHiding.GetComponent("HidingObject") as HidingObject;
                        Vector2      hPos = h.node.transform.position;
                        gameObject.transform.position = new Vector2(hPos.x, hPos.y);

                        if (gameObject.renderer.material.color != Color.clear)
                        {
                            //HidingObject h = targetHiding.GetComponent("HidingObject") as HidingObject;
                            Collider2D look = Physics2D.OverlapCircle(transform.position, 1f, 1 << LayerMask.NameToLayer("Victim"));
                            if (look != null && look.gameObject.renderer.material.color == Color.clear)
                            {
                                h.forceOut(gameObject);
                            }

                            else
                            {
                                look = Physics2D.OverlapCircle(transform.position, 1f, 1 << LayerMask.NameToLayer("Bomb"));
                                if (look != null && look.gameObject.renderer.material.color == Color.clear)
                                {
                                    h.forceOut(gameObject);
                                    brain.pickUpBomb(look.gameObject);
                                }

                                else
                                {
                                    targetHiding       = null;
                                    lastKnownPlayerPos = transform.position;
                                    PanicHiding        = false; Flee = true;
                                }
                            }
                        }


                        else
                        {
                            PanicHiding = false; Flee = true;
                        }

                        wait = (int)(3 * Time.deltaTime);
                    }
                }


                if (DesperateWithBomb)
                {
                    int chance = Random.Range(1, 101);
                    if ((chance > 10 && chance < 15) || (chance > 70 && chance < 75))
                    {
                        brain.fiddle();
                    }
                    else if (chance < 75 && chance > 15)
                    {
                        Collider2D n = Physics2D.OverlapCircle(lastKnownPlayerPos, 0.3f, 1 << LayerMask.NameToLayer("Node"));
                        if (n != null)
                        {
                            brain.interruptPath();
                            brain.seek(brain.closestNode(), n.gameObject);
                            DesperateWithBomb = false; SuicideOnPlayer = true;
                        }

                        else
                        {
                            randomCheck(); DesperateWithBomb = false; Flee = true;
                        }
                    }

                    else
                    {
                        randomCheck(); DesperateWithBomb = false; Flee = true;
                    }

                    wait = (int)(3 * Time.deltaTime);
                }



                if (SuicideOnPlayer)
                {
                    if (!brain.pathing)
                    {
                        if (visiblePlayer != null)
                        {
                            if (Vector2.Distance(transform.position, lastKnownPlayerPos) < 3f)
                            {
                                brain.fiddle(); SuicideOnPlayer = false; Flee = true;
                            }
                            else
                            {
                                Collider2D n = Physics2D.OverlapCircle(lastKnownPlayerPos, 0.3f, 1 << LayerMask.NameToLayer("Node"));
                                if (n != null)
                                {
                                    brain.seek(brain.closestNode(), n.gameObject);
                                }
                                else
                                {
                                    randomCheck(); SuicideOnPlayer = false; Flee = true;
                                }
                            }
                        }

                        else
                        {
                            randomCheck(); SuicideOnPlayer = false; Flee = true;
                        }
                        wait = (int)(3 * Time.deltaTime);
                    }
                }


                if (count == 0)
                {
                    brain.interruptPath();
                    Panic             = false;
                    Flee              = false;
                    Fleeing           = false;
                    PanicHide         = false;
                    PanicHiding       = false;
                    DesperateWithBomb = false;
                    SuicideOnPlayer   = false;
                    Idle              = true;
                }
            }



            if (ChasingPlayer)
            {
                Collider2D p = Physics2D.OverlapCircle(brain.hitSpace.transform.position, 0.3f, 1 << LayerMask.NameToLayer("Player"));
                if (p != null)
                {
                    brain.attack();
                }

                Collider2D n = Physics2D.OverlapCircle(lastKnownPlayerPos, 0.3f, 1 << LayerMask.NameToLayer("Node"));
                if (n != null)
                {
                    brain.interruptPath();
                    brain.seek(brain.closestNode(), n.gameObject);
                    ChasingPlayer = false; Attacking = true;
                }

                else
                {
                    randomCheck(); ChasingPlayer = false; Idle = true;
                }

                wait = (int)(3 * Time.deltaTime);
            }


            if (Attacking)
            {
                Collider2D p = Physics2D.OverlapCircle(brain.hitSpace.transform.position, 0.3f, 1 << LayerMask.NameToLayer("Player"));
                if (p != null)
                {
                    brain.attack();
                }

                if (!brain.pathing)
                {
                    if (visiblePlayer != null)
                    {
                        if (Vector2.Distance(transform.position, lastKnownPlayerPos) < 0.2f)
                        {
                            brain.attack(); Attacking = false; ChasingPlayer = true;
                        }
                        else
                        {
                            Collider2D n = Physics2D.OverlapCircle(lastKnownPlayerPos, 0.3f, 1 << LayerMask.NameToLayer("Node"));
                            if (n != null)
                            {
                                brain.seek(brain.closestNode(), n.gameObject);
                            }
                            else
                            {
                                randomCheck(); Attacking = false; Idle = true;
                            }
                        }
                    }

                    else
                    {
                        randomCheck(); Attacking = false; Idle = true;
                    }
                    wait = (int)(3 * Time.deltaTime);
                }
            }
        }


        if (wait > 0)
        {
            wait--;
        }
    }