To get started on using the turn system, you must download the folder, locate the folder inside src folder, and include the API javascript files in the index.html in the project.
Add the following lines above your scene files in the index.html:
<script type="text/javascript" src="src/TurnSystemAPI/Settings.js"></script>
<script type="text/javascript" src="src/TurnSystemAPI/TurnSystem.js"></script>
<script type="text/javascript" src="src/TurnSystemAPI/UserState.js"></script>
<script type="text/javascript" src="src/TurnSystemAPI/Action.js"></script>
To initialize the turn system in the scene, you must have following parameters:
- turntype ('timed', 'conditional', 'priority')
- max number of player in the game.
Do the following 4 steps to initialize the turn system:
1. Construct the Setting.Builder class:
const settingsBuilder = new Settings.Builder();
settingsBuilder.setTurnType(this.mTurnType);
settingsBuilder.setMaxUsers(this.mMaxPlayers);
2. Set parameters based on turn type (Description in later section)
3. Construct the Setting object using the Setting.Builder object:
const settings = settingsBuilder.build();
4. Construct the TurnSystem object using the Setting object:
this.turnSystem = new TurnSystem(settings);
const settingsBuilder = new Settings.Builder();
settingsBuilder.setTurnType('timed');
settingsBuilder.setTurnTime(60);
When building your scene, ensure to call TurnSystem's calculateNextTurn()
function within the update function of your scene.
To set the parameters for conditional turn-type, the developer must provide a conditional function to pass into the Setting.Builder object.
This function must return true to move onto the next player and return false to stay as current player's turn.
Set the conditional function by passing the function into Setting.Builder object:
const settingsBuilder = new Settings.Builder();
settingsBuilder.setTurnType('conditional');
settingsBuilder.setCallbackFunction(conditionalFunction);
Tips: If the user object is extended from UserState, turn isMetCondition to true when condition is made. For condition function, pass a function that return isMetCondition. TurnSystem will reset isMetCondition when condition is true.
const settingsBuilder = new Settings.Builder();
settingsBuilder.setTurnType('priority');
settings.builder.setCallbackFunction((usersArr) => userArr.sort());
Where to call TurnSystem's calculateNextTurn() is flexible here. You could call it within your
Scene's update function, and have the array repeatedly be sorted. However, you can also limit the
calls to calculateNextTurn() by only calling it when necessary. For example, if you have users that
roll dice, you can call calculateNextTurn() after all users have rolled.
To add players, the developer can self-define a user object that extends from the UserState class and add the user object to the turn system.
const user = new UserState();
this.turnSystem.addUser(user);
To remove a specific player, the developer can get the user object by index inside the TurnSystem storage. The developer should use some self-define parameters to locate specific user and then remove the user from the system by:
const lastPlayer = this.turnSystem.getUserByIndex(this.numPlayers - 1);
this.turnSystem.removeUser(lastPlayer);
To check if it is time to switch turns based on the turn type. Call the function to calculate if it is time to switch turn:
this.turnSystem.calculateNextTurn();
Function Names | Description | Parameter(s) (if any) | Return Type (if any) |
---|---|---|---|
Settings.Builder() | Constructor of the Settings.Builder class | None | None |
setTurnType(type) | Set the turn-type to 'timed'/'conditional'/'priority' | type : string ('timed'/'conditional'/'priority') | None |
setMaxUsers(max) | Set the max number of player. Minimum of 2 players | max : Number | None |
setCallbackFunction(callback) | Set the callback function for 'conditional' or 'priority' turn-type | callback : function | None |
setTurnTime(time) | Set the timer for 'timed' turn-type. Value should be in seconds. | time : Number | None |
build() | Build and return the Settings object | None | Setting |
Function Names | Description | Parameter(s) (if any) | Return Type (if any) |
---|---|---|---|
Settings(builder) | Constructor of the Settings class | builder : Setting.Builder | None |
getTurnType() | Get the turn-type | None | String |
getMaxUsers() | Get the max number of player | None | Number |
getCallbackFunction() | Get the callback function for 'conditional' or 'priority' turn-type | None | Function |
getTurnTime() | Get the timer for 'timed' turn-type | None | Number |
Function Names | Description | Parameter(s) (if any) | Return Type (if any) |
---|---|---|---|
TurnSystem(settings) | Constructor of the TurnSystem class | settings : Settings | None |
calculateNextTurn() | calculate if conditions are reached, if true, switch to next turn | None | None |
maxOutTimeTicker() | Maxes out the time ticker to one update before the max | None | None |
remainingTime() | Returns the remaining amount of time left before the timer reaches 0 | None | Number |
getCurrentUser() | Returns the current user object | None | the current UserState instance |
getNextUser() | Returns the user that immediately succeeds the current user | None | a UserState instance |
getUserByIndex(index) | Return the user at specific index | index : Number | the UserState instance at the provided index, if any. |
getAllUsers() | Return the list of all users | None | List of all UserState instances |
getNumUsers() | Return the number of users in the system | None | Number |
addUser(user) | Returns true if the user passed in is successfully added. Otherwise, returns false. | user : UserState | Boolean |
removeUser(user) | Returns true if the user passed in is successfully removed. Otherwise, returns false. | user : UserState | Boolean |
Function Names | Description | Parameter(s) (if any) | Return Type (if any) |
---|---|---|---|
UserState() | Constructor of the UserState class | None. | None. |
initialize() | Initializes the UserState Object | None. | None. |
update() | Updates the state of the UserState object. | None. | None. |
action() | If the user can do an action of some sort, execute it. Should return an Action class object. | None. | None. |
equals(otherUserState) | Checks if two UserState objects are the same. | otherUserState: UserState | Boolean: true if the UserStates are equal. |
Function Names | Description | Parameter(s) (if any) | Return Type (if any) |
---|---|---|---|
Action(user, action) | Constructor of the Action class | Sets the properties for the Action class. User should be a UserState instance. Action should be the result of the UserState action() function, whatever it may be. | None. |