<reponame>wkta/Python-Bataille-Navale
#  Copyright © 2019 <NAME>.
from copy import copy

from engine.logic.Math import Mat3x3, Vector2, IDENTITY_MATRIX, ZERO_VECTOR, UNIT_VECTOR

class Transform:
    """
    Simple transform.
    Used to give information about the objects in the scene.

    Attributes:
        parent      Parent transform of this object.
        position    Position of the transform in parent-relative space.
        rotation    Rotation of the transform in parent-relative space.
        scale       Scale of the transform in parent-relative space.
        gameobject  GameObject instance attached to this transform.
        children    List of all the children of this transform.
    """

    def __init__(self, parent, position=ZERO_VECTOR, offset=ZERO_VECTOR, rotation=0, scale=UNIT_VECTOR, gameobject=None):
        """
        Class constructor.
        Creates a new Transform instance.
        :param parent: The parent of this transform.
        :param offset: The offset of the transform.
        :param position: The position of this transform.
        :param rotation: The rotation of this transform.
        :param scale: The scale of this transform.
        :param gameobject: The game object that this transform represents.
        """
        self.parent = parent
        self.offset = copy(offset)
        self.position = copy(position)
        self.rotation = rotation
        self.scale = scale
        self.gameobject = gameobject
        self.children = []

        # If there is a parent.
        if parent is not None:
            # Append ourselves to the children list.
            parent.children.append(self)

    def apply(self, position, world=False):
        """
        Applies the transform to the specified position.
        :param position: The position to apply the transform to.
        :param world: If True, applies world transformation instead of the local one.
        :returns: The transformed position.
        """
        return (self.get_world_matrix() if world else self.get_matrix()) * position

    def get_matrix(self, ignore_position=False, ignore_rotation=False, ignore_scale=True):
        """
        Returns the matrix generated by this transform.
        """
        return Mat3x3.create_matrix(
            ZERO_VECTOR if ignore_position else self.position,
            0 if ignore_rotation else self.rotation,
            UNIT_VECTOR if ignore_scale else self.scale)

    def get_world_matrix(self):
        """
        Returns the 3x3 matrix of this transform in world space.
        """
        return (self.parent.get_world_matrix() if self.parent is not None else IDENTITY_MATRIX) * self.get_matrix()

    def get_world_position(self):
        """
        Returns the world position of this transform.
        """
        # Compute the position of the center.
        center = (self.parent.get_world_matrix() if self.parent is not None else IDENTITY_MATRIX) * self.position
        offset = self.get_matrix(ignore_position=True) * self.offset
        return center + offset

    def set_world_position(self, position):
        """
        Defines the world position of the object.
        :param position: The position the object is expected to be in.
        """
        self.position = position - (self.parent.get_world_position() if self.parent is not None else ZERO_VECTOR)

    def get_world_rotation(self):
        """
        Returns the world rotation of this transform.
        """
        return self.rotation + (self.parent.get_world_rotation() if self.parent is not None else 0)

    def get_world_scale(self):
        """
        Returns the world scale of this transform.
        """
        return self.scale * (self.parent.get_world_scale() if self.parent is not None else UNIT_VECTOR)

#  Copyright © 2019 <NAME>.
from copy import copy

from engine.logic.Math import Mat3x3, Vector2, IDENTITY_MATRIX, ZERO_VECTOR, UNIT_VECTOR

class Transform:
    """
    Simple transform.
    Used to give information about the objects in the scene.

    Attributes:
        parent      Parent transform of this object.
        position    Position of the transform in parent-relative space.
        rotation    Rotation of the transform in parent-relative space.
        scale       Scale of the transform in parent-relative space.
        gameobject  GameObject instance attached to this transform.
        children    List of all the children of this transform.
    """

    def __init__(self, parent, position=ZERO_VECTOR, offset=ZERO_VECTOR, rotation=0, scale=UNIT_VECTOR, gameobject=None):
        """
        Class constructor.
        Creates a new Transform instance.
        :param parent: The parent of this transform.
        :param offset: The offset of the transform.
        :param position: The position of this transform.
        :param rotation: The rotation of this transform.
        :param scale: The scale of this transform.
        :param gameobject: The game object that this transform represents.
        """
        self.parent = parent
        self.offset = copy(offset)
        self.position = copy(position)
        self.rotation = rotation
        self.scale = scale
        self.gameobject = gameobject
        self.children = []

        # If there is a parent.
        if parent is not None:
            # Append ourselves to the children list.
            parent.children.append(self)

    def apply(self, position, world=False):
        """
        Applies the transform to the specified position.
        :param position: The position to apply the transform to.
        :param world: If True, applies world transformation instead of the local one.
        :returns: The transformed position.
        """
        return (self.get_world_matrix() if world else self.get_matrix()) * position

    def get_matrix(self, ignore_position=False, ignore_rotation=False, ignore_scale=True):
        """
        Returns the matrix generated by this transform.
        """
        return Mat3x3.create_matrix(
            ZERO_VECTOR if ignore_position else self.position,
            0 if ignore_rotation else self.rotation,
            UNIT_VECTOR if ignore_scale else self.scale)

    def get_world_matrix(self):
        """
        Returns the 3x3 matrix of this transform in world space.
        """
        return (self.parent.get_world_matrix() if self.parent is not None else IDENTITY_MATRIX) * self.get_matrix()

    def get_world_position(self):
        """
        Returns the world position of this transform.
        """
        # Compute the position of the center.
        center = (self.parent.get_world_matrix() if self.parent is not None else IDENTITY_MATRIX) * self.position
        offset = self.get_matrix(ignore_position=True) * self.offset
        return center + offset

    def set_world_position(self, position):
        """
        Defines the world position of the object.
        :param position: The position the object is expected to be in.
        """
        self.position = position - (self.parent.get_world_position() if self.parent is not None else ZERO_VECTOR)

    def get_world_rotation(self):
        """
        Returns the world rotation of this transform.
        """
        return self.rotation + (self.parent.get_world_rotation() if self.parent is not None else 0)

    def get_world_scale(self):
        """
        Returns the world scale of this transform.
        """
        return self.scale * (self.parent.get_world_scale() if self.parent is not None else UNIT_VECTOR)
#  Copyright © 2019 <NAME>. Simple transform.     Used to give information about the objects in the scene.      Attributes:         parent      Parent transform of this object.         position    Position of the transform in parent-relative space.         rotation    Rotation of the transform in parent-relative space.         scale       Scale of the transform in parent-relative space.         gameobject  GameObject instance attached to this transform.         children    List of all the children of this transform. Class constructor.         Creates a new Transform instance.         :param parent: The parent of this transform.         :param offset: The offset of the transform.         :param position: The position of this transform.         :param rotation: The rotation of this transform.         :param scale: The scale of this transform.         :param gameobject: The game object that this transform represents. # If there is a parent. # Append ourselves to the children list. Applies the transform to the specified position.         :param position: The position to apply the transform to.         :param world: If True, applies world transformation instead of the local one.         :returns: The transformed position. Returns the matrix generated by this transform. Returns the 3x3 matrix of this transform in world space. Returns the world position of this transform. # Compute the position of the center. Defines the world position of the object.         :param position: The position the object is expected to be in. Returns the world rotation of this transform. Returns the world scale of this transform.