示例#1
0
 // Start is called before the first frame update
 void Start()
 {
     pawn   = GetComponentInChildren <EnemyPawn>();
     target = GameObject.FindGameObjectWithTag("Player").transform;
     agent  = pawn.GetComponent <NavMeshAgent>();
     GameManager.instance.enemies.Add(this.gameObject);
     if (pawn.stats.weaponEquipped == null)
     {
         //Add the weapons in the scene to the list
         allWeaponsInWorld = FindObjectsOfType <Weapon>();
         //Cycle through all weapons in the scene
         //foreach(Weapon obj in allWeaponsInWorld)
         //{
         //    //If we are close to the weapon it becomes the target to chase
         //    if (Vector3.Distance(pawn.transform.position, obj.transform.position) <= 10)
         //        target = obj.transform;
         //    //Chase the weapon
         //    pawn.ChangeMoveStateTo(EnemyPawn.AIMoveState.Chase);
         //}
     }
     else
     {
         pawn.stats.weaponEquipped.owner = pawn;
         //Chase the player instead
         target = GameObject.FindGameObjectWithTag("Player").transform;
         pawn.ChangeMoveStateTo(EnemyPawn.AIMoveState.Idle);
     }
 }
 public enum AIState { Idle, Patrol, Attack, Search, Pursue, Flee, Investigate, Avoidance, Alerted } // Selections for AI State
 public virtual void Start()
 {
     enemyPawn    = GetComponent <EnemyPawn>();
     enemyData    = GetComponent <EnemyData>();
     aiVision     = GetComponentInChildren <AIVision>();
     aiHearing    = GetComponentInChildren <AIHearing>();
     sensoryRange = GetComponentInChildren <SensoryRange>();
 }
示例#3
0
 public void EquipGun(GameObject weaponType)
 {
     // play the designated audioclip
     AudioSource.PlayClipAtPoint(audio, transform.position, vol);
     // Create a weapon
     if (owner.tag == "Player")
     {
         playerPawn = owner.GetComponent <PlayerPawn>();
         if (playerPawn.weaponEquipped == true)
         {
             // do nothing if the player already has a weapon
             return;
         }
         GameObject gun = Instantiate(weaponType, playerPawn.attachPoint.position, Quaternion.identity) as GameObject;
         if (gun.tag == "Rifle")
         {
             weapon = gun.GetComponent <Rifle>();
         }
         else if (gun.tag == "RocketLauncher")
         {
             weapon = gun.GetComponent <RocketLauncher>();
         }
         // Make the gun a child of the owner
         gun.transform.SetParent(playerPawn.attachPoint, false);
         gun.transform.localPosition = weaponType.transform.localPosition;
         gun.transform.localRotation = weaponType.transform.localRotation;
         // Let Player Pawn know the player is using a weapon
         playerPawn.weaponEquipped = true;
         playerPawn.gun            = gun;
         playerPawn.weapon         = weapon;
     }
     else if (owner.tag == "Enemy")
     {
         if (enemyPawn.weaponEquipped == true)
         {
             // do nothing if the enemy already has a weapon
             return;
         }
         enemyPawn = owner.GetComponent <EnemyPawn>();
         GameObject gun = Instantiate(weaponType, enemyPawn.attachPoint.position, Quaternion.identity) as GameObject;
         if (gun.tag == "Rifle")
         {
             weapon = gun.GetComponent <Rifle>();
         }
         else if (gun.tag == "RocketLauncher")
         {
             weapon = gun.GetComponent <RocketLauncher>();
         }
         // Make the gun a child of the owner
         gun.transform.SetParent(enemyPawn.attachPoint, false);
         gun.transform.localPosition = weaponType.transform.localPosition;
         gun.transform.localRotation = weaponType.transform.localRotation;
         // Let Player Pawn know the player is using a weapon
         enemyPawn.weaponEquipped = true;
         enemyPawn.gun            = gun;
         enemyPawn.weapon         = weapon;
     }
 }
示例#4
0
    // Once the player is out of eye sight, go back to the recover state
    private void OnTriggerExit2D(Collider2D collision)
    {
        EnemyPawn pawn = GetComponent <EnemyPawn>();

        if (collision.gameObject.tag == "Player")
        {
            pawn.ramming = false;
            pawn.SetState(EnemyPawn.State.Recover);
        }
    }
示例#5
0
    public EnemyPawn Create(int playerCount, int level)
    {
        EnemyPawn enemy = new EnemyPawn(this.name, (int)(Random.Range(minMaxHp, maxMaxHp + 1) * playerCount * (1 + (level * 0.10))));

        knownSpells.ForEach(enemy.AddSpell);
        enemy.spellWeight   = spellWeight;
        enemy.useItemWeight = useItemWeight;
        enemy.passWeight    = passWeight;
        return(enemy);
    }
示例#6
0
    // Destroy the enemy and play the enemy death sound
    private void OnCollisionEnter2D(Collision2D other)
    {
        // Find the audio source
        audioSource = GameObject.Find("Audio").GetComponent <AudioSource>();

        /*
         * If the enemy and the player collided then the player will die
         * If the players ability collided with the enemy then the enemy will die
         */
        if (other.gameObject.tag == "Enemy")
        {
            if (gameObject.tag == "Player")
            {
                if (!(other.collider is CircleCollider2D))
                {
                    audioSource.clip = GameManager.instance.playerDeath;
                    audioSource.Play();

                    // Set isDead to true so that the respawn process can start
                    GameManager.instance.isDead = true;
                    // Decrement the player lives by one
                    GameManager.instance.livesLeft -= 1;
                    // Update the UI to show the new lives
                    GameManager.instance.UpdateLives(GameManager.instance.livesLeft);

                    EnemyPawn enemyPawn = other.gameObject.GetComponent <EnemyPawn>();

                    // Set the enemy state to recover since the player is dead
                    enemyPawn.SetState(EnemyPawn.State.Recover);

                    if (enemyPawn.ramming)
                    {
                        // Set ramming to false if its true
                        other.gameObject.GetComponent <EnemyPawn>().ramming = false;
                    }
                }
            }
            else
            {
                audioSource.clip = GameManager.instance.enemyDeath;
                audioSource.Play();

                // Destroy the enemy game object
                Destroy(other.gameObject);
                GameManager.instance.currentEnemies -= 1;

                // Destroy the players fireball too
                if (gameObject.tag == "Weapon")
                {
                    Destroy(gameObject);
                }
            }
        }
    }
示例#7
0
 public override void Update(Pawn pawn)
 {
     base.Update(pawn);
     if (pawn is EnemyPawn)
     {
         EnemyPawn other = pawn as EnemyPawn;
         spellWeight   = other.spellWeight;
         passWeight    = other.passWeight;
         useItemWeight = other.useItemWeight;
     }
 }
示例#8
0
    public void AddPawnToLists(List <EnemyPawn> enemyPawns, bool sortEnemyPawns)
    {
        _enemyPawns.Clear();
        _enemyPairs.Clear();
        _enemySingles.Clear();
        List <EnemyPawn> enemyPawnsWithPawnTargets = new List <EnemyPawn>(); //_enemyPawns.FindAll(pawn => pawn.TargetIsOtherPawn);

        foreach (var pawn in enemyPawns)
        {
            if (pawn.TargetIsOtherPawn)
            {
                enemyPawnsWithPawnTargets.Add(pawn);
            }
            else
            {
                _enemySingles.Add(pawn);
            }
        }

        //Debug.Log(GetType() + "." + enemyPawnsWithPawnTargets.Count);
        for (int i = 0; i < enemyPawnsWithPawnTargets.Count; i++)
        {
            EnemyPawn pawnI = enemyPawnsWithPawnTargets[i];
            for (int j = i + 1; j < enemyPawnsWithPawnTargets.Count; j++)
            {
                EnemyPawn pawnJ = enemyPawnsWithPawnTargets[j];
                if (pawnI.Target == pawnJ.GetComponent <EnemyPawnTarget>() &&
                    pawnJ.Target == pawnI.GetComponent <EnemyPawnTarget>())
                {
                    _enemyPairs.Add(new EnemyPawnsPair(pawnI, pawnJ));
                }
            }
        }
        if (sortEnemyPawns)
        {
            //_enemyPawns.Sort((pawnA, pawnB) => {
            //    bool pawnATargetIsOtherPawn = pawnA.TargetIsOtherPawn;
            //    bool pawnBTargetIsOtherPawn = pawnB.TargetIsOtherPawn;
            //    if (pawnATargetIsOtherPawn == pawnBTargetIsOtherPawn)
            //        return 0;
            //    else if (pawnATargetIsOtherPawn && !pawnBTargetIsOtherPawn)
            //        return 1;
            //    else if (pawnBTargetIsOtherPawn && !pawnATargetIsOtherPawn)
            //        return -1;
            //    else
            //        return 0;
            //});
            enemyPawns.Clear();
            enemyPawns.AddRange(_enemySingles);
            enemyPawns.AddRange(enemyPawnsWithPawnTargets);
        }
        _enemyPawns.AddRange(enemyPawns);
    }
示例#9
0
    /*
     * If the player is within the enemies vision collider,
     * the enemy has a chance of trying to ram the player
     */
    private void OnTriggerEnter2D(Collider2D collision)
    {
        EnemyPawn pawn = GetComponent <EnemyPawn>();

        if (collision.gameObject.tag == "Player")
        {
            bool ram = (Random.Range(0, 2) < 1);
            if (ram)
            {
                pawn.ramming = true;
                pawn.target  = collision.gameObject.transform;
            }
        }
    }
    protected override void OnCollision(GameObject target)
    {
        EnemyPawn enemyPawn = target.GetComponent <EnemyPawn>();

        if (null == enemyPawn)
        {
            return;
        }

        if (enemyPawn.Data.IsInvincible)
        {
            return;
        }

        enemyPawn.Data.HP -= Data.Damage;
        Die();
    }
示例#11
0
    private void Start()
    {
        MapStateMachine.CreateInstance();
        MapFactory.CreateMap(6, 6);
        PlayerMapController.CreateInstance();

        GameObject playerPrefab = Resources.Load <GameObject>("Prefabs/Player");
        PlayerPawn player       = Instantiate <GameObject>(playerPrefab).GetComponent <PlayerPawn>();

        player.SetPosition(new Position(1, 1));
        player.Initialize();

        GameObject enemyPrefab = Resources.Load <GameObject>("Prefabs/Enemy");
        EnemyPawn  enemy       = Instantiate <GameObject>(enemyPrefab).GetComponent <EnemyPawn>();

        enemy.SetPosition(new Position(3, 4));
        enemy.Initialize();

        Destroy(gameObject);
    }
示例#12
0
    public IEnumerator DoAITurn(EnemyPawn enemyPawn)
    {
        yield return(new WaitForSeconds(1.5F));

        enemyPawn.DoTurn(this);
    }
示例#13
0
    public IEnumerator MoveEnemyPawnsRoutine(Action <bool> onEnd, Action onSuccess, Action onFail)
    {
        int  surroundedEnemiesCount = 0;
        int  movedEnemiesCount      = 0;
        bool enemyReachedTarget     = false;

        _pathfinder.ClearSprites();

        List <PathResult>     pathResults     = new List <PathResult>();
        List <PawnTransition> pawnTransitions = new List <PawnTransition>();

        foreach (var enemyPawn in _enemySingles)
        {
            PathResult pathResult = null;
#if DEBUG_PATHFINDING
            yield return(_pathfinder.FindPathRoutine(enemyPawn, enemyPawn.Target, aPathResult => {
                pathResult = aPathResult;
            }, pawnTransitions, _playerPawnsContainer, _enemyPawnsContainer, _enemyTargetsContainer));
#else
            pathResult = _pathfinder.FindPath(enemyPawn, enemyPawn.Target, pawnTransitions,
                                              _playerPawnsContainer, _enemyPawnsContainer, _enemyTargetsContainer, _enemyPawnsPathSpritesContainer);
#endif
            pathResults.Add(pathResult);
            var path = pathResult.Path;
            if (pathResult.PathFound && path.Count > 0)
            {
                _pathfinder.CreatePathSprites(path, 1, 1, Color.white);
                PawnTransition pawnTransition = new PawnTransition(enemyPawn, _tilemap, path.PickLastElement());
                pawnTransitions.Add(pawnTransition);
            }
        }
        if (pawnTransitions.Count > 0)
        {
            yield return(MoveEnemyPawnsRoutine(pawnTransitions));

            movedEnemiesCount += pawnTransitions.Count;
        }
        for (int i = 0; i < pathResults.Count; i++)
        {
            EnemyPawn  enemyPawn  = _enemyPawns[i];
            PathResult pathResult = pathResults[i];
            var        path       = pathResult.Path;
            if (pathResult.PathFound)
            {
                if (path.Count <= 1)
                {
                    enemyReachedTarget = true;
                    break;
                }
            }
            else
            {
                bool targetSurrounded = !_pathfinder.FindPathToBoundsMin(enemyPawn.Target, _playerPawnsContainer, _enemyTargetsContainer).PathFound;
                bool enemySurrounded  = targetSurrounded ? !_pathfinder.FindPathToBoundsMin(enemyPawn, _playerPawnsContainer).PathFound : true;
                surroundedEnemiesCount += enemySurrounded ? 1 : 0;
            }
        }
        foreach (var pair in _enemyPairs)
        {
            EnemyPawn  enemyPawnA = pair.Pawn1;
            EnemyPawn  enemyPawnB = pair.Pawn2;
            PathResult pathResult = null;
#if DEBUG_PATHFINDING
            yield return(_pathfinder.FindPathRoutine(enemyPawnA, enemyPawnB, aPathResult => {
                pathResult = aPathResult;
            }, null, _playerPawnsContainer, _enemyPawnsContainer, _enemyTargetsContainer));
#else
            pathResult = _pathfinder.FindPath(enemyPawnA, enemyPawnB, null, _playerPawnsContainer, _enemyPawnsContainer, _enemyTargetsContainer);
#endif
            if (pathResult.PathFound)
            {
                var path = pathResult.Path;
                if (path.Count > 0)
                {
                    path.RemoveAt(0);
                    if (path.Count > 0)
                    {
                        List <SpriteRenderer> pathSprites = _pathfinder.CreatePathSprites(path, 1, 1, Color.white);
                        yield return(MoveEnemyPawnsPairRoutine(pair, path));

                        movedEnemiesCount += 2;
                    }
                    if (path.Count < 1)
                    {
                        enemyReachedTarget = true;
                        break;
                    }
                }
            }
            else
            {
                bool pawnASurrounded = !_pathfinder.FindPathToBoundsMin(enemyPawnA, _playerPawnsContainer, _enemyTargetsContainer).PathFound;
                bool pawnBSurrounded = !_pathfinder.FindPathToBoundsMin(enemyPawnB, _playerPawnsContainer, _enemyTargetsContainer).PathFound;
                surroundedEnemiesCount += pawnASurrounded ? 1 : 0;
                surroundedEnemiesCount += pawnBSurrounded ? 1 : 0;
            }
        }
        if (enemyReachedTarget)
        {
            yield return(new WaitForSeconds(0.5f));

            onFail?.Invoke();
        }
        else if (surroundedEnemiesCount == _enemyPawns.Count)
        {
            yield return(new WaitForSeconds(0.5f));

            onSuccess?.Invoke();
        }
        onEnd?.Invoke(movedEnemiesCount > 0);
    }
示例#14
0
 // Start is called before the first frame update
 void Start()
 {
     pawn     = GetComponent <EnemyPawn>();
     animator = GetComponent <Animator>();
     target   = pawn.target;
 }
    public EnemyPawn epawn;   //Setting a variable to reference the EnemyPawn

    // Start is called before the first frame update
    public override void Start()
    {
        player = GameManager.instance.player;
        epawn  = GetComponent <EnemyPawn>();
        base.Start();
    }
示例#16
0
 // Use this for initialization
 void Start()
 {
     // Cast the controller pawn to enemy pawn for access to special variables
     enemyPawn = (EnemyPawn)pawn;
 }