Dart DocumentationdartemisTeamManager

TeamManager class

Use this class together with PlayerManager.

You may sometimes want to create teams in your game, so that some players are team mates.

A player can only belong to a single team.

class TeamManager extends Manager {
 Map<String, Bag<String>> _playersByTeam;
 Map<String, String> _teamByPlayer;

 TeamManager() {
   _playersByTeam = new Map<String, Bag<String>>();
   _teamByPlayer = new Map<String, String>();
 }

 void initialize() {}

 String getTeam(String player) => _teamByPlayer[player];

 void setTeam(String player, String team) {
   removeFromTeam(player);

   _teamByPlayer[player] = team;

   Bag<String> players = _playersByTeam[team];
   if(players == null) {
     players = new Bag<String>();
     _playersByTeam[team] = players;
   }
   players.add(player);
 }

 ReadOnlyBag<String> getPlayers(String team) {
   var result = _playersByTeam[team];
   return result == null ? null : result.readOnly;
 }

 void removeFromTeam(String player) {
   String team = _teamByPlayer.remove(player);
   if(team != null) {
     Bag<String> players = _playersByTeam[team];
     if(players != null) {
       players.remove(player);
     }
   }
 }

}

Extends

Manager > TeamManager

Constructors

new TeamManager() #

TeamManager() {
 _playersByTeam = new Map<String, Bag<String>>();
 _teamByPlayer = new Map<String, String>();
}

Methods

void added(Entity e) #

inherited from Manager
void added(Entity e) {}

void changed(Entity e) #

inherited from Manager
void changed(Entity e) {}

void deleted(Entity e) #

inherited from Manager
void deleted(Entity e) {}

void disabled(Entity e) #

inherited from Manager
void disabled(Entity e) {}

void enabled(Entity e) #

inherited from Manager
void enabled(Entity e) {}

ReadOnlyBag<String> getPlayers(String team) #

ReadOnlyBag<String> getPlayers(String team) {
 var result = _playersByTeam[team];
 return result == null ? null : result.readOnly;
}

String getTeam(String player) #

String getTeam(String player) => _teamByPlayer[player];

void initialize() #

void initialize() {}

void removeFromTeam(String player) #

void removeFromTeam(String player) {
 String team = _teamByPlayer.remove(player);
 if(team != null) {
   Bag<String> players = _playersByTeam[team];
   if(players != null) {
     players.remove(player);
   }
 }
}

void setTeam(String player, String team) #

void setTeam(String player, String team) {
 removeFromTeam(player);

 _teamByPlayer[player] = team;

 Bag<String> players = _playersByTeam[team];
 if(players == null) {
   players = new Bag<String>();
   _playersByTeam[team] = players;
 }
 players.add(player);
}