コード例 #1
0
    public void Awake()
    {
        if (Instance != null)
        {
            Debug.LogError("GlobalJigsawSettings was constructed with an instance already in existence.");
            return;
        }
        Instance = this;

        MouseWorldHeight = PuzzlePieceSelectedHeight + 0.2f; // Add the thickness of the puzzle piece mesh
    }
コード例 #2
0
 private void Update()
 {
     if (isServer)
     {
         GlobalJigsawSettings settings = GlobalJigsawSettings.Get();
         float newX = Mathf.Clamp(transform.position.x, settings.PuzzleBoardBoundsX.x, settings.PuzzleBoardBoundsX.y);
         float newY = Mathf.Clamp(transform.position.y, settings.PuzzleBoardBoundsY.x, settings.PuzzleBoardBoundsY.y);
         float newZ = Mathf.Clamp(transform.position.z, settings.PuzzleBoardBoundsZ.x, settings.PuzzleBoardBoundsZ.y);
         if (transform.position.x != newX || transform.position.y != newY || transform.position.z != newZ)
         {
             transform.position = new Vector3(newX, newY, newZ);
         }
     }
 }
コード例 #3
0
    public void SelectedBy(int PlayerId)
    {
        PlayerControllerId = PlayerId;

        // Round yaw to the nearest 90 degrees
        float yaw      = transform.eulerAngles.y;
        float finalYaw = Mathf.Round(yaw / 90.0f) * 90.0f;

        if (yaw != finalYaw)
        {
            transform.Rotate(Vector3.up, finalYaw - yaw, Space.World);
        }

        // Set the height appropriately
        Vector3 pos = transform.position;

        pos.y = GlobalJigsawSettings.Get().PuzzlePieceSelectedHeight;
        transform.position = pos;

        Rbody.useGravity = false;
    }
コード例 #4
0
    void GeneratePuzzlePieces(Texture2D PuzzleTexture)
    {
        if (Pieces != null)
        {
            Debug.LogWarning("GeneratePuzzlePieces was called after Pieces array was already initialized");
        }
        Pieces = new PuzzlePiece[GridWidth * GridHeight];

        // TODO: choose a different random seed, possibly from user input
        PuzzleRandomizer.InitializeRandomizer(0, GridWidth, GridHeight);

        // Compute the scale and offset, cutting off edges from the texture as needed (rather than letterboxing)
        // Texture UV coords are [0,0] in the lower-left to [1,1] in the upper-right
        float   pieceScaleX;
        float   pieceScaleY;
        Vector2 puzzleRootUV;
        float   wScale = (float)PuzzleTexture.width / (float)GridWidth;
        float   hScale = (float)PuzzleTexture.height / (float)GridHeight;

        if (wScale < hScale)
        {
            pieceScaleX  = 1.0f / (float)GridWidth;
            pieceScaleY  = ((float)PuzzleTexture.width / (float)(PuzzleTexture.height)) * pieceScaleX;
            puzzleRootUV = new Vector2(0.0f, 0.5f * (1.0f - GridHeight * pieceScaleY)); // TODO
        }
        else
        {
            pieceScaleY  = 1.0f / (float)GridHeight;
            pieceScaleX  = ((float)PuzzleTexture.height / (float)(PuzzleTexture.width)) * pieceScaleY;
            puzzleRootUV = new Vector2(0.5f * (1.0f - GridWidth * pieceScaleX), 0.0f); // TODO
        }

        // Spawn and initialize puzzle pieces
        for (uint y = 0; y < GridHeight; ++y)
        {
            for (uint x = 0; x < GridWidth; ++x)
            {
                int        ind      = (int)(y * GridWidth + x);
                Quaternion spawnRot = new Quaternion();
                spawnRot.eulerAngles = new Vector3(270, 0, 0);

                PuzzlePiece piece = Instantiate(PuzzlePiecePrefab, Vector3.zero, spawnRot).GetComponent <PuzzlePiece>();
                PuzzleRandomizer.SetupPiece(ref piece, x, y);
                piece.SetMaterialInfo(PuzzleTexture, pieceScaleX, pieceScaleY, puzzleRootUV.x + x * pieceScaleX, puzzleRootUV.y + y * pieceScaleY);
                piece.SetId(ind);
                Pieces[ind] = piece;
            }
        }

        GlobalJigsawSettings settings = FindObjectOfType <GlobalJigsawSettings>();
        Rect puzzleRegion             = new Rect(
            -0.5f * PuzzleSpaceGridScale * GridWidth,
            -0.5f * PuzzleSpaceGridScale * GridHeight,
            PuzzleSpaceGridScale * GridWidth,
            PuzzleSpaceGridScale * GridHeight);
        float minX       = Mathf.Max(settings.PuzzleBoardBoundsX.x, -SpawnSpaceGridScale * GridWidth);
        float maxX       = Mathf.Min(settings.PuzzleBoardBoundsX.y, SpawnSpaceGridScale * GridWidth);
        float minZ       = Mathf.Max(settings.PuzzleBoardBoundsZ.x, -SpawnSpaceGridScale * GridHeight);
        float maxZ       = Mathf.Min(settings.PuzzleBoardBoundsZ.y, SpawnSpaceGridScale * GridHeight);
        Rect  playRegion = new Rect(minX, minZ, maxX - minX, maxZ - minZ);

        ScatterPiecesRandomly(puzzleRegion, playRegion, Pieces);

        //AlignPiecesNeatly(new Vector3(-0.5f * GridWidth, 0.0f, -0.5f * GridHeight), Pieces);
        foreach (PuzzlePiece piece in Pieces)
        {
            NetworkServer.Spawn(piece.gameObject);
        }
    }