[GroovyとProcessing]エンティティを作る

マウスカーソルについてくる点が箱に重なったら箱が画面外に移動していく。
動画内でinterfaceやclassで実装しているのを@Delegateを使って実装してみた。

動画リスト：[LWJGLのチュートリアル](http://www.youtube.com/playlist?list=PL19F2453814E0E315)
動画は[ココ](http://www.youtube.com/watch?v=-LwBWysU-FY)

```groovy:DisplayTest.groovy
package episode008

import groovy.swing.SwingBuilder
import processing.core.PApplet

import javax.swing.*

class DisplayTest extends PApplet {

    def lastFrame

    def box
    def point

    def void setup() {
        frameRate(60)

        lastFrame = System.currentTimeMillis()

        box = new Box(this, 100, 100, 50, 50)
        point = new Point(this, 10, 10, 5)
    }

    def void draw() {
        background(0, 0, 0)

        point.setLocation(mouseX, mouseY)

        def delta = delta   // getDelta()
        box.update(delta)
        point.update(delta)

        if (box.intersects(point)) {
            box.setDx(0.2)
            box.setDy(0.1)
        }

        box.draw()
        point.draw()
    }

    def getDelta() {
        def currentTime = System.currentTimeMillis()
        def delta = currentTime - lastFrame
        lastFrame = currentTime
        delta
    }

    def static void main(args) {
        def display = new DisplayTest()
        new SwingBuilder().frame(
                title: 'Episode 8',
                defaultCloseOperation: JFrame.EXIT_ON_CLOSE,
                size: [640, 480], show: true) {
            widget(display)
        }
        display.init()
    }
}

class Entity {
    def x, y, width, height
    def display

    def Entity(display, x, y, width, height) {
        this.display = display
        this.x = x
        this.y = y
        this.width = width
        this.height = height
    }

    def setLocation(x, y) {
        this.x = x
        this.y = y
    }

    def intersects(other) {
        x < other.x && y < other.y && x + width > other.x && y + height > other.y
    }
}

class MovableEntity extends Entity {
    def dx, dy

    def MovableEntity(display, x, y, width, height) {
        super(display, x, y, width, height)
        this.dx = 0
        this.dy = 0
    }

    def update(delta) {
        x += delta * dx
        y += delta * dy
    }
}

class Box {
    @Delegate MovableEntity entity

    def Box(display, x, y, width, height) {
        this.entity = new MovableEntity(display, x, y, width, height)
    }

    def draw() {
        def posX = x
        def posY = y
        def boxWidth = width
        def boxHeight = height
        display.with {
            noStroke()
            fill(255, 255, 255)
            rect(posX, posY, boxWidth, boxHeight)
        }
    }
}

class Point {
    def weight
    @Delegate Entity entity

    def Point(display, x, y, weight) {
        this.entity = new Entity(display, x, y, weight, weight)
        this.weight = weight
    }

    def draw() {
        def posX = x
        def posY = y
        display.with {
            noSmooth()
            stroke(255, 255, 255)
            strokeWeight(weight)
            point(posX, posY)
        }
    }

    def update(delta) {
    }
}
```

![grocessingGL008.png](https://qiita-image-store.s3.amazonaws.com/0/17179/e21065af-bdea-c17e-b05c-399f55e6c109.png)