Package model_pkg

Source Code of model_pkg.CloudInitializer$CloudSpec

package model_pkg;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;

import def_classes.Box;
import def_classes.Point;

/**
*
* @author Alving J.
*/
public class CloudInitializer {

  private File        file;        // To hold the file
  private BufferedReader     reader;        // To buffer and read from the file
  private String         line;        // To hold a line from the file
  private int         cloudAmount;    // Amount of clouds in the level
  private int         cloudSize;      // Type of cloud (0:small,1:medium:,2:big)
  private int         motionType;      // Motion type of the cloud (circular or linear)
  private Point         startPosition;    // The start position of the cloud
  private Point         endPosition;    // The end position of the cloud
  private CloudSpec       cloudSpec;      // To hold specifications of the cloud
  private int         velocity;      // Velocity of the cloud
  private Box         boundingBox;    // The bounding-box of the cloud
  private ArrayList<CloudSpec> cloudSpecArray;    // Array to hold CloudSpec objects

  /**
   * Nested class containing the specification of the cloud to be created
   * @author Alving J.
   */
  public class CloudSpec {
   
    public  int    cloudSize;   
    private Point  startPosition; 
    private Point  endPosition;  
    private int    velocity;   
    private int    motionType;   
    private Box    boundingBox;

    /**
     * Constructs a CloudSpec object
     * @param cloudType
     * @param startPosition
     * @param endPosition
     * @param velocity
     * @param boundingBox
     * @param motionType
     */
    public CloudSpec(
        int cloudType,
        Point startPosition,
        Point endPosition,
        int velocity,
        Box boundingBox,
        int motionType ) {
      this.cloudSize = cloudType;
      this.startPosition = startPosition;
      this.endPosition = endPosition;
      this.motionType = motionType;
      this.velocity = velocity;
      this.boundingBox = boundingBox;
    }
   
    /**
     * Returns the cloud type
     * @return The cloud type (integer) of the cloud
     */
    public int getCloudSize() {
      return cloudSize;
    }
   
    /**
     * Gets the start position of the cloud
     * @return Start position of the cloud
     */
    public Point getStartPosition() {
      return startPosition;
    }

    /**
     * Gets the end position of the cloud
     * @return End position of the cloud
     */
    public Point getEndPosition() {
      return endPosition;
    }

    /**
     * Gets the velocity of the cloud
     * @return Velocity of the cloud
     */
    public int getVelocity() {
      return velocity;
    }

    /**
     * Gets the motion type of the cloud
     * @return Motion type of the cloud
     */
    public int getMotionType() {
      return motionType;
    }

    /**
     * Gets the bounding-box of the cloud
     * @return Bounding box of the cloud
     */
    public Box getBBox() {
      return boundingBox;
    }
  }
 
  /**
   * Constructs a CloudInitializer object and reads from the text-file
   * @param initFile
   */
  public CloudInitializer(String initFile) {
    cloudSpecArray = new ArrayList<CloudSpec>();
    try {
      file = new File(initFile);
      reader = new BufferedReader(new FileReader(file));
      try {
        readCloudAmount();
        for (int i = 0; i < cloudAmount; i++) {
          setCloud();
          cloudSpec = new CloudSpec(cloudSize, startPosition, endPosition, velocity, boundingBox, motionType);
          cloudSpecArray.add(cloudSpec);
        }
      } 
      finally {
        reader.close();
     
    }
    catch (FileNotFoundException e) {
      e.printStackTrace();
    }
    catch (IOException e) {
      e.printStackTrace()
    }
  }

  /**
   * Reads from the text-file and extracts all information required to specify a cloud
   * @throws IOException
   */
  public void setCloud() throws IOException {
    readSize();
    readMotionType();
    readStartPosition();
    readEndPosition();
    readVelocity();
    readBoundingBox();
  }
 
  /**
   * Reads the value of CLOUD_AMOUNT
   * @throws IOException
   */
  private void readCloudAmount() throws IOException {
    getIdentifier();
    if (line.contains("CLOUD_AMOUNT")) {
      cloudAmount = Integer.valueOf(line.substring(line.indexOf('=')+1, line.indexOf(';')));
    }
  }

  /**
   * Reads the value of TYPE
   * @throws IOException
   */
  private void readSize() throws IOException {
    getIdentifier();
    if (line.contains("SIZE")) {
      cloudSize = Integer.valueOf(line.substring(line.indexOf('=')+1, line.indexOf(';')));
    }
  }

  /**
   * Reads the value of MOTION_TYPE
   * @throws IOException
   */
  private void readMotionType() throws IOException {
    getIdentifier();
    if (line.contains("MOTION_TYPE")) {
      motionType = Integer.valueOf(line.substring(line.indexOf('=')+1, line.indexOf(';')));
    }
  }

  /**
   * Reads the value of START_POSITION
   * @throws IOException
   */
  private void readStartPosition() throws IOException {
    int x, y;
    getIdentifier();
    if (line.contains("START_POSITION")) {
      x = Integer.valueOf(line.substring(line.indexOf('(')+1, line.indexOf(',')));
      y = Integer.valueOf(line.substring(line.indexOf(',')+1, line.indexOf(')')));
      startPosition = new Point(x, y);
    }
  }

  /**
   * Reads the value of END_POSITION
   * @throws IOException
   */
  private void readEndPosition() throws IOException {
    int x, y;
    getIdentifier();
    if (line.contains("END_POSITION")) {
      x = Integer.valueOf(line.substring(line.indexOf('(')+1, line.indexOf(',')));
      y = Integer.valueOf(line.substring(line.indexOf(',')+1, line.indexOf(')')));
      endPosition = new Point(x, y);
    }
  }

  /**
   * Reads the value of VELOCITY
   * @throws IOException
   */
  private void readVelocity() throws IOException {
    getIdentifier();
    if (line.contains("VELOCITY")) {
      velocity = Integer.valueOf(line.substring(line.indexOf('=')+1, line.indexOf(';')));
    }
  }

  /**
   * Reads the value of BOX
   * @throws IOException
   */
  private void readBoundingBox() throws IOException {
    int x, y;
    int width, height;
    int i, j;
    getIdentifier();
    if (line.contains("BOX")) {
      x = Integer.valueOf(line.substring(line.indexOf('(')+1,(i = line.indexOf(','))));
      y = Integer.valueOf(line.substring((i+1), (j = line.indexOf(',', i+1))));
      width = Integer.valueOf(line.substring((j+1), (i = line.indexOf(',', j+1))));
      height = Integer.valueOf(line.substring((i+1), line.indexOf(')')));
      boundingBox = new Box(x, y, width, height);

    }
  }

  /**
   * Searches the text-file for a line containing an identifier (e.g. CLOUD_AMOUNT)
   * @throws IOException
   */
  private void getIdentifier() throws IOException {
    line = reader.readLine();
    while (!line.contains("=")) {
      line = reader.readLine();
    }
  }

  /**
   * Gets the amount of clouds in the level
   * @return Amount of clouds in the level
   */
  public int getCloudAmount() {
    return cloudAmount;
  }

  /**
   * Gets a CloudSpec object from the array of CloudSpec objects
   * @param index
   * @return CloudSpec object
   */
  public CloudSpec getCloudSpec(int index) {
    return cloudSpecArray.get(index);
  }
}
TOP

Related Classes of model_pkg.CloudInitializer$CloudSpec

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.