2021-06-06 14:37:26 +12:00
*| [JavaECS ](../../README.md ) | [docs ](../overview.md ) | [entity ](./dir.md ) | EntityManager[]().md*
# Entity Manager
| **In this Section** |
|-|
| [About ](#about ) |
| [Implementation ](#implementation ) |
| [Constructors ](#constructors ) |
| [Methods ](#methods ) |
| [Fields ](#fields ) |
| [Examples ](#examples )|
| [Notes ](#notes )|
## About
2021-06-08 21:13:34 +12:00
Controls adding and removing entities, and registration and unregistration of components to specific entities.
2021-06-06 14:37:26 +12:00
## Implementation
Before attempting to use a system, it must be registered.
Registration requires a call to `registerSystem(String systemName, ECSSystem system)` , with params of the `String name` and a object reference of the [ECSSystem ](./ECSSystem.md ). Using the object reference, a system can be invoked at any time.
See [EntityManager.java ](/BrychanD/JaveECS/src/branch/master/javaecs/src/main/java/nz/ac/massey/programming_project_159333_s1_2021/EntityManager.java )
## Constructors
``` java
2021-06-08 21:13:34 +12:00
protected EntityManager(){}
2021-06-06 14:37:26 +12:00
```
2021-06-08 21:54:36 +12:00
The default constructor initialises [unusedEntities ](#unusedentities ) to a new queue containing [maxSize ](#maxsize ) (`1024` ) unique integers.
2021-06-06 14:37:26 +12:00
< br >
``` java
2021-06-08 21:13:34 +12:00
protected EntityManager(int maxEntities){}
2021-06-06 14:37:26 +12:00
```
Initialises [unusedEntities ](#unusedEntities ) to a new queue containing `maxEntities` unique integers.
< br >
## Methods
### addEntity
``` java
2021-06-08 21:13:34 +12:00
protected Integer addEntity();
2021-06-06 14:37:26 +12:00
```
Pops the next available entity index off the queue, and returns its value.
Returns `-1` if there is no remaining unused entities.
< br >
2021-06-08 21:13:34 +12:00
### getMaxSize
``` java
protected Integer getMaxSize();
```
Gets the current maximum entity count.
< br >
2021-06-06 14:37:26 +12:00
### getRegistrations
``` java
2021-06-08 21:13:34 +12:00
protected BitSet getRegistrations(int entity);
2021-06-06 14:37:26 +12:00
```
2021-06-08 21:13:34 +12:00
Returns the `BitSet` of registrations the `entity` has.
If the call results in an `IndexOutOfBoundsError` , then returns a new BitSet();
2021-06-06 14:37:26 +12:00
< br >
### registerComponent
``` java
2021-06-08 21:13:34 +12:00
protected void registerComponent(int component, int entity);
2021-06-06 14:37:26 +12:00
```
Sets the bit `component` in [entityRegistrations ](#entityRegistrations ) for `entity` to `true` .
< br >
### removeEntity
``` java
2021-06-08 21:13:34 +12:00
protected void removeEntity(int entity);
2021-06-06 14:37:26 +12:00
```
Adds the entity's index back to the unused queue, and clears the assigned registration bits
#### Note
>This function should not be called directly. Doing so will leave component data in memory, and will cause desynchronisation of the system registrations.
< br >
2021-06-08 21:13:34 +12:00
### resize
``` java
protected boolean resize(int newSize, SystemManager systemManager, ComponentManager componentManager);
```
Resizes the currentSize of the entity manager. Moves elements above `newSize` if space is available.
If the current defined elements won't fit, the operation returns `false` and the previous state is maintained.
< br >
### setRegistrations
``` java
protected void setRegistrations(int entity, BitSet registrations);
```
Sets the entity's registrations to the provided BitSet
< br >
2021-06-06 14:37:26 +12:00
### unregisterComponent
``` java
2021-06-08 21:13:34 +12:00
protected void unregisterComponent(int component, int entity);
2021-06-06 14:37:26 +12:00
```
Sets the bit `component` in [entityRegistrations ](#entityRegistrations ) for `entity` to `false` .
2021-06-08 21:13:34 +12:00
Returns `true` if successful.
2021-06-06 14:37:26 +12:00
< br >
## Fields
### entityRegistrations
``` java
List< BitSet > entityRegistrations;
```
2021-06-08 21:13:34 +12:00
The BitSet of registrations for each entity
2021-06-06 14:37:26 +12:00
< br >
2021-06-08 21:13:34 +12:00
### maxSize
2021-06-06 14:37:26 +12:00
``` java
2021-06-08 21:13:34 +12:00
private int maxSize = 1024;
2021-06-06 14:37:26 +12:00
```
2021-06-08 21:13:34 +12:00
The current maximum entity count.
Default `1024`
2021-06-06 14:37:26 +12:00
2021-06-08 21:13:34 +12:00
### unusedEntities
``` java
Queue< Integer > unusedEntities;
```
The queue of unused entity values
2021-06-06 14:37:26 +12:00