com.artemis.managers.TeamManager Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of artemis-odb Show documentation
Show all versions of artemis-odb Show documentation
Fork of Artemis Entity System Framework.
package com.artemis.managers;
import java.util.HashMap;
import java.util.Map;
import com.artemis.Manager;
import com.artemis.utils.Bag;
import com.artemis.utils.ImmutableBag;
/**
* 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.
*
*
* @author Arni Arent
*/
public class TeamManager extends Manager {
/** Teams mapped to their players. */
private final Map> playersByTeam;
/** Players mapped to their teams. */
private final Map teamByPlayer;
/**
* Creates a new TeamManager instance.
*/
public TeamManager() {
playersByTeam = new HashMap>();
teamByPlayer = new HashMap();
}
@Override
protected void initialize() {
}
/**
* The the name of the team the given player is in.
*
* @param player
* the player
*
* @return the player's team
*/
public String getTeam(String player) {
return teamByPlayer.get(player);
}
/**
* Set the player's team.
*
* Each player can only be in one team at a time.
*
*
* @param player
* the player
* @param team
* the team to put the player in
*/
public void setTeam(String player, String team) {
removeFromTeam(player);
teamByPlayer.put(player, team);
Bag players = playersByTeam.get(team);
if(players == null) {
players = new Bag();
playersByTeam.put(team, players);
}
players.add(player);
}
/**
* Get all players on a team.
*
* @param team
* the team
*
* @return all players on the team in a bag
*/
public ImmutableBag getPlayers(String team) {
return playersByTeam.get(team);
}
/**
* Remove a player from his team.
*
* @param player
* the player to remove
*/
public void removeFromTeam(String player) {
String team = teamByPlayer.remove(player);
if(team != null) {
Bag players = playersByTeam.get(team);
if(players != null) {
players.remove(player);
}
}
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy