public WWResource(string assetBundleTag, string path) { prefab = null; _metadata = null; this.assetBundleTag = assetBundleTag; this.path = path; }
private void LoadMetaData() { if (prefab != null) { _metadata = prefab.GetComponent <WWResourceMetadata>(); } }
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); }
/// <summary> /// Handles the aspects of WWObject Instantiation that rely on the resource metadata. /// </summary> /// <param name="gameObject">The base GameObject which contains only resource, location, and rotation data</param> /// <param name="metadata">The metadata which will be used to construct the WWObject</param> /// <returns></returns> public static WWObject ConstructWWObject(GameObject gameObject, WWResourceMetadata metadata) { // Make the GameObject into a Tile, Prop, etc. Type type = WWTypeHelper.ConvertToSysType(metadata.wwObjectMetadata.type); /*Debug.Log(gameObject.name); * if (gameObject.name == "Skeleton(Clone)" || gameObject.name == "Zombie(Clone)") * { * * } else if (gameObject.name == "Tile_FloorBrick(Clone)" || gameObject.name == "Tile_Staircase(Clone)") * { * //terrain * gameObject.layer = 9; * } else { * //obstacle * gameObject.layer = 8; * }*/ var wwObject = gameObject.AddComponent(type) as WWObject; // Scale the object to the current tile scale. wwObject.transform.localScale = Vector3.one * CoordinateHelper.tileLengthScale * gameObject.transform.localScale.x;// * metadata.wwObjectMetadata.baseTileSize; // remove the WWResourceMetadata component for a microptimization #if UNITY_EDITOR Object.DestroyImmediate(wwObject.GetComponent <WWResourceMetadata>()); #else GameObject.Destroy(wwObject.GetComponent <WWResourceMetadata>()); #endif wwObject.gameObject.SetActive(true); return(wwObject); }
/// <summary> /// Initialize the WWObject /// </summary> /// <param name="objectData">The instance data for the WWObject.</param> /// <param name="resourceMetadata">The resource meta data that describes this WWObject.</param> public void Init(WWObjectData objectData, WWResourceMetadata resourceMetadata) { this.objectData = objectData; this.ResourceMetadata = resourceMetadata; var switchMaterial = Resources.Load("Materials/TileFadeMat") as Material; MaterialSwitcher = new MaterialSwitcher(gameObject, switchMaterial); }
public static WWWalls GetRotatedWWWalls(WWResourceMetadata metadata, int rotation) { int yRotation = rotation % 360 + (rotation < 0 ? 360 : 0); // rotation should only be 1 of 4 discrete values, 0, 90, 180, and 270 bool isvalidRotation = yRotation == 0 || yRotation == 90 || yRotation == 180 || yRotation == 270 || yRotation == 360; if (!isvalidRotation) { Debug.LogError(string.Format("WWWallsHelper : {0} is an invalid rotation.", yRotation)); } bool north; bool east; bool south; bool west; if (yRotation == 0 || yRotation == 360) { north = metadata.wwTileMetadata.wwWallMetadata.north; east = metadata.wwTileMetadata.wwWallMetadata.east; south = metadata.wwTileMetadata.wwWallMetadata.south; west = metadata.wwTileMetadata.wwWallMetadata.west; } else if (yRotation == 90) { north = metadata.wwTileMetadata.wwWallMetadata.west; east = metadata.wwTileMetadata.wwWallMetadata.north; south = metadata.wwTileMetadata.wwWallMetadata.east; west = metadata.wwTileMetadata.wwWallMetadata.south; } else if (yRotation == 180) { north = metadata.wwTileMetadata.wwWallMetadata.south; east = metadata.wwTileMetadata.wwWallMetadata.west; south = metadata.wwTileMetadata.wwWallMetadata.north; west = metadata.wwTileMetadata.wwWallMetadata.east; } else // (yRotation == 270) { north = metadata.wwTileMetadata.wwWallMetadata.east; east = metadata.wwTileMetadata.wwWallMetadata.south; south = metadata.wwTileMetadata.wwWallMetadata.west; west = metadata.wwTileMetadata.wwWallMetadata.north; } bool top = metadata.wwTileMetadata.wwWallMetadata.top; bool bottom = metadata.wwTileMetadata.wwWallMetadata.bottom; var rotatedMetaData = new WWWallMetadata(north, east, south, west, top, bottom); WWWalls walls = rotatedMetaData.GetWallsEnum(); return(walls); }
/// <summary> /// Handles the aspects of WWObject Instantiation that rely on the resource metadata. /// </summary> /// <param name="gameObject">The base GameObject which contains only resource, location, and rotation data</param> /// <param name="metadata">The metadata which will be used to construct the WWObject</param> /// <returns></returns> public static WWObject ConstructWWObject(GameObject gameObject, WWResourceMetadata metadata) { // Make the GameObject into a Tile, Prop, etc. Type type = WWTypeHelper.ConvertToSysType(metadata.wwObjectMetadata.type); var wwObject = gameObject.AddComponent(type) as WWObject; // Scale the object to the current tile scale. wwObject.transform.localScale = Vector3.one * CoordinateHelper.tileLengthScale; // remove the WWResourceMetadata component for a microptimization #if UNITY_EDITOR Object.DestroyImmediate(wwObject.GetComponent <WWResourceMetadata>()); #else GameObject.Destroy(wwObject.GetComponent <WWResourceMetadata>()); #endif wwObject.gameObject.SetActive(true); return(wwObject); }
private void DisplayCollisionsProperties(WWResourceMetadata script) { GUILayout.Label("Walls"); EditorGUILayout.BeginHorizontal(); script.wwTileMetadata.wwWallMetadata.north = GUILayout.Toggle( script.wwTileMetadata.wwWallMetadata.north, "North"); script.wwTileMetadata.wwWallMetadata.east = GUILayout.Toggle( script.wwTileMetadata.wwWallMetadata.east, "East"); script.wwTileMetadata.wwWallMetadata.south = GUILayout.Toggle( script.wwTileMetadata.wwWallMetadata.south, "South"); EditorGUILayout.EndHorizontal(); EditorGUILayout.BeginHorizontal(); script.wwTileMetadata.wwWallMetadata.west = GUILayout.Toggle( script.wwTileMetadata.wwWallMetadata.west, "West"); script.wwTileMetadata.wwWallMetadata.top = GUILayout.Toggle( script.wwTileMetadata.wwWallMetadata.top, "Top"); script.wwTileMetadata.wwWallMetadata.bottom = GUILayout.Toggle( script.wwTileMetadata.wwWallMetadata.bottom, "Bottom"); EditorGUILayout.EndHorizontal(); }
/// <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 } } }
/// <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); }