[그루비와 프로세싱] 엔티티 만들기

16315 단어 processingGroovy
마우스 커서에 대한 점이 상자에 겹치면 상자가 화면 밖으로 이동합니다.
동영상 내에서 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) {
    }
}

좋은 웹페이지 즐겨찾기