Advertisement
Guest User

cellgrid

a guest
Nov 17th, 2015
188
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 18.40 KB | None | 0 0
  1. package Core;
  2.  
  3. import java.awt.Rectangle;
  4. import java.util.ArrayList;
  5. import java.util.HashSet;
  6. import java.util.Random;
  7. import java.util.Set;
  8.  
  9. import Base.custom.IntArr;
  10. import Base.custom.PairMap;
  11. import Base.custom.SetPairMap;
  12.  
  13. public class CellGrid {
  14.     BinarySpaceTree bst;   
  15.     int CELL_SIZE = 64;
  16.     int minSize = 35;
  17.     int doorSize = 1;
  18.     PairMap<IntArr,Rectangle> cellMap = new PairMap<>();
  19.     SetPairMap<IntArr,Rectangle> cellDivisions = new SetPairMap<>();  
  20.     SetPairMap<IntArr,Rectangle> cellPath = new SetPairMap<>();
  21.     SetPairMap<IntArr,Rectangle> cellMarked = new SetPairMap<>();
  22.     //PairMap<Rectangle,Rectangle> connectMap = new PairMap<>();
  23.     SetPairMap<Rectangle,Rectangle> parentMap = new SetPairMap<>();
  24.     Set<Rectangle> borders = new HashSet<>();  
  25.     SetPairMap<Rectangle,Rectangle> rooms = new SetPairMap<>();
  26.     PairMap<IntArr,int[][]> roomLanding = new PairMap<>();
  27.    
  28.     public CellGrid(){
  29.     //  setupGrid(3,3);
  30.     //  pathGrid(cellDivisions.getValues());
  31.     }
  32.    
  33.    
  34.     public void setupGrid(int w, int h){
  35.  
  36.         Rectangle fill;
  37.         IntArr pos;
  38.         for(int o = 0; o < h; o++){
  39.             for(int i = 0; i < w; i++){        
  40.                 cellMap.put(pos=new IntArr(i,o),fill = new Rectangle(i*CELL_SIZE,o*CELL_SIZE,CELL_SIZE,CELL_SIZE));
  41.                 roomLanding.put(pos, new int[CELL_SIZE/doorSize][CELL_SIZE/doorSize]);
  42.                 bst = new BinarySpaceTree(fill);
  43.                 for(Rectangle r: bst.Split(minSize,minSize)){
  44.                     cellDivisions.put(pos, r);
  45.                 }
  46.             }
  47.         }      
  48.     }
  49.    
  50.     Random r = new Random();
  51.     public Set<Rectangle> pathGrid(Set<Rectangle> availableRects){
  52.        
  53.         Set<Rectangle> middleCellRects = new HashSet<>(availableRects);
  54.         Rectangle start =(Rectangle) middleCellRects.toArray()[r.nextInt(middleCellRects.size())]; //Get the center cell's random rectangle
  55.        
  56.         ArrayList<Rectangle> next = new ArrayList<Rectangle>();
  57.         next.add(start);
  58.         Rectangle current = start;
  59.         int cycles = 0;
  60.         Set<Rectangle> added = new HashSet<>();
  61.        
  62.         SetPairMap<IntArr,Rectangle> cellPathB = new SetPairMap<IntArr,Rectangle>();
  63.         SetPairMap<IntArr,Rectangle> cellMarkedB = new SetPairMap<IntArr,Rectangle>();
  64.         SetPairMap<Rectangle,Rectangle> parentMapB = new SetPairMap<>();
  65.         Set<Rectangle> bordersB = new HashSet<>();
  66.         while(true){
  67.            
  68.             IntArr cell = new IntArr(current.x/CELL_SIZE, current.y/CELL_SIZE);
  69.             cellMarkedB.put(cell,current);
  70.             cellPathB.put(cell, current);      
  71.            
  72.             Rectangle inbounds = new Rectangle(current.x-1,current.y-1,current.width+2,current.height+2);
  73.            
  74.             if(!cellMap.get(cell).contains(inbounds)){
  75.                 bordersB.add(current);
  76.             }
  77.            
  78.             PairMap<Rectangle,Rectangle> choices = new PairMap<>();
  79.             PairMap<Rectangle,Integer> adjacent = getAdjacent(current);
  80.                 IntArr adjCell;
  81.                 for(Rectangle adjA: adjacent.getKeys()){
  82.                     adjCell = new IntArr(adjA.x/CELL_SIZE,adjA.y/CELL_SIZE);
  83.                     if(!cellMarkedB.getValues().contains(adjA) && !cellMarked.getValues().contains(adjA)){
  84.                         PairMap<Rectangle,Integer> across = getAdjacent(adjA);
  85.                         IntArr acrossCell;
  86.                         if(across.containsValue(adjacent.get(adjA))){
  87.                             for(Rectangle acrA: across.getKeysOfValue(adjacent.get(adjA))){
  88.                                 acrossCell = new IntArr(acrA.x/CELL_SIZE,acrA.y/CELL_SIZE);
  89.                                 if(!acrA.equals(current)){
  90.                                     if(!cellMarkedB.getValues().contains(acrA)){
  91.                                        
  92.                                         if(!cellPath.containsValue(acrA)){
  93.                                         choices.put(acrA,adjA);
  94.                                         }
  95.                                     }
  96.                                 }
  97.                             }
  98.                         }
  99.                     }
  100.                 }
  101.  
  102.                 if(choices.size() > 0){
  103.                     Rectangle choice =(Rectangle) choices.getKeys().toArray()[(r.nextInt(choices.getKeys().size()))];                  
  104.                     Rectangle adjacentRect = choices.get(choice);                  
  105.                     cellPathB.put(adjCell=new IntArr(adjacentRect.x/CELL_SIZE,adjacentRect.y/CELL_SIZE),adjacentRect);
  106.                     cellMarkedB.put(adjCell, adjacentRect);
  107.                    
  108.                     if(true){
  109.                         Set<Rectangle> oppRects = new HashSet<>();
  110.                         int dir = -1;
  111.                         switch(adjacent.get(adjacentRect)){
  112.                         case 0:
  113.                             dir = 1;
  114.                             break;
  115.                         case 1:
  116.                             dir = 0;
  117.                             break;
  118.                         case 2:
  119.                             dir = 3;                       
  120.                             break;
  121.                         case 3:
  122.                             dir = 2;
  123.                             break;
  124.                         }
  125.                        
  126.                         if(adjacent.containsValue(dir)){
  127.                             oppRects.addAll(adjacent.getKeysOfValue(dir));
  128.                             Rectangle oppRect = (Rectangle) oppRects.toArray()[r.nextInt(oppRects.size())];
  129.                             IntArr oppCell = new IntArr(oppRect.x/CELL_SIZE,oppRect.y/CELL_SIZE);
  130.                             for(Rectangle r: oppRects){
  131.                             oppCell =   new IntArr(r.x/CELL_SIZE,r.y/CELL_SIZE);
  132.                             cellMarkedB.put(oppCell, r);
  133.                             }
  134.                         }
  135.                    
  136.                     }else{
  137.                    
  138.                         for(Rectangle r: adjacent.getKeys()){
  139.                             IntArr rCell = new IntArr(r.x/CELL_SIZE,r.y/CELL_SIZE);
  140.                             cellMarkedB.put(rCell, r);
  141.                         }
  142.                    
  143.                     }
  144.                    
  145.                     if(!cell.equals(new IntArr(choice.x/CELL_SIZE,choice.y/CELL_SIZE))){                       
  146.                     //  connectMap.put(current, choice);   
  147.                     //  System.out.println(current+":"+choice);
  148.                     }
  149.                    
  150.                     parentMapB.put(choice,adjacentRect);
  151.                     parentMapB.put(adjacentRect,current);
  152.                     if(parentMap.containsKey(choice)){
  153.                         for(Rectangle p: parentMap.get(choice)){
  154.                         parentMapB.put(choice, p);
  155.                         parentMapB.put(p,choice);
  156.                         }
  157.                     }
  158.                     if(parentMap.containsKey(adjacentRect)){
  159.                         for(Rectangle p: parentMap.get(adjacentRect)){
  160.                             parentMapB.put(adjacentRect, p);
  161.                             parentMapB.put(p, adjacentRect);
  162.                         }
  163.                     }
  164.                     if(parentMap.containsKey(current)){
  165.                         for(Rectangle p: parentMap.get(current)){
  166.                         parentMapB.put(current, p);
  167.                         parentMapB.put(p,current);
  168.                         }
  169.                     }
  170.                    
  171.                     if(parentMap.containsValue(choice)){
  172.                         for(Rectangle p : parentMap.getKeysOfValue(choice)){
  173.                         parentMapB.put(p, choice);
  174.                         parentMapB.put(choice, p);
  175.                         }
  176.                     }
  177.                     if(parentMap.containsValue(adjacentRect)){
  178.                         for(Rectangle p : parentMap.getKeysOfValue(adjacentRect)){
  179.                         parentMapB.put(p, adjacentRect);
  180.                         parentMapB.put(adjacentRect, p);
  181.                         }
  182.                     }
  183.                     if(parentMap.containsValue(current)){
  184.                         for(Rectangle p : parentMap.getKeysOfValue(current)){
  185.                         parentMapB.put(p, current);
  186.                         parentMapB.put(current, p);
  187.                         }
  188.                     }
  189.                    
  190.                     next.add(current);
  191.                     next.add(choice);
  192.                     added.add(current);
  193.                     added.add(choice);
  194.                     added.add(adjacentRect);
  195.                     current = choice;
  196.                 }else{
  197.                     if(next.isEmpty()){
  198.                         System.out.println("::"+added.size()+":"+cell);
  199.                         if(cycles == 1){
  200.                             current = ((Rectangle)middleCellRects.toArray()[r.nextInt(middleCellRects.size())]);
  201.                             added.clear();
  202.                             cycles = 0;
  203.                             cellPathB.clear();
  204.                             cellMarkedB.clear();
  205.                             parentMapB.clear();
  206.                             bordersB.clear();
  207.                             continue;
  208.                         }else
  209.                         if(added.size() < 20){
  210.                             current = ((Rectangle)middleCellRects.toArray()[r.nextInt(middleCellRects.size())]);
  211.                             cycles = 0;                        
  212.                             added.clear();
  213.                             cellPathB.clear();
  214.                             cellMarkedB.clear();
  215.                             parentMapB.clear();
  216.                             bordersB.clear();
  217.                             continue;
  218.                         }
  219.                         break;
  220.                     }else{
  221.                         current = next.remove(next.size()-1);
  222.                     }
  223.                 }
  224.                
  225.                 cycles++;  
  226.         }
  227.        
  228.         for(IntArr cp: cellPathB.getKeys()){
  229.             for(Rectangle r: cellPathB.get(cp)){
  230.                 cellPath.put(cp, r);
  231.             }
  232.         }
  233.         for(IntArr cp: cellMarkedB.getKeys()){
  234.             for(Rectangle r: cellMarkedB.get(cp)){
  235.                 cellMarked.put(cp, r);
  236.             }
  237.         }
  238.        
  239.         for(Rectangle p: parentMapB.getKeys()){
  240.             for(Rectangle c: parentMapB.get(p)){
  241.                 parentMap.put(p, c);
  242.             }
  243.         }
  244.        
  245.         for(Rectangle b: bordersB){
  246.             borders.add(b);
  247.         }
  248.        
  249.         for(Rectangle add: added){
  250.             pushRoom(add);
  251.         }
  252.        
  253.             openDoors(added);
  254.             return added;
  255.     }
  256.    
  257.     public PairMap<Rectangle,Integer> getAdjacent(Rectangle area){
  258.         PairMap<Rectangle,Integer> rects = new PairMap<>();
  259.            
  260.             Rectangle[] checkArea =
  261.                     {new Rectangle(area.x,area.y-1,area.width,area.height),
  262.                     new Rectangle(area.x,area.y+1,area.width,area.height),
  263.                     new Rectangle(area.x-1,area.y,area.width,area.height),
  264.                     new Rectangle(area.x+1,area.y,area.width,area.height)}
  265.             ;
  266.            
  267.             IntArr cell = new IntArr(area.x/CELL_SIZE,area.y/CELL_SIZE);
  268.            
  269.             IntArr[] adjPos = { new IntArr(cell.get(0),cell.get(1)),
  270.                                 new IntArr(cell.get(0),cell.get(1)-1),
  271.                                 new IntArr(cell.get(0),cell.get(1)+1),
  272.                                 new IntArr(cell.get(0)-1,cell.get(1)),
  273.                                 new IntArr(cell.get(0)+1,cell.get(1))};
  274.            
  275.             for(IntArr ia: adjPos){
  276.                 if(cellDivisions.containsKey(ia)){
  277.                     for(Rectangle r: cellDivisions.get(ia)){
  278.                         for(int i = 0; i < checkArea.length; i++){
  279.                             if(checkArea[i].intersects(r) || checkArea[i].contains(r)){
  280.                                 if(!checkArea[i].equals(area)){
  281.                                 rects.put(r, i);
  282.                                 }
  283.                             }
  284.                         }
  285.                     }
  286.                 }
  287.             }
  288.            
  289.            
  290.            
  291.          
  292.         return rects;
  293.     }
  294.    
  295.    
  296.     public Set<Rectangle> appendGrid(Set<IntArr> cellPositions){
  297.         Rectangle fill;
  298.         Set<Rectangle> hold = new HashSet<>();
  299.         for(IntArr cp : cellPositions){
  300.             if(!cellDivisions.containsKey(cp)){
  301.                 cellMap.put(cp, fill = new Rectangle(cp.get(0)*CELL_SIZE,cp.get(1)*CELL_SIZE,CELL_SIZE,CELL_SIZE));
  302.                 roomLanding.put(cp, new int[CELL_SIZE][CELL_SIZE]);
  303.                 bst = new BinarySpaceTree(fill);
  304.                 for(Rectangle r: bst.Split(minSize,minSize)){
  305.                     cellDivisions.put(cp, r);
  306.                     //hold.add(r);
  307.                 }
  308.            
  309.                 for(Rectangle r: borders){
  310.                     Rectangle outerCell = new Rectangle(cellMap.get(cp));
  311.                     outerCell.setRect(outerCell.x-1,outerCell.y-1,outerCell.width+2,outerCell.height+2);
  312.                     if(outerCell.intersects(r) || outerCell.contains(r)){
  313.                         hold.add(r);
  314.                     }
  315.                 }
  316.             }
  317.             //hold.addAll(borders);
  318.         }      
  319.        
  320.         return pathGrid(hold);
  321.     }
  322.    
  323.     public void pushRoom(Rectangle r){     
  324.         Set<Rectangle> placeDown = new HashSet<>();
  325.         Set<Rectangle> remove = new HashSet<>();
  326.         //rooms.removeKey(r);
  327.                 if(rooms.containsKey(r)){
  328.                     return;
  329.                 }
  330.         for(int o = 0; o < r.height/doorSize; o++){
  331.             for(int i = 0; i < r.width/doorSize; i++){
  332.                
  333.                 Rectangle hold = new Rectangle(r.x+i,r.y+o,doorSize,doorSize);
  334.                 if(i == 0 || o == 0 || o==r.height-doorSize || i == r.width-doorSize){
  335.                         placeDown.add(hold.getBounds());
  336.                     if((i == 0 && o == 0)||(i == 0 && o == r.height/doorSize-1)||(i == r.width/doorSize-1 && o == r.height/doorSize-1) || (i == r.width/doorSize-1 && o == 0)){
  337.                         continue;
  338.                     }              
  339.                    
  340.                     Rectangle holdB = hold.getBounds();
  341.                     if(o == 0){
  342.                         holdB.translate(0, -doorSize);
  343.                         if(rooms.containsValue(holdB)){
  344.                         remove.add(holdB.getBounds());
  345.                         holdB.translate(0, doorSize);
  346.                         remove.add(holdB.getBounds());
  347.                         }
  348.                     }
  349.                     if(o == r.height-doorSize){
  350.                         holdB.translate(0, doorSize);
  351.                         if(rooms.containsValue(holdB)){
  352.                         remove.add(holdB.getBounds());
  353.                         holdB.translate(0, -doorSize);
  354.                         remove.add(holdB.getBounds());
  355.                         }
  356.                     }
  357.                    
  358.                     if(i == 0){
  359.                         holdB.translate(-doorSize, 0);
  360.                         if(rooms.containsValue(holdB)){
  361.                         remove.add(holdB.getBounds());
  362.                         holdB.translate(doorSize, 0);
  363.                         remove.add(holdB.getBounds());
  364.                         }
  365.                     }
  366.                     if(i == r.width/doorSize-doorSize){
  367.                         holdB.translate(doorSize, 0);
  368.                         if(rooms.containsValue(holdB)){
  369.                         remove.add(holdB.getBounds());
  370.                         holdB.translate(-doorSize, 0);
  371.                         remove.add(holdB.getBounds()); 
  372.                         }
  373.                     }
  374.                    
  375.                 }
  376.                
  377.             }
  378.         }
  379.        
  380.         for(Rectangle p: placeDown){
  381.             rooms.put(r, p);
  382.             roomLanding.get(new IntArr(p.x/CELL_SIZE,p.y/CELL_SIZE))[(p.y-((p.y/CELL_SIZE)*CELL_SIZE))/doorSize][(p.x-((p.x/CELL_SIZE)*CELL_SIZE))/doorSize] = 1;
  383.         }
  384.         /*
  385.         Set<Rectangle> connectors = new HashSet<>();
  386.         if(parentMap.containsKey(r)){
  387.             connectors.addAll(parentMap.get(r));
  388.         }
  389.         if(parentMap.containsValue(r)){
  390.             connectors.addAll(parentMap.getKeysOfValue(r));
  391.         }
  392.             connectors.add(r);
  393.         for(Rectangle c: connectors){
  394.             for(Rectangle p: remove){              
  395.                 if(rooms.containsKey(c) && rooms.get(c).contains(p)){
  396.                     rooms.removeValue(p);
  397.                 }
  398.             }
  399.         }
  400.         //for(Rectangle p: remove){
  401.         //  rooms.removeValue(p);
  402.         //}
  403.         */
  404.          
  405.     }
  406.    
  407.     SetPairMap<Rectangle,Rectangle> doorMap = new SetPairMap<>();
  408.     public void openDoors(Set<Rectangle> rects){
  409.         Set<Rectangle> remove = new HashSet<>();
  410.         for(Rectangle r: rects){
  411.             Set<Rectangle> connectors = new HashSet<>();
  412.                 if(parentMap.containsKey(r)){
  413.                     connectors.addAll(parentMap.get(r));
  414.                 }
  415.                 if(parentMap.containsValue(r)){
  416.                     connectors.addAll(parentMap.getKeysOfValue(r));
  417.                 }
  418.                 connectors.remove(r);
  419.             for(Rectangle wall: rooms.get(r)){
  420.                
  421.                 if(wall.x == r.x && (wall.y == r.y || wall.y == r.getMaxY()-doorSize)){
  422.                     continue;
  423.                 }
  424.                 if(wall.x == r.getMaxX()-doorSize && (wall.y == r.y || wall.y == r.getMaxY()-doorSize)){
  425.                     continue;
  426.                 }
  427.                    
  428.                 Rectangle hold = wall.getBounds();             
  429.                 hold.translate(0, -doorSize);
  430.                 if(connectors.contains(rooms.getAvailableKeyOfValue(hold))){
  431.                     remove.add(hold.getBounds());
  432.                 }
  433.                 hold = wall.getBounds();               
  434.                 hold.translate(0, doorSize);
  435.                 if(connectors.contains(rooms.getAvailableKeyOfValue(hold))){
  436.                     remove.add(hold.getBounds());
  437.                 }
  438.                 hold = wall.getBounds();               
  439.                 hold.translate(-doorSize, 0);
  440.                 if(connectors.contains(rooms.getAvailableKeyOfValue(hold))){
  441.                     remove.add(hold.getBounds());
  442.                 }
  443.                 hold = wall.getBounds();               
  444.                 hold.translate(doorSize, 0);
  445.                 if(connectors.contains(rooms.getAvailableKeyOfValue(hold))){
  446.                     remove.add(hold.getBounds());
  447.                 }
  448.                
  449.             }          
  450.         }
  451.        
  452.         for(Rectangle room: rects){        
  453.             SetPairMap<Integer,Rectangle> sides = new SetPairMap<>();
  454.             PairMap<Integer,Boolean> doorDir = new PairMap<>();
  455.             boolean hasDoor = false;
  456.             for(Rectangle rem: remove){
  457.                 if(rem.x == room.x && (rem.y == room.getMaxY()-doorSize || rem.y == room.y)){                  
  458.                     continue;
  459.                 }
  460.                 if(rem.x == room.getMaxX()-doorSize && (rem.y == room.getMaxY()-doorSize || rem.y == room.y)){
  461.                    
  462.                     continue;
  463.                 }
  464.            
  465.                 if(room.contains(rem)||room.intersects(rem)){
  466.                 //  rooms.removeValue(rem);
  467.                     int dir = -1;
  468.                     if(rem.y == room.y){//removing from top
  469.                         sides.put(dir=0, rem.getBounds());                     
  470.                     }
  471.                     if(rem.y == room.getMaxY()-doorSize){//removing from bottom
  472.                         sides.put(dir=1, rem.getBounds());
  473.                     }
  474.                     if(rem.x == room.x){//removing from left
  475.                         sides.put(dir=2, rem.getBounds());
  476.                     }
  477.                     if(rem.x == room.getMaxX()-doorSize){//removing from right
  478.                         sides.put(dir=3, rem.getBounds());
  479.                     }
  480.                     int type = 0;
  481.                     hasDoor = false;
  482.                     if(!doorDir.containsKey(dir)){
  483.                     doorDir.put(dir, hasDoor);
  484.                     }
  485.                     IntArr rCell = new IntArr(rem.x/CELL_SIZE,rem.y/CELL_SIZE);
  486.                     int previousType = roomLanding.get(rCell)[(rem.y-((rem.y/CELL_SIZE)*CELL_SIZE))/doorSize][(rem.x-((rem.x/CELL_SIZE)*CELL_SIZE))/doorSize];                 
  487.                     if(previousType != 2){
  488.                     roomLanding.get(rCell)[(rem.y-((rem.y/CELL_SIZE)*CELL_SIZE))/doorSize][(rem.x-((rem.x/CELL_SIZE)*CELL_SIZE))/doorSize]=type;
  489.                     }else{
  490.                         hasDoor = true;
  491.                         doorDir.put(dir,hasDoor);
  492.                     }
  493.                 }
  494.             }
  495.             if(doorDir.containsValue(false)){
  496.                 //System.out.println("test");
  497.                 for(Integer side: doorDir.getKeysOfValue(false)){
  498.                     if(!sides.containsKey(side)){
  499.                         continue;
  500.                     }
  501.                    
  502.                     int doorPos = sides.get(side).size();
  503.                     switch(sides.get(side).size()){
  504.                     case 1:
  505.                         doorPos = 0;
  506.                         break;
  507.                     case 2:
  508.                         doorPos = r.nextInt(2);
  509.                         break;
  510.                     case 3:
  511.                         doorPos = 1;
  512.                         break;
  513.                     default:
  514.                         doorPos = r.nextInt(sides.get(side).size()-(2))+(2);
  515.                         if(doorPos == 0){
  516.                             doorPos = 1;
  517.                         }
  518.                         if(doorPos == sides.get(side).size()-1){
  519.                             doorPos = sides.get(side).size()-2;
  520.                         }
  521.                         break;
  522.                     }
  523.                    
  524.                     Set<Rectangle> availableDoors = new HashSet<>(sides.get(side));
  525.                     if(sides.get(side).size() > 2){
  526.                         if(side == 0 || side == 1){
  527.                             Rectangle minX=null;
  528.                             Rectangle maxX = null;
  529.                             for(Rectangle s: availableDoors){
  530.                                 if(minX == null || s.getMinX() < minX.getMinX()){
  531.                                     minX = s;
  532.                                 }
  533.                                 if(maxX == null || s.getMaxX() > maxX.getMaxX()){
  534.                                     maxX = s;
  535.                                 }
  536.                             }
  537.                             availableDoors.remove(minX);
  538.                             availableDoors.remove(maxX);
  539.                            
  540.                         }else if(side == 3 || side == 2){
  541.                             Rectangle minY=null;
  542.                             Rectangle maxY = null;
  543.                             for(Rectangle s: availableDoors){
  544.                                 if(minY == null || s.getMinY() < minY.getMinY()){
  545.                                     minY = s;
  546.                                 }
  547.                                 if(maxY == null || s.getMaxY() > maxY.getMaxY()){
  548.                                     maxY = s;
  549.                                 }
  550.                             }
  551.                             availableDoors.remove(minY);
  552.                             availableDoors.remove(maxY);
  553.                         }
  554.                     }
  555.                    
  556.                     for(Rectangle doorHold: availableDoors){
  557.                         Rectangle connectorA;
  558.                         Rectangle connectorB;
  559.                         Rectangle doorHoldA;
  560.                         Rectangle doorHoldB;
  561.                        
  562.                         Rectangle doorA = doorHold.getBounds();                
  563.                         connectorA = rooms.getAvailableKeyOfValue(doorA);
  564.                         doorHoldA = doorA.getBounds();
  565.                         //rooms.removeValue(doorA);
  566.                         IntArr rCell = new IntArr(doorA.x/CELL_SIZE,doorA.y/CELL_SIZE);
  567.                         roomLanding.get(rCell)[(doorA.y-((doorA.y/CELL_SIZE)*CELL_SIZE))/doorSize][(doorA.x-((doorA.x/CELL_SIZE)*CELL_SIZE))/doorSize] = 2;
  568.                         doorA = doorA.getBounds();
  569.                         switch(side){
  570.                         case 0:
  571.                             doorA.translate(0, -doorSize);
  572.                             break;
  573.                         case 1:
  574.                             doorA.translate(0, doorSize);
  575.                             break;
  576.                         case 2:
  577.                             doorA.translate(-doorSize, 0);
  578.                             break;
  579.                         case 3:
  580.                             doorA.translate(doorSize, 0);
  581.                             break;
  582.                         }
  583.                         connectorB = rooms.getAvailableKeyOfValue(doorA.getBounds());
  584.                         doorHoldB = doorA.getBounds();
  585.                        
  586.                         doorMap.put(doorHoldA, connectorA);
  587.                         doorMap.put(doorHoldA, connectorB);
  588.                         doorMap.put(doorHoldB, connectorA);
  589.                         doorMap.put(doorHoldB, connectorB);
  590.                    
  591.                         //rooms.removeValue(doorA);
  592.                         rCell = new IntArr(doorA.x/CELL_SIZE,doorA.y/CELL_SIZE);
  593.                         roomLanding.get(rCell)[(doorA.y-((doorA.y/CELL_SIZE)*CELL_SIZE))/doorSize][(doorA.x-((doorA.x/CELL_SIZE)*CELL_SIZE))/doorSize] = 2;
  594.                     }
  595.                 }
  596.             }
  597.            
  598.         }
  599.         /* 
  600.             Rectangle current = rooms.getAvailableKeyOfValue(r);
  601.             if(r.x == current.x && (r.y == current.getMaxY()-1 || r.y == current.y)){
  602.                 restore.put(current,r);
  603.                 continue;
  604.             }
  605.             if(r.x == current.getMaxX()-1 && (r.y == current.getMaxY()-1 || r.y == current.y)){
  606.                 restore.put(current,r);
  607.                 continue;
  608.             }
  609.             if(!restore.getValues().contains(r)){
  610.             rooms.removeValue(r);
  611.             }
  612.         }
  613.         for(Rectangle r: restore.getKeys()){
  614.             for(Rectangle res: restore.get(r)){
  615.             rooms.put(r, res);
  616.             }
  617.         }
  618.        
  619.     */
  620.        
  621.     }
  622.    
  623. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement