Example #1
0
        void FixedUpdate()
        {
#if UNITY_EDITOR
            if (isDummy)
            {
                return;
            }
            if (actorController == null)
            {
                return;                 // prevents error spam after editing a script
            }
#endif
            actorPhysics.UpdateInternalStateFromPhysicsResult();
            actorController.FixedUpdateCommands();
            MovementStateType nextState = movementState.FixedUpdateState();
            if (nextState != MovementStateType.NONE)
            {
                if (!movementStateLookup.TryGetValue(nextState, out IMovementState newMovement))
                {
                    Debug.Log(this.name + " could not find movementState for " + nextState.ToString());
                }
                else
                {
                    MovementStateType previousState = movementState.ExitState(nextState);
                    movementState = newMovement;
                    movementState.EnterState(previousState);
                    currentMovementState = nextState;
                }
            }

            actorPhysics.ApplyToPhysics();
        }
 public override void UpdateState()
 {
     if (currentStateImplementation == null)
     {
         if (!TryTransitionToState(defaultState))
         {
             Debug.LogError(actor.name + " " + actor.name + " has no MovementImplementation.");
         }
     }
     else
     {
         MovementStateType nextState = currentStateImplementation.OnUpdate();
         if (nextState != currentState)
         {
             if (!TryTransitionToState(nextState))
             {
                 Debug.LogWarning(actor.name + " could not transition from " + currentState + " to " + nextState);
             }
         }
     }
 }
 public MovementState(TActor owner, MovementStateType stateType)
 {
     actor             = owner;
     movementStateType = stateType;
 }
Example #4
0
        public void Awake()
        {
            sprite       = GetComponent <SpriteRenderer>();
            actorPhysics = GetComponent <IActorPhysics>();
            if (!animator)
            {
                animator = GetComponent <Animator>();
                if (!animator)
                {
                    Debug.LogWarning("No animator found for " + gameObject.name);
                }
            }

            actionStateLookup   = new Dictionary <ActionStateType, IActionState>();
            movementStateLookup = new Dictionary <MovementStateType, IMovementState>();

            if (!standingState)
            {
                standingState = GetComponentInChildren <StandingState>();
            }
            if (standingState)
            {
                standingState.SetActor(this);
                movementStateLookup.Add(MovementStateType.STANDING, standingState);
            }

            if (!duckingState)
            {
                duckingState = GetComponentInChildren <KneelingState>();
            }
            if (duckingState)
            {
                duckingState.SetActor(this);
                movementStateLookup.Add(MovementStateType.KNEELING, duckingState);
            }

            if (!walkingState)
            {
                walkingState = GetComponentInChildren <WalkingState>();
            }
            if (walkingState)
            {
                walkingState.SetActor(this);
                movementStateLookup.Add(MovementStateType.WALKING, walkingState);
            }

            if (stairsState)
            {
                stairsState.SetActor(this);
                movementStateLookup.Add(MovementStateType.STAIRS, stairsState);
            }

            if (!airbornState)
            {
                airbornState = GetComponentInChildren <FallingState>();
            }
            if (airbornState)
            {
                airbornState.SetActor(this);
                movementStateLookup.Add(MovementStateType.FALLING, airbornState);
            }

            if (!jetpackState)
            {
                jetpackState = GetComponentInChildren <JetpackState>();
            }
            if (jetpackState)
            {
                jetpackState.SetActor(this);
                movementStateLookup.Add(MovementStateType.JETPACK, jetpackState);
            }

            elevatorState = GetComponentInChildren <ElevatorState>();
            if (elevatorState)
            {
                elevatorState.SetActor(this);
                movementStateLookup.Add(MovementStateType.ELEVATOR, elevatorState);
                actionStateLookup.Add(ActionStateType.ELEVATOR, elevatorState);
            }



            NoActionState awaitingState = new NoActionState();

            awaitingState.SetActor(this);
            actionStateLookup.Add(ActionStateType.AWAITING_ACTION, awaitingState);

            if (!shootingState)
            {
                shootingState = GetComponentInChildren <ShootingState>();
            }
            if (shootingState)
            {
                shootingState.SetActor(this);
                actionStateLookup.Add(ActionStateType.SHOOT, shootingState);
            }

            currentMovementState = MovementStateType.FALLING;
            movementState        = airbornState;
            currentActionState   = ActionStateType.AWAITING_ACTION;
            actionState          = actionStateLookup[ActionStateType.AWAITING_ACTION];
        }