Help us understand the problem. What is going on with this article?

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

More than 5 years have passed since last update.

マウスカーソルについてくる点が箱に重なったら箱が画面外に移動していく。
動画内で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

hina0118
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away