コード例 #1
0
ファイル: Unit.cs プロジェクト: MartGon/DungeonGen
    // Use this for initialization
    void Start()
    {
        // State
        state    = UnitState.IDLE;
        subState = UnitSubState.IDLE_STOPPED;

        // Stats
        getLevelByRound();
        generateStatsByLevel();
        currentHealthPoints = healthPoints;

        // Timers
        recoverTimerCount = recoverTimer;
        speedRecoverCount = speedRecoverRate;

        // Position
        patrolPosition = transform.position;

        // Unit UI
        unitUI.setName(unitName);
        unitUI.setLevel(level);
        unitUI.maxHealthPoints = healthPoints;
        unitUI.updateHealthPoints(currentHealthPoints);

        // Components
        Player        = GameObject.FindGameObjectWithTag("Player");
        navMeshAgent  = GetComponentInChildren <NavMeshAgent>();
        movementSpeed = navMeshAgent.speed;
    }
コード例 #2
0
ファイル: Unit.cs プロジェクト: MartGon/DungeonGen
    public void recover()
    {
        recoverTimerCount -= Time.deltaTime;
        if (recoverTimerCount < 0)
        {
            // Enables/disables
            navMeshAgent.enabled = true;
            animator.SetBool("Death", false);
            //boxCollider.enabled = true;
            gameObject.layer = 8;

            // Reset health and timer
            recoverTimerCount   = recoverTimer;
            currentHealthPoints = healthPoints / 2;
            unitUI.updateHealthPoints(currentHealthPoints);
            unitRigidbody.isKinematic = true;

            state    = UnitState.IDLE;
            subState = UnitSubState.IDLE_LEFT_COMBAT;
        }
    }
コード例 #3
0
ファイル: Unit.cs プロジェクト: MartGon/DungeonGen
    void updateState()
    {
        if (state != UnitState.RECOVERING)
        {
            checkDeath();
        }

        switch (state)
        {
        case UnitState.IDLE:
            unitUI.gameObject.SetActive(false);

            switch (subState)
            {
            case UnitSubState.IDLE_LEFT_COMBAT:
                goToPosition(patrolPosition);
                subState = UnitSubState.IDLE_MOVING;
                break;

            case UnitSubState.IDLE_STOPPED:
                if (homeRoom == null)
                {
                    break;
                }

                Vector3 position = homeRoom.getRandomPosition();
                goToPosition(position);

                subState = UnitSubState.IDLE_MOVING;
                break;

            case UnitSubState.IDLE_MOVING:
                if (navMeshAgent.remainingDistance < 5)
                {
                    subState = UnitSubState.IDLE_STOPPED;
                }
                if (navMeshAgent.pathStatus == NavMeshPathStatus.PathInvalid)
                {
                    Debug.Log("Path inválido");
                    subState = UnitSubState.IDLE_STOPPED;
                }
                break;
            }

            if (canSeePlayer())
            {
                state = UnitState.IN_COMBAT;
            }

            break;

        case UnitState.IN_COMBAT:
            if (!canSeePlayer())
            {
                leaveCombatTimerCount -= Time.deltaTime;
                if (leaveCombatTimerCount < 0)
                {
                    state    = UnitState.IDLE;
                    subState = UnitSubState.IDLE_LEFT_COMBAT;
                    leaveCombatTimerCount = leaveCombatTimer;
                }
            }
            else
            {
                leaveCombatTimerCount = leaveCombatTimer;
            }

            if (isInCombatDistance())
            {
                if (isAttackReady())
                {
                    startAttack();
                }
                else
                {
                    stopChasing();
                }
            }
            else
            {
                chasePlayer();
            }

            updateAttackTimer();
            break;

        case UnitState.ATTACKING:
            if (attackAnimationIsOver())
            {
                performAttack();
            }
            break;

        case UnitState.ON_ATTACK:
            break;

        case UnitState.DEAD:
            unitUI.gameObject.SetActive(false);

            // Check item drops
            checkDrops();

            // Disable flags
            navMeshAgent.enabled = false;
            animator.SetBool("Attack", false);
            animator.SetBool("MoveForward", false);
            animator.SetBool("Death", true);
            unitRigidbody.isKinematic = false;
            gameObject.layer          = 9;

            state = UnitState.RECOVERING;

            break;

        case UnitState.RECOVERING:
            recover();
            break;
        }
    }