トーフメモ

主にゲーム制作

【Processing】 ボールが跳ね返って波みたいに消えるプログラム

おもいつきプログラミング
Processingはいいぞ

BallAndWave.pde

ArrayList<Ball> ball=new ArrayList<Ball>();
ArrayList<Effect> effect=new ArrayList<Effect>();

void setup() {
  size(1300, 1300);

  colorMode(HSB, 360, 100, 100, 100);
  textAlign(CENTER, CENTER);
  rectMode(CENTER);

  for (int i=0; i<50; i++) {
    ball.add(new Ball(ball.size(), new PVector(random(width), random(height)), new PVector(random(30)*random(100)>=50?1:-1, random(30)*random(100)>=50?1:-1), random(5, 100)));
  }
}

void draw() {
  background(0, 0, 100);

  for (int i=ball.size()-1; i>=0; i--) {
    ball.get(i).run();
  }

  for (int i=effect.size()-1; i>=0; i--) {
    effect.get(i).run();
    if (effect.get(i).removeFlag) {
      effect.remove(i);
    }
  }
}

class Ball {
  int index;
  PVector pos;
  PVector spd;
  float s;

  boolean wait;
  float waitTime;

  Ball(int index, PVector pos, PVector spd, float s) {
    this.index=index;
    this.pos=new PVector(pos.x,pos.y);
    this.spd=new PVector(spd.x,spd.y);
    this.s=s;
  }

  void run() {
    controller();
    model();
    view();
  }

  void model() {
    //right
    if (pos.x>=(width-s/2)) {
      pos.x=(width-s/2);
      spd.x*=(-1);
    }

    //left
    if (pos.x<=s/2) {
      pos.x=s/2;
      spd.x*=(-1);
    }

    //down
    if (pos.y>=(height-s/2)) {
      pos.y=(height-s/2);
      spd.y*=(-1);
    }

    //up
    if (pos.y<=s/2) {
      pos.y=s/2;
      spd.y*=(-1);
    }

    //other balls
    if (!wait) {
      for (int i=0; i<ball.size(); i++) {
        Ball other=ball.get(i);
        if (i!=index) {
          float _dist=dist(other.pos.x, other.pos.y, pos.x, pos.y);
          if (_dist<=(other.s/2+s/2)) {
            PVector tmpOtSpd=other.spd;
            PVector tmpMeSpd=spd;

            other.spd.x=(tmpMeSpd.x-tmpOtSpd.x)*0.8f;
            other.spd.y=(tmpMeSpd.y-tmpOtSpd.y)*0.8f;
            spd.x=(-tmpMeSpd.x+tmpOtSpd.x)*0.8f;
            spd.y=(-tmpMeSpd.y+tmpOtSpd.y)*0.8f;

            wait=true;

            effect.add(new Effect(pos, s, random(360)));
            println(effect.size());
          }
        }
      }
    }

    pos.x+=spd.x;
    pos.y+=spd.y;

    if (wait) {
      waitTime++;
      if (waitTime>=60) {
        waitTime=0;
        wait=false;
      }
    }
  }

  void view() {
    strokeWeight(5);

    noFill();
    if (!wait) {
      stroke(0, 0, 0, 100);
    } else {
      stroke(0, 0, 0, 0);
    }
    ellipse(pos.x, pos.y, s, s);
  }

  void controller() {
    if (mousePressed) {
      spd.x+=random(-5, 5);
      spd.y+=random(-5, 5);
    }
  }
}

class Effect {
  PVector pos;
  float s;
  float _color;

  boolean removeFlag;

  Effect(PVector pos, float s, float _color) {
    this.pos=new PVector(pos.x, pos.y);
    this.s=s;
    this._color=_color;
  }

  void run() {
    model();
    view();
  }

  void model() {
    s+=10;

    if (s>=500) {
      removeFlag=true;
    }
  }

  void view() {
    strokeWeight(5);

    noFill();
    stroke(_color, 100, 100, 100-s/5);
    ellipse(pos.x, pos.y, s, s);
  }
}