Newer
Older
AlgebraicDataflowArchitectureModel / GameEngine / src / main / java / gameEngine / entites / gameComponents / Mesh.java
package gameEngine.entites.gameComponents;

import gameEngine.entites.Entity;
import gameEngine.entites.GameObject;
import gameEngine.views.Color;
import gameEngine.views.Sprite;
import org.joml.Vector3f;

public class Mesh extends Draggable {

    public enum MeshType {
        SPRITE, CUBE
    }

    private final MeshType type;
    private Sprite sprite;

    public Mesh(GameObject parent, MeshType type, String texturePath) {
        this.parent = parent;
        this.type = type;
        if (type == MeshType.SPRITE) {
            this.sprite = new Sprite(texturePath);
            sprite.updateSpriteDimensions();
        }
    }
    public Mesh(Mesh original, GameObject newParent) {
        this.type = original.type;
        this.sprite = original.sprite;
        this.parent = newParent;
        sprite.updateSpriteDimensions();
    }

    @Override
    public GameComponent copy() {
        return this;
    }

    @Override
    public void init() {
        if (type == MeshType.SPRITE) {
            sprite.update();
            sprite.updateSpriteDimensions();
        }
    }

    @Override
    public void update() {
        if (type == MeshType.SPRITE) {
            Vector3f globalPosition = new Vector3f(parent.transform.position).add(localPosition);
            sprite.setPosition(globalPosition);
            Vector3f globalRotation = new Vector3f(parent.transform.rotation).add(localRotation);
            sprite.setRotation(globalRotation);
            Vector3f globalScale = new Vector3f(parent.transform.scale).add(localScale);
            sprite.setScale(globalScale);
            sprite.update();
        }
    }

    @Override
    protected boolean isMouseOver(float mouseX, float mouseY) {
        return sprite.isMouseOver(mouseX, mouseY);
    }

    @Override
    protected void draggingAction(float mouseX, float mouseY) {
        sprite.setPosition(mouseX - sprite.getDisplayedWidth() / 2,
                mouseY - sprite.getDisplayedHeight() / 2);
    }

    public void setSprite(String texturePath){
        sprite.setTexturePath(texturePath);
    }

    public void setColor(Color color){
        sprite.setColor(color);
        sprite.update();
    }

    public float getDisplayedWidth() {
        return sprite != null ? sprite.getDisplayedWidth() : 0;
    }

    public float getDisplayedHeight() {
        return sprite != null ? sprite.getDisplayedHeight() : 0;
    }
}