public static WWObject Instantiate(WWObjectData objectData)
        {
            Vector3 spawnPos = CoordinateHelper.WWCoordToUnityCoord(objectData.wwTransform.coordinate);

            // Load resource and check to see if it is valid.
            WWResource         resource = WWResourceController.GetResource(objectData.resourceTag);
            GameObject         gameObject;
            WWResourceMetadata resourceMetadata = resource.GetMetaData();

            if (resource.GetPrefab() == null)
            {
                gameObject       = GameObject.CreatePrimitive(PrimitiveType.Cube);
                resourceMetadata = gameObject.AddComponent <WWResourceMetadata>();
                gameObject.transform.Translate(spawnPos);
            }
            else
            {
                if (resource.GetMetaData() == null)
                {
                    Debug.Log("There is no metadata for this resource, so it cannot be instantiated.");
                    return(null);
                }
                // Create a GameObject at the correct location and rotation.
                gameObject = Object.Instantiate(resource.GetPrefab(), spawnPos,
                                                Quaternion.Euler(0, objectData.wwTransform.rotation, 0));
            }

            // Use ResourceMetaData to construct the object.
            WWObject wwObject = ConstructWWObject(gameObject, resourceMetadata);

            // Give the new WWObject the data used to create it.
            wwObject.Init(objectData, resourceMetadata);
            wwObject.SetTransform(objectData.wwTransform);
            return(wwObject);
        }
 public static void LoadResource(string tag, string assetBundleTag, string path)
 {
     if (bundles.ContainsKey(tag))
     {
         Debug.Log("resourceTag: " + tag + " has already been used.");
     }
     else
     {
         var resource = new WWResource(assetBundleTag, path);
         bundles.Add(tag, resource);
     }
 }
Beispiel #3
0
        /// <summary>
        /// Loads from the resourceTag a WWObject, then attempts to find a valid rotation for the WWObject,
        /// such that it fits with existing Objects at the specified Coordinate Index, and finally places and
        /// adds the WWObject wall to the active Scene Graph.
        /// </summary>
        /// <param name="coordIndex">The coordinate index for this perimeter wall</param>
        /// <param name="perimeterWallOpening">The direction for the perimeter wall</param>
        /// <param name="resourceTag">The resource tag for of the WWObject to place as the perimeter wall.</param>
        private static void TryToPlaceWall(IntVector3 coordIndex, WWWalls perimeterWallOpening, string resourceTag)
        {
            // the collisions that need to be taken into account to see if the WWObject belonging to resourceTag can fit
            WWWalls wallsToFit = ~perimeterWallOpening |
                                 ManagerRegistry.Instance.GetAnInstance <SceneGraphManager>().
                                 GetWallsAtCoordinate(new Coordinate(coordIndex));
            WWResource         resource         = WWResourceController.GetResource(resourceTag);
            WWResourceMetadata resourceMetadata = resource.GetMetaData();

            // Try possible rotations for the resource, and place the resource at the first rotation that fits.
            for (var r = 0; r < 360; r += 90) // only rotations of 90 degrees are valid
            {
                WWWalls wallToPlace = WWWallsHelper.GetRotatedWWWalls(resourceMetadata, r);
                bool    doesCollide = Convert.ToBoolean(wallToPlace & wallsToFit); // should be 0 or False if no collision
                if (!doesCollide)
                {
                    PlaceWallObject(coordIndex, r, resourceTag); // add the wall to the active Scene Graph
                    return;                                      // a valid rotation was found for the WWObject, no need to try further rotations
                }
            }
        }
Beispiel #4
0
        /// <summary>
        /// Given a resourceTag, loads the WWObject belonging to the resource and returns a list of all possible
        /// valid rotations for the WWObject that do not collide with existing WWObjects at the given position
        /// in the active Scene Graph. Useful for assisting the player with auto rotation of tiles.
        /// </summary>
        /// <param name="position">The Unity Space position to convert to Coordinate Index to determine valid rotations in.</param>
        /// <param name="resourceTag">The resourceTag belonging to the desired WWObject.</param>
        /// <returns></returns>
        public static List <int> GetPossibleRotations(Vector3 position, string resourceTag)
        {
            var        result     = new List <int>(); // the resulting list of possible rotations, currently empty
            Coordinate coordinate = CoordinateHelper.UnityCoordToWWCoord(position);
            WWWalls    wallsToFit = ManagerRegistry.Instance.GetAnInstance <SceneGraphManager>().GetWallsAtCoordinate(coordinate);

            // check to see if any of the 4 possible rotations would fit given resource's walls
            WWResource         resource         = WWResourceController.GetResource(resourceTag);
            WWResourceMetadata resourceMetadata = resource.GetMetaData();

            for (var r = 0; r < 360; r += 90) // only rotations of 90 degrees are valid
            {
                WWWalls newWalls    = WWWallsHelper.GetRotatedWWWalls(resourceMetadata, r);
                bool    doesCollide = Convert.ToBoolean(newWalls & wallsToFit); // should be 0 or False if no collision
                if (!doesCollide)
                {
                    result.Add(r);
                }
            }
            return(result);
        }