//MA_ means the functions are the ones invoked during an attack

    //1. starts the swing animation, and the attack on a target - at the end of the swing, we check if the target is still in range and if he does not block
    void MA_PrepareMeleeAttack()
    {
        currentCombatState = CombatState.PreparingMeleeAttack;


        invokeTime = Time.time + currentAttack.prepareMeleeAttackTime;


        if (currentTargetsUnitAI != null)
        {
            currentTargetsUnitAI.OnEnemySwingsWeaponOnMe();
        }

        unitAnimation.PlayPrepareAttackAnimation(currentAttack.prepareMeleeAttackTime, currentAttack.animatorTriggerID);

        movement.Stop();

        //move the unit in the right range of the attack
        //movement.MoveTo(currentTarget.transform.position + (transform.position - currentTarget.transform.position).normalized * currentAttack.meleeRange);
        if (currentAttack.hasMovement)
        {
            //movement.MoveTo(transform.position + currentAttack.movementDirection*currentAttack.movementDistance);
            movement.MoveTo(myTransform.position + myTransform.TransformDirection(currentAttack.movementDirection * currentAttack.movementDistance));
        }
    }
Example #2
0
    public override void OnBehaviourEnter()
    {
        switch (state)
        {
        case WorkerState.Construction:

            constructionState = ConstructionState.Idle;

            break;

        case WorkerState.Harvesting:

            if (ressourceInventory.currentRessourceTransportLoad < ressourceInventory.ressourceTransportLoad)
            {
                harvestingState = HarvestingState.Idle;
            }
            else
            {
                harvestingState = HarvestingState.TransportingRessourceToHarvester;
                Vector3 targetPosition = assignedHarvester.transform.position + (entity.transform.position - assignedHarvester.transform.position).normalized * assignedHarvester.width / 2;
                movement.MoveTo(targetPosition);
            }

            break;

        case WorkerState.Depositing:

            state = WorkerState.Idle;

            break;
        }
    }
Example #3
0
    /*enum MeleeFighterState
     * {
     *  TooFar,
     *  TooNear,
     *  InMeleeDistance
     * }
     *
     * MeleeFighterState state;*/

    protected override void Update()
    {
        if (Time.time > nextDistanceCheckTime)
        {
            nextDistanceCheckTime = Time.time + distanceCheckingInterval;

            myWidth    = entity.width;
            enemyWidth = enemySensing.nearestEnemy.width;

            Vector3 nearestEnemyPosition = enemySensing.nearestEnemy.transform.position;
            Vector3 myPosition           = entity.transform.position;

            float   widthFactor    = myWidth + enemyWidth; //multiply the resulting distanceVectorBythisFactor to also use width
            Vector3 distanceVector = nearestEnemyPosition - myPosition;
            //float distanceToEnemySquared = (distanceVector - distanceVector.normalized * widthFactor).sqrMagnitude;
            float distanceToEnemy = (distanceVector - distanceVector.normalized * widthFactor).magnitude;

            //if the enemy is moving, we move to the position he will be at the time we arrive
            EC_Movement enemyMovement = enemySensing.nearestEnemy.GetComponent <EC_Movement>();


            if (enemyMovement.IsMoving())
            {
                //heuristically calculae future position
                //1. how long will it take for me to reach the enemy?
                float timeToReachEnemy = distanceToEnemy / movement.GetMaxSpeed();
                //2. where will the enemy be after this time
                Vector3 futurePosition = nearestEnemyPosition + enemyMovement.GetCurrentVelocity() * timeToReachEnemy;


                movement.MoveTo(futurePosition);
            }
            else
            {
                movement.MoveTo(nearestEnemyPosition + (myPosition - nearestEnemyPosition).normalized * (perfectMeleeDistance + myWidth + enemyWidth));
            }

            if ((nearestEnemyPosition - myPosition).sqrMagnitude > maxMeleeDistance)
            {
                inRange = false;
                movement.StopLookAt();
            }
            else
            {
                inRange = true;
                movement.LookAt(enemySensing.nearestEnemy.transform);
            }
        }

        if (inRange)
        {
            if (weapon.CanAttack())
            {
                weapon.Attack();
            }
        }
    }
Example #4
0
 protected override void Update()
 {
     if (sensing.nearestEnemy != null)
     {
         movement.MoveTo((entity.transform.position - sensing.nearestEnemy.transform.position).normalized * 5);
     }
     else
     {
         movement.Stop();
     }
 }
    // Update is called once per frame
    void Update()
    {
        if (Input.GetKeyDown(KeyCode.M))
        {
            movement.MoveTo(transform.position + transform.forward * 20);
        }

        if (Input.GetKeyDown(KeyCode.L))
        {
            movement.LookAt(targetToLookAt);
        }
    }
Example #6
0
    protected override void Update()
    {
        Vector3 wanderPoint = UnityEngine.Random.insideUnitSphere * 4;

        wanderPoint.y = entity.transform.position.y;

        wanderPoint += entity.transform.forward * 4 + entity.transform.position;

        //if he would stray off to far, bring him back to base
        if (Vector3.Distance(positionToWanderAround.position, wanderPoint) > maxDistanceToPositionWhileWandering)
        {
            wanderPoint += (positionToWanderAround.position - wanderPoint) / 4;
        }

        movement.MoveTo(wanderPoint);
    }
Example #7
0
    protected override void Update()
    {
        if (Time.time > nextDistanceCheckTime)
        {
            nextDistanceCheckTime = Time.time + distanceCheckingInterval;

            myWidth    = entity.width;
            enemyWidth = enemySensing.nearestEnemy.width;

            Vector3 nearestEnemyPosition = enemySensing.nearestEnemy.transform.position;
            Vector3 myPosition           = entity.transform.position;

            float   widthFactor     = myWidth + enemyWidth; //multiply the resulting distanceVectorBythisFactor to also use width
            Vector3 distanceVector  = nearestEnemyPosition - myPosition;
            float   distanceToEnemy = (distanceVector - distanceVector.normalized * widthFactor).sqrMagnitude;

            movement.MoveTo(nearestEnemyPosition + (myPosition - nearestEnemyPosition).normalized * (perfectShootingDistance + myWidth + enemyWidth));

            if ((nearestEnemyPosition - myPosition).sqrMagnitude > maxShootingDistance)
            {
                inRange = false;
                movement.StopLookAt();
                weapon.StopAiming();
                //Debug.Log("stop aim");
            }
            else
            {
                inRange = true;
                // Debug.Log("start aim");

                movement.LookAt(enemySensing.nearestEnemy.transform);
                weapon.AimAt(enemySensing.nearestEnemy);
            }
        }

        if (inRange)
        {
            if (weapon.CanShoot())
            {
                weapon.Shoot();
            }
        }
    }
Example #8
0
    protected override void Update()
    {
        if (Time.time > nextDistanceCheckTime)
        {
            myWidth            = entity.width;
            enemyBuildingWidth = targetBuilding.width;

            nextDistanceCheckTime = Time.time + distanceCheckingInterval;

            Vector3 buildingPosition = targetBuilding.transform.position;
            Vector3 myPosition       = entity.transform.position;

            float   widthFactor     = myWidth + enemyBuildingWidth; //multiply the resulting distanceVectorBythisFactor to also use width
            Vector3 distanceVector  = buildingPosition - myPosition;
            float   distanceSquared = (distanceVector - distanceVector.normalized * widthFactor).sqrMagnitude;

            if (distanceSquared > maxMeleeDistance)
            {
                inRange = false;
            }
            else
            {
                inRange = true;
            }

            movement.MoveTo(buildingPosition + (myPosition - buildingPosition).normalized * (perfectMeleeDistance + myWidth + enemyBuildingWidth));
        }

        if (inRange)
        {
            if (weapon.CanAttack())
            {
                weapon.Attack();
            }
        }
    }
Example #9
0
    public override void BehaviourUpdate()
    {
        //Profiler.BeginSample("BehaviourUpdate");
        #region debug
        if (Input.GetKeyDown(KeyCode.B))
        {
            combat.Block(currentStance.blocks[0]);
        }
        if (Input.GetKeyDown(KeyCode.N))
        {
            combat.B_BlockFlinch();
        }
        if (Input.GetKeyDown(KeyCode.M))
        {
            combat.Evade(currentStance.evades[1]);
        }
        if (Input.GetKeyDown(KeyCode.L))
        {
            combat.MeleeAttack(null, currentStance.attacks[1]);
        }
        #endregion


        //cache some variables for later
        nearestEnemy        = sensing.nearestEnemy;
        currentStance       = unitAI.currentStance;
        myTransformPosition = entity.myTransform.position;
        enemyIsNull         = nearestEnemy == null;


        //1. look at the enemy, if no enemy present -> continue looking in the movement direction


        if (!enemyIsNull)
        {
            //but only if something chenged
            if (nearestEnemyLastFrame != nearestEnemy)
            {
                nearestEnemysTransform = nearestEnemy.myTransform;
                movement.LookAt(nearestEnemysTransform);
            }

            nearestEnemyPosition = nearestEnemysTransform.position;
            currentSquaredDistanceToNearestEnemy = (nearestEnemyPosition - myTransformPosition).sqrMagnitude;
        }
        else
        {
            if (nearestEnemyLastFrame != nearestEnemy)
            {
                movement.StopLookAt();
            }
        }

        switch (meleeAttackBehaviourState)
        {
        //2. if theres an enemy -> walk towards him
        case MeleeAttackBehaviourState.Idle:

            if (!enemyIsNull)
            {
                movement.MoveTo(nearestEnemyPosition);
                meleeAttackBehaviourState = MeleeAttackBehaviourState.MovingToNearestEnemy;
            }

            break;

        case MeleeAttackBehaviourState.MovingToNearestEnemy:

            //check if the enemy is still alive
            if (enemyIsNull)
            {
                meleeAttackBehaviourState = MeleeAttackBehaviourState.Idle;
                movement.Stop();
            }
            //else check if we reached the destination
            else if (currentSquaredDistanceToNearestEnemy < rangeToEnterMeleeCombatSquared)
            {
                meleeAttackBehaviourState = MeleeAttackBehaviourState.InMeleeFight;
            }
            //if not, check if we need to update the destination
            else
            {
                if (Time.time > nextUpdateDestinationTime)
                {
                    nextUpdateDestinationTime = Time.time + updateDestinationInterval;
                    movement.MoveTo(nearestEnemyPosition);
                }
            }

            break;

        case MeleeAttackBehaviourState.InMeleeFight:

            //check if the enemy is still alive
            if (enemyIsNull)
            {
                meleeAttackBehaviourState = MeleeAttackBehaviourState.Idle;
                movement.Stop();
            }
            //check if the enemy has not escaped
            else if (currentSquaredDistanceToNearestEnemy > rangeToEnterMeleeCombatSquared)
            {
                if (combat.currentCombatState == CombatState.CombatIdle)
                {
                    movement.MoveTo(nearestEnemyPosition);
                    meleeAttackBehaviourState = MeleeAttackBehaviourState.MovingToNearestEnemy;
                }
            }
            //else check if i am in range for an attack - fix range and attack
            else
            {
                //decision making .- thats the tricky part here
                if (combat.currentCombatState == CombatState.CombatIdle)
                {
                    //check if we should block because our adversary was faster
                    if (nearestEnemy.combat.currentCombatState == CombatState.PreparingMeleeAttack)
                    {
                        //if he is lucky he will block or evade, if not then not :D
                        if (Random.Range(0, 2) == 0)
                        {
                            if (Random.Range(0, 2) == 0)
                            {
                                combat.Block(currentStance.blocks[0]);
                            }
                            else
                            {
                                combat.Evade(currentStance.evades[0]);
                            }
                        }
                        else
                        {
                            combat.MeleeAttack(nearestEnemy, currentStance.attacks[Random.Range(0, currentStance.attacks.Length)]);
                        }
                    }
                    else
                    {
                        combat.MeleeAttack(nearestEnemy, currentStance.attacks[Random.Range(0, currentStance.attacks.Length)]);
                    }
                }
                else if (combat.currentCombatState == CombatState.Blocking)
                {
                    //check if we can stop blocking and attack
                    if (nearestEnemy.combat.currentCombatState != CombatState.PreparingMeleeAttack)
                    {
                        combat.MeleeAttack(nearestEnemy, currentStance.attacks[Random.Range(0, currentStance.attacks.Length)]);
                    }
                }
            }
            break;
        }

        nearestEnemyLastFrame = nearestEnemy;

        // Profiler.EndSample();
    }
Example #10
0
    void Update()
    {
        float horizontalInput = Input.GetAxis("Horizontal");
        float verticalInput   = Input.GetAxis("Vertical");

        if (unitIsPosessedByPlayer)
        {
            if (movement.agent.desiredVelocity.sqrMagnitude > 0)
            {
                movement.RotateTo(movement.agent.desiredVelocity, Time.deltaTime);
            }

            #region movement


            if (horizontalInput != 0 || verticalInput != 0)
            {
                Vector3 flatUpVectorOfCamera = Camera.main.transform.up;
                flatUpVectorOfCamera.y = 0;
                flatUpVectorOfCamera.Normalize();
                Vector3 flatRightVectorOfCamera = Camera.main.transform.right;
                flatRightVectorOfCamera.y = 0;
                flatRightVectorOfCamera.Normalize();

                flatRightVectorOfCamera *= horizontalInput;
                flatUpVectorOfCamera    *= verticalInput;

                Vector3 movementVector = flatRightVectorOfCamera + flatUpVectorOfCamera;

                if (combat.currentCombatState == CombatState.CombatIdle)
                {
                    movement.MoveTo(playerEntity.transform.position + movementVector);
                }
            }
            else
            {
                // movement.Stop();
            }

            #endregion

            #region combat

            if (Input.GetMouseButtonDown(1))
            {
                combat.Block(unitAI.currentStance.blocks[0]);
            }
            else if (!Input.GetMouseButton(1))
            {
                if (Input.GetKeyDown(KeyCode.G))
                {
                    combat.MeleeAttack(null, unitAI.currentStance.attacks[1]);
                }

                if (Input.GetMouseButtonDown(0))
                {
                    if (combat.currentCombatState == CombatState.Blocking || combat.currentCombatState == CombatState.CombatIdle || combat.currentCombatState == CombatState.PreparingBlock)
                    {
                        Debug.Log("hor: " + horizontalInput);
                        Debug.Log("vert: " + verticalInput);
                        if (horizontalInput != 0 || verticalInput != 0)
                        {
                            Debug.Log("yes");
                            combat.MeleeAttack(null, unitAI.currentStance.attacks[1]);
                        }
                        else
                        {
                            combat.MeleeAttack(null, unitAI.currentStance.attacks[0]);
                        }
                    }
                }
                else if (Input.GetKeyDown(KeyCode.E))
                {
                    combat.MeleeAttack(null, unitAI.currentStance.attacks[2]);
                }
                else if (combat.currentCombatState == CombatState.Blocking || combat.currentCombatState == CombatState.PreparingBlock)
                {
                    combat.StopBlocking();
                }
            }
            if (Input.GetKeyDown(KeyCode.Space))
            {
                if (horizontalInput != 0 || verticalInput != 0)
                {
                    Debug.Log("yep");
                    Vector3 inputDirection = new Vector3(horizontalInput, 0f, verticalInput);
                    combat.Evade(unitAI.currentStance.evades[0]);
                }
            }

            #endregion
        }
        else
        {
            //simple rts camera movement

            Vector3 flatUpVectorOfCamera = Camera.main.transform.up;
            flatUpVectorOfCamera.y = 0;
            flatUpVectorOfCamera.Normalize();
            Vector3 flatRightVectorOfCamera = Camera.main.transform.right;
            flatRightVectorOfCamera.y = 0;
            flatRightVectorOfCamera.Normalize();

            flatRightVectorOfCamera *= horizontalInput;
            flatUpVectorOfCamera    *= verticalInput;

            Vector3 movementVector = flatRightVectorOfCamera + flatUpVectorOfCamera;

            cam.transform.position += movementVector;
        }

        if (Input.GetAxis("Mouse ScrollWheel") != 0)
        {
            cam.GetComponent <Camera>().orthographicSize -= Input.GetAxis("Mouse ScrollWheel") * 2;
        }

        if (Input.GetKeyDown(KeyCode.P))
        {
            if (unitIsPosessedByPlayer)
            {
                Deposess();
            }
            else
            {
                Posess();
            }
        }
    }
Example #11
0
    public override void UpdateComponent()
    {
        switch (state)
        {
        case MissileFighterState.Idle:

            if (sensing.nearestEnemy != null)
            {
                state = MissileFighterState.AttackingEnemy;
            }

            break;

        case MissileFighterState.AttackingEnemy:

            break;

        case MissileFighterState.AttackingBase:

            break;
        }

        switch (shootingState)
        {
        case ShootingState.TooNear:

            if (Time.time > nextCheckTime)
            {
                nextCheckTime = Time.time + checkingInterval;

                float distanceToEnemy = (sensing.nearestEnemy.transform.position - transform.position).sqrMagnitude;
                if (distanceToEnemy > minimalShootingDistance)
                {
                    shootingState = ShootingState.InShootingDistance;
                    movement.Stop();
                }
                else
                {
                    //move to a position perfectly in the shooting range
                    movement.MoveTo((transform.position - sensing.nearestEnemy.transform.position).normalized * (minimalShootingDistance + (maximalShootingDistance - minimalShootingDistance) / 2));
                    weapon.AimAt(sensing.nearestEnemy);
                    if (weapon.CanShoot())
                    {
                        weapon.Shoot();
                    }
                }
            }

            break;

        case ShootingState.TooFarAway:

            if (Time.time > nextCheckTime)
            {
                nextCheckTime = Time.time + checkingInterval;

                float distanceToEnemy = (sensing.nearestEnemy.transform.position - transform.position).sqrMagnitude;
                if (distanceToEnemy < maximalShootingDistance)
                {
                    shootingState = ShootingState.InShootingDistance;
                    movement.LookAt(sensing.nearestEnemy.transform);
                    weapon.AimAt(sensing.nearestEnemy);
                    movement.Stop();
                }
                else
                {
                    movement.MoveTo((transform.position - sensing.nearestEnemy.transform.position).normalized * (minimalShootingDistance + (maximalShootingDistance - minimalShootingDistance) / 2));
                }
            }
            break;

        case ShootingState.InShootingDistance:

            //1. check if we need to change state
            if (Time.time > nextCheckTime)
            {
                nextCheckTime = Time.time + checkingInterval;
                float distanceToEnemy = (sensing.nearestEnemy.transform.position - transform.position).sqrMagnitude;

                if (distanceToEnemy < minimalShootingDistance)
                {
                    shootingState = ShootingState.TooNear;
                    movement.LookAt(sensing.nearestEnemy.transform);
                    movement.MoveTo((transform.position - sensing.nearestEnemy.transform.position).normalized * (minimalShootingDistance + (maximalShootingDistance - minimalShootingDistance) / 2));
                }
                else if (distanceToEnemy > maximalShootingDistance)
                {
                    shootingState = ShootingState.TooFarAway;
                    movement.StopLookAt();
                    weapon.StopAiming();
                    movement.MoveTo((transform.position - sensing.nearestEnemy.transform.position).normalized * (minimalShootingDistance + (maximalShootingDistance - minimalShootingDistance) / 2));
                }
                else
                {
                    if (weapon.CanShoot())
                    {
                        weapon.Shoot();
                    }
                }
            }

            break;
        }
    }