Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package Game.Pworld.Pmapping;
- import java.awt.Point;
- import java.awt.geom.Point2D;
- import java.util.ArrayList;
- import java.util.HashMap;
- import java.util.HashSet;
- import Game.Pworld.Pentities.EntityData;
- import Game.Pworld.Pentitydefinitions.EntityAttribute;
- import Game.Pworld.Pentitydefinitions.GameObjects;
- public class TotalEntityMap{
- public final int GRID_SIZE = 192;
- GameObjects gObjs = new GameObjects();
- Indexer EntityIndX = new Indexer();
- Indexer SectionIndX = new Indexer();
- /**
- * This holds a HashSet of Entity Tags (Int)
- * Referenced by an EntityAttribute Key.
- *
- * @EntityAttribute
- * Entity Classification
- * @Integer
- * Entity Tag Set
- */
- HashMap<EntityAttribute, HashSet<Integer>>
- EntityAttributeSet = new HashMap<EntityAttribute, HashSet<Integer>>();
- /**
- * This holds an EntityData object
- * Referenced by an Entity Tag (Int) Key.
- *
- * @Integer
- * Entity Tag
- * @EntityData
- * Entity Variables
- */
- HashMap<Integer,EntityData>
- EntityDataMap = new HashMap<Integer,EntityData>();
- /**
- * This holds an Entity Name/Label (String)
- * Referenced by an Entity Tag (Int) Key.
- *
- * @Integer
- * Entity Tag
- * @EntityData
- * Entity Name
- */
- HashMap<Integer,String>
- EntityTagNameMap = new HashMap<Integer,String>();
- HashMap<Point, HashSet<Integer>>
- EntityGridMap = new HashMap<Point, HashSet<Integer>>();
- /**
- * This holds a HashSet of Entity Tags (Int)
- * Referenced by a Section Tag (Int) Key.
- *
- * @Integer
- * Section Tag
- * @EntityData
- * Entity Tag Set
- */
- HashMap<Integer, HashSet<Integer>>
- SectionEntitySet = new HashMap<Integer, HashSet<Integer>>();
- /**
- * This holds a SectionInfo Object
- * Referenced by a Section Tag (Int) Key.
- *
- * @Integer
- * Section Tag
- * @EntityData
- * SectionInfo Object
- */
- HashMap<Integer, SectionInfo>
- SectionInfoMap = new HashMap<Integer, SectionInfo>();
- public TotalEntityMap(){
- for(int i = 0; i < EntityAttribute.values().length; i++){
- EntityAttributeSet.put(EntityAttribute.values()[i],new HashSet<Integer>());
- }
- }
- /**
- *
- * Adds an Entity to the maps.
- * If the tag exists, generated from {@link #EntityIndX} then it will not store the Entity and return.
- *
- * EntityInfo must be complete!
- *
- * @param entityData
- */
- public void addEntity(EntityData entityData){
- int tag = EntityIndX.getNextTag();
- if(EntityTagNameMap.containsKey(tag)){
- System.out.println("Tag in use... "+tag+": ");
- System.out.println("In Class: "+ this.getClass().getSimpleName()+": Method addEntity");
- return;
- }
- EntityDataMap.put(tag, entityData);
- EntityTagNameMap.put(tag, entityData.getName());
- SectionEntitySet.get(entityData.getSection()).add(tag);
- Point pos = getIndexPoint(entityData.getPosition());
- if(!EntityGridMap.containsKey(pos)){
- EntityGridMap.put(pos, new HashSet<Integer>());
- }
- EntityGridMap.get(pos).add(tag);
- for(int i = 0; i < EntityAttribute.values().length; i++){
- if(gObjs.getGameObject(entityData.getName()).getAttributes().containsKey(EntityAttribute.values()[i])){
- EntityAttributeSet.get(EntityAttribute.values()[i]).add(tag);
- }
- }
- }
- public void removeEntity(int tag){
- EntityIndX.removeTag(tag);
- EntityData ED = EntityDataMap.remove(tag);
- String name = EntityTagNameMap.remove(tag);
- removeEntityFromGrid(tag, ED.getPosition());
- SectionEntitySet.get(ED.getSection()).remove(tag);
- for(int i = 0; i < EntityAttribute.values().length; i++){
- if(gObjs.getGameObject(ED.getName()).getAttributes().containsKey(EntityAttribute.values()[i])){
- EntityAttributeSet.get(EntityAttribute.values()[i]).remove(tag);
- //Queue tagged Attribute Events here
- }
- }
- }
- /**
- *
- * Adds a Section to the maps.
- * If the tag exists, generated from {@link #SectionIndX} then is will not store the Entity and return.
- * This requires a HashSet of SectionEntities and SectionInfo to be complete on creation.
- * @param SectionEntities
- * @param sectionInfo
- */
- public void addSection(HashSet<Integer> SectionEntities, SectionInfo sectionInfo){
- int tag = SectionIndX.getNextTag();
- if(SectionEntitySet.containsKey(tag)){
- System.out.println("Tag in use... "+tag+": ");
- System.out.println("In Class: "+ this.getClass().getSimpleName()+": Method addSection");
- return;
- }
- SectionEntitySet.put(tag, SectionEntities);
- SectionInfoMap.put(tag, sectionInfo);
- }
- /**
- *
- * This places a Section tag in the section map/set creating an empty hashmap in {@link #SectionEntitySet}.
- * Section info must be provided.
- *
- * @param sectionInfo
- * @return
- */
- public int insertEmptySection(SectionInfo sectionInfo){
- int tag = SectionIndX.getNextTag();
- if(SectionEntitySet.containsKey(tag)){
- System.out.println("Tag in use... "+tag+": ");
- System.out.println("In Class: "+ this.getClass().getSimpleName()+": Method addSection");
- return -1;
- }
- SectionEntitySet.put(tag, new HashSet<Integer>());
- SectionInfoMap.put(tag, sectionInfo);
- return tag;
- }
- /**
- * PointHold, keeps a field to set points instead of creating new objects.
- */
- Point PointHold = new Point();
- /**
- *
- * Returns a point as an index as GRID_SIZE
- *
- * @param x
- * @param y
- * @return
- */
- private Point getIndexPoint(double x, double y){
- PointHold.setLocation((int)x/GRID_SIZE,(int)y/GRID_SIZE);
- return PointHold;
- }
- /**
- * Same as above.
- * @param p
- * @return
- */
- private Point getIndexPoint(Point2D p){
- PointHold.setLocation((int)p.getX()/GRID_SIZE,(int)p.getY()/GRID_SIZE);
- return PointHold;
- }
- /**
- *
- * sets the grid key, Check if it doesn't already exist.
- *
- * @param p
- */
- private void setGridKey(Point p){
- EntityGridMap.put(p, new HashSet<Integer>());
- }
- /**
- *
- * Translates the entity Position and adjust the position of the entity in the {@link #EntityGridMap}.
- * THIS DOES NOT SET THE POSITION.
- *
- * @param tag
- * @param x
- * @param y
- */
- public void translateEntity(int tag, double x, double y){
- Point2D pastPos = EntityDataMap.get(tag).getPosition();
- Point2D nextPos = new Point2D.Double(pastPos.getX()+x,pastPos.getY()+y);
- entityGridInsert(tag,nextPos,pastPos);
- }
- /**
- *
- * Sets the entity Position and adjust the position of the entity in the {@link #EntityGridMap}.
- * THIS DOES NOT TRANSLATE THE POSITION.
- *
- * @param tag
- * @param x
- * @param y
- */
- public void setEntityPosition(int tag, double x, double y){
- Point2D pastPos = EntityDataMap.get(tag).getPosition();
- Point nextPos = getIndexPoint(x,y);
- entityGridInsert(tag,nextPos,pastPos);
- }
- /**
- *
- * This is the method that adjusts the position in the {@link #EntityGridMap}.
- *
- * @param tag
- * @param holdPos
- * @param pastPos
- */
- private void entityGridInsert(int tag, Point2D nextPos, Point2D pastPos){
- if(!EntityGridMap.containsKey(getIndexPoint(nextPos))){
- setGridKey(getIndexPoint(nextPos));
- }
- if(!EntityGridMap.containsKey(getIndexPoint(pastPos))){
- setGridKey(getIndexPoint(pastPos));
- }else{
- EntityGridMap.get(getIndexPoint(pastPos)).remove(tag);
- }
- EntityGridMap.get(getIndexPoint(nextPos)).add(tag);
- EntityDataMap.get(tag).getPosition().setLocation(nextPos);
- }
- private void removeEntityFromGrid(int tag, Point2D pos){
- EntityGridMap.get(getIndexPoint(pos)).remove(tag);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement