All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.almasb.fxgl.entity.EntityView.kt Maven / Gradle / Ivy

There is a newer version: 21.1
Show newest version
/*
 * FXGL - JavaFX Game Library. The MIT License (MIT).
 * Copyright (c) AlmasB ([email protected]).
 * See LICENSE for details.
 */

package com.almasb.fxgl.entity

import com.almasb.fxgl.core.Disposable
import com.almasb.fxgl.core.View
import com.almasb.sslogger.Logger
import javafx.collections.ObservableList
import javafx.scene.Node
import javafx.scene.Parent
import javafx.scene.shape.Circle

/**
 * Represents the visual aspect of an entity.
 * Note that the view need not be associated with an entity.
 *
 * @author Almas Baimagambetov (AlmasB) ([email protected])
 */
open class EntityView : Parent, View {

    companion object {
        protected val log = Logger.get()
    }

    /**
     * Returns nodes attached to this view.
     * Do NOT modify the list.
     *
     * @return list of children
     */
    val nodes: ObservableList
        get() = children

    constructor() {}

    /**
     * Constructs a view with given graphics content.
     *
     * @param graphics the view content
     */
    constructor(graphics: Node) {
        addNode(graphics)
    }

    /**
     * Add a child node to this view.
     *
     * @param node graphics
     */
    fun addNode(node: Node) {
        if (node is Circle) {
            node.centerX = node.radius
            node.centerY = node.radius
        }

        children.add(node)
    }

    /**
     * Removes a child node attached to this view.
     *
     * @param node graphics
     */
    fun removeNode(node: Node) {
        children.remove(node)
    }

    /**
     * Removes all attached nodes.
     */
    fun clearChildren() {
        children.clear()
    }

    override fun getNode(): Node {
        return this
    }

    override fun onUpdate(tpf: Double) {
    }

    override fun dispose() {
        // we only call dispose to let children to do manual cleanup
        // but we do not remove them from the parent
        // which would have been done by now by JavaFX
        for (n in children) {
            if (n is Disposable) {
                n.dispose();
            }
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy