Esempio n. 1
0
        public TileProxy(GameObject prefab, bool ignoreSpriteRendererBounds, Vector3 upVector)
        {
            prefab.transform.localPosition = Vector3.zero;
            prefab.transform.localRotation = Quaternion.identity;

            Prefab     = prefab;
            PrefabTile = prefab.GetComponent <Tile>();

            if (PrefabTile == null)
            {
                PrefabTile = prefab.AddComponent <Tile>();
            }

            Placement = new TilePlacementData();

            // Add proxy doorways
            Doorways = new ReadOnlyCollection <DoorwayProxy>(doorways);

            var allDoorways = prefab.GetComponentsInChildren <Doorway>();

            for (int i = 0; i < allDoorways.Length; i++)
            {
                var doorway = allDoorways[i];

                Vector3    localPosition = doorway.transform.position;
                Quaternion localRotation = doorway.transform.rotation;

                var proxyDoorway = new DoorwayProxy(this, i, doorway.Socket, localPosition, localRotation);
                doorways.Add(proxyDoorway);

                if (PrefabTile.Entrance == doorway)
                {
                    Entrance = proxyDoorway;
                }
                if (PrefabTile.Exit == doorway)
                {
                    Exit = proxyDoorway;
                }
            }

            // Calculate bounds
            Bounds bounds;

            if (PrefabTile != null && PrefabTile.OverrideAutomaticTileBounds)
            {
                bounds = PrefabTile.TileBoundsOverride;
            }
            else
            {
                bounds = UnityUtil.CalculateProxyBounds(Prefab, ignoreSpriteRendererBounds, upVector);
            }

            // Let the user know if the automatically calculated bounds are incorrect
            if (bounds.size.x <= 0f || bounds.size.y <= 0f || bounds.size.z <= 0f)
            {
                Debug.LogError(string.Format("Tile prefab '{0}' has automatic bounds that are zero or negative in size. The bounding volume for this tile will need to be manually defined.", prefab), prefab);
            }

            Placement.LocalBounds = UnityUtil.CondenseBounds(bounds, Prefab.GetComponentsInChildren <Doorway>());
        }
        public TilePlacementData(TilePlacementData copy)
        {
            PathDepth             = copy.PathDepth;
            NormalizedPathDepth   = copy.NormalizedPathDepth;
            BranchDepth           = copy.BranchDepth;
            NormalizedBranchDepth = copy.NormalizedDepth;
            IsOnMainPath          = copy.IsOnMainPath;
            LocalBounds           = copy.LocalBounds;
            Transform             = copy.Transform;
            GraphNode             = copy.GraphNode;
            GraphLine             = copy.GraphLine;
            Archetype             = copy.Archetype;
            TileSet = copy.TileSet;

            position = copy.position;
            rotation = copy.rotation;
            RecalculateTransform();
        }
Esempio n. 3
0
        public TileProxy(TileProxy existingTile)
        {
            Prefab     = existingTile.Prefab;
            PrefabTile = existingTile.PrefabTile;
            Placement  = new TilePlacementData(existingTile.Placement);

            // Copy proxy doorways
            Doorways = new ReadOnlyCollection <DoorwayProxy>(doorways);

            foreach (var existingDoorway in existingTile.doorways)
            {
                var doorway = new DoorwayProxy(this, existingDoorway.Index, existingDoorway.Socket, existingDoorway.LocalPosition, existingDoorway.LocalRotation);
                doorways.Add(doorway);

                if (existingTile.Entrance == existingDoorway)
                {
                    Entrance = doorway;
                }
                if (existingTile.Exit == existingDoorway)
                {
                    Exit = doorway;
                }
            }
        }
Esempio n. 4
0
        protected TilePlacementResult TryPlaceTile(DoorwayPair pair, DungeonArchetype archetype, out Tile tile)
        {
            tile = null;

            var toTemplate  = pair.NextTemplate;
            var fromDoorway = pair.PreviousDoorway;

            if (toTemplate == null)
            {
                return(TilePlacementResult.TemplateIsNull);
            }

            int toDoorwayIndex = pair.NextTemplate.Doorways.IndexOf(pair.NextDoorway);

            if (fromDoorway != null)
            {
                // Move the proxy object into position
                GameObject toProxyDoor = toTemplate.ProxySockets[toDoorwayIndex];
                UnityUtil.PositionObjectBySocket(toTemplate.Proxy, toProxyDoor, fromDoorway.gameObject);

                Bounds proxyBounds = toTemplate.Proxy.GetComponent <Collider>().bounds;

                // Check if the new tile is outside of the valid bounds
                if (RestrictDungeonToBounds && !TilePlacementBounds.Contains(proxyBounds))
                {
                    return(TilePlacementResult.OutOfBounds);
                }

                // Check if the new tile is colliding with any other
                if (IsCollidingWithAnyTile(proxyBounds))
                {
                    return(TilePlacementResult.TileIsColliding);
                }
            }

            TilePlacementData newTile = new TilePlacementData(toTemplate, (Status == GenerationStatus.MainPath), archetype, pair.NextTileSet, currentDungeon);

            if (newTile == null)
            {
                return(TilePlacementResult.NewTileIsNull);
            }

            if (newTile.IsOnMainPath)
            {
                if (pair.PreviousTile != null)
                {
                    newTile.PathDepth = pair.PreviousTile.Placement.PathDepth + 1;
                }
            }
            else
            {
                newTile.PathDepth   = pair.PreviousTile.Placement.PathDepth;
                newTile.BranchDepth = (pair.PreviousTile.Placement.IsOnMainPath) ? 0 : pair.PreviousTile.Placement.BranchDepth + 1;
            }

            if (fromDoorway != null)
            {
                newTile.Root.transform.parent = Root.transform;
                Doorway toDoorway = newTile.AllDoorways[toDoorwayIndex];

                UnityUtil.PositionObjectBySocket(newTile.Root, toDoorway.gameObject, fromDoorway.gameObject);
                currentDungeon.MakeConnection(fromDoorway, toDoorway, RandomStream);
            }
            else
            {
                newTile.Root.transform.parent        = Root.transform;
                newTile.Root.transform.localPosition = Vector3.zero;
            }

            currentDungeon.AddTile(newTile.Tile);

            if (!newTile.Tile.OverrideAutomaticTileBounds)
            {
                newTile.RecalculateBounds(IgnoreSpriteBounds, UpVector);
            }
            else
            {
                newTile.Bounds      = newTile.Tile.transform.TransformBounds(newTile.Tile.TileBoundsOverride);
                newTile.LocalBounds = newTile.Tile.TileBoundsOverride;
            }

            if (PlaceTileTriggers)
            {
                newTile.Tile.AddTriggerVolume();
                newTile.Root.layer = TileTriggerLayer;
            }

            allDoorways.AddRange(newTile.AllDoorways);

            tile = newTile.Tile;
            placedTilePrefabs[tile] = toTemplate.Prefab;

            return(TilePlacementResult.None);
        }