Esempio n. 1
0
    public void Init(int id, CitizenType citizenType)
    {
        #region 初始化参数
        timeAlive           = 0;
        ID                  = id;
        CitizenType         = citizenType;
        bWearingMask        = false;
        bRecalculatePathKey = false;
        bCheckAmbulanceKey  = false;

        CureTimeTotal   = CitizenMgr.GenerateCureTime();
        LatentTimeTotal = CitizenMgr.GenerateLatentTime();
        ToHospTimeTotal = CitizenMgr.GenerateToHospitalTime();
        AliveTimeTotal  = CitizenMgr.GenerateAliveTime();
        InfectRate      = CitizenMgr.GenerateInfectRate();
        #endregion

        #region 初始化组件
        spriteRenderer = GetComponent <SpriteRenderer>();
        navAgent       = GetComponent <NavMeshAgent>();
        animator       = GetComponent <Animator>();
        citizenStatus  = GetComponent <CitizenStatus>();
        boxCollider    = GetComponent <BoxCollider>();

        navAgent.updateRotation = false;
        citizenStatus.Init(transform);
        #endregion

        #region 初始化状态机
        fsmSystem = new FSMSystem();

        FSMHealthyState healthyState = new FSMHealthyState(fsmSystem);
        healthyState.AddTransition(FSMTransition.Infected, FSMStateID.LatentFSMStateID);

        FSMLatentState latentState = new FSMLatentState(fsmSystem);
        latentState.AddTransition(FSMTransition.AfterLatent, FSMStateID.IllFSMStateID);
        latentState.AddTransition(FSMTransition.TookByAmbulance, FSMStateID.ToHospitalFSMStateID);
        latentState.AddTransition(FSMTransition.CantTakeAmbulance, FSMStateID.WaitToHospFSMStateID);

        FSMIllState illState = new FSMIllState(fsmSystem);
        illState.AddTransition(FSMTransition.WantHealing, FSMStateID.WaitToHospFSMStateID);
        illState.AddTransition(FSMTransition.ToLate, FSMStateID.DeadFSMStateID);
        illState.AddTransition(FSMTransition.TookByAmbulance, FSMStateID.ToHospitalFSMStateID);
        illState.AddTransition(FSMTransition.CantTakeAmbulance, FSMStateID.WaitToHospFSMStateID);

        FSMWaitHospState waitToHospitalState = new FSMWaitHospState(fsmSystem);
        waitToHospitalState.AddTransition(FSMTransition.BedsAvailable, FSMStateID.ToHospitalFSMStateID);
        waitToHospitalState.AddTransition(FSMTransition.ToLate, FSMStateID.DeadFSMStateID);

        FSMToHospitalState toHospitalState = new FSMToHospitalState(fsmSystem);
        toHospitalState.AddTransition(FSMTransition.ReachHospital, FSMStateID.InHospitalFSMStateID);
        toHospitalState.AddTransition(FSMTransition.ToLate, FSMStateID.DeadFSMStateID);

        FSMInHospitalState inHospitalState = new FSMInHospitalState(fsmSystem);
        inHospitalState.AddTransition(FSMTransition.Healed, FSMStateID.CuredFSMStateID);
        inHospitalState.AddTransition(FSMTransition.HealingFailed, FSMStateID.DeadFSMStateID);
        inHospitalState.AddTransition(FSMTransition.ToLate, FSMStateID.DeadFSMStateID);

        FSMCuredState curedState = new FSMCuredState(fsmSystem);
        FSMDeadState  deadState  = new FSMDeadState(fsmSystem);

        List <FSMBaseState> statesList = new List <FSMBaseState> {
            healthyState, latentState, illState, waitToHospitalState,
            toHospitalState, inHospitalState, curedState, deadState
        };
        foreach (FSMBaseState state in statesList)
        {
            state.Init(this, animator);
        }
        switch (citizenType)
        {
        case CitizenType.Healthy: fsmSystem.AddFSMSate(healthyState); CitizenType = CitizenType.Healthy; break;

        case CitizenType.Latent: fsmSystem.AddFSMSate(latentState); CitizenType = CitizenType.Latent; break;

        case CitizenType.Ill: fsmSystem.AddFSMSate(illState); CitizenType = CitizenType.Ill; break;

        case CitizenType.InHospital: fsmSystem.AddFSMSate(inHospitalState); CitizenType = CitizenType.InHospital; break;

        case CitizenType.Cured: fsmSystem.AddFSMSate(curedState); CitizenType = CitizenType.Cured; break;

        case CitizenType.Dead: fsmSystem.AddFSMSate(deadState); CitizenType = CitizenType.Dead; break;
        }
        foreach (FSMBaseState state in statesList)
        {
            fsmSystem.AddFSMSate(state);
        }
        #endregion

        #region 初始化动画
        int controllerIndex = Random.Range(0, 4);
        AnimatorOverrideController controller = new AnimatorOverrideController();
        switch (controllerIndex)
        {
        case 0:
            controller.runtimeAnimatorController = CitizenMgr.Instance.F1;
            break;

        case 1:
            controller.runtimeAnimatorController = CitizenMgr.Instance.F2;
            break;

        case 2:
            controller.runtimeAnimatorController = CitizenMgr.Instance.M1;
            break;

        case 3:
            controller.runtimeAnimatorController = CitizenMgr.Instance.M2;
            break;

        default:
            controller.runtimeAnimatorController = CitizenMgr.Instance.F1;
            break;
        }
        animator.runtimeAnimatorController = controller;
        #endregion
    }