Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (494)
Games in Android Showcase (114)
games submitted by our members
Games in WIP (563)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
  JavaGaming.org - Pastebin



Author: kutucuk (posted 2013-06-26 08:31:52, viewed 97 times)

1   2   3   4   5   6   7   8   9   10   11   12   13   14   15   16   17   18   19   20   21   22   23   24   25   26   27   28   29   30   31   32   33   34   35   36   37   38   39   40   41   42   43   44   45   46   47   48   49   50   51   52   53   54   55   56   57   58   59   60   61   62   63   64   65   66   67   68   69   70   71   72   73   74   75   76   77   78   79   80   81   82   83   84   85   86   87   88   89   90   91   92   93   94   95   96   97   98   99   100   101   102   103   104   105   106   107   108   109   110   111   112   113   114   115   116   117   118   119   120   121   122   123   124   125   126   127   128   129   130   131   132   133   134   135   136   137   138   139   140   141   142   143   144   145   146   147   148   149   150   151   152   153   154  
public class FindRoadLine {
   Arena arena; // My rendering class. Easy access for map.
   TiledMap map; // My map, with all of its layers and properties.
   TiledMapTileLayer myLayer; // A layer which I'll operate on
   // Count of the total roads, not in order. From my map class.
   Array<TiledMapTileLayer.Cell> totalRoadTiles;
   // firstCell is the beginning of the road
   TiledMapTileLayer.Cell firstCell, tempCell;
   int x_tile, y_tile; // Storing the cell locations
   Array<TiledMapTileLayer.Cell> myRoad; // Storing all road cells in order.
   // Storing locations to use them on rectangles and vertices of polyline
   Array<Vector2> roadLocations; // Obvious
   Array<Rectangle> roadRectangles; // My first idea was to build rectangles
   Array<Vector2> centerPoints; // Centers of the rectangles
   Polyline polyLine; // My sweet polyline which I don't know how to use yet
   float[] vertices; // For shape renderer.
   // When finding roads, it should know where it went last time. That's for it.
   int direction = 0;

   public FindRoadLine(Arena arena) { // Initialization
      this.arena = arena;
      map = arena.getMap();
      myLayer = (TiledMapTileLayer) map.getLayers().get("road_layer");
      totalRoadTiles = arena.getMyMap().getRoadCell();
   }

   public void getFirstRoad() {
      // The first catch: There should be only 1 road in the x=0 line.
      // I could include x to the loop, result would be the same.
      for (int y = 0; y < myLayer.getHeight(); y++) {
         TiledMapTileLayer.Cell cell = myLayer.getCell(0, y);
         roadLocations = new Array<Vector2>();
         if (cell.getTile().getProperties().get("tid") != null && cell.getTile().getProperties().get("tid").equals("road")) {
            firstCell = cell;
            roadLocations.add(new Vector2(0, y));
            x_tile = 0;
            y_tile = y;
            break;
         }
      }
   }

   public void lineTheRoad() {
      // 1 = left side, 3 = right side, 2 = down, 5 = up --> Numpad for
      // remembering :D
      myRoad = new Array<TiledMapTileLayer.Cell>();
      myRoad.add(firstCell);
      /*
       * It starts from the first road cell and checks if the surrounding cells
       * are roads as well. Like I said, direction variable is for it not to
       * return and add what it already added. x_tile and y_tile are updated
       * accordingly, if the road is found. null check, because I might forget
       * to add tid property to other tiles.
       */
      while (totalRoadTiles.size != myRoad.size) {
         if (direction != 1 && myLayer.getCell(x_tile + 1, y_tile).getTile().getProperties().get("tid") != null
               && myLayer.getCell(x_tile + 1, y_tile).getTile().getProperties().get("tid").equals("road")) {
            tempCell = myLayer.getCell(x_tile + 1, y_tile);
            myRoad.add(tempCell);
            roadLocations.add(new Vector2(x_tile + 1, y_tile));
            x_tile++;
            direction = 3;
         }
         if (direction != 3 && myLayer.getCell(x_tile - 1, y_tile).getTile().getProperties().get("tid") != null
               && myLayer.getCell(x_tile - 1, y_tile).getTile().getProperties().get("tid").equals("road")) {
            tempCell = myLayer.getCell(x_tile - 1, y_tile);
            myRoad.add(tempCell);
            roadLocations.add(new Vector2(x_tile - 1, y_tile));
            x_tile--;
            direction = 1;
         }
         if (direction != 5 && myLayer.getCell(x_tile, y_tile + 1).getTile().getProperties().get("tid") != null
               && myLayer.getCell(x_tile, y_tile + 1).getTile().getProperties().get("tid").equals("road")) {
            tempCell = myLayer.getCell(x_tile, y_tile + 1);
            roadLocations.add(new Vector2(x_tile, y_tile + 1));
            myRoad.add(tempCell);
            y_tile++;
            direction = 2;
         }
         if (direction != 2 && myLayer.getCell(x_tile, y_tile - 1).getTile().getProperties().get("tid") != null
               && myLayer.getCell(x_tile, y_tile - 1).getTile().getProperties().get("tid").equals("road")) {
            tempCell = myLayer.getCell(x_tile, y_tile - 1);
            myRoad.add(tempCell);
            roadLocations.add(new Vector2(x_tile, y_tile - 1));
            y_tile--;
            direction = 5;
         }
      }
   }

   /*
    * Each method calls another one in the end, I made it like this in case I
    * want to start from somewhere else than the beginning
    */

   public Array<Rectangle> getRectangles() {
      // My first intention was draw rectangles and then merge them. That's why
      // this method exists.
      roadRectangles = new Array<Rectangle>();
      for (int i = 0; i < myRoad.size; i++) {
         // 32 is the tile width and height. I have a method for these in my map
         // class, but for testing, I knew the width and just typed in
         Rectangle rec = new Rectangle(roadLocations.get(i).x * 32, roadLocations.get(i).y * 32, 32, 32);
         roadRectangles.add(rec);
      }
      getPoints();
      return roadRectangles; // Returns this, because I might need it in other
                              // classes.
   }

   public void getPoints() {
      /*
       * The center of every rectangle are saved as Vector2. But I could just
       * add them in order to an array. I didn't know what I needed for shape
       * renderer. That's why I saved them into vectors.
       */
      centerPoints = new Array<Vector2>();
      for (int i = 0; i < roadRectangles.size; i++) {
         Vector2 center = new Vector2(roadRectangles.get(i).x + roadRectangles.get(i).width / 2, roadRectangles.get(i).y
               + roadRectangles.get(i).height / 2);
         centerPoints.add(center);
      }
      for (int i = 0; i < centerPoints.size; i++) {
         System.out.println(centerPoints.get(i));
      }
      getPolyline();
   }

   public void getPolyline() {
      /*
       * This would just return the polyline but shaperenderer doesn't accept
       * like this. So, below is just my pathetic method to put the Vector
       * points into an array in order; x of 1 is 0th, y of 1 is 1st etc.
       */
      vertices = new float[centerPoints.size * 2];
      float[] vertices_x = new float[centerPoints.size];
      float[] vertices_y = new float[centerPoints.size];
      for (int i = 0; i < centerPoints.size; i++) {
         vertices_x[i] = centerPoints.get(i).x;
         vertices_y[i] = centerPoints.get(i).y;
      }
      for (int i = 0; i < vertices_x.length; i++) {
         vertices[2 * i] = vertices_x[i];
         vertices[2 * i + 1] = vertices_y[i];
      }
      polyLine = new Polyline(vertices);

   }

   public float[] getVertices() {
      // Returns the vertices for the shaperenderer.
      return vertices;
   }
}





Dump your java code here :



Special syntax:
  • To highlight a line (yellow background), prefix it with '@@'
  • To indicate that a line should be removed (red background), prefix it with '-'
  • To indicate that a line should be added (green background), prefix it with '+'
  • To post multiple snippets, seperate them by '~~~~'
  EOF
 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

Dwinin (21 views)
2014-09-12 09:08:26

Norakomi (55 views)
2014-09-10 13:57:51

TehJavaDev (66 views)
2014-09-10 06:39:09

Tekkerue (33 views)
2014-09-09 02:24:56

mitcheeb (54 views)
2014-09-08 06:06:29

BurntPizza (38 views)
2014-09-07 01:13:42

Longarmx (24 views)
2014-09-07 01:12:14

Longarmx (30 views)
2014-09-07 01:11:22

Longarmx (28 views)
2014-09-07 01:10:19

mitcheeb (37 views)
2014-09-04 23:08:59
List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!