Ejemplo n.º 1
0
    // Update is called once per frame
    void Update()
    {
        MoveForward();
        EnemyEngine         enemyEngine     = GetComponent <EnemyEngine>();
        List <RaycastHit2D> bumperRay       = new List <RaycastHit2D>();
        ContactFilter2D     contactFilter2D = new ContactFilter2D();
        Vector2             dir             = (pMovementSpeed < 0) ? Vector2.left : Vector2.right;
        int PlayerRayCount = Physics2D.BoxCast(viewPoint.transform.position, new Vector2(detectionDistance, 0.5f), 0f, dir, contactFilter2D, bumperRay, detectionDistance);

        int  i    = 0;
        bool stop = false;

        while (i < PlayerRayCount && !stop)
        {
            if (bumperRay[i].collider.gameObject.layer != enemyEngine.GetGroundLayer() && bumperRay[i].collider.gameObject.name != "BaseEnemy")
            {
                ChangeDir();
                Renderer rend = GetComponent <Renderer>();
                if (pMovementDir < 0)
                {
                    viewPoint.transform.position = new Vector2(transform.position.x + rend.bounds.extents.x, transform.position.y);
                }
                else
                {
                    viewPoint.transform.position = new Vector2(transform.position.x - rend.bounds.extents.x, transform.position.y);
                }
                stop = true;
            }
            i++;
        }
    }
Ejemplo n.º 2
0
    /// <summary>
    /// Checks if there is ground beneath this GameObject.
    /// If there isn´t, we rotate the gameObject
    /// </summary>
    protected override void Check()
    {
        EnemyEngine enemyEngine = GetComponent <EnemyEngine>();

        List <RaycastHit2D> rayCastInfo     = new List <RaycastHit2D>();
        ContactFilter2D     contactFilter2D = new ContactFilter2D();
        int sensorRay = Physics2D.Raycast(raycastEmitter.transform.position, Vector2.down, contactFilter2D, rayCastInfo, 1);

        Debug.DrawRay(raycastEmitter.transform.position, Vector2.down, Color.green);


        int  i          = 0;
        bool floorFound = false;

        while (i < sensorRay)
        {
            if (rayCastInfo[i].collider.gameObject.layer == enemyEngine.GetGroundLayer())
            {
                floorFound = true;
            }
            i++;
        }

        //If my ray did NOT detect any floor, we change dir
        if (!floorFound)
        {
            ChangeDir();
        }
    }
Ejemplo n.º 3
0
    // Update is called once per frame
    void Update()
    {
        base.CheckForDeactivateStateChange();

        EnemyEngine enemyEngine = GetComponent <EnemyEngine>();
        Vector3     target      = enemyEngine.GetTargetPosition();
        float       distance    = 0;

        switch (sensorType)
        {
        case RangeSensorType.Horizontal:
            distance = Mathf.Abs(transform.position.x - target.x);
            break;

        case RangeSensorType.Vertical:
            distance = Mathf.Abs(target.y - transform.position.y);
            break;

        case RangeSensorType.DistanceBased:
            distance = Vector2.Distance(transform.position, target);
            break;
        }

        if (!sensorActive && distance < detectionRange)
        {
            OnSensorActive();
        }

        else if (sensorActive && distance > detectionRange)
        {
            OnSensorExit();
        }
    }
Ejemplo n.º 4
0
 // Use this for initialization
 void Start()
 {
     startPos = spawner.transform.position;
     eEng     = enemy.GetComponent <EnemyEngine>();
     aiPath   = enemy.GetComponent <AIPath>();
     StartCoroutine(RoamRange());
 }
Ejemplo n.º 5
0
    private void OnTriggerExit2D(Collider2D collision)
    {
        EnemyEngine enemyEngine = GetComponent <EnemyEngine>();

        if (enemyEngine != null)
        {
            if (sensorActive)
            {
                if (!collision.gameObject.layer.Equals(enemyEngine.GetGroundLayer()))
                {
                    sensorActive = false;
                    OnSensorExit();
                }
            }
        }
    }
Ejemplo n.º 6
0
    private void OnTriggerEnter2D(Collider2D collision)
    {
        EnemyEngine enemyEngine = GetComponent <EnemyEngine>();

        if (enemyEngine != null)
        {
            if (!collision.gameObject.layer.Equals(enemyEngine.GetGroundLayer()))
            {
                sensorActive = true;
                OnSensorActive();
            }
        }
        else
        {
            Debug.LogWarning("No components detected! A sensor can´t work without at least one");
        }
    }
Ejemplo n.º 7
0
    private void Update()
    {
        if (canJump)
        {
            GetComponent <Rigidbody2D>().velocity = jumper.GetMovement();  //Jump!

            if (updatePlayerPosition)
            {
                //Where do we jump now to get to the target?
                jumperModifier = (transform.position.x > enemyEngine.GetTargetPosition().x) ? 1 : -1;
                RotateToTarget();
                updatePlayerPosition = false;
                StartCoroutine(SetTargetPositionAfterSeconds());
            }

            GetComponent <Rigidbody2D>().velocity += bullet.GetMovement() * jumperModifier; //Move towards target
            canJump = false;                                                                //We shall not jump until next timer states so
        }
        else
        {
            if (Time.time - lastJumpTimer > jumpDelay)
            {
                //Cast a 2-width box into the ground. The box is so that the entity doesn´t get stuck in platform´s edges
                //((which used to happen and was pretty annoying))
                EnemyEngine         enemyEngine     = GetComponent <EnemyEngine>();
                List <RaycastHit2D> groundRay       = new List <RaycastHit2D>();
                ContactFilter2D     contactFilter2D = new ContactFilter2D();
                int groundRayCount = Physics2D.BoxCast(groundPoint.transform.position, new Vector2(2, groundCheckRadius), 0f, Vector2.down, contactFilter2D, groundRay, groundCheckRadius);

                int  i    = 0;
                bool stop = false;

                while (i < groundRayCount && !stop)
                {
                    if (groundRay[i].collider.gameObject.layer == enemyEngine.GetGroundLayer())
                    {
                        canJump       = true;
                        lastJumpTimer = Time.time;
                        stop          = true;
                    }
                    i++;
                }
            }
        }
    }
Ejemplo n.º 8
0
    //Is this entity touching the ground?
    public void CheckIfGrounded()
    {
        EnemyEngine         enemyEngine     = GetComponent <EnemyEngine>();
        List <RaycastHit2D> groundRay       = new List <RaycastHit2D>();
        ContactFilter2D     contactFilter2D = new ContactFilter2D();
        int  groundRayCount = Physics2D.BoxCast(groundPoint.transform.position, new Vector2(2, groundCheckRadius), 0f, Vector2.down, contactFilter2D, groundRay, groundCheckRadius);
        int  i    = 0;
        bool stop = false;

        while (i < groundRayCount && !stop)
        {
            if (groundRay[i].collider.gameObject.layer == enemyEngine.GetGroundLayer())
            {
                canJump       = true;
                lastJumpTimer = Time.time;
            }
            i++;
        }
    }
Ejemplo n.º 9
0
    // Update is called once per frame
    void Update()
    {
        CheckForDeactivateStateChange();

        List <RaycastHit2D> rayCastInfo     = new List <RaycastHit2D>();
        ContactFilter2D     contactFilter2D = new ContactFilter2D();
        int sensorRay = Physics2D.Raycast(transform.position, LineDirection, contactFilter2D, rayCastInfo, LineDistance);

        Debug.DrawRay(transform.position, LineDirection * LineDistance, Color.green);

        int  i    = 0;
        bool stop = false;

        while (i < sensorRay && !stop)
        {
            EnemyEngine enemyEngine = GetComponent <EnemyEngine>();
            if (enemyEngine != null)
            {
                if (rayCastInfo[i].collider.gameObject.layer == enemyEngine.GetGroundLayer() && !SeeThroughWalls)
                {
                    //Stop looking
                    stop = true;
                }
                else
                {
                    if (rayCastInfo[i].collider.gameObject.layer == enemyEngine.GetPlayerLayer())
                    {
                        OnSensorDetection();
                        //Stop looking
                        stop = true;
                    }
                }
            }
            i++;
        }
    }
Ejemplo n.º 10
0
    protected EnemyEngine enemyEngine;                  //Instance of the enemy engine

    //public bool SetInactiveAfterStart = false;               //Should this behaviour shut itself up after Start()? Used in all Sensors, to set up the behaviour when needed.

    // Start is called before the first frame update
    public void Awake()
    {
        enemyEngine = GetComponent <EnemyEngine>();
        //enemyEngine.RegistrerBehaviour(this);
    }
Ejemplo n.º 11
0
    public void Attack()
    {
        if (countDown >= coolDown && magTar != null)
        {
            float aff;
            bool  boss = false;

            if (magTar.tag != "Ezekiel")
            {
                eEng = magTar.GetComponent <EnemyEngine>();
                if (eEng.eWeak == 0)
                {
                    aff = 75;
                }
                else if (eEng.ePower == 0)
                {
                    aff = 45;
                }
                else
                {
                    aff = 65;
                }
            }
            else
            {
                bEng = magTar.GetComponent <Ezekiel>();
                boss = true;
                if (bEng.weak == 0)
                {
                    aff = 75;
                }
                else if (bEng.pow == 0)
                {
                    aff = 45;
                }
                else
                {
                    aff = 65;
                }
            }

            float hitChance = aff * (magLvl + magAttType) / eDef;
            float hit       = Random.Range(0, 100);
            if (hit < hitChance)
            {
                float maxHit  = Mathf.Ceil(0.5f * (magLvl + magAttType));
                int   magRoll = Mathf.RoundToInt(Random.Range(1, maxHit));
                MagicGiveExp(magRoll);
                PlayerHealth.GiveHealthExp(magRoll);
                magChild = magTar.GetChild(magAttType + 1);

                if (boss)
                {
                    if (bEng.health - magRoll > 0)
                    {
                        StartCoroutine(MagicAnimTimer(magChild));
                    }
                    else
                    {
                        magChild.parent = null;
                        StartCoroutine(MagicAnimTimer(magChild));
                    }
                    bEng.health -= magRoll;
                }
                else
                {
                    if (magRoll > eEng.eHealth)
                    {
                        magRoll = Mathf.RoundToInt(eEng.eHealth);
                    }

                    if (eEng.eHealth - magRoll > 0)
                    {
                        StartCoroutine(MagicAnimTimer(magChild));
                    }
                    else
                    {
                        magChild.parent = null;
                        StartCoroutine(MagicAnimTimer(magChild));
                    }
                    eEng.eHealth -= magRoll;
                }

                DamageController.CreateDamageText(magRoll.ToString(), magTar.transform);
                Debug.Log("MAG - We hit: " + (magRoll));
            }
            else
            {
                Debug.Log("MAG - P:0");
                DamageController.CreateDamageText("0", magTar.transform);
            }
            if (!boss)
            {
                eEng.Aggro = true;
            }
            countDown = 0;
        }
    }
Ejemplo n.º 12
0
    //Gets called when an enemy is going to be attacked with a strength hit.
    public void StrAttackEnemy()
    {
        if (ePos == null)
        {
            return;
        }

        bool  boss = false;
        float aff;

        if (ePos.tag != "Ezekiel")
        {
            eEng = ePos.GetComponent <EnemyEngine>();
            if (eEng.eWeak == 0)
            {
                aff = 75;
            }
            else if (eEng.ePower == 0)
            {
                aff = 45;
            }
            else
            {
                aff = 65;
            }
        }
        else
        {
            bEng = ePos.GetComponent <Ezekiel>();
            boss = true;
            if (bEng.weak == 0)
            {
                aff = 75;
            }
            else if (bEng.pow == 0)
            {
                aff = 45;
            }
            else
            {
                aff = 65;
            }
        }

        float hitChance = aff * (strLvl + PlayerEngine.pWeaponLvl * 2) / eDef;
        float hit       = Random.Range(0, 100);

        if (hit < hitChance)
        {
            float maxHit  = Mathf.Ceil(0.5f * strLvl + PlayerEngine.pWeaponLvl);
            int   strRoll = Mathf.RoundToInt(Random.Range(1, maxHit));
            StrengthGiveExp(strRoll);
            PlayerHealth.GiveHealthExp(strRoll);
            DamageController.CreateDamageText(strRoll.ToString(), ePos.transform);

            if (boss)
            {
                bEng.health -= strRoll;
            }
            else
            {
                if (strRoll > eEng.eHealth)
                {
                    strRoll = Mathf.RoundToInt(eEng.eHealth);
                }
                eEng.eHealth -= strRoll;
            }

            Debug.Log("STR - We hit: " + (strRoll));
        }
        else
        {
            Debug.Log("STR - P:0");
            DamageController.CreateDamageText("0", ePos.transform);
        }

        if (!boss)
        {
            eEng.Aggro = true;
        }
    }