Bear MODULE PSEUDOCODE
Bear module (State Machine)
//runs as a state machine, handles detect/reset of the force sensor turning on the bear lights, and randomly activating the bear.
define MAX_HEALTH
define KILL_TIME, MIN_WAIT_TIME, WAIT_TIME_RANGE
define HIT_THRESHOLD, DAMGE_FACTOR
define BEAR_TOP_THRESHOLD, BEAR_BOT_THRESHOLD
static variable myHealth, opHealth
static local variable currentState BearState_t
//states include: BEAR_INIT, BEAR_LIGHT_OFF, BEAR_LIGHT_ON, BEAR_KILLED
initBear takes nothing, returns true
//AD pins are initialized in main function
set myHealth as MAX_HEALTH
set opHealth as MAX_HEALTH
post myHealth to HealthBar module
disable peak hold
set currentState as BEAR_INIT
End of initBear
runBear takes events
If currentState is BEAR_INIT
If EventType is ES_TREE_DONE
set currentState to BEAR_LIGHT_OFF
start BEAR_TIMER with random value //random time until bear is ready to hit
Else if EventType is ES_CARD_REMOVED
reset Bear module
End if
Else if currentState is BEAR_LIGHT_OFF
If EventType is ES_TIMEOUT
if EventParam is BEAR_TIMER
able peak hold //bear is ready to hit
turn on the bear light
set currentState to BEAR_LIGHT_ON
start BEAR_TIMER with KILL_TIME
End if
Else if EventType is ES_CARD_REMOVED
reset Bear module
Else if EventType is ES_BEAR_HEALTH_TOP_THRESHOLD
set opHealth as BEAR_TOP_THRESHOLD - 1
Else if EventType is ES_BEAR_HEALTH_BOT_THRESHOLD
set opHealth as BEAR_BOT_THRESHOLD - 1
Else if EventType is ES_BEAR_HEALTH_ZERO
set opHealth as 0
Else if EventType is ES_GAME_STOPPED
set currentState to BEAR_KILLED
Else if EventType is ES_GAME_OVER
set currentState to BEAR_KILLED
End if
Else if currentState is BEAR_LIGHT_ON
If EventType is ES_TIMEOUT
if EventParam is BEAR_TIMER
disable peak hold // bear is not ready to be hit
turn off the bear lights
set currentState to BEAR_LIGHT_OFF
start BEAR_TIMER with random time
End if
Else if EventType is ES_BEAR_HIT
use adjustDamage to get adjusted damage with handicap and save to damage
If damage is less than myHealth
deduce myHealth by damage
start BEAR_TIMER with random time
set currentState to BEAR_LIGHT_OFF
Else
set myHealth to 0
post ES_BEAR_DEAD to HammerDectect module
stop BEAR_TIMER
set currentState to BEAR_KILLED
End if
post myHealth to HealthBar module
turn off bear lights
disable peak hold
Else if EventType is ES_CARD_REMOVED
reset Bear module
Else if EventType is ES_BEAR_HEALTH_TOP_THRESHOLD
set opHealth as BEAR_TOP_THRESHOLD - 1
Else if EventType is ES_BEAR_HEALTH_BOT_THRESHOLD
set opHealth as BEAR_BOT_THRESHOLD - 1
Else if EventType is ES_BEAR_HEALTH_ZERO
set opHealth as 0
Else if EventType is ES_GAME_STOPPED
turn off bear lights
disable peak hold
set currentState to BEAR_KILLED
Else if EventType is ES_GAME_OVER
turn off bear lights
disable peak hold
set currentState to BEAR_KILLED
End if
End of runBear
check4BearHit takes nothing, returns true
returnVal is false
read AD0 analog input and save to force
If force is greater than bearHitThreshold, and shows a peak
start GAME_STOP_TIMER
post damage calculated from the force to Bear module
returnVal is true
End if
return returnVal
End of check4BearHit
adjustDamage takes damage, returns adjusted damage
get handicap zone from myHealth and opHealth
If oppZone is 4 greater than myZone
additionalDamage is two times damage
Else if oppZone is 3 greater than myZone
additionalDamage is equal damage
Else if oppZone is 2 greater than myZone
additionalDamage is 0.25 times damage
If oppZone is 1 greater than myZone
additionalDamage is 0.125 times damage
End if
return damage + additionalDamage
End of adjustDamage
//runs as a state machine, handles detect/reset of the force sensor turning on the bear lights, and randomly activating the bear.
define MAX_HEALTH
define KILL_TIME, MIN_WAIT_TIME, WAIT_TIME_RANGE
define HIT_THRESHOLD, DAMGE_FACTOR
define BEAR_TOP_THRESHOLD, BEAR_BOT_THRESHOLD
static variable myHealth, opHealth
static local variable currentState BearState_t
//states include: BEAR_INIT, BEAR_LIGHT_OFF, BEAR_LIGHT_ON, BEAR_KILLED
initBear takes nothing, returns true
//AD pins are initialized in main function
set myHealth as MAX_HEALTH
set opHealth as MAX_HEALTH
post myHealth to HealthBar module
disable peak hold
set currentState as BEAR_INIT
End of initBear
runBear takes events
If currentState is BEAR_INIT
If EventType is ES_TREE_DONE
set currentState to BEAR_LIGHT_OFF
start BEAR_TIMER with random value //random time until bear is ready to hit
Else if EventType is ES_CARD_REMOVED
reset Bear module
End if
Else if currentState is BEAR_LIGHT_OFF
If EventType is ES_TIMEOUT
if EventParam is BEAR_TIMER
able peak hold //bear is ready to hit
turn on the bear light
set currentState to BEAR_LIGHT_ON
start BEAR_TIMER with KILL_TIME
End if
Else if EventType is ES_CARD_REMOVED
reset Bear module
Else if EventType is ES_BEAR_HEALTH_TOP_THRESHOLD
set opHealth as BEAR_TOP_THRESHOLD - 1
Else if EventType is ES_BEAR_HEALTH_BOT_THRESHOLD
set opHealth as BEAR_BOT_THRESHOLD - 1
Else if EventType is ES_BEAR_HEALTH_ZERO
set opHealth as 0
Else if EventType is ES_GAME_STOPPED
set currentState to BEAR_KILLED
Else if EventType is ES_GAME_OVER
set currentState to BEAR_KILLED
End if
Else if currentState is BEAR_LIGHT_ON
If EventType is ES_TIMEOUT
if EventParam is BEAR_TIMER
disable peak hold // bear is not ready to be hit
turn off the bear lights
set currentState to BEAR_LIGHT_OFF
start BEAR_TIMER with random time
End if
Else if EventType is ES_BEAR_HIT
use adjustDamage to get adjusted damage with handicap and save to damage
If damage is less than myHealth
deduce myHealth by damage
start BEAR_TIMER with random time
set currentState to BEAR_LIGHT_OFF
Else
set myHealth to 0
post ES_BEAR_DEAD to HammerDectect module
stop BEAR_TIMER
set currentState to BEAR_KILLED
End if
post myHealth to HealthBar module
turn off bear lights
disable peak hold
Else if EventType is ES_CARD_REMOVED
reset Bear module
Else if EventType is ES_BEAR_HEALTH_TOP_THRESHOLD
set opHealth as BEAR_TOP_THRESHOLD - 1
Else if EventType is ES_BEAR_HEALTH_BOT_THRESHOLD
set opHealth as BEAR_BOT_THRESHOLD - 1
Else if EventType is ES_BEAR_HEALTH_ZERO
set opHealth as 0
Else if EventType is ES_GAME_STOPPED
turn off bear lights
disable peak hold
set currentState to BEAR_KILLED
Else if EventType is ES_GAME_OVER
turn off bear lights
disable peak hold
set currentState to BEAR_KILLED
End if
End of runBear
check4BearHit takes nothing, returns true
returnVal is false
read AD0 analog input and save to force
If force is greater than bearHitThreshold, and shows a peak
start GAME_STOP_TIMER
post damage calculated from the force to Bear module
returnVal is true
End if
return returnVal
End of check4BearHit
adjustDamage takes damage, returns adjusted damage
get handicap zone from myHealth and opHealth
If oppZone is 4 greater than myZone
additionalDamage is two times damage
Else if oppZone is 3 greater than myZone
additionalDamage is equal damage
Else if oppZone is 2 greater than myZone
additionalDamage is 0.25 times damage
If oppZone is 1 greater than myZone
additionalDamage is 0.125 times damage
End if
return damage + additionalDamage
End of adjustDamage