Advertisement
Guest User

Untitled

a guest
Oct 30th, 2014
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.69 KB | None | 0 0
  1. package Game.Pworld.Pmapping;
  2.  
  3. import java.awt.Point;
  4. import java.awt.geom.Point2D;
  5. import java.util.ArrayList;
  6. import java.util.HashMap;
  7. import java.util.HashSet;
  8.  
  9. import Game.Pworld.Pentities.EntityData;
  10. import Game.Pworld.Pentitydefinitions.EntityAttribute;
  11. import Game.Pworld.Pentitydefinitions.GameObjects;
  12.  
  13. public class TotalEntityMap{
  14.  
  15.    
  16.     public final int GRID_SIZE = 192;
  17.    
  18.     GameObjects gObjs = new GameObjects();
  19.    
  20.     Indexer EntityIndX = new Indexer();
  21.     Indexer SectionIndX = new Indexer();
  22.    
  23.     /**
  24.      *  This holds a HashSet of Entity Tags (Int)
  25.      *  Referenced by an EntityAttribute Key.
  26.      *  
  27.      *  @EntityAttribute
  28.      *  Entity Classification
  29.      *  @Integer
  30.      *  Entity Tag Set
  31.      */
  32.    
  33.     HashMap<EntityAttribute, HashSet<Integer>>
  34.                     EntityAttributeSet = new HashMap<EntityAttribute, HashSet<Integer>>();
  35.    
  36.    
  37.     /**
  38.      *  This holds an EntityData object
  39.      *  Referenced by an Entity Tag (Int) Key.
  40.      *  
  41.      *  @Integer
  42.      *  Entity Tag
  43.      *  @EntityData
  44.      *  Entity Variables
  45.      */
  46.    
  47.     HashMap<Integer,EntityData>
  48.                     EntityDataMap = new HashMap<Integer,EntityData>();
  49.  
  50.    
  51.     /**
  52.      *  This holds an Entity Name/Label (String)
  53.      *  Referenced by an Entity Tag (Int) Key.
  54.      *  
  55.      *  @Integer
  56.      *  Entity Tag
  57.      *  @EntityData
  58.      *  Entity Name
  59.      */
  60.    
  61.     HashMap<Integer,String>
  62.                     EntityTagNameMap = new HashMap<Integer,String>();  
  63.    
  64.    
  65.     HashMap<Point, HashSet<Integer>>
  66.                     EntityGridMap = new HashMap<Point, HashSet<Integer>>();
  67.    
  68.    
  69.    
  70.     /**
  71.      *  This holds a HashSet of Entity Tags (Int)
  72.      *  Referenced by a Section Tag (Int) Key.
  73.      *  
  74.      *  @Integer
  75.      *  Section Tag
  76.      *  @EntityData
  77.      *  Entity Tag Set
  78.      */
  79.    
  80.     HashMap<Integer, HashSet<Integer>>
  81.                     SectionEntitySet = new HashMap<Integer, HashSet<Integer>>();
  82.    
  83.     /**
  84.      *  This holds a SectionInfo Object
  85.      *  Referenced by a Section Tag (Int) Key.
  86.      *  
  87.      *  @Integer
  88.      *  Section Tag
  89.      *  @EntityData
  90.      *  SectionInfo Object
  91.      */
  92.    
  93.     HashMap<Integer, SectionInfo>
  94.                     SectionInfoMap = new HashMap<Integer, SectionInfo>();
  95.    
  96.    
  97.    
  98.    
  99.     public TotalEntityMap(){           
  100.         for(int i = 0; i < EntityAttribute.values().length; i++){
  101.             EntityAttributeSet.put(EntityAttribute.values()[i],new HashSet<Integer>());
  102.         }      
  103.     }
  104.    
  105.    
  106.     /**
  107.      *
  108.      * Adds an Entity to the maps.
  109.      * If the tag exists, generated from {@link #EntityIndX} then it will not store the Entity and return.
  110.      *
  111.      * EntityInfo must be complete!
  112.      *
  113.      * @param entityData
  114.      */
  115.    
  116.     public void addEntity(EntityData entityData){
  117.         int tag = EntityIndX.getNextTag();     
  118.        
  119.         if(EntityTagNameMap.containsKey(tag)){
  120.             System.out.println("Tag in use... "+tag+": ");
  121.             System.out.println("In Class: "+ this.getClass().getSimpleName()+": Method  addEntity");
  122.             return;
  123.         }
  124.        
  125.         EntityDataMap.put(tag, entityData);
  126.         EntityTagNameMap.put(tag, entityData.getName());
  127.         SectionEntitySet.get(entityData.getSection()).add(tag);
  128.        
  129.         Point pos = getIndexPoint(entityData.getPosition());
  130.        
  131.         if(!EntityGridMap.containsKey(pos)){
  132.             EntityGridMap.put(pos, new HashSet<Integer>());
  133.         }
  134.        
  135.         EntityGridMap.get(pos).add(tag);
  136.        
  137.         for(int i = 0; i < EntityAttribute.values().length; i++){
  138.             if(gObjs.getGameObject(entityData.getName()).getAttributes().containsKey(EntityAttribute.values()[i])){
  139.                 EntityAttributeSet.get(EntityAttribute.values()[i]).add(tag);
  140.             }
  141.         }
  142.     }
  143.    
  144.     public void removeEntity(int tag){
  145.        
  146.         EntityIndX.removeTag(tag);
  147.         EntityData ED = EntityDataMap.remove(tag);     
  148.         String name = EntityTagNameMap.remove(tag);
  149.         removeEntityFromGrid(tag, ED.getPosition());
  150.        
  151.         SectionEntitySet.get(ED.getSection()).remove(tag);
  152.        
  153.         for(int i = 0; i < EntityAttribute.values().length; i++){
  154.             if(gObjs.getGameObject(ED.getName()).getAttributes().containsKey(EntityAttribute.values()[i])){
  155.                 EntityAttributeSet.get(EntityAttribute.values()[i]).remove(tag);
  156.                 //Queue tagged Attribute Events here
  157.             }
  158.         }
  159.        
  160.        
  161.        
  162.        
  163.     }
  164.    
  165.     /**
  166.      *
  167.      * Adds a Section to the maps.
  168.      * If the tag exists, generated from {@link #SectionIndX} then is will not store the Entity and return.
  169.      * This requires a HashSet of SectionEntities and SectionInfo to be complete on creation.
  170.      * @param SectionEntities
  171.      * @param sectionInfo
  172.      */
  173.    
  174.     public void addSection(HashSet<Integer> SectionEntities, SectionInfo sectionInfo){
  175.         int tag = SectionIndX.getNextTag();
  176.        
  177.         if(SectionEntitySet.containsKey(tag)){
  178.             System.out.println("Tag in use... "+tag+": ");
  179.             System.out.println("In Class: "+ this.getClass().getSimpleName()+": Method  addSection");
  180.             return;
  181.         }
  182.        
  183.         SectionEntitySet.put(tag, SectionEntities);
  184.         SectionInfoMap.put(tag, sectionInfo);
  185.        
  186.     }
  187.    
  188.    
  189.     /**
  190.      *
  191.      * This places a Section tag in the section map/set creating an empty hashmap in {@link #SectionEntitySet}.
  192.      * Section info must be provided.
  193.      *
  194.      * @param sectionInfo
  195.      * @return
  196.      */
  197.    
  198.     public int insertEmptySection(SectionInfo sectionInfo){
  199.    
  200.         int tag = SectionIndX.getNextTag();
  201.        
  202.         if(SectionEntitySet.containsKey(tag)){
  203.             System.out.println("Tag in use... "+tag+": ");
  204.             System.out.println("In Class: "+ this.getClass().getSimpleName()+": Method  addSection");
  205.             return -1;
  206.         }
  207.        
  208.         SectionEntitySet.put(tag, new HashSet<Integer>());
  209.         SectionInfoMap.put(tag, sectionInfo);
  210.        
  211.         return tag;
  212.     }
  213.    
  214.     /**
  215.      * PointHold, keeps a field to set points instead of creating new objects.
  216.      */
  217.    
  218.     Point PointHold = new Point();
  219.    
  220.     /**
  221.      *
  222.      * Returns a point as an index as GRID_SIZE
  223.      *
  224.      * @param x
  225.      * @param y
  226.      * @return
  227.      */
  228.    
  229.     private Point getIndexPoint(double x, double y){
  230.         PointHold.setLocation((int)x/GRID_SIZE,(int)y/GRID_SIZE);
  231.         return PointHold;
  232.     }
  233.    
  234.     /**
  235.      * Same as above.
  236.      * @param p
  237.      * @return
  238.      */
  239.    
  240.     private Point getIndexPoint(Point2D p){
  241.         PointHold.setLocation((int)p.getX()/GRID_SIZE,(int)p.getY()/GRID_SIZE);
  242.         return PointHold;
  243.     }
  244.    
  245.    
  246.     /**
  247.      *
  248.      * sets the grid key, Check if it doesn't already exist.
  249.      *
  250.      * @param p
  251.      */
  252.    
  253.     private void setGridKey(Point p){      
  254.         EntityGridMap.put(p, new HashSet<Integer>());      
  255.     }
  256.    
  257.     /**
  258.      *
  259.      * Translates the entity Position and adjust the position of the entity in the {@link #EntityGridMap}.
  260.      * THIS DOES NOT SET THE POSITION.
  261.      *
  262.      * @param tag
  263.      * @param x
  264.      * @param y
  265.      */
  266.    
  267.     public void translateEntity(int tag, double x, double y){
  268.        
  269.         Point2D pastPos = EntityDataMap.get(tag).getPosition();
  270.         Point2D nextPos =   new Point2D.Double(pastPos.getX()+x,pastPos.getY()+y);
  271.        
  272.         entityGridInsert(tag,nextPos,pastPos);
  273.     }
  274.    
  275.    
  276.     /**
  277.      *
  278.      * Sets the entity Position and adjust the position of the entity in the {@link #EntityGridMap}.
  279.      * THIS DOES NOT TRANSLATE THE POSITION.
  280.      *
  281.      * @param tag
  282.      * @param x
  283.      * @param y
  284.      */
  285.    
  286.     public void setEntityPosition(int tag, double x, double y){
  287.        
  288.         Point2D pastPos = EntityDataMap.get(tag).getPosition();
  289.         Point nextPos = getIndexPoint(x,y);
  290.        
  291.         entityGridInsert(tag,nextPos,pastPos);
  292.     }
  293.    
  294.     /**
  295.      *
  296.      * This is the method that adjusts the position in the {@link #EntityGridMap}.
  297.      *
  298.      * @param tag
  299.      * @param holdPos
  300.      * @param pastPos
  301.      */
  302.    
  303.     private void entityGridInsert(int tag, Point2D nextPos, Point2D pastPos){
  304.         if(!EntityGridMap.containsKey(getIndexPoint(nextPos))){        
  305.             setGridKey(getIndexPoint(nextPos));
  306.         }
  307.        
  308.         if(!EntityGridMap.containsKey(getIndexPoint(pastPos))){
  309.             setGridKey(getIndexPoint(pastPos));
  310.         }else{
  311.             EntityGridMap.get(getIndexPoint(pastPos)).remove(tag);
  312.         }
  313.            
  314.             EntityGridMap.get(getIndexPoint(nextPos)).add(tag);
  315.            
  316.        
  317.             EntityDataMap.get(tag).getPosition().setLocation(nextPos);
  318.        
  319.     }
  320.    
  321.     private void removeEntityFromGrid(int tag, Point2D pos){       
  322.         EntityGridMap.get(getIndexPoint(pos)).remove(tag);     
  323.     }
  324.    
  325.    
  326. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement