Skip to content

Instantly share code, notes, and snippets.

@pjxiao
Created June 19, 2012 04:08
Show Gist options
  • Save pjxiao/2952246 to your computer and use it in GitHub Desktop.
Save pjxiao/2952246 to your computer and use it in GitHub Desktop.
processingHoge01
/**
* Mod Processing
* @author Taka Hi
* @version 0.2
*/
/** 背景色 */
private final int BACKGROUND_COLOR = #ffffff;
/** Window */
private Window window = null;
/** 星 */
private Star star = null;
public void setup() {
size(250, 250);
window = new Window(this, BACKGROUND_COLOR);
window.init();
star = new Star(window.getWidth() / 2, 0, 0, #97fb98);
star.draw();
}
public void draw() {
window.refresh();
star.setAngle(star.getAngle() + 0.1);
star.draw();
}
protected class AnimationManager {
/** 単位時間*/
protected final int unitTime = 1;
private Window window = null;
private ArrayList<Shape> shapes = null;
private Shape[] shapeArray = null;
protected AnimationManager(Window window) {
this.window = window;
}
public AnimationManager(Window window, int length) {
this(window);
this.shapeArray = new Shape[length];
}
public int size() {
return this.shapeArray.length;
}
public Shape get(int index) {
return this.shapeArray[index];
}
public int add(Shape shape) {
int index = size();
this.shapeArray[index] = shape;
return index;
}
/**
* アニメーション処理
*/
public void doAnimateProcedure() {
for (Shape shape: shapeArray) {
checkShapeStatus(shape);
int xAfter = calculateCoordinate(shape.x, shape.vx, shape.ax);
int yAfter = calculateCoordinate(shape.y, shape.vy, shape.ay);
shape.x = xAfter;
shape.y = yAfter;
}
}
/**
* 図形の状態チェック(イベント発火装置)
*/
protected void checkShapeStatus(Shape shape) {
int top = shape.y;
int bottom = shape.y + shape.getHeight();
int left = shape.x;
int right = shape.x + shape.getWidth();
int bottomDiff = this.window.getHeight() - bottom;
int rightDiff = this.window.getWidth() - right;
if (top == 0 || left == 0 || bottomDiff == 0 || rightDiff == 0) {
shape.onShapeHitEvent();
}
}
/**
* 座標計算メソッド
*/
protected int calculateCoordinate(int r, float v, float a) {
return (int)(r + (v * this.unitTime) + ( ( 1 / 2 ) * a * this.unitTime * this.unitTime));
}
}
protected class Window {
/** 画面幅 */
private int width = 0;
/** 画面高 */
private int height = 0;
/** 背景色 */
private int backgroundColor = 0;
/**
* Window コンストラクタ
* @param int width 画面幅
* @param int height 画面高
* @param int backgroundColor 背景色
*/
protected Window(int width, int height, int backgroundColor) {
this.width = width;
this.height = height;
this.backgroundColor = backgroundColor;
}
/**
* Window コンストラクタ
* 表示が正常にできない糞仕様に対応
* @param PApplet context コンテクスト
* @param int backgroundColor 背景色
* @since ver0.2
*/
protected Window(PApplet context, int backgroundColor) {
this.width = context.width;
this.height = context.height;
this.backgroundColor = backgroundColor;
}
/**
* 初期化処理
*/
protected void init() {
size(width, height);
background(backgroundColor);
smooth();
}
/**
* 画面のお掃除
*/
protected void refresh() {
background(backgroundColor);
}
/**
* 画面幅の取得
* @retrun int 画面幅
*/
public int getWidth() {
return this.width;
}
/**
* 画面高の取得
* @retrun int 画面高
*/
public int getHeight() {
return this.height;
}
}
/**
* 汎用図形の基底クラス
 */
abstract protected class Shape {
/** x 座標 */
public int x = 0;
/** y 座標 */
public int y = 0;
/** x 方向速度 */
public float vx = 0;
/** y 方向速度 */
public float vy = 0;
/** 方向加速度 */
public float ax = 0;
/** 方向加速度 */
public float ay = 0;
/** 図形の幅 */
protected int width = 0;
/** 図形の高さ */
protected int height = 0;
/** 塗るか */
protected boolean isFill = false;
/** 塗り色 */
protected int fill = 0;
public Shape() {
throw new RuntimeException();
}
protected Shape(int width, int height) {
this.width = width;
this.height = height;
}
/**
* 図形の衝突時に呼び出されるイベント
*/
public void onShapeHitEvent() {
}
/**
* 図形の座標を設定
* @param int 図形の x 座標
* @param int 図形の y 座標
*/
protected void setPosition(int x, int y) {
this.x = x;
this.y = y;
}
/**
* 図形を描写
*/
abstract protected void draw();
/**
* 塗る
*/
protected void fillShape() {
if (this.isFill) {
smooth();
fill(this.fill);
noStroke();
}
}
/**
* 図形幅の取得
* @retrun int 図形幅
*/
public int getWidth() {
return this.width;
}
/**
* 図形高の取得
* @retrun int 図形高
*/
public int getHeight() {
return this.height;
}
}
protected class Star extends Shape {
private int radius = -1;
private int DIVID = 6;
/** 角度 rad */
private float angle = 0;
protected Star(int radius, int x, int y) {
super(radius * 2, radius * 2);
this.radius = radius;
this.x = x;
this.y = y;
}
protected Star(int radius, int x, int y, int fill) {
this(radius, x, y);
this.fill = fill;
this.isFill = true;
}
protected void setAngle(float rad) {
this.angle = rad;
}
public float getAngle() {
return this.angle;
}
@Override
protected void draw() {
fillShape();
beginShape();
for (int i = 0; i < DIVID; i++) {
float deg = 144.0 * (float)i - 90.0;
float rad = radians(deg) + this.getAngle();
vertex(radius * cos(rad) + radius + x,
radius * sin(rad) + radius + y);
}
endShape();
}
}
protected class Circle extends Shape {
private int radius = -1;
protected Circle(int radius, int x, int y) {
super(radius * 2, radius * 2);
this.radius = radius;
this.x = x;
this.y = y;
}
protected Circle(int radius, int x, int y, int fill) {
this(radius, x, y);
this.fill = fill;
this.isFill = true;
}
@Override
protected void draw() {
fillShape();
ellipse(x + radius, y + radius, radius * 2, radius * 2);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment