OneHourProcessing 十一日目

OneHourProcessing 十一日目

今日作ったのはこちら

まだ未完成です…
作るのにかかった時間は「1時間26分」

コード

Controller controller;

void setup()
{
  size(800, 450);
  reset();
}

void draw()
{
  background(0);
  controller.update();
  controller.display();
}

void keyPressed()
{
  controller.keyPress();
}

void reset()
{
  controller = null;
  controller = new Controller(new PVector (8, 15), 30);
}

Controllerクラス

class Controller
{
  PVector squareNum;
  float squareSize;
  ArrayList<Puyo> puyos = new ArrayList<Puyo>();
  Puyo[][] squares;
  Puyo operatePuyo;

  Controller(PVector squareNum, float squareSize)
  {
    this.squareNum  = squareNum;
    this.squareSize = squareSize;
    squares = new Puyo[int(squareNum.x)][int(squareNum.y)];
    addPuyo();
  }

  void update()
  {
    for (Puyo puyo : puyos)
      puyo.update();

    if (operatePuyo.pos.y % squareSize > squareSize / 2)
    {
      int puyoX = int(operatePuyo.pos.x / squareSize);
      int puyoY = int(operatePuyo.pos.y / squareSize);

      if (puyoY <= squareNum.y - 2)
        if (squares[puyoX][puyoY + 1] == null)
          return;

      operatePuyo.vec = new PVector(0, 0);
      operatePuyo.status = "STOP";
      squares[puyoX][puyoY] = operatePuyo;

      for (int x = -1; x <= 1; x += 2)
        if (puyoX + x >= 0 && puyoX + x < squareNum.x)
        {
          Puyo puyo = squares[puyoX + x][puyoY];
          if (puyo != null)
            operatePuyo.checkParents(puyo);
        }
      for (int y = -1; y <= 1; y += 2)
        if (puyoY + y >= 0 && puyoY + y < squareNum.y)
        {
          Puyo puyo = squares[puyoX][puyoY + y];
          if (puyo != null)
            operatePuyo.checkParents(puyo);
        }

      println(operatePuyo.connectNum + 1);
      if (operatePuyo.connectNum + 1 >= 4)
      {
        removePuyo(operatePuyo);
      }

      addPuyo();
    }
  }

  void display()
  {
    for (Puyo puyo : puyos)
      puyo.display();
  }

  void keyPress()
  {
    if (operatePuyo == null)
      return;

    switch(keyCode)
    {
    case LEFT:
      if (operatePuyo.returnX(squareSize) > 0)
        operatePuyo.pos.x -= squareSize;
      break;
    case RIGHT:
      if (operatePuyo.returnX(squareSize) < squareNum.x - 1)
        operatePuyo.pos.x += squareSize;
      break;
    }
  }

  void addPuyo()
  {
    PVector puyoPos = new PVector (squareNum.x / 2 * squareSize + squareSize / 2, squareSize / 2);
    PVector puyoVec = new PVector (0, 3);
    float   puyoRad = squareSize;
    String  puyoSta = "FALL";
    String  puyoTyp;
    int random = int(random(5));

    if (random == 0)
      puyoTyp = "RED";
    else if (random == 1)
      puyoTyp = "YELLOW";
    else if (random == 2)
      puyoTyp = "GREEN";
    else if (random == 3)
      puyoTyp = "BLUE";
    else
      puyoTyp = "PURPLE";

    Puyo puyo = new Puyo(puyoPos, puyoVec, puyoRad, puyoSta, "PURPLE");
    operatePuyo = puyo;
    puyos.add(puyo);
  }

  void removePuyo(Puyo puyo)
  {
    for (int i = 0; i < puyo.parents.size(); i++)
    {
      removePuyo(puyo.parents.get(i));
    }
    puyos.remove(puyo);
    squares[puyo.returnX(squareSize)][puyo.returnY(squareSize)] = null;
  }

  void resetPuyo()
  {
    for (Puyo puyo : puyos)
    {
      int puyoX = puyo.returnX(squareSize);
      int puyoY = puyo.returnY(squareSize);

      if (puyoY <= squareNum.y - 2)
        if (squares[puyoX][puyoY + 1] == null)
        {
          puyo.vec = new PVector(0, 3);
          puyo.status = "FALL";
          puyo.connectNum = 0;
          puyo.parents.clear();
          squares[puyoX][puyoY] = null;
        }
    }
  }
}

Puyoクラス

class Puyo
{
  PVector pos;
  PVector vec;
  float radius;
  String status; // MASTER, SLAVE, STOP, FALL
  String type; //RED, YELLOW, GREEN, BLUE, PURPLE
  ArrayList<Puyo> parents = new ArrayList<Puyo>();
  int connectNum;

  Puyo(PVector pos, PVector vec, float radius, String status, String type)
  {
    this.pos    = pos;
    this.vec    = vec;
    this.radius = radius;
    this.status = status;
    this.type   = type;
    connectNum = 0;
  }

  void update()
  {
    pos.add(vec);
  }

  void display()
  {
    if (type.equals("RED"))
      fill(255, 0, 0);
    else if (type.equals("YELLOW"))
      fill(255, 255, 0);
    else if (type.equals("GREEN"))
      fill(0, 255, 0);
    else if (type.equals("BLUE"))
      fill(0, 0, 255);
    else if (type.equals("PURPLE"))
      fill(255, 0, 255);

    ellipse(pos.x, pos.y, radius, radius);
  }

  void setParents(Puyo puyo)
  {
    parents.add(puyo);
  }

  void checkParents(Puyo puyo)
  {
    if (!type.equals(puyo.type))
      return;
    connectNum += puyo.connectNum + 1;
    parents.add(puyo);
  }
  
  int returnX(float squareSize)
  {
    return int(pos.x / squareSize);
  }
  
  int returnY(float squareSize)
  {
    return int(pos.y / squareSize);
  }
}