Dart DocumentationdartemisPlayerManager

PlayerManager class

You may sometimes want to specify to which player an entity belongs to.

An entity can only belong to a single player at a time.

class PlayerManager extends Manager {
 Map<Entity, String> _playerByEntity;
 Map<String, Bag<Entity>> _entitiesByPlayer;

 PlayerManager() {
   _playerByEntity = new Map<Entity, String>();
   _entitiesByPlayer = new Map<String, Bag<Entity>>();
 }

 void setPlayer(Entity e, String player) {
   _playerByEntity[e] = player;
   Bag<Entity> entities = _entitiesByPlayer[player];
   if(entities == null) {
     entities = new Bag<Entity>();
     _entitiesByPlayer[player] = entities;
   }
   entities.add(e);
 }

 ReadOnlyBag<Entity> getEntitiesOfPlayer(String player) {
   Bag<Entity> entities = _entitiesByPlayer[player];
   if(entities == null) {
     entities = new Bag<Entity>();
   }
   return entities.readOnly;
 }

 void removeFromPlayer(Entity e) {
   String player = _playerByEntity[e];
   if(player != null) {
     Bag<Entity> entities = _entitiesByPlayer[player];
     if(entities != null) {
       entities.remove(e);
     }
   }
 }

 String getPlayer(Entity e) => _playerByEntity[e];

 void initialize() {}

 void deleted(Entity e) => removeFromPlayer(e);

}

Extends

Manager > PlayerManager

Constructors

new PlayerManager() #

PlayerManager() {
 _playerByEntity = new Map<Entity, String>();
 _entitiesByPlayer = new Map<String, Bag<Entity>>();
}

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) #

void deleted(Entity e) => removeFromPlayer(e);

void disabled(Entity e) #

inherited from Manager
void disabled(Entity e) {}

void enabled(Entity e) #

inherited from Manager
void enabled(Entity e) {}

ReadOnlyBag<Entity> getEntitiesOfPlayer(String player) #

ReadOnlyBag<Entity> getEntitiesOfPlayer(String player) {
 Bag<Entity> entities = _entitiesByPlayer[player];
 if(entities == null) {
   entities = new Bag<Entity>();
 }
 return entities.readOnly;
}

String getPlayer(Entity e) #

String getPlayer(Entity e) => _playerByEntity[e];

void initialize() #

void initialize() {}

void removeFromPlayer(Entity e) #

void removeFromPlayer(Entity e) {
 String player = _playerByEntity[e];
 if(player != null) {
   Bag<Entity> entities = _entitiesByPlayer[player];
   if(entities != null) {
     entities.remove(e);
   }
 }
}

void setPlayer(Entity e, String player) #

void setPlayer(Entity e, String player) {
 _playerByEntity[e] = player;
 Bag<Entity> entities = _entitiesByPlayer[player];
 if(entities == null) {
   entities = new Bag<Entity>();
   _entitiesByPlayer[player] = entities;
 }
 entities.add(e);
}