/// <see cref="SceneGraphManager.Load"/> public void Load(string filePath) { string json = FileIO.LoadJsonFromFile(filePath); var objectsToRestore = JsonConvert.DeserializeObject <List <WWObjectJSONBlob> >(json); Debug.Log(string.Format("Loaded {0} objects from file", objectsToRestore.Count)); foreach (WWObjectJSONBlob obj in objectsToRestore) { var objectData = new WWObjectData(obj); WWObject go = WWObjectFactory.Instantiate(objectData); Add(go); } // re-link children since all the objects have been instantiated in game world foreach (WWObjectJSONBlob obj in objectsToRestore) { WWObject root = Get(obj.id); var childrenToRestore = new List <WWObject>(); foreach (Guid childID in obj.children) { WWObject childObject = Get(childID); childrenToRestore.Add(childObject); } root.AddChildren(childrenToRestore); } }
private void Remove(Guid id, bool delete) { if (sceneDictionary.ContainsGuid(id)) { WWObject rootObject = Get(id); // remove child from parent if there is a parent WWObjectData parent = rootObject.GetParent(); if (parent != null) { WWObject parentObject = Get(parent.id); parentObject.RemoveChild(rootObject); } List <WWObjectData> objectDescendents = rootObject.GetAllDescendents(); // include the root objectDescendents.Add(rootObject.objectData); foreach (WWObjectData objectToDelete in objectDescendents) { WWObject objectToDestroy = RemoveObject(objectToDelete.id); if (delete) { Destroy(objectToDestroy); } } } }
private void RotateObjects() { if (curObject == null) { return; } Vector3 curPos = curObject.transform.position; if (Input.GetKeyDown(KeyCode.LeftArrow)) { Debug.Log("Rotate left"); curRotation += 90; curRotation = curRotation % 360 + (curRotation < 0 ? 360 : 0); Destroy(curObject.gameObject); curObject = ForceRotateAndPlaceObject(curPos); } else if (Input.GetKeyDown(KeyCode.RightArrow)) { Debug.Log("Rotate right"); curRotation -= 90; curRotation = curRotation % 360 + (curRotation < 0 ? 360 : 0); Destroy(curObject.gameObject); curObject = ForceRotateAndPlaceObject(curPos); } }
/// <summary> /// Building a perimeter wall around a large floor or dungeon map is time consuming, especially /// when wall pieces have to constantly be rotated into place due to World Wizard's /// slightly elaborate tile system. /// Build Perimeter Walls assists the player by searching for the perimeter of all contiguous /// tiles. Once the perimeter has been found, the walls are erected and added to the /// current Scene Graph. /// </summary> /// <param name="resourceTag">the resource to place around the perimeter</param> /// <param name="wwObject">the floor tile to start the search from</param> public static void BuildPerimeterWalls(string resourceTag, WWObject wwObject) { IntVector3 curIndex = wwObject.GetCoordinate().Index; Dictionary <IntVector3, WWWalls> coordToWalls = SelectPerimeter(curIndex); foreach (KeyValuePair <IntVector3, WWWalls> coordToWall in coordToWalls) { // Note: WWWalls is a bitmask, an there may be multiple perimeter walls // for a given coordinate index. Hence why these are if statements and not if elses. // if the perimeter wall is on the north side... if (Convert.ToBoolean(WWWalls.North & coordToWall.Value)) { TryToPlaceWall(coordToWall.Key, WWWalls.North, resourceTag); } // if the perimeter wall is on the east side... if (Convert.ToBoolean(WWWalls.East & coordToWall.Value)) { TryToPlaceWall(coordToWall.Key, WWWalls.East, resourceTag); } // if the perimeter wall is on the south side... if (Convert.ToBoolean(WWWalls.South & coordToWall.Value)) { TryToPlaceWall(coordToWall.Key, WWWalls.South, resourceTag); } // if the perimeter wall is on the west side... if (Convert.ToBoolean(WWWalls.West & coordToWall.Value)) { TryToPlaceWall(coordToWall.Key, WWWalls.West, resourceTag); } } }
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); }
private void TryPlaceDoor(Vector3 hitPoint) { Debug.Log("TryPlaceDoor called."); Coordinate coord = CoordinateHelper.UnityCoordToWWCoord(hitPoint); List <WWObject> objects = ManagerRegistry.Instance.GetAnInstance <SceneGraphManager>().GetObjectsInCoordinateIndex(coord); Debug.Log("objects count " + objects.Count); foreach (WWObject obj in objects) { Debug.Log(" object type " + obj.ResourceMetadata.wwObjectMetadata.type); if (obj.ResourceMetadata.wwObjectMetadata.type == WWType.Tile) { Debug.Log("A tile was in the coordinate"); if (curObject.ResourceMetadata.wwObjectMetadata.type == WWType.Door) { Debug.Log("The current Object is a door"); if (ManagerRegistry.Instance.GetAnInstance <SceneGraphManager>().AddDoor((Door)curObject, (Tile)obj, hitPoint)) { curObject = null; } } } } }
public void PlaceObject() { if (curObject != null) { MoveObject(); curObject.SetPosition(CoordinateHelper.UnityCoordToWWCoord(curObject.transform.position)); if (!ManagerRegistry.Instance.GetAnInstance <SceneGraphManager>().Add(curObject)) { Destroy(curObject.gameObject); // If the object collided with another, destroy it. } else { if (curObject.gameObject.layer == 8 || curObject.gameObject.layer == 9) { ManagerRegistry.Instance.GetAnInstance <WWAIManager>().RefreshGrid(); } else if (curObject.gameObject.layer == 10) { WWSeeker script = curObject.GetComponent <WWSeeker>(); script.Place(); } } curObject = null; } }
private void Destroy(WWObject objectToDestroy) { #if UNITY_EDITOR Object.DestroyImmediate(objectToDestroy.gameObject); #else GameObject.Destroy(objectToDestroy.gameObject); #endif }
/// <see cref="SceneGraphManager.Add"/> public bool Add(WWObject worldWizardsObject) { if (worldWizardsObject != null) { return(sceneDictionary.Add(worldWizardsObject)); } return(false); }
private void Update() { CheckForStateChange(); RotateObjects(); if (state == State.PerimeterWalls) { BuildWallsInput(); return; } DeleteHitObject(); Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); RaycastHit raycastHit; if (gridCollider.Raycast(ray, out raycastHit, 1000f)) { Vector3 position = raycastHit.point; // because the tile center is in the middle need to offset position.x += .5f * CoordinateHelper.baseTileLength * CoordinateHelper.tileLengthScale; position.y += CoordinateHelper.baseTileLength * CoordinateHelper.tileLengthScale; position.z += .5f * CoordinateHelper.baseTileLength * CoordinateHelper.tileLengthScale; CycleObjectsScrollWheel(position); coordDebugText.text = string.Format("x : {0}, z : {1}", position.x, position.z); Debug.DrawRay(raycastHit.point, Camera.main.transform.position, Color.red, 0, false); if (curObject == null) { curObject = PlaceObject(position); } else { curObject.transform.position = new Vector3(raycastHit.point.x, raycastHit.point.y + 0.5f * CoordinateHelper.baseTileLength * CoordinateHelper.tileLengthScale, raycastHit.point.z); } if (Input.GetMouseButtonUp(0)) { if (curObject != null) { Destroy(curObject.gameObject); curObject = PlaceObject(position); if (state == State.DoorAttach) { TryPlaceDoor(position); } else if (state == State.Normal) { if (ManagerRegistry.Instance.GetAnInstance <SceneGraphManager>().Add(curObject)) { curObject = null; } } } } } }
private WWObject ForceRotateAndPlaceObject(Vector3 position) { int theRot = curRotation; Coordinate coordRotated = CoordinateHelper.UnityCoordToWWCoord(position); var wwTransform = new WWTransform(coordRotated, theRot); WWObjectData objData = WWObjectFactory.CreateNew(wwTransform, GetResourceTag()); WWObject go = WWObjectFactory.Instantiate(objData); return(go); }
private WWObject PlaceObject(Vector3 position) { int tileIndex = Mathf.Abs(curTile) % possibleTiles.Count; Coordinate coordinate = CoordinateHelper.UnityCoordToWWCoord(position); var wwTransform = new WWTransform(coordinate, curRotation); WWObjectData objData = WWObjectFactory.CreateNew(wwTransform, possibleTiles[tileIndex]); WWObject go = WWObjectFactory.Instantiate(objData); return(go); }
public static void AddObjectToSceneGraph() { Assert.AreEqual(0, ManagerRegistry.Instance.GetAnInstance <SceneGraphManager>().SceneSize()); var coordinate = new Coordinate(0, 0, 0); var wwTransform = new WWTransform(coordinate); WWObjectData wwObjectData = WWObjectFactory.CreateNew(wwTransform, "white"); WWObject wwObject = WWObjectFactory.Instantiate(wwObjectData); ManagerRegistry.Instance.GetAnInstance <SceneGraphManager>().Add(wwObject); Assert.AreEqual(1, ManagerRegistry.Instance.GetAnInstance <SceneGraphManager>().SceneSize()); }
public void PlaceObject() { if (curObject != null) { MoveObject(); curObject.SetPosition(CoordinateHelper.UnityCoordToWWCoord(curObject.transform.position)); if (!ManagerRegistry.Instance.GetAnInstance <SceneGraphManager>().Add(curObject)) { Destroy(curObject.gameObject); // If the object collided with another, destroy it. } curObject = null; } }
private void CreateCube(Vector3 controllerPos) { Coordinate cubePosition = CoordinateHelper.UnityCoordToWWCoord(controllerPos); Debug.Log("Cube Position: " + cubePosition.Index.x + ", " + cubePosition.Index.y + ", " + cubePosition.Index.z); var wwTransform = new WWTransform(cubePosition); WWObjectData data = WWObjectFactory.CreateNew(wwTransform, "white"); WWObject obj = WWObjectFactory.Instantiate(data); ManagerRegistry.Instance.GetAnInstance <SceneGraphManager>().Add(obj); }
private void CycleObjectsSwipe(Vector3 position) { var offset = (int)(possibleTiles.Count * 0.3f); if (offset != 0) { curTile = (curTile + offset) % possibleTiles.Count; if (curObject != null) { Destroy(curObject.gameObject); } curObject = PlaceObject(position); } }
private readonly string assetName = "tile_wallbrick"; // the exact name of the tile inside of the Asset Bundle private void Start() { // create a coordinate to place the tile var coordinate = new Coordinate(0, 0, 0); var wwTransform = new WWTransform(coordinate, 0); // create the data needed to instantiate this tile WWObjectData tileData = WWObjectFactory.CreateNew(wwTransform, string.Format("{0}_{1}", assetBundleName, assetName)); // instantiate the tile in the world WWObject tile = WWObjectFactory.Instantiate(tileData); // add the newly created tile to the SceneGraph ManagerRegistry.Instance.GetAnInstance <SceneGraphManager>().Add(tile); }
/// <summary> /// Instantiates the WwObject wall belonging to the resource and adds the wall to the active /// Scene Graph. /// </summary> /// <param name="coordIndex">The coordinate index for this perimeter wall.</param> /// <param name="rotation">The valid rotation for this perimeter.</param> /// <param name="resourceTag">The resourceTag from which to load the WWObject from.</param> private static void PlaceWallObject(IntVector3 coordIndex, int rotation, string resourceTag) { var coordinate = new Coordinate(coordIndex); var wwTransform = new WWTransform(coordinate, rotation); WWObjectData objData = WWObjectFactory.CreateNew(wwTransform, resourceTag); WWObject go = WWObjectFactory.Instantiate(objData); if (!ManagerRegistry.Instance.GetAnInstance <SceneGraphManager>().Add(go)) { // this error should never happen as TryToPlaceWall checks for collision before calling this function Debug.LogError("Could not place wall because of collision, deleting temp"); UnityEngine.Object.Destroy(go.gameObject); } }
// Grip public override void OnUngrip() // Delete object { if (curObject == null) { RaycastHit raycastHit; if (Physics.Raycast(input.GetControllerPoint(), input.GetControllerDirection(), out raycastHit, 100)) { WWObject wwObject = raycastHit.transform.gameObject.GetComponent <WWObject>(); if (wwObject != null) { ManagerRegistry.Instance.GetAnInstance <SceneGraphManager>().Delete(wwObject.GetId()); } } } }
private void CreateObject(Vector3 position) { if (curObject != null) { Destroy(curObject.gameObject); } Coordinate coordinate = CoordinateHelper.UnityCoordToWWCoord(position); var wwTransform = new WWTransform(coordinate, curRotation); if (ManagerRegistry.Instance.GetAnInstance <WWObjectGunManager>().GetPossibleObjectKeys().Count > 0) { WWObjectData objData = WWObjectFactory.CreateNew(wwTransform, ManagerRegistry.Instance.GetAnInstance <WWObjectGunManager>().GetPossibleObjectKeys()[curTileIndex]); curObject = WWObjectFactory.Instantiate(objData); } }
public static List <Coordinate> CreateTerrainFromImage(Texture2D heightmap) { var coordinates = new List <Coordinate>(); var maxHeight = 10; for (var x = 0; x < heightmap.width; x++) { for (var y = 0; y < heightmap.height; y++) { var height = (int)(heightmap.GetPixel(x, y).r *maxHeight); var c = new Coordinate(x, height, y); coordinates.Add(c); var wwTransform = new WWTransform(c, 0); WWObjectData parentData = WWObjectFactory.CreateNew(wwTransform, "white"); WWObject parentObj = WWObjectFactory.Instantiate(parentData); ManagerRegistry.Instance.GetAnInstance <SceneGraphManager>().Add(parentObj); while (height > 0) { height--; c = new Coordinate(x, height, y); coordinates.Add(c); wwTransform = new WWTransform(c, 0); WWObjectData childData = WWObjectFactory.CreateNew(wwTransform, "white"); WWObject childObj = WWObjectFactory.Instantiate(childData); ManagerRegistry.Instance.GetAnInstance <SceneGraphManager>().Add(childObj); var children = new List <WWObject>(); children.Add(childObj); parentObj.AddChildren(children); parentObj = childObj; } } } return(coordinates); }
private void RotateObjects(Vector3 position) { if (Input.GetKeyDown(KeyCode.LeftArrow)) { curRotation += 90; if (curObject != null) { Destroy(curObject.gameObject); } curObject = PlaceObject(position); } else if (Input.GetKeyDown(KeyCode.RightArrow)) { curRotation -= 90; if (curObject != null) { Destroy(curObject.gameObject); } curObject = PlaceObject(position); } }
private void CycleObjectsScrollWheel(Vector3 position) { if (Input.GetAxis("Mouse ScrollWheel") > 0) { curTile++; if (curObject != null) { Destroy(curObject.gameObject); } curObject = PlaceObject(position); } else if (Input.GetAxis("Mouse ScrollWheel") < 0) { curTile--; if (curObject != null) { Destroy(curObject.gameObject); } curObject = PlaceObject(position); } }
private WWObject PlaceObject(Vector3 position) { List <int> possibleConfigurations = BuilderAlgorithms.GetPossibleRotations(position, GetResourceTag()); if (possibleConfigurations.Count == 0) { return(null); } int theRot = possibleConfigurations[0]; if (possibleConfigurations.Contains(curRotation)) { theRot = curRotation; } Coordinate coordRotated = CoordinateHelper.UnityCoordToWWCoord(position); var wwTransform = new WWTransform(coordRotated, theRot); WWObjectData objData = WWObjectFactory.CreateNew(wwTransform, GetResourceTag()); WWObject go = WWObjectFactory.Instantiate(objData); return(go); }
/// <summary> /// Consumes a WWObject and determines whether this object can fit at its current coordinate, /// taking into consideration rotation, or if it collides with other WWObjects being maintained /// by this data structure. This is private method and it safe to assume that the WWObject being tested /// for collision has not yet been added to this data structure. Note: it would be easy to do a Guid /// comparison before checking collision if the use case ever arises in the future. /// </summary> /// <param name="wwObject">The object to test for collision.</param> /// <returns>True if the WWObject can fit without colliding given its current coordinate.</returns> private bool Collides(WWObject wwObject) { if (coordinates.ContainsKey(wwObject.GetCoordinate().Index)) // any objects at coordinate? { List <WWObject> objectsAtCoord = GetObjects(wwObject.GetCoordinate()); WWWalls totalWalls = 0; // this is a bit mask which will keep the running total of wall collisions foreach (WWObject obj in objectsAtCoord) // only need to do collision checking at the coordinate index { if (obj.ResourceMetadata.wwObjectMetadata.type.Equals(WWType.Tile)) // ignore non Tile types { // get the walls of the WWObject after applying its rotation transformation WWWalls walls = WWWallsHelper.GetRotatedWWWalls(obj.ResourceMetadata, obj.GetRotation()); totalWalls = totalWalls | walls; // OR the walls with the running sum stored in totalWalls } } // now get the walls for the object that is being collision checked for WWWalls newWalls = WWWallsHelper.GetRotatedWWWalls(wwObject.ResourceMetadata, wwObject.GetRotation()); bool doesCollide = Convert.ToBoolean(newWalls & totalWalls); // 0 or False if no collision return(doesCollide); } return(false); // if there are no objects at the index, obviously there are no collisions }
private void Start() { ResourceLoader.LoadResources(); for (var i = 0; i < 5; i++) { WWObjectData objData = WWObjectFactory.CreateNew(new WWTransform(new Coordinate(i, i, i), 0), "ww_basic_assets_Tile_Grass"); WWObject go = WWObjectFactory.Instantiate(objData); ManagerRegistry.Instance.GetAnInstance <SceneGraphManager>().Add(go); } for (var i = 0; i < 5; i++) { WWObjectData objData = WWObjectFactory.CreateNew(new WWTransform(new Coordinate(i, i + 1, i), 0), "ww_basic_assets_Tile_Arch"); WWObject go = WWObjectFactory.Instantiate(objData); ManagerRegistry.Instance.GetAnInstance <SceneGraphManager>().Add(go); } for (var i = 0; i < 5; i++) { WWObjectData objData = WWObjectFactory.CreateNew(new WWTransform(new Coordinate(i, i + 2, i), 0), "ww_basic_assets_Tile_FloorBrick"); WWObject go = WWObjectFactory.Instantiate(objData); ManagerRegistry.Instance.GetAnInstance <SceneGraphManager>().Add(go); } for (var i = 0; i < 5; i++) { WWObjectData objData = WWObjectFactory.CreateNew(new WWTransform(new Coordinate(i, i + 2, i), 0), "ww_basic_assets_blueCube"); WWObject go = WWObjectFactory.Instantiate(objData); ManagerRegistry.Instance.GetAnInstance <SceneGraphManager>().Add(go); } }
/// <summary> /// Attempt to Add a WWObject to the data structure. /// </summary> /// <param name="wwObject">The object to Add.</param> /// <returns>True if the object can be Added to the data structure.</returns> public bool Add(WWObject wwObject) { Coordinate coord = wwObject.GetCoordinate(); Guid guid = wwObject.GetId(); if (Collides(wwObject) && wwObject.ResourceMetadata.wwObjectMetadata.type.Equals(WWType.Tile)) { Debug.Log("Tile collides with existing tiles. Preventing placement of new tile."); return(false); } if (coordinates.ContainsKey(coord.Index)) { coordinates[coord.Index].Add(guid); // append the existing list of Guids for this index } else // create new entry in the map { var guidList = new List <Guid>(); guidList.Add(guid); coordinates.Add(coord.Index, guidList); } objects.Add(wwObject.GetId(), wwObject); return(true); // sucessfully added the object to the data structure }
public static WWObject RaycastNoGrid(Vector3 origin, Vector3 direction, float rayDistance) { WWObject resultWwObject = null; var minDistance = float.MaxValue; var colliders = ManagerRegistry.Instance.GetAnInstance <SceneGraphManager>().GetAllColliders(); Ray ray = new Ray(origin, direction); foreach (var c in colliders) { RaycastHit raycastHit; if (c.Raycast(ray, out raycastHit, rayDistance)) { var dist = Vector3.Distance(origin, raycastHit.point); if (dist < minDistance) { minDistance = dist; resultWwObject = raycastHit.transform.gameObject.GetComponent <WWObject>(); } } } return(resultWwObject); }
private void Update() { DeleteHitObject(); // move the builder grid Vector3 gridPosition = gridCollider.transform.position; if (Input.GetKeyDown(KeyCode.UpArrow)) { gridPosition.y += CoordinateHelper.baseTileLength * CoordinateHelper.tileLengthScale; Vector3 oldCamPosition = VRCameraRig.position; oldCamPosition.y += CoordinateHelper.baseTileLength * CoordinateHelper.tileLengthScale; VRCameraRig.position = oldCamPosition; } else if (Input.GetKeyDown(KeyCode.DownArrow)) { gridPosition.y -= CoordinateHelper.baseTileLength * CoordinateHelper.tileLengthScale; Vector3 oldCamPosition = VRCameraRig.position; oldCamPosition.y -= CoordinateHelper.baseTileLength * CoordinateHelper.tileLengthScale; VRCameraRig.position = oldCamPosition; } gridCollider.transform.position = gridPosition; //Ray ray = Camera.main.ScreenPointToRay (trackedObj.transform.position); //Ray ray = new Ray(trackedObj.transform.position, trackedObj.transform.forward); //laserTransform.position = Vector3.Lerp(trackedObj.transform.position, hitPoint, .5f); RaycastHit raycastHit; // if (gridCollider.Raycast (ray, out raycastHit, 1000f )) { if (Physics.Raycast(trackedObj.transform.position, transform.forward, out raycastHit, 100, teleportMask)) { Vector3 position = raycastHit.point; // because the tile center is in the middle need to offset position.x += .5f * CoordinateHelper.baseTileLength * CoordinateHelper.tileLengthScale; position.y += CoordinateHelper.baseTileLength * CoordinateHelper.tileLengthScale; position.z += .5f * CoordinateHelper.baseTileLength * CoordinateHelper.tileLengthScale; coordDebugText.text = string.Format("x : {0}, z : {1}", position.x, position.z); CycleObjectsSwipe(position); RotateObjects(position); Debug.DrawRay(raycastHit.point, Camera.main.transform.position, Color.red, 0, false); if (curObject == null) { curObject = PlaceObject(position); } else { curObject.transform.position = new Vector3(raycastHit.point.x, raycastHit.point.y + 0.5f * CoordinateHelper.baseTileLength * CoordinateHelper.tileLengthScale, raycastHit.point.z); } if (Controller.GetHairTriggerDown()) { if (curObject != null) { Destroy(curObject.gameObject); curObject = PlaceObject(position); ManagerRegistry.Instance.GetAnInstance <SceneGraphManager>().Add(curObject); curObject = null; } } } }