public override void OnUpdatedNeighbour(PlacedTile placedTile, params PlacedTile[] neighbours)
 {
     if (placeAgainOnUpdate)
     {
         Place(placedTile);
     }
 }
        public PlacedTile[] GetNeighbours(PlacedTile target, int layerIndex)
        {
            List <PlacedTile> neighbs = new List <PlacedTile>();
            // for now only get direct neighbours
            PlacedTile neighb;

            if (layers[layerIndex].TryGetValue(target.position + new int3(-1, 0, 0), out neighb))
            {
                neighbs.Add(neighb);
            }
            if (layers[layerIndex].TryGetValue(target.position + new int3(1, 0, 0), out neighb))
            {
                neighbs.Add(neighb);
            }
            if (layers[layerIndex].TryGetValue(target.position + new int3(0, 0, -1), out neighb))
            {
                neighbs.Add(neighb);
            }
            if (layers[layerIndex].TryGetValue(target.position + new int3(0, 0, 1), out neighb))
            {
                neighbs.Add(neighb);
            }

            return(neighbs.ToArray());
        }
Ejemplo n.º 3
0
 public void PlaceBehaviour(PlacedTile placed, params PlacedTile[] neighbours)
 {
     foreach (var behaviour in behaviours)
     {
         behaviour.OnPlacement(placed, neighbours);
     }
 }
Ejemplo n.º 4
0
 public void UpdateBehaviour(PlacedTile placed, params PlacedTile[] neighbours)
 {
     foreach (var behaviour in behaviours)
     {
         behaviour.OnUpdatedNeighbour(placed, neighbours);
     }
 }
        public override void OnUpdatedNeighbour(PlacedTile placedTile, params PlacedTile[] neighbours)
        {
#if UNITY_EDITOR
            if (rotation == RotationBehaviour.AlignToNeighbours)
            {
                AlignRotationToNeighbours(placedTile, true, neighbours);
            }
#endif
        }
        public override void OnPlacement(PlacedTile placedTile, params PlacedTile[] neighbours)
        {
            Vector3 rasterSize = placedTile.layer.rasterSize;

            placedTile.placedObject.transform.position += new Vector3(
                placedTile.layer.rasterSize.x * (Random.value - 0.5f) * xRange,
                placedTile.layer.rasterSize.y * (Random.value) * yRange,
                placedTile.layer.rasterSize.z * (Random.value - 0.5f) * zRange
                );
        }
        public override void OnPlacement(PlacedTile placedTile, params PlacedTile[] neighbours)
        {
#if UNITY_EDITOR
            switch (rotation)
            {
            case RotationBehaviour.RotateRandomY:
                placedTile.placedObject.transform.localRotation = Quaternion.Euler(0f, UnityEngine.Random.value * 360f, 0f);
                break;

            case RotationBehaviour.RotateRandomY90Degree:
                placedTile.placedObject.transform.localRotation = Quaternion.Euler(0f, UnityEngine.Random.Range(0, 4) * 90f, 0f);
                break;

            case RotationBehaviour.AlignToNeighbours:
                AlignRotationToNeighbours(placedTile, false, neighbours);
                break;
            }
#endif
        }
        public void Place(PlacedTile placedTile)
        {
            var myColliders     = placedTile.placedObject.GetComponentsInChildren <Collider>();
            var placedTransform = placedTile.placedObject.transform;
            var startPoint      = new Vector3(
                placedTransform.position.x,
                (placedTile.position.y + 1) * placedTile.layer.rasterSize.y,
                placedTransform.position.z);
            float closestDistance = placedTile.layer.rasterSize.y;

            foreach (var hit in Physics.RaycastAll(startPoint, Vector3.down, placedTile.layer.rasterSize.y))
            {
                if (myColliders.Contains(hit.collider))
                {
                    continue;
                }
                closestDistance = Mathf.Min(closestDistance, hit.distance);
            }

            placedTransform.position = startPoint + Vector3.down * closestDistance;
        }
        public void RemoveTile(PlacedTile target, int layerIndex)
        {
            Undo.RegisterCompleteObjectUndo(this, "Before Removed TileObject");
            if (target.placedObject)
            {
                Undo.DestroyObjectImmediate(target.placedObject);
            }

            //DestroyImmediate(target.placedObject);
            int3 tileSize = target.tileObject.GetSize(target.layer.rasterSize);

            for (int x2 = 0; x2 < tileSize.x; x2++)
            {
                for (int y2 = 0; y2 < tileSize.y; y2++)
                {
                    for (int z2 = 0; z2 < tileSize.z; z2++)
                    {
                        layers[layerIndex].Remove(target.position + new int3(x2, y2, z2));
                    }
                }
            }
        }
Ejemplo n.º 10
0
 public abstract void OnUpdatedNeighbour(PlacedTile placedTile, params PlacedTile[] neighbours);
Ejemplo n.º 11
0
 public abstract void OnPlacement(PlacedTile placedTile, params PlacedTile[] neighbours);
 public override void OnUpdatedNeighbour(PlacedTile placedTile, params PlacedTile[] neighbours)
 {
     //throw new System.NotImplementedException();
 }
 public override void OnPlacement(PlacedTile placedTile, params PlacedTile[] neighbours)
 {
     Place(placedTile);
 }
        public void PlaceTile(string guid, int3 position, Vector3 euler, int layerIndex, TileAtlas.TagLayer tagLayer, bool updateNeighbours = true)
        {
            Undo.RegisterCompleteObjectUndo(this, "Created New Tile Object");
            TileObject atlasTile;

            tileSet.TileFromGuid.TryGetValue(guid, out atlasTile);
            if (!atlasTile)
            {
                Debug.LogError("No Tile with guid [" + guid + "] found in atlas " + tileSet.name, tileSet); return;
            }
            var go = (GameObject)PrefabUtility.InstantiatePrefab(atlasTile.prefab) as GameObject;

            if (!go)
            {
                Debug.LogError("No GameObject found at Tile with guid " + guid); return;
            }

            if (layerIndex >= layers.Count)
            {
                for (int i = 0; i <= layerIndex - layers.Count; i++)
                {
                    layers.Add(new IntTileDictionary());
                }
            }

            int3 tileSize = atlasTile.GetSize(tagLayer.rasterSize);

            go.transform.SetParent(transform);
            if (!hierarchyVisibility)
            {
                go.hideFlags |= HideFlags.HideInHierarchy;
            }

            //atlasTile.size

            go.transform.localPosition = TilePositionToLocalPosition(position, tileSize, tileSet.layers[layerIndex]) + atlasTile.offset;
            go.transform.localRotation = Quaternion.Euler(euler);
            var placedTile = new PlacedTile(guid, position, atlasTile, go, tagLayer);

            for (int x = 0; x < tileSize.x; x++)
            {
                for (int y = 0; y < tileSize.y; y++)
                {
                    for (int z = 0; z < tileSize.z; z++)
                    {
                        var        pos = position + new int3(x, y, z);
                        PlacedTile alreadyPlaced;
                        if (layers[layerIndex].TryGetValue(pos, out alreadyPlaced))
                        {
                            RemoveTile(alreadyPlaced, layerIndex);
                        }
                        if (layers[layerIndex].ContainsKey(pos))
                        {
                            layers[layerIndex].Remove(pos);
                        }
                        layers[layerIndex].Add(pos, placedTile);
                    }
                }
            }
            var neighbs = GetNeighbours(placedTile, layerIndex);

            atlasTile.PlaceBehaviour(placedTile, neighbs);
            if (updateNeighbours)
            {
                foreach (var neighb in neighbs)
                {
                    //Undo.RecordObject(neighb.placedObject, "Before Updating Object");
                    neighb.tileObject.UpdateBehaviour(neighb, GetNeighbours(neighb, layerIndex));
                }
            }

            Undo.RegisterCreatedObjectUndo(placedTile.placedObject, "Created New Tile Object");
            EditorUtility.SetDirty(gameObject);
        }
        public void AlignRotationToNeighbours(PlacedTile placedTile, bool registerUndo = true, params PlacedTile[] neighbours)
        {
            //UnityEditor.Undo.RegisterCompleteObjectUndo(placedTile.placedObject.transform, "Updated Placed Object Transform");
            if (neighbours.Length == 0)
            {
                return;
            }
            TileObject.Neighbour neighbourMask = TileObject.Neighbour.None;
            foreach (var neighbour in neighbours)
            {
                if (neighbour.guid != placedTile.guid)
                {
                    continue;
                }
                int3 relative = neighbour.position - placedTile.position;
                neighbourMask |= GetNeighbourEnumFromRelative(relative);
            }

            foreach (var variant in placedTile.tileObject.variants)
            {
                if (neighbourMask.HasFlag(variant.condition))
                {
                    //GameObject linkedPrefab = (GameObject)UnityEditor.PrefabUtility.GetPrefabInstanceHandle(placedTile.placedObject) as GameObject;
                    if (true)//(linkedPrefab != variant.prefab)
                    {
                        //Debug.Log("[TileBehaviour] Replacing " + linkedPrefab.name + " by " + variant.prefab.name);
                        var parent   = placedTile.placedObject.transform.parent;
                        var position = placedTile.placedObject.transform.localPosition;
                        if (registerUndo)
                        {
                            UnityEditor.Undo.DestroyObjectImmediate(placedTile.placedObject);
                        }
                        else
                        {
                            DestroyImmediate(placedTile.placedObject);
                        }
                        placedTile.placedObject = (GameObject)UnityEditor.PrefabUtility.InstantiatePrefab(variant.prefab) as GameObject;
                        placedTile.placedObject.transform.SetParent(parent);
                        placedTile.placedObject.transform.localPosition = position + variant.position;
                        if (registerUndo)
                        {
                            UnityEditor.Undo.RegisterCreatedObjectUndo(placedTile.placedObject, "Updated Placed Object");
                        }
                    }
                    var transf = placedTile.placedObject.transform;
                    transf.localRotation = Quaternion.Euler(variant.rotation);
                    transf.localScale    = variant.scale;
                    return;
                }
            }

            //if no variants can be applied - rotate to neighbour
            if (neighbourMask.HasFlag(TileObject.Neighbour.Left) || neighbourMask.HasFlag(TileObject.Neighbour.Right))
            {
                placedTile.placedObject.transform.localRotation = Quaternion.identity;
            }
            else
            {
                placedTile.placedObject.transform.localRotation = Quaternion.Euler(Vector3.up * 90f);
            }
        }