Newer
Older
AlgebraicDataflowArchitectureModel / GameEngine / src / main / java / gameEngine / entites / gameComponents / Connection.java
NoranekoFelician 1 day ago 1 KB Editorに表示するものの微調整
package gameEngine.entites.gameComponents;

import gameEngine.views.Color;
import gameEngine.views.Sprite;
import org.joml.Vector3f;

public class Connection {
    private PortView inPort;
    private PortView outPort;
    private Sprite lineSprite;

    public Connection(PortView inPort, PortView outPort) {
        this.inPort = inPort;
        this.outPort = outPort;
        this.lineSprite = new Sprite("GameEngine/resources/Line.png");
    }

    public void render() {
        Vector3f posA = inPort.getPosition();
        Vector3f posB = outPort.getPosition();

        Vector3f linePosition = calculatePosition(posA, posB);
        float lineLength = calculateDistance(posA, posB);
        float angle = calculateAngle(posA, posB);

        linePosition.x -= (float) (lineLength / 2 * Math.cos(Math.toRadians(angle)));
        linePosition.y -= (float) (lineLength / 2 * Math.sin(Math.toRadians(angle)));

        lineSprite.setPosition(linePosition.x+8, linePosition.y+8);
        lineSprite.setRotation(new Vector3f(0, 0, angle)); // z軸の回転を設定
        lineSprite.setScale(new Vector3f(lineLength, 3, 1)); // 長さをスケールで調整

        lineSprite.update();
    }

    private Vector3f calculatePosition(Vector3f posA, Vector3f posB) {
        return new Vector3f((posA.x + posB.x) / 2, (posA.y + posB.y) / 2, 0);
    }

    private float calculateDistance(Vector3f posA, Vector3f posB) {
        return (float) Math.sqrt(Math.pow(posB.x - posA.x, 2) + Math.pow(posB.y - posA.y, 2));
    }

    private float calculateAngle(Vector3f posA, Vector3f posB) {
        return (float) Math.toDegrees(Math.atan2(posB.y - posA.y, posB.x - posA.x));
    }

    public boolean isClicked(float mouseX, float mouseY) {
        return lineSprite.isMouseOver(mouseX, mouseY);
    }
}