Esempio n. 1
0
    private void MovePiece()
    {
        if (thisTransform.position != ParentTile.thisTransform.position)
        {
            Tile    targetTile  = null;
            APiece  targetPiece = null;
            Vector3 moveVector  = Vector3.zero;

            if (!IsMoving)
            {
                IsMoving = true;
                BoardData.Instance.Moving++;
                Velocity = 0.0f;
            }

            targetTile = ParentTile.GravityTargetTile;

            targetPiece = targetTile ? targetTile.Piece : null;

            MatchTargetPieceVelocity(targetPiece);
            AcceleratePiece();

            FixPiecePosition();

            moveVector              = moveDirection(moveVector);
            moveVector              = moveVector.normalized * Velocity;
            thisTransform.position += moveVector * Time.deltaTime;
        }
        else
        {
            // Rechecks the gravity on the current parent tile as the piece falls
            // Makes the pieces falls smoothly from tile to tile
            ParentTile.GravityHandler();

            if (IsMoving)
            {
                IsMoving = false;
                Velocity = 0;
                BoardData.Instance.Moving--;
            }
        }
    }
    public static void CreateUnit(string unitName, string unitType, UnityEngine.Object unitSprite)
    {
        ///<summary>
        /// This function will create a new game object, assign it the components our Tile needs then save the object as a prefab
        /// so we can use it later.
        /// </summary>

        // Force our prefab name to a specific syntax. No spaces, no hyphens
        name = unitName.Replace(" ", "");
        name = name.Replace("-", "");

        // Create a filepath for the prefab using it's name.
        string prefabPath = "Assets/Prefabs/Units/" + name + ".prefab";

        // create a new game object. We need to do this so we can add components to it then store it as a prefab.
        newObj = new GameObject();
        newObj.transform.parent = GameObject.FindGameObjectWithTag("Grid").transform;         // force gameobject's parent to be the grid.

        // Start adding components to the prefab
        // First, we need a RectTransform since the Tile will be a UI object attached to a canvas.
        RectTransform newRect = newObj.AddComponent <RectTransform>();

        newRect.anchorMin     = new Vector2(0, 1);     // this will set our anchoring. By default, we want the anchor to be top-left.
        newRect.anchorMax     = new Vector2(0, 1);
        newRect.sizeDelta     = new Vector2(120, 120); // default the width and height of the tile to 120 x 120
        newRect.localPosition = new Vector3(0, 0, 90); // make sure the local position is 0, 0, 90 by default.

        Image newImage = newObj.AddComponent <Image>();

        newImage.sprite = (Sprite)unitSprite;         // set the Image sprite to the sprite the user specified from the popup window
        // The only reason we have a sprite renderer on the tile is so that the sprite shows up in the editor for the prefab. Without this, we would not be able to see the sprite attached to this game object in the inspector.
        SpriteRenderer newRend = newObj.gameObject.AddComponent <SpriteRenderer>();

        newRend.sprite = (Sprite)unitSprite;

        LayoutElement newLayoutElement = newObj.gameObject.AddComponent <LayoutElement>();

        newLayoutElement.ignoreLayout = true;         // make sure the LayoutElement ignores layout by default.

        Rigidbody2D newRigidbody = newObj.AddComponent <Rigidbody2D>();

        newRigidbody.gravityScale = 0f;

        // Tile needs a collider on it to detect unit movements.
        BoxCollider2D newCollider = newObj.gameObject.AddComponent <BoxCollider2D>();

        newCollider.isTrigger = true;                                                                               // set the collider to be a trigger.
        newCollider.size      = GameObject.FindGameObjectWithTag("Grid").GetComponent <GridLayoutGroup>().cellSize; // set collider size to the cell size of our tiles

        // Add ParentTile script. This is used for placing tiles in the editor and making sure their positions
        // snap to the grid properly.
        ParentTile newParentTile = newObj.gameObject.AddComponent <ParentTile>();

        newParentTile.rows    = 8;      // set default rows and columns to 9 and 16 since that is what our 120x120 tile size gets us at 16:9 aspect ratio
        newParentTile.columns = 16;

        newObj.AddComponent <Unit>();

        /*if (unitType == "infantry")
         *      newObj.AddComponent<Infantry>();
         * else if (unitType == "tank")
         *      newObj.AddComponent<Tank>();
         * else if (unitType == "helicopter")
         *      newObj.AddComponent<Helicopter>();*/

        newObj.tag = "Unit";                                                    // set the object's tag appropraitely. Tagging is important for defense, attack, and movement mods.

        newObj.name = unitName;                                                 // set prefab name

        PrefabUtility.CreatePrefab(prefabPath, newObj.gameObject);              // create prefab at the path we made earlier using our gameobject.
        GameObject.DestroyImmediate(newObj.gameObject);                         // Once prefab is saved, destroy the game object from our scene.

        EditorUtility.FocusProjectWindow();                                     // force the Editor to focus on project window
        Selection.activeObject = AssetDatabase.LoadMainAssetAtPath(prefabPath); // force the Editor to navigate to the new file that was just created.
    }