Examples:
- Finite State Example: The
Paddle:
- A-Button: to create a new random shooting ball
- B-Button: to destroy current moving ball
- Left-Trigger: trigger the left-side paddles
- Right-Trigger: trigger the right-side paddles
- The Paddle class:
- Two states: Rest and PaddleReturning
- Update() method test and call each explicitly
- Rest: if trigger angle, then rotate the paddle and goto
PaddleReturning
- PaddleReturning: gradually return, if user has new input,
maintain the paddle rotation if necessary
- Left/Right paddles' rotations are exactly opposite.
- Record hit/miss for fun. Really? Not fun at all!
- SimplePatrol:
- A-Button: to add a new patrol (try adding 100 patrols!)
- B-Button: to remove an existing patrol
- Notice behavior of patrol:
- appears random
- moves in counter-clockwise order
- speed is sort of random
-
Class to check out: PatrolObject
- Update function: define a state service function for each state!!
- RandomXXRegionPosition() functions: generate a random position in
the XX region, where XX can be top/bottom-left/right.
- ComputePositionAndVelocity(): Compute a new velocity (with 20%
randomness) and use time (number of ticks) as condition for transition
to the next state.
- RandomPatrol:
- Identical to above, except,
- Randomize next states, look it is relatively straightforward to
determine what each state should transition to (localized in each individual
state's service routine!).
- Some randomness is kind of cool?
- SmoothTransitionPatrol:
- Exactly the same as the above, except
- We do not compute new Velocity direction in
ComputePositionAndVelocity() anymore!!
- Instead: during each update, we call ComputeNewDirection() to rotate
gradually from FrontDirection towards the final target position.
- Oh, the patrols are smaller and we assign a random color to each patrol
- PatrolThatChases:
- Notice: straightforward to add in a new state: just add in new
service routine into Update()
- Notice: "partial" keyword to split complex class into multiple files
- All Hero related behaviord are in PatrolObject_HeroSupport.cs
- All other service routines now check for Hero being near by (DetectHero()):
In DetectHero():
- Compute distance to Hero
- If sufficiently close: transit to ChaseHero state!
-
Hero state service:
- Always try to follow the hero (by setting mTargetPosition to
hero.Center)
- If cannot catch hero in specific time (mStateTimer),
transition back into regular state by determining which quadrant
the patrol is currently in.
-
The app removes patrol if there is a collision with hero.