2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

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

Posted at

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

動画リスト:LWJGLのチュートリアル
動画はココ

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

2
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?