private bool _isMove = false; //能否开始移动 // Use this for initialization void Start() { //初始化状态 _idle = new LYState("Idle"); _idle.OnEnter += (IState state) => { Debug.Log("进入Idle状态"); }; _move = new LYState("Move"); _move.OnUpdate += (float f) => { transform.position += transform.forward * f * speed; }; _idleMove = new LYTransition("IdleMove", _idle, _move); _idleMove.OnCheck += () => _isMove; _idle.AddTransition(_idleMove); //_idleMove.OnTransition += () => { } _moveIdle = new LYTransition("MoveIdle", _move, _idle); _moveIdle.OnCheck += () => !_isMove; _move.AddTransition(_moveIdle); _fsm = new LYStateMacine("Root", _idle); _fsm.AddState(_move); }
private void InitFSM() { _changeIntensity = new LYState("ChangeInitensity"); _changeColor = new LYState("ChangeColor"); _changeColor.OnEnter += (IState state) => { _isAnimation = false; }; _changeColor.OnUpdate += (float f) => { if (_isAnimation) { if (_colorTimer >= 1f) { _isAnimation = false; } else { _colorTimer += Time.deltaTime * colorSpeed; _light.color = Color.Lerp(_startColor, _targetColor, _colorTimer); } } else { float r = Random.Range(0f, 1f); float g = Random.Range(0f, 1f); float b = Random.Range(0f, 1f); _targetColor = new Color(r, g, b); _startColor = _light.color; _colorTimer = 0f; _isAnimation = true; } }; _changeIntensity.OnUpdate += (float f) => { if (_isAnimation) { if (_isRest) { if (FadeTo(maxIntensity)) { _isRest = false; _isAnimation = false; } } else { if (FadeTo(_target)) { _isRest = true; } } } else { _target = Random.Range(0.3f, 0.7f); _isAnimation = true; } }; _colorIntensity = new LYTransition("ColorIntensity", _changeColor, _changeIntensity); _colorIntensity.OnCheck += () => { return(_ischangeColor); }; _changeColor.AddTransition(_colorIntensity); _intensityColor = new LYTransition("IntensityColor", _changeIntensity, _changeColor); _intensityColor.OnCheck += () => { return(!_ischangeColor); }; _changeIntensity.AddTransition(_intensityColor); _open = new LYStateMacine("Open", _changeIntensity); _open.OnEnter += (IState state) => { _light.intensity = maxIntensity; }; _close = new LYState("Close"); _close.OnEnter += (IState state) => { _light.intensity = 0f; }; _openClose = new LYTransition("OpenClose", _open, _close); _openClose.OnCheck += () => !_isOpen; _openClose.OnTransition += () => FadeTo(0f); _open.AddTransition(_openClose); _closeOpen = new LYTransition("CloseOpen", _close, _open); _closeOpen.OnCheck += () => _isOpen; _closeOpen.OnTransition += () => FadeTo(maxIntensity); _close.AddTransition(_closeOpen); _fsm = new LYStateMacine("Root", _open); _fsm.AddState(_close); }