Package generators

Source Code of generators.GridLineGenerator

package generators;

import squaresgame.ColourManager;
import squaresgame.Grid;
import squaresgame.LineComponent;
import framework.collision.CollisionComponent;
import framework.collision.LineCollisionMask;
import framework.component.ParentComponent;
import framework.rendering.SpriteComponent;
import framework.spacial.Line;
import framework.spacial.OrientationComponent;
import framework.spacial.PositionComponent;

public abstract class GridLineGenerator {

  public static ParentComponent generateLineGrid(Grid grid, ParentComponent gridParent, float spacing){
    int dotsWidth = grid.getDotsWidth();
    int dotsHeight = grid.getDotsHeight();
   
    //Horizontal lines
    for(int i = 0; i < dotsWidth - 1; i++){
      for(int j = 0; j < dotsHeight; j++){
        gridParent.addComponent(generateGridLine(i, j, true, spacing, grid));
      }
    }

    //Horizontal lines
    for(int i = 0; i < dotsWidth; i++){
      for(int j = 0; j < dotsHeight -1; j++){
        gridParent.addComponent(generateGridLine(i, j, false, spacing, grid));
      }
    }

    return gridParent;
  }

  private static ParentComponent generateGridLine(int gridX, int gridY, boolean isHorizontal, float spacing, Grid grid){
    ParentComponent lineParent = generateGridLine(spacing*gridX, spacing*gridY, isHorizontal);

    LineComponent lineComp = new LineComponent(isHorizontal ? grid.getHorizontalLine(gridX,gridY) : grid.getVerticalLine(gridX,gridY));
    lineParent.addComponent(lineComp);
   
    lineParent.setLabel(getLineLabel(gridX, gridY, isHorizontal));
    return lineParent;
  }


  private static ParentComponent generateGridLine(float screenX, float screenY, boolean isHorizontal){
    ParentComponent lineParent = new ParentComponent();

    int lineWidth = 50;

    lineParent.addComponent(new CollisionComponent(new LineCollisionMask(new Line(0, 0, 0,lineWidth))));

    lineParent.addComponent(new SpriteComponent(ColourManager.getUnplacedLine()));
   
    if(isHorizontal){
      lineParent.addComponent(new OrientationComponent(270));
    }
   
    PositionComponent pos;

    //TODO remove this weird pixel adjustment to make it cleaner, and independant of image size.
    if(isHorizontal){
      pos = new PositionComponent(screenX + 12, screenY+ 10);
    }else{
      pos = new PositionComponent(screenX + 5, screenY + 15);
    }

    lineParent.addComponent(pos);

    return lineParent;
  }
 
  public static String getLineLabel(squaresgame.Line line){
    return getLineLabel(line.getX(), line.getY(), line.isHorizontal());
  }
 
  public static String getLineLabel(int x, int y, boolean isHorizontal){
    return "Line: "+x+", "+y+" "+(isHorizontal ? "Horizontal" : "Vertical");
  }

}
TOP

Related Classes of generators.GridLineGenerator

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.