Skip to content

Commit

Permalink
Code cleanup + Added documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
DemiKnight committed Nov 20, 2018
1 parent 96049f4 commit ade34df
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 31 deletions.
79 changes: 72 additions & 7 deletions src/main/java/com/krytpicalknight/processingMe/EntityManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import com.krytpicalknight.processingMe.render.ResourceRequirements;
import processing.core.PApplet;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;

Expand All @@ -19,33 +20,53 @@
public class EntityManager {

/**
*
* Stores the all entities active in the application, including any rendered or unrendered.
*/
private List<Entity> entityList;

/**
*
* Used to lookup the Entity ID's to the relvant ones store within the <tt>entityList</tt>.
*/
private HashMap<String, Integer> nameLookup;

/**
* Reference to the renderManager, to setup rendered entities.
*/
private RenderManager renderManager;

/**
* Reference to the ResourceManager, allowing any resources required by a entity to be stored here.
*/
private ResourceManager resourceM;

// private PApplet parent;

/**
* To initialise all variables and references.
* @param renderM Reference to the RenderManager
* @param resourceManager Reference to the ResourceManager
*/
public EntityManager(RenderManager renderM, ResourceManager resourceManager)
{
this.renderManager = renderM;
// this.parent = P;
this.resourceM = resourceManager;

entityList = new LinkedList<>();
}

/**
* Register and store entities to be used later in the application.
*
* @param intendedRegistry A Class containing entities that need to be stored here.
*/
public void Registry(EntityRegistry intendedRegistry)
{
intendedRegistry.RegisterEntities(this);
}

/**
* Gather and store all resources required by the application.
*
* @implNote To be called after all entities are stored.
*/
public void registerResource()
{
for (Entity entSelect: entityList )
Expand All @@ -54,28 +75,72 @@ public void registerResource()
}
}

public void giveParentInstance(PApplet PA)
/**
* Gives a link to the main processing application to all entities, allowing for them to interact.
*
* @param parentInstance Link to main Processing instance
*/
public void giveParentInstance(PApplet parentInstance)
{
for (Entity entitySelected:entityList)
{
entitySelected.setParentProcessing(PA);
entitySelected.setParentProcessing(parentInstance);
}
}

/**
*
*/
public void registerIDs()
{
nameLookup = new HashMap<>();

for (int index = 0; index < entityList.size();index++)
{
// String temp = String.format("%d:%s", index, entityList.get(index).getID());
nameLookup.put(String.format("%d:%s", index, entityList.get(index).getID()), index);
}
}

/**
* Add entity to registry & setup with <tt>renderManager</tt>
* By default, will also setup the entity to update every frame.
* <b>Warning, frame-rate may vary</b>
* @param entToAdd Entity to add to list
*/
public void addToRegistry(Entity entToAdd)
{
entityList.add(entToAdd);
renderManager.addToRender(entToAdd,true);
}

/**
* Add entity to registry & setup with <tt>renderManager</tt>
*
* <b>Warning, frame-rate may vary</b>
* @param entToAdd Entity to add to list
* @param willUpdate Whether the entity will be updated every frame.
*/
public void addToRegistry(Entity entToAdd, boolean willUpdate)
{
entityList.add(entToAdd);
renderManager.addToRender(entToAdd, willUpdate);
}

/**
* Retrieve a stored entity. Using a numerical index.
*
* @param index Index of entity to retrieve.
* @return The Entity at the given index.
*/
public Entity getEntity(int index) { return this.entityList.get(index); }


/**
*
* @deprecated
* @return Entire List of entites currently being stored
*/
public List<Entity> getEntityList()
{
return this.entityList;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package com.krytpicalknight.processingMe;

import processing.core.PApplet;

/**
* When a game/application wants to register their Entities, implementing this will then allow the
* Entity Manager to use the class.
Expand All @@ -10,6 +8,4 @@
public interface EntityRegistry {

void RegisterEntities(EntityManager manager);


}
11 changes: 3 additions & 8 deletions src/main/java/com/krytpicalknight/processingMe/MainApp.java
Original file line number Diff line number Diff line change
@@ -1,21 +1,16 @@
package com.krytpicalknight.processingMe;

import processing.core.PApplet;
import com.krytpicalknight.processingMe.render.RenderManager;
import com.krytpicalknight.processingMe.render.ResourceManager;
import com.krypticalKnight.testApp.entities.TestEntity;
import processing.core.PApplet;

public class MainApp extends PApplet{

private static String version = "0.0.4";

// private com.krytpicalknight.processingMe.ConfigManager configM = new com.krytpicalknight.processingMe.ConfigManager();

protected static RenderManager renderM;
private static RenderManager renderM;
protected static EntityManager entityM;
protected static ResourceManager resourceM;
private PApplet instance;

private static ResourceManager resourceM;

public static void main(String[] args)
{
Expand Down
25 changes: 15 additions & 10 deletions src/main/java/com/krytpicalknight/processingMe/render/Entity.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,14 @@
*/
public class Entity {

protected static String ID = "Entity";
/**
* Set's an ID for the entity, allow for quick lookup.
*/
private final String ID = "Entity";

public String getID() {
return this.ID;
}

/**
* States whether the entity is being rendered or just existing in memory.
Expand All @@ -29,15 +36,6 @@ public class Entity {
*/
protected PApplet parent;

/**
* BY default only sets up the parent processing instance
* @param P main processing instance
*/
public Entity(PApplet P)
{
this.parent = P;
}

/**
* Will be used when the parent Processing instance is set later on.
*/
Expand All @@ -46,6 +44,12 @@ public Entity()

}

/**
* Will allow entity to draw to the main processing window.
*
* Not needed if the entity does not interact with Processing
* @param p Sets the pointer to the main Processing instance, used to interact with the draw window.
*/
public void setParentProcessing(PApplet p)
{
this.parent = p;
Expand Down Expand Up @@ -89,4 +93,5 @@ public void update()
{
if(!canUpdate()) return;
}

}
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package com.krytpicalknight.processingMe.render;

import com.krytpicalknight.processingMe.MainApp;
import processing.core.PApplet;
import processing.core.PImage;
import processing.core.PShape;

Expand Down

0 comments on commit ade34df

Please sign in to comment.