Package game

Source Code of game.Model

package game;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import org.newdawn.slick.opengl.Texture;
import org.newdawn.slick.opengl.TextureLoader;
import org.newdawn.slick.util.ResourceLoader;

public class Model {
  private Algorithm algorithm;
  private Config config;
  private GameState state;
  private float width, height, block_width, block_height, exp_speed;
  private int rows, cells, row_count, bear_count, add_time, clicks;
  private List<Square2D[]> squares;
  private List<Square2D> bears;
  private List<Texture> textures;
 
  public GameState getState() {
    return state;
  }
 
  public int getRows() {
    return rows;
  }
 
  public int getCells() {
    return cells;
  }
 
  public int getRowCount() {
    return row_count;
  }
 
  public float getWidth() {
    return width;
  }
 
  public float getHeight() {
    return height;
  }
 
  public float getBlockWidth() {
    return block_width;
  }
 
  public float getBlockHeight() {
    return block_height;
  }
 
  public List<Square2D[]> getSquares() {
    return squares;
  }
 
  public List<Texture> getTextures() {
    return textures;
  }
 
  public Model(Config config) {
    this.config = config;
    this.state = GameState.PLAY;
    this.add_time = 0;
    this.bear_count = config.getBearCount();
    this.width = config.getWidth();
    this.height = config.getHeight();
    this.rows = config.getRows();
    this.cells = config.getCells();
    this.row_count = config.getStartRows();
    this.clicks = config.getClickCount();
    if (config.getStartRows() <= config.getRows()) {
      this.row_count = config.getRows() + 1;
    }
    this.exp_speed = config.getExplosionSpeed();
    this.squares = new ArrayList<Square2D[]>();
    this.fillObjects();
    this.algorithm = new Algorithm(squares, bears, block_width, block_height, rows, cells, row_count, exp_speed);
    algorithm.prepareCoord();
  }

  public void clicked(float x, float y) {
    if (state == GameState.PLAY) {
      int pos_x = (int)(x/ block_width);
      int pos_y = row_count - (int)(y/ block_height) - 1;
      algorithm.Check(pos_y, pos_x);
      state = algorithm.getState();
      if (state == GameState.EXPLODE) {
        clicks --;
      }
      if (clicks == 0) {
        state = GameState.LOSE;
      }
    }
  }

  public void play() {
    switch(state) {
    case PLAY: checkRow(); break;
    case EXPLODE: checkRow(); algorithm.explode(); break;
    default: break;
    }
    state = algorithm.getState();
  }
 
  public void loadTextures() {
    textures = new ArrayList<Texture>();
    try {
      for (String name : config.getTextureNames()) {
        Texture texture = TextureLoader.getTexture("PNG", ResourceLoader.getResourceAsStream("game/res/" + name + ".png"));
        textures.add(texture);
      }
    } catch (FileNotFoundException e) {
      e.printStackTrace();
    } catch (IOException e) {
      e.printStackTrace();
    }
  }
 
  private void fillObjects() {
    block_width = width/ (float)cells;
    block_height = height/ (float)rows;
    bears = new ArrayList<Square2D>();
    for (int i = 0; i < row_count; ++i) {
      Square2D[] new_list = new Square2D[cells];
      int j;
      if (config.getStartRows() - row_count + i <= 0) {
        for (j = 0; j < cells; ++j) {
          new_list[j] = new EmptySquare(i, j);
        }
        if (config.getStartRows() - row_count + i == 0) {
          int b_count = 0;
          while (b_count != bear_count) {
            j = new Random().nextInt(cells);
            if (new_list[j].getType() == SquareType.EMPTY) {
              Square2D bear = new BearSquare(i, j);
              bears.add(bear);
              new_list[j] = bear;
              b_count ++;
            }
          }
        }
      } else {
        addRow(new_list, i);
      }
      squares.add(new_list);
    }
  }
 
  private void addRow(Square2D[] new_list, int i) {
    for (int j = 0; j < cells; ++j) {
      int rand_int = new Random().nextInt(1000);
      if (rand_int < 2) {
        new_list[j] = new ColoredSquare(i, j, ColorEnum.nextRand());
      } else {
        new_list[j] = new SimpleSquare(i, j, ColorEnum.nextRand());
      }
    }
  }
 
  private void checkRow() {
    add_time ++;
    if (add_time > config.getAddTime() && state == GameState.PLAY) {
      add_time = 0;
      Square2D[] new_list = new Square2D[cells];
      addRow(new_list, row_count);
      squares.add(new_list);
      row_count ++;
      algorithm.setRowCount(row_count);
      algorithm.prepareCoord();
    }
  }
}
TOP

Related Classes of game.Model

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.