Ejemplo n.º 1
0
    def tmatrix_to_position(self, transform):
        """Transforms a TMatrix into screen coordinates.

        Arguments:
            transform (TMatrix) -- A TMatrix created using the TMatrix constructor.
        """

        canvasWidth, canvasHeight = self.registry.currentWindow.get_size()

        localTransform = transform * self.transform.get_matrix_inverse()
        localTransform.set_scale(1)

        if localTransform.get_value("zp") > 0:
            zScreen = -localTransform.get_value("zp")

            screenPosition = TMatrix(0, 0, 0, 0, 0, 0, 0, 0, 0,
                                     localTransform.get_value("xp") * zScreen,
                                     localTransform.get_value("yp") * zScreen,
                                     0)

            #TODO: Proper clipping
            #if abs((screenPosition.get_value("xp") > canvasWidth) or (screenPosition.get_value("yp") > canvasHeight)):
            #    return False

            # Normalize screen pos

            # screenPosition.set_value("xp", (screenPosition.get_value("xp") + frameSize / 2) / frameSize)
            # screenPosition.set_value("yp", (screenPosition.get_value("yp") + frameSize / 2) / frameSize)
            screenPosition *= self.blockScale
            return ([
                int(screenPosition.get_value("xp") + canvasWidth / 2),
                int(screenPosition.get_value("yp") + canvasHeight / 2)
            ])

        return False
Ejemplo n.º 2
0
    def getMovementSet(self):
        transform = self.transform
        #print(pygame.mouse.get_rel())

        oldTransform = self.transform
        self.transform = TMatrix()
        return(transform)
Ejemplo n.º 3
0
        def get_vertex_screen_pos(x, y, z):
            vertexPosition = TMatrix(0, 0, 0, 0, 0, 0, 0, 0, 0, x, y, z)

            vertexPosition *= self.registry.currentScene.blocks[
                blockUuid].transform
            vertexPosition = self.tmatrix_to_position(vertexPosition)

            return vertexPosition
Ejemplo n.º 4
0
    def on_move(self, x, y):
        x *= self.registry.settings["MouseSpeed"]["Value"]
        y *= self.registry.settings["MouseSpeed"]["Value"]

        #TransformMatrixX = TMatrix()
        #TransformMatrixX.matrix = [
        #    [math.cos(math.radians(x)), -math.sin(math.radians(x)), 0, 0],
        #    [math.sin(math.radians(x)), math.cos(math.radians(x)), 0, 0],
        #    [0, 0, 1, 0],
        #    [0, 0, 0, 1]
        #]

        TransformMatrixX = TMatrix()
        TransformMatrixX.matrix = [
            [math.cos(math.radians(x)), 0, -math.sin(math.radians(x)), 0],
            [0, 1, 0, 0],
            [math.sin(math.radians(x)), 0, math.cos(math.radians(x)), 0],
            [0, 0, 0, 1]
        ]

        TransformMatrixY = TMatrix()
        TransformMatrixY.matrix = [
            [1, 0, 0, 0],
            [0, math.cos(math.radians(y)), math.sin(math.radians(y)), 0],
            [0, -math.sin(math.radians(y)), math.cos(math.radians(y)), 0],
            [0, 0, 0, 1]
        ]

        self.transform *= TransformMatrixX * TransformMatrixY
Ejemplo n.º 5
0
    def render3d(self):
        def get_vertex_screen_pos(x, y, z):
            vertexPosition = TMatrix(0, 0, 0, 0, 0, 0, 0, 0, 0, x, y, z)

            vertexPosition *= self.registry.currentScene.blocks[
                blockUuid].transform
            vertexPosition = self.tmatrix_to_position(vertexPosition)

            return vertexPosition

        # Reset scene
        self.registry.currentWindow.fill((0, 0, 0))

        for blockUuid in self.registry.currentScene.blocks:
            #print("found block")
            for vertexNumber in self.registry.currentScene.blocks[
                    blockUuid].obj["Vertices"]:
                vertexPosition = get_vertex_screen_pos(
                    self.registry.currentScene.blocks[blockUuid].
                    obj["Vertices"][vertexNumber][0],
                    self.registry.currentScene.blocks[blockUuid].
                    obj["Vertices"][vertexNumber][1],
                    self.registry.currentScene.blocks[blockUuid].
                    obj["Vertices"][vertexNumber][2])

                # print("Drawing point at " + str(vertexPosition))

                if vertexPosition:
                    pygame.draw.circle(self.registry.currentWindow,
                                       (50, 50, 50), vertexPosition, 2)

                    # Draw secondary vertex pos with line
                    for secondaryVertexNumber in self.registry.currentScene.blocks[
                            blockUuid].obj["Vertices"]:
                        secondaryVertexPosition = get_vertex_screen_pos(
                            self.registry.currentScene.blocks[blockUuid].
                            obj["Vertices"][secondaryVertexNumber][0],
                            self.registry.currentScene.blocks[blockUuid].
                            obj["Vertices"][secondaryVertexNumber][1],
                            self.registry.currentScene.blocks[blockUuid].
                            obj["Vertices"][secondaryVertexNumber][2])

                        if secondaryVertexPosition:
                            pygame.draw.line(self.registry.currentWindow,
                                             (255, 255, 255), vertexPosition,
                                             secondaryVertexPosition)

        self.tmatrix_to_position(
            TMatrix(0, 0, 0, 0, 0, 0, 0, 0, 0, 200, 100, 200))
Ejemplo n.º 6
0
    def key_down(self, event):
        if event[pygame.K_SPACE]:
            self.transform *= TMatrix(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0.1, 0)
        if event[pygame.key.get_mods() & pygame.KMOD_SHIFT]:
            self.transform *= TMatrix(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -0.1, 0)
        if event[pygame.K_w]:
            self.transform *= TMatrix(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -0.1)
        if event[pygame.K_a]:
            self.transform *= TMatrix(0, 0, 0, 0, 0, 0, 0, 0, 0, 0.1, 0, 0)
        if event[pygame.K_s]:
            self.transform *= TMatrix(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0.1)
        if event[pygame.K_d]:
            self.transform *= TMatrix(0, 0, 0, 0, 0, 0, 0, 0, 0, -0.1, 0, 0)
        if event[pygame.K_f]:
            TransformMatrix = TMatrix()
            TransformMatrix.matrix = [
                [1, 0, 0, 0],
                [0, math.cos(math.radians(10)), math.sin(math.radians(10)), 0],
                [0, -math.sin(math.radians(10)), math.cos(math.radians(10)), 0],
                [0, 0, 0, 1]
            ]

            self.transform *= TransformMatrix
Ejemplo n.º 7
0
 def __init__(self, registry):
     self.transform = TMatrix()
     self.registry = registry
Ejemplo n.º 8
0
# Debug

from Blocks.Block import Block

# # # Main Code # # #

pygame.init()

window = pygame.display.set_mode((1520, 800))
pygame.display.set_caption("Pycraft")

# Set up registry

gameRegistry = Registry()

gameRegistry.currentCamera = Camera(90, TMatrix(), gameRegistry)
gameRegistry.currentController = MovementController(gameRegistry)
gameRegistry.currentMainMenu = MainMenu(gameRegistry)
gameRegistry.currentScene = Scene()

gameRegistry.currentWindow = window

gameRegistry.LastRun = datetime.datetime.now()
gameRegistry.Run = CEnum.GameState.Active

gameRegistry.GameScene = CEnum.GameScene.MainMenu

# Debug generate blocks

Block(TMatrix(0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0), gameRegistry.currentScene)
Block(TMatrix(0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1), gameRegistry.currentScene)