Newer
Older
AlgebraicDataflowArchitectureModel / GameEngine / src / main / java / gameEngine / entites / gameComponents / Connection.java
NoranekoFelician 3 days ago 1 KB Portをクリックし、判別します
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/EditorFrame.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);

        lineSprite.setPosition(linePosition.x, linePosition.y);
        lineSprite.setRotation(new Vector3f(0, 0, angle)); // z軸の回転を設定
        lineSprite.setScale(new Vector3f(lineLength, 1, 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);
    }
}