Esempio n. 1
0
    private void CreateGameObjectFromPrefab(Vector3Int position, PrefabTile prefabTile)
    {
        var instance = PrefabUtility.InstantiatePrefab(prefabTile.prefab,
                                                       parent: transform) as GameObject;

        if (instance == null)
        {
            return;
        }

        instance.transform.position = grid.CellToWorld(position) + new Vector3(.5f, 0f, .5f);
        instance.transform.rotation = Quaternion.identity;
        gameObjects.Add(position, instance);

        var pawn = instance.GetComponent <Pawn>();

        if (pawn != null)
        {
            pawn.position = position;
        }
        else
        {
            var tile = instance.GetComponent <Tile>();
            if (tile != null)
            {
                tile.position = position;
            }
        }
    }
Esempio n. 2
0
    void Update()
    {
        Ray        ray = Camera.main.ScreenPointToRay(Input.mousePosition);
        RaycastHit hitInfo;

        if (GetComponent <Collider>().Raycast(ray, out hitInfo, Mathf.Infinity))
        {
            PrefabTile prefabTile = hitInfo.transform.GetComponent <PrefabTile>();
            if (prefabTile.mNetData.mX == mNetData.mX && mNetData.mY == prefabTile.mNetData.mY)
            {
                if (Input.GetMouseButtonDown(0))
                {
                    mDownTime = Time.realtimeSinceStartup;
                    mDownPos  = Input.mousePosition;
                }
                else if (Input.GetMouseButtonUp(0))
                {
                    float distance = (Input.mousePosition - mDownPos).magnitude;
                    if (Time.realtimeSinceStartup - mDownTime > 0.04f && distance < 0.5f) /*点击的时候触发*/
                    {
                        if (onTileClick != null)
                        {
                            onTileClick(gameObject);
                        }
                    }
                }
            }
        }
    }
Esempio n. 3
0
 public bool TryRemoveTile(Vector3Int position, out PrefabTile tile)
 {
     tile = placeables.GetTile(position) as PrefabTile;
     if (tile == null)
     {
         return(false);
     }
     else
     {
         placeables.SetTile(position, null);
         return(true);
     }
 }
Esempio n. 4
0
    private BaseTileData GetBaseTileData(Vector3Int currentPosition)
    {
        ITilemap tilemap   = null;
        TileBase foundTile = m_baseTileMap.GetTile(currentPosition);

        if (foundTile.GetType() == typeof(PrefabTile))
        {
            PrefabTile tile = foundTile as PrefabTile;
            return(tile.GetBaseTileObject(currentPosition).GetComponent <BaseTileData>());
        }
        //foundTile.GetTileData(currentPosition, tilemap, ref m_activeTile);
        //return m_activeTile.gameObject.GetComponent<BaseTileData>();
        return(null);
    }
Esempio n. 5
0
    /*注: 这里可以采用对象池的方式处理,没必要每次都删除,另外这里也可以考虑在协成里面处理,如果是单纯数据的话,另启动一个线程处理数据也是可以的*/
    private void DrawMap(List <NetData> netDataList)
    {
        for (int i = 0; i < transform.childCount; i++)
        {
            GameObject obj = transform.GetChild(i).gameObject;
            if (obj != null)
            {
                PrefabTile prefabTile = obj.GetComponent <PrefabTile>();
                if (prefabTile != null)
                {
                    prefabTile.onTileClick  -= HandleTileClick;
                    prefabTile.onTileUpdate -= HandleTileUpdate;
                }
                GameObject.Destroy(obj);
            }
        }

        mMapGameObjectList.Clear();

        for (int i = 0; i < netDataList.Count; i++)
        {
            /*注:这里在真实项目中需要用对象池做处理*/
            GameObject prefabObj = Resources.Load(netDataList[i].mTileName) as GameObject;
            if (prefabObj != null)
            {
                GameObject obj = GameObject.Instantiate(prefabObj);
                if (obj != null)
                {
                    obj.transform.parent     = transform;
                    obj.transform.localScale = new Vector3(1.0f, 1.0f, 1.0f);
                    obj.transform.position   = new Vector3(netDataList[i].mX, netDataList[i].mY);
                    obj.name = "[" + netDataList[i].mX + "," + netDataList[i].mY + "]";

                    PrefabTile prefabTile = obj.GetComponent <PrefabTile>();
                    if (prefabTile != null)
                    {
                        prefabTile.SetData(netDataList[i]);
                        prefabTile.onTileClick  += HandleTileClick;
                        prefabTile.onTileUpdate += HandleTileUpdate;
                        mMapGameObjectList.Add(prefabTile);
                    }
                }
            }
            else
            {
                Debug.LogError(netDataList[i].mTileName + " 不存在!!!!");
            }
        }
    }
Esempio n. 6
0
    /// <summary>
    /// Executes an Action depending on the CurrentEquipped Item. (Build or use Weapon)
    /// </summary>
    internal void ProcessAction()
    {
        if (Attributes.IsAlive)
        {
            IInventoryItem currentItem = Attributes.CurrentEquippedItem;
            if (currentItem is InventoryTile)
            {
                InventoryTile inventoryTile = (InventoryTile)currentItem;
                PrefabTile    tileToBuild   = inventoryTile.Tile;

                if (resourceManager.ConstructionMaterialAvailable >= tileToBuild.BuildingCosts)
                {
                    if (!tileController.TryAddTile(tileToBuild, transform.position))
                    {
                        var fadeout = Instantiate(fadeoutTextPrefab, transform);
                        fadeout.TextMesh.text  = "#@!&$%";
                        fadeout.TextMesh.color = new Color(1.0f, 0.0f, 0.0f, 1);
                    }
                    // Consume should always work in this case, because we checked for available resources
                    else if (!resourceManager.TryConsume(tileToBuild.BuildingCosts))
                    {
                        Debug.LogError("Could not consume resources for building even though enough resources were available");
                    }
                    else
                    {
                        // Success, create building
                        var fadeout = Instantiate(fadeoutTextPrefab, transform);
                        fadeout.TextMesh.text  = $"-{(float)tileToBuild.BuildingCosts}";
                        fadeout.TextMesh.color = new Color(0.6392157F, 0.5019608F, 0.3892157F, 1.0F);
                    }
                }
                else
                {
                    var fadeout = Instantiate(fadeoutTextPrefab, transform);
                    fadeout.TextMesh.text  = "Not enought Resources!";
                    fadeout.TextMesh.color = new Color(1.0f, 0.0f, 0.0f, 1);
                }
            }
            else if (currentItem is InventoryWeapon)
            {
                InventoryWeapon inventoryWeapon = (InventoryWeapon)currentItem;
                inventoryWeapon.Weapon.GetComponent <IPlayerWeapon>().Fire();
            }
        }
    }
Esempio n. 7
0
        public override void Paint(GridLayout grid, GameObject brushTarget, Vector3Int position)
        {
            // Do not allow editing palettes
            if (brushTarget.layer == 31)
            {
                return;
            }

            var placementPosition = grid.LocalToWorld(grid.CellToLocalInterpolated(new Vector3Int(position.x, position.y, m_Z) + new Vector3(.5f, .5f, .5f)));

            var cell = GetCellAtPoint(placementPosition);

            // Don't place cell prefab if overlap not allowed and one already exists
            if (!allowOverlap && cell)
            {
                return;
            }

            // If overlap is allowed, then destroy the previous cell before placing prefab
            if (cell)
            {
                Erase(grid, brushTarget, position);
            }

            //int index = Mathf.Clamp(Mathf.FloorToInt(GetPerlinValue(position, m_PerlinScale, k_PerlinOffset)*m_Prefabs.Length), 0, m_Prefabs.Length - 1);

            PrefabTile tile     = (PrefabTile)base.cells[0].tile;
            GameObject prefab   = tile.TileAssociatedPrefab;
            GameObject instance = (GameObject)PrefabUtility.InstantiatePrefab(prefab);

            if (instance != null)
            {
                Undo.MoveGameObjectToScene(instance, brushTarget.scene, "Paint Prefabs");
                Undo.RegisterCreatedObjectUndo((Object)instance, "Paint Prefabs");
                instance.transform.SetParent(brushTarget.transform);
                instance.transform.position = placementPosition + new Vector3(0f, 0f, m_Z);
            }
        }
    public override void Pick(GridLayout gridLayout, GameObject brushTarget, BoundsInt position, Vector3Int pivot)
    {
        // Want to make sure people can only pick one prefab at a time
        //BoundsInt bounds = new BoundsInt(min, randomBrush.size);
        //if(position.allPositionsWithin.MoveNext() == position.allPositionsWithin.)
        //{
        //    Debug.LogWarning("You can only pick one tile at a time with sortedTileBrush!");
        //    return;
        //}

        Vector3Int brushPosition = new Vector3Int(position.position.x, position.position.y, 0);
        Tilemap    map           = brushTarget.GetComponent <Tilemap>();
        PrefabTile tile          = map.GetTile <PrefabTile>(brushPosition);

        if (tile)
        {
            tilePrefab = tile.tilePrefab;
        }
        else
        {
            Debug.Log("Brush position: " + brushPosition);
        }
    }
Esempio n. 9
0
 public InventoryTile(PrefabTile tile)
 {
     this.Tile = tile;
 }
Esempio n. 10
0
    public static void LoadTiles()
    {
        PrefabTile tile = new PrefabTile();
        TextAsset  txt  = Resources.Load("Meta/Tiles") as TextAsset;

        string[] lines = txt.text.Split("\n"[0]);
        string   s;
        int      n = 0;
        int      t = 0;

        for (int l = 0; l < lines.Length; l++)
        {
            s = lines[l];

            /* Name */
            if (n == 0)
            {
                tile      = new PrefabTile();
                tile.name = s;
            }

            /* Mesh */
            if (n == 1)
            {
                tile.obj = Resources.Load("Models/Tiles/" + s.Substring(0, s.Length - 5)) as GameObject;
            }

            /* Attribute */
            if (n == 2)
            {
                tile.att = int.Parse(s);
            }

            /* Material */
            if (n == 3)
            {
                tile.mat = int.Parse(s);
            }

            /* Delphi RX */
            if (n == 4)
            {
                tile.d_rx = float.Parse(s);
            }

            /* Delphi RY */
            if (n == 5)
            {
                tile.d_ry = float.Parse(s);
            }

            /* Delphi RZ */
            if (n == 6)
            {
                tile.d_rz = float.Parse(s);
            }

            /* Unity RX */
            if (n == 7)
            {
                tile.u_rx = float.Parse(s);
            }

            /* Unity RY */
            if (n == 8)
            {
                tile.u_ry = float.Parse(s);
            }

            /* Unity RZ */
            if (n == 9)
            {
                tile.u_rz = float.Parse(s);
            }

            /* Delphi Scale */
            if (n == 10)
            {
                tile.d_s = float.Parse(s);
            }

            /* Unity Scale */
            if (n == 11)
            {
                tile.u_s = float.Parse(s);
            }

            /* Increment */
            n++;
            if (n == 12)
            {
                n = 0;
                Manager.instance.prefab_tiles[t] = tile;
                //Debug.Log(Manager.instance.prefab_tiles[t]);
                t++;
            }
        }
    }
Esempio n. 11
0
 public bool TryRemoveTile(Vector3 position, out PrefabTile tile) =>
 TryRemoveTile(placeables.WorldToCell(position), out tile);
Esempio n. 12
0
 private void OnEnable()
 {
     tgt = (PrefabTile)target;
 }