Sei sulla pagina 1di 3

***********************************************************************************

**
Fish Service:

The Fish service controls the fish position and orientation.


Checks if the hook lies in the region of the the vision of the fish

List of Events: ES_INIT, LongButtonPress, ShortButtonPress, ES_HOOKREACHED


List of States in FishState_t: InitFishState, FishWandering, FishLooking

Timers:
N/A

Module Vars:
static variable of type FishState_t CurrentState
static integer fishX that stores fish x-position on OLED screen
static integer fishY that stores fish y-position on OLED screen
static integer fishOrient that stores fish orientation on OLED screen
static integer finalHookX that stores hook x-position on OLED screen
static integer finalHookY that stores hook y-position on OLED screen

Module Functions:
getNewFish: A module function that generates a new fish
isFishCaught: A module function that checks if fish is caught
getFishHookPos: A setter function that updates the final hook position

Event Checkers:
N/A
***********************************************************************************
**
Pseudo-code for the Fish Service module:

InitFishFSM
Takes a priority number, returns True
Set current state to ES_Init;
post the initial transition event;

PostFishFSM
Takes in an event ThisEvent and returns a call to ES_PostToService

RunFishFSM
Takes in an event ThisEvent and returns ES_NO_EVENT

Based on the state of the CurrentState variable choose one of the following
blocks of code:

CurrentState is InitFishState

If event type of ThisEvent is ES_INIT


Calls updateFishPos to OLED
Change State to FishWandering

CurrentState is FishWandering:

If event type of ThisEvent is LongButtonPress


Create random fish position and orientation
Calls updateFishPos to OLED

If event type of ThisEvent is ShortButtonPress


Call function to generate new fish
Update the values on Oled
Post event NewFishReq to Score service to reduce the score
by 5

If event type of ThisEvent is ES_HOOKREACHED


Change State to FishLooking

CurrentState is FishLooking:

If event type of ThisEvent is ES_TIMEOUT and EventParam is


DISPLAY_TIMER
Check if FinalHookPos is within triangle by calling isFishCaught
Generate a new fish
Calls updateFishPos to OLED
Post FishCaught event to Joyhook service, Score service
Set current state to FishWandering

Module function getFishHookPos:


A setter function that, when called, updates the final hook position variables
within the fish service
Takes in the values of newHookX and newHookY within Joyhook Service, returns
nothing

Store the value of newHookX within Joyhook Service into finalHookX within
this service
Store the value of newHookY into finalHookY, decrement by 8 pixels to account
for hook length
on the OLED screen, so that when throwing hook, we will be aiming for the
hook tip, instead of the hook bottom

Module function getNewFish:


This function generates a new fish with new fish location and orientation using the
random number generator
The function takes in no parameters and returns nothing

Seed the random number generator with current time from ES_Timer_GetTime() to
ensure
randomness of number generation
Randomly generate a value for fishX between 45 to 88 to allow for buffer
region enough to
place hooks towards any random fish
Randomly generate a value for fishY between 25 to 50 to allow for buffer
region enough to
place hooks towards any random fish
Randomly generate a value for fishOrient between 0 and 1 denoting fish
fishing left or right
Print new fish location and orientation to console

Module function isFishCaught:


Once final hook position has been reached, this function checks if fish is caught
by checking
if the hook position is within the fish's vision, as mapped by an area of a
equalateral inverted triangle with a vertex at the center of the fish's head.
The function takes in no parameters and returns true if fish is caught, false if
otherwise.

Declare new bool returnVal and set to false


If hook is out of screen, return fish is not caught
If fish is facing left
If vision triangle is completely on screen
If fishX is in range of a catch
If fishY is in triangle
return true

Else, vision triangle is NOT completely on screen


If fishX is in range of a catch
If fishY is in triangle
return true

Else, fish is facing right


If vision triangle is completely on screen
If fishX is in range of a catch
If fishY is in triangle
return true

Else, vision triangle is NOT completely on screen


If fishX is in range of a catch
If fishY is in triangle
return true
return returnVal

***********************************************************************************
**

Potrebbero piacerti anche