Assign states various values in Processing

2019-07-10 04:00发布

I am working on a tile based game, where various tiles have different states. I am now looking to add a player object, this object will continually check the states of the tiles in order to move.

My Tile class:

class Tile {

  int x, y;
  boolean empty = true;
  boolean baseCell = true; 
  boolean active = false; 

  public static final int EMPTY = 0;
  public static final int BASE = 1;
  public static final int ACTIVE = 2;

  Tile(int x_, int y_, boolean baseCell_) {
    x=x_;
    y=y_;
    baseCell=baseCell_;
  }

  void display() {
    if (baseCell) {
      fill(0, 0, 255);
    } else {
      if (empty) {
        fill (255);
      } else if (active) {
        fill(255, 100, 50);
      }
    }
    stroke(0);
    rect(x, y, 50, 50);
  }
}

When the game starts, 4 baseCells are drawn and cannot be changed. The user is able to click on other cells to change their values from empty to active or active to empty.

Currently I am unsure how to assign booleans the static values I have set - or perhaps this is not the best approach.

Any help or guidance would be much appreciated.

2条回答
Deceive 欺骗
2楼-- · 2019-07-10 04:48

First of all, given that you want to assign to boolean values, the values of the static variables, I would say that it would be impossible since ACTIVE = 2. This value cannot be used as a boolean since it is not 0 nor 1.So if you want to use boolean variables all static final variable values should be 0 or 1.

Secondly, you should consider gathering all the constants (static final variables) in an interface which will be implemented by each class that needs one of those constants. It is a better programming practice and this will help you organize your project better.

Finally, if you want to change the value of the variables empty,active,baseCell, you should either include them in the constructor like so:

Tile(int x_, int y_, boolean baseCell_, boolean active_, boolean empty_)    
{
    x=x_;
    y=y_;
    baseCell=baseCell_;
    active = active_;
    empty = empty_;
  }

and/or you should implement getters and setters for each one of them like so:

//setter
protected void setEmpty(boolean empty_)
{
  empty = empty_;
}

//getter
public boolean getEmpty()
{
  return empty;
}

And the way to change the respective values is by simply calling either the constructor, when the object is first constructed, and the setters after that like so:

(..) new Tile( x, y, BASE, ACTIVE, EMPTY); //constructor
tile1.setEmpty(EMPTY); //tile1 is an instance of the Tile class

Final notes: Regarding the visibility of the setter you should be careful, because if you make it public, anyone can access it and change its' value, and this affects the security of your application. The way to tackle this is through the hierarchy.

I suggest you read this: http://docs.oracle.com/javaee/6/tutorial/doc/gjbbp.html (basic info on getters/setters) and this: https://docs.oracle.com/javase/tutorial/java/IandI/subclasses.html (required knowledge to tackle the setter problem)

查看更多
甜甜的少女心
3楼-- · 2019-07-10 04:49

As I was mentioning in the commend, rather than using a boolean for each state, you can use a single integer to keep track of the cell state. This will also allow you to use a switch statement. There's nothing wrong with if/else, you can achieve the same thing, but it may help things tidy.

Here's a basic proof of the above concept based mostly on your existing code:

//a few test tiles
int numTiles = 3;
//prepare an array to store each tile
Tile[] tiles = new Tile[numTiles];

void setup(){
  size(150,150);
  //initialize each tile
  for(int i = 0 ; i  < numTiles; i++) 
    tiles[i] = new Tile(50*i,50,random(1) > .5);//50% pseudo-random change of getting a BASE cell
}
void draw(){
  background(255);
  //render tiles (they'll handle their own states internally)
  for(int i = 0 ; i  < numTiles; i++){ 
    tiles[i].display();
  }
}
void mouseReleased(){
  //update each tile on click
  for(int i = 0 ; i  < numTiles; i++)
    tiles[i].click(mouseX,mouseY);
}

class Tile {

  int x, y;
  int size = 50;
  int state = 0; 

  public static final int EMPTY = 0;
  public static final int BASE = 1;
  public static final int ACTIVE = 2;

  Tile(int x_, int y_, boolean baseCell_) {
    x=x_;
    y=y_;
    if(baseCell_) state = BASE;
    else          state = EMPTY;
  }

  void click(int mx,int my){
    //check if the mouse coordinates are within the tile's rectangle
    boolean isOver = (mx >= x && mx <= x + size) && (my >= y && my <= y + size);
    if(isOver){//if so, update states
      switch(state){
        case EMPTY:
          state = ACTIVE;
        break;
        case ACTIVE:
          state = EMPTY;
        break;
        case BASE:
          println("BASE cell clicked, change what happens here");
        break;
      }
    }
  }

  void display() {
    switch(state){
        case EMPTY:
          fill(255);
        break;
        case ACTIVE:
          fill(255, 100, 50);
        break;
        case BASE:
          fill(0,0,255);
        break;
      }

    stroke(0);
    rect(x, y, size, size);
  }
}

Later on you may choose to call functions from within the switch statements to keep the code easier to manage (especially if you might use many states)

查看更多
登录 后发表回答