public void SetCurrentReticle(GameObject obj) { //Debug.LogError("Building SetCurrentReticle"); if (currentReticle != null) { DestroyImmediate(currentReticle); } currentReticle = obj; if (GetBuildingState() == WorldBuildingState.MoveItem) { moveObjectOrgPosition = obj.transform.position; moveObjectOrgRotation = obj.transform.rotation; } currentClaimObject = obj.GetComponentInChildren <ClaimObject>(); // Generate Bounds before turning colliders off currentReticleBounds = new Bounds(currentReticle.transform.position, Vector3.zero); Collider[] colliders = currentReticle.GetComponents <Collider>(); foreach (Collider col in colliders) { currentReticleBounds.Encapsulate(col.bounds); col.enabled = false; } colliders = currentReticle.GetComponentsInChildren <Collider>(); foreach (Collider col in colliders) { currentReticleBounds.Encapsulate(col.bounds); col.enabled = false; } // Disable mouse wheel scroll ClientAPI.GetInputController().MouseWheelDisabled = true; }
void SpawnClaimObject(ClaimObjectData objectData) { // Add the gameObject to the claim Claim claim = GetClaim(objectData.claimID); if (claim == null) { AtavismLogger.LogWarning("No Claim found for Claim Object"); return; } if (claim.claimObjects.ContainsKey(objectData.objectID)) { return; } // Spawn the object in the world string prefabName = objectData.prefabName; int resourcePathPos = prefabName.IndexOf("Resources/"); prefabName = prefabName.Substring(resourcePathPos + 10); prefabName = prefabName.Remove(prefabName.Length - 7); GameObject prefab = (GameObject)Resources.Load(prefabName); if (prefab == null) { Debug.LogError("Builder prefab " + prefabName + " not found in the resources"); return; } GameObject claimObject = (GameObject)UnityEngine.Object.Instantiate(prefab, objectData.loc + claim.loc, objectData.orient); // TEMP: set claim object to don't delete on load DontDestroyOnLoad(claimObject); // Get the Claim Object Component ClaimObject cObject = claimObject.GetComponentInChildren <ClaimObject>(); if (cObject == null) { cObject = claimObject.AddComponent <ClaimObject>(); } cObject.ClaimID = objectData.claimID; cObject.StateUpdated(objectData.state); cObject.ID = objectData.objectID; cObject.TemplateID = objectData.templateID; cObject.Health = objectData.health; cObject.MaxHealth = objectData.maxHealth; cObject.Complete = objectData.complete; //cObject.ItemReqs = objectData. claim.claimObjects.Add(objectData.objectID, cObject); if (cObject.ID == selectedID) { SelectedObject = cObject; } }
public void SendEditObjectPosition(ClaimObject cObject, int parent) { Dictionary <string, object> props = new Dictionary <string, object>(); props.Add("action", "save"); props.Add("claimID", activeClaim.id); props.Add("objectID", cObject.ID); props.Add("loc", cObject.gameObject.transform.position); props.Add("orient", cObject.gameObject.transform.rotation); props.Add("parent", parent); NetworkAPI.SendExtensionMessage(ClientAPI.GetPlayerOid(), false, "voxel.EDIT_CLAIM_OBJECT", props); }
public void RemoveClaimObjectMessage(Dictionary <string, object> props) { int objectID = (int)props["id"]; int claimID = (int)props["claimID"]; Claim claim = GetClaim(claimID); if (claim != null) { if (selectedObject != null && objectID == selectedObject.ID) { selectedID = objectID; selectedObject = null; // Dispatch event to be picked up by UI string[] args = new string[1]; args[0] = ""; AtavismEventSystem.DispatchEvent("CLAIM_OBJECT_UPDATED", args); } Destroy(claim.claimObjects[objectID].gameObject); claim.claimObjects.Remove(objectID); } }
// Update is called once per frame void Update() { if (AtavismCursor.Instance == null || AtavismCursor.Instance.IsMouseOverUI()) { return; } if (WorldBuilder.Instance.ActiveClaim != null) { if (WorldBuilder.Instance.ActiveClaim.permissionlevel < 1 && !WorldBuilder.Instance.ActiveClaim.playerOwned) { return; } } if (GetBuildingState() == WorldBuildingState.SelectItem) { Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); RaycastHit hit; // Casts the ray and get the first game object hit if (Physics.Raycast(ray, out hit, Mathf.Infinity, objectSelectLayers)) { ClaimObject cObject = hit.transform.gameObject.GetComponent <ClaimObject>(); if (cObject == null) { cObject = hit.transform.gameObject.GetComponentInChildren <ClaimObject>(); } if (cObject == null && hit.transform.parent != null) { cObject = hit.transform.parent.GetComponent <ClaimObject>(); } if (cObject != null) { int objectID = cObject.ID; if (WorldBuilder.Instance.ActiveClaim.claimObjects.ContainsKey(objectID)) { if (hoverObject != cObject) { if (hoverObject != null) { hoverObject.ResetHighlight(); } hoverObject = cObject; cObject.Highlight(); } if (Input.GetMouseButtonDown(0)) { StopSelectObject(); //SetBuildingState(WorldBuildingState.EditItem); //objectBeingEdited = activeClaim.claimObjects[objectID].gameObject; WorldBuilder.Instance.RequestClaimObjectInfo(objectID); cObject.Highlight(); WorldBuilder.Instance.SelectedObject = cObject; } } } else if (hoverObject != null) { hoverObject.ResetHighlight(); hoverObject = null; } } else if (hoverObject != null) { hoverObject.ResetHighlight(); hoverObject = null; } } if ((GetBuildingState() == WorldBuildingState.PlaceItem || GetBuildingState() == WorldBuildingState.MoveItem) && currentReticle != null) { if (Input.GetMouseButtonDown(1)) { ClearCurrentReticle(true); WorldBuilder.Instance.BuildingState = WorldBuildingState.None; return; } GetHitLocation(); bool legalPlacement = IsLegalClaimObjectPlacement(); if (!legalPlacement) { currentReticle.GetComponentInChildren <ClaimObject>().HighlightError(); } else { currentReticle.GetComponentInChildren <ClaimObject>().ResetHighlight(); } float delta = Input.GetAxis("Mouse ScrollWheel"); if (mouseWheelBuildMode == MouseWheelBuildMode.Rotate) { Vector3 currentRotation = currentReticle.transform.rotation.eulerAngles; if (delta > 0) { //currentReticle.transform.Rotate (new Vector3 (0, -rotationAmount, 0)); currentRotation.y -= 90; } else if (delta < 0) { //currentReticle.transform.Rotate (new Vector3 (0, rotationAmount, 0)); currentRotation.y += 90; } Quaternion rotation = Quaternion.Euler(currentRotation); currentReticle.transform.rotation = rotation; } else if (mouseWheelBuildMode == MouseWheelBuildMode.MoveVertical) { if (delta > 0) { if (currentClaimObject.verticalSnapDistance > 0) { currentVerticalOffset += currentClaimObject.verticalSnapDistance; } else { currentVerticalOffset += verticalMovementAmount; } } else if (delta < 0) { if (currentClaimObject.verticalSnapDistance > 0) { currentVerticalOffset -= currentClaimObject.verticalSnapDistance; } else { currentVerticalOffset -= verticalMovementAmount; } } } //snapped = false; if ((currentReticle != null) && (WorldBuilder.Instance.ActiveClaim != null) && (WorldBuilder.Instance.InsideClaimArea(WorldBuilder.Instance.ActiveClaim, hitPoint))) { if (snap) { float newX = Mathf.Round(hitPoint.x * (1 / snapGap)) / (1 / snapGap); float newZ = Mathf.Round(hitPoint.z * (1 / snapGap)) / (1 / snapGap); float newY = hitPoint.y + currentVerticalOffset; // If the claimobject has a horizontal snap value, override default snap values if (currentClaimObject.horizontalSnapDistance > 0) { newX = Mathf.Round(hitPoint.x * (1 / currentClaimObject.horizontalSnapDistance)) / (1 / currentClaimObject.horizontalSnapDistance); newZ = Mathf.Round(hitPoint.z * (1 / currentClaimObject.horizontalSnapDistance)) / (1 / currentClaimObject.horizontalSnapDistance); } // If the claimobject has a vertical snap value, set that as well if (currentClaimObject.verticalSnapDistance > 0) { newY = Mathf.Round((hitPoint.y + currentVerticalOffset) * (1 / currentClaimObject.verticalSnapDistance)) / (1 / currentClaimObject.verticalSnapDistance); } hitPoint = new Vector3(newX, newY, newZ); // snapped = true; } if (currentClaimObject.placementType == BuildObjectPlacementType.Terrain && hitObject != null && (hitObject.GetComponent <ClaimObject>() != null || hitObject.GetComponentInParent <ClaimObject>() != null)) { // Custom snap system to line them up with each other // Get size of block Vector3 baseSize = GetSizeOfBase(currentReticle).size; // Move block along z axis by size / 2, set y to same as hit object if (hitNormal == Vector3.back) { hitPoint = hitObject.transform.position; hitPoint.z -= baseSize.z; // snapped = true; } else if (hitNormal == Vector3.forward) { hitPoint = hitObject.transform.position; hitPoint.z += baseSize.z; // snapped = true; } else if (hitNormal == Vector3.right) { hitPoint = hitObject.transform.position; hitPoint.x += baseSize.x; // snapped = true; } else if (hitNormal == Vector3.left) { hitPoint = hitObject.transform.position; hitPoint.x -= baseSize.x; // snapped = true; } // Check terrain height if (baseOverTerrainThreshold > 0) { float height = Terrain.activeTerrain.SampleHeight(hitPoint) + Terrain.activeTerrain.GetPosition().y; //Debug.Log("Hitpoint y = " + hitPoint.y + " with terrain height: " + height); if (hitPoint.y - baseOverTerrainThreshold > height) { legalPlacement = false; currentClaimObject.HighlightError(); Debug.Log("Too high above terrain"); } } } currentReticle.transform.position = hitPoint; if (currentClaimObject.autoRotateToHitNormal) { //Rotate the object to match normals Vector3 currentRotation = currentReticle.transform.rotation.eulerAngles; if (hitNormal == Vector3.back) { currentRotation.y = 180; //currentReticle.transform.rotation = Quaternion.AngleAxis(180, Vector3.up); //hitPoint = new Vector3(newX, newY, hitPoint.z); } else if (hitNormal == Vector3.forward) { currentRotation.y = 0; } else if (hitNormal == Vector3.right) { currentRotation.y = 90; } else if (hitNormal == Vector3.left) { currentRotation.y = -90; } Quaternion rotation = Quaternion.Euler(currentRotation); currentReticle.transform.rotation = rotation; } } if (Input.GetKeyDown(KeyCode.LeftShift)) { if (mouseWheelBuildMode == MouseWheelBuildMode.MoveVertical) { mouseWheelBuildMode = MouseWheelBuildMode.Rotate; #if AT_I2LOC_PRESET ClientAPI.Write(I2.Loc.LocalizationManager.GetTranslation("Changed to Rotate Mode")); #else ClientAPI.Write("Changed to Rotate Mode"); #endif } else { mouseWheelBuildMode = MouseWheelBuildMode.MoveVertical; #if AT_I2LOC_PRESET ClientAPI.Write(I2.Loc.LocalizationManager.GetTranslation("Changed to Move Vertical Mode")); #else ClientAPI.Write("Changed to Move Vertical Mode"); #endif } } if (Input.GetMouseButtonDown(0) && !AtavismUiSystem.IsMouseOverFrame()) { if (!legalPlacement) { string[] args = new string[1]; #if AT_I2LOC_PRESET args[0] = I2.Loc.LocalizationManager.GetTranslation("That object cannot be placed there"); #else args[0] = "That object cannot be placed there"; #endif AtavismEventSystem.DispatchEvent("ERROR_MESSAGE", args); } else if (GetBuildingState() == WorldBuildingState.PlaceItem) { int parent = -1; if (currentClaimObject.canHaveParent && hitObject.GetComponentInParent <ClaimObject>() != null) { parent = hitObject.GetComponentInParent <ClaimObject>().ID; } WorldBuilder.Instance.SendPlaceClaimObject(buildTemplate.id, itemBeingPlaced, currentReticle.transform, parent); // Play a coord effect if (placementCoordEffect != null) { Dictionary <string, object> props = new Dictionary <string, object>(); props["gameObject"] = ClientAPI.GetPlayerObject().GameObject; CoordinatedEffectSystem.ExecuteCoordinatedEffect(placementCoordEffect.name, props); } #if AT_I2LOC_PRESET ClientAPI.Write(I2.Loc.LocalizationManager.GetTranslation("Send place claim object message")); #else ClientAPI.Write("Send place claim object message"); #endif ClearCurrentReticle(true); itemBeingPlaced = null; SetBuildingState(WorldBuildingState.Standard); AtavismCursor.Instance.ClearItemClickedOverride(WorldBuilder.Instance.StartPlaceClaimObject); } else if (GetBuildingState() == WorldBuildingState.MoveItem) { int parent = -1; if (hitObject.GetComponent <ClaimObject>() != null) { parent = hitObject.GetComponent <ClaimObject>().ID; } WorldBuilder.Instance.SendEditObjectPosition(WorldBuilder.Instance.SelectedObject, parent); //SetBuildingState(WorldBuildingState.EditItem); SetBuildingState(WorldBuildingState.Standard); ClearCurrentReticle(false); itemBeingPlaced = null; //objectBeingEdited.GetComponent<ClaimObject>().ResetHighlight(); //objectBeingEdited = null; } } } }
public bool IsLegalClaimObjectPlacement() { // First check if the objects bounds are fully encapsulated within the claim currentReticleBounds = new Bounds(currentReticle.transform.position, Vector3.zero); Renderer[] colliders = currentReticle.GetComponents <Renderer>(); if (currentClaimObject.placementMeshes.Count > 0) { foreach (Renderer rend in currentClaimObject.placementMeshes) { currentReticleBounds.Encapsulate(rend.bounds); } } else { foreach (Renderer col in colliders) { currentReticleBounds.Encapsulate(col.bounds); } colliders = currentReticle.GetComponentsInChildren <Renderer>(); foreach (Renderer col in colliders) { currentReticleBounds.Encapsulate(col.bounds); } } if (!WorldBuilder.Instance.ActiveClaim.IsObjectFullyInsideClaim(currentReticleBounds)) { Debug.Log("Placement failed due to object not fully inside claim"); return(false); } //Debug.Log("Intersected objects count: " + ClientAPI.ScriptObject.GetComponent<WorldBuilder>().IntersectedObjects.Count); // Check if the placement is intersecting any other objects /*if (ClientAPI.ScriptObject.GetComponent<WorldBuilder>().IntersectedObjects.Count > 0) { * Debug.Log("Placement failed due to intersecting another object. Count = " + ClientAPI.ScriptObject.GetComponent<WorldBuilder>().IntersectedObjects.Count); * //foreach(Collider collider in ClientAPI.ScriptObject.GetComponent<WorldBuilder>().IntersectedObjects) { * // Debug.Log("Intersecting another object: " + collider.name); * //} * return false; * }*/ // Check if the hit point is on another object and if it allows children and that this object wants a parent if (hitObject != null) { ClaimObject cObject = hitObject.GetComponent <ClaimObject>(); if (cObject == null && currentClaimObject.requiresParent) { if (hitObject.GetComponent <ClaimObjectChild>() == null) { Debug.Log("Placement failed due to no claimobject script on hit object"); return(false); } } if (cObject != null) { if (cObject.placementType == BuildObjectPlacementType.Terrain && currentClaimObject.placementType == BuildObjectPlacementType.Terrain) { return(true); } if (!currentClaimObject.canHaveParent) { Debug.Log("Placement failed due to not allowed a parent"); return(false); } if (!cObject.allowChildren) { Debug.Log("Placement failed due to parent not allowing children"); return(false); } } } else if (currentClaimObject.requiresParent) { Debug.Log("Placement failed due to no hit object"); return(false); } // Does the object intersect with any existing claim objects foreach (ClaimObject cObject in WorldBuilder.Instance.ActiveClaim.claimObjects.Values) { if (cObject == null || cObject.gameObject == currentReticle) { continue; } if (cObject.blockingCollisionVolumes != null && cObject.blockingCollisionVolumes.Count > 0) { foreach (Collider c in cObject.blockingCollisionVolumes) { if (c != null) { if (c.bounds.Intersects(currentReticleBounds)) { Debug.Log("Placement failed due to intersecting another object"); return(false); } } } } else { foreach (Collider c in cObject.gameObject.GetComponentsInChildren <Collider>()) { if (c.bounds.Intersects(currentReticleBounds)) { Debug.Log("Placement failed due to intersecting another object"); return(false); } } } } //if (buildTemplate.objectType == "Base") { // return true; //} // Check if the placement type is valid /*if (snapped) { * Debug.Log ("Got snapped, so returning true"); * return true; * }*/ if (currentClaimObject.placementType == BuildObjectPlacementType.Terrain || currentClaimObject.placementType == BuildObjectPlacementType.TerrainOrFloor) { if (hitObject != null && hitObject.GetComponent <Terrain>()) { return(true); } } if (currentClaimObject.placementType == BuildObjectPlacementType.Floor || currentClaimObject.placementType == BuildObjectPlacementType.TerrainOrFloor) { if (hitNormal == Vector3.up) { return(true); } } if (currentClaimObject.placementType == BuildObjectPlacementType.Ceiling) { if (hitNormal == Vector3.down) { return(true); } } if (currentClaimObject.placementType == BuildObjectPlacementType.Wall) { if (hitNormal == Vector3.back || hitNormal == Vector3.forward || hitNormal == Vector3.right || hitNormal == Vector3.left) { return(true); } } //Debug.Log("Placement failed due to invalid placement location"); return(false); }
// Update is called once per frame void Update() { // Get the active claim on each frame activeClaim = ClientAPI.ScriptObject.GetComponent<WorldBuilder>().ActiveClaim; if (GetBuildingState() == WorldBuildingState.SelectItem) { Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition); RaycastHit hit; // Casts the ray and get the first game object hit if (Physics.Raycast (ray, out hit, Mathf.Infinity)) { ClaimObject cObject = hit.transform.gameObject.GetComponent<ClaimObject>(); if (cObject == null) cObject = hit.transform.gameObject.GetComponentInChildren<ClaimObject>(); if (cObject == null && hit.transform.parent != null) cObject = hit.transform.parent.GetComponent<ClaimObject>(); if (cObject != null) { int objectID = cObject.ID; if (activeClaim.claimObjects.ContainsKey(objectID)) { if (hoverObject != cObject) { if (hoverObject != null) { hoverObject.ResetHighlight(); } hoverObject = cObject; cObject.Highlight(); } if (Input.GetMouseButtonDown(0)) { StopSelectObject(); SetBuildingState(WorldBuildingState.EditItem); objectBeingEdited = activeClaim.claimObjects[objectID]; cObject.Selected = true; } } } else if (hoverObject != null) { hoverObject.ResetHighlight(); hoverObject = null; } } else if (hoverObject != null) { hoverObject.ResetHighlight(); hoverObject = null; } } if ((GetBuildingState() == WorldBuildingState.PlaceItem || GetBuildingState() == WorldBuildingState.MoveItem) && currentReticle != null) { GetHitLocation(); float delta = Input.GetAxis ("Mouse ScrollWheel"); if (mouseWheelBuildMode == MouseWheelBuildMode.Rotate) { if (delta > 0) currentReticle.transform.Rotate (new Vector3 (0, -rotationAmount, 0)); else if (delta < 0) currentReticle.transform.Rotate (new Vector3 (0, rotationAmount, 0)); } else if (mouseWheelBuildMode == MouseWheelBuildMode.MoveVertical) { if (delta > 0) { currentVerticalOffset += verticalMovementAmount; } else if (delta < 0) { currentVerticalOffset -= verticalMovementAmount; } } if ((currentReticle != null) && (activeClaim != null) && (ClientAPI.ScriptObject.GetComponent<WorldBuilder>().InsideClaimArea (activeClaim, hitPoint))) { if (snap) { float newX = Mathf.Round(hitPoint.x * (1 / snapGap)) / (1 / snapGap); float newZ = Mathf.Round(hitPoint.z * (1 / snapGap)) / (1 / snapGap); hitPoint = new Vector3(newX, hitPoint.y + currentVerticalOffset, newZ); } currentReticle.transform.position = hitPoint; } if (Input.GetKeyDown(KeyCode.LeftShift)) { if (mouseWheelBuildMode == MouseWheelBuildMode.MoveVertical) { mouseWheelBuildMode = MouseWheelBuildMode.Rotate; ClientAPI.Write("Changed to Rotate Mode"); } else { mouseWheelBuildMode = MouseWheelBuildMode.MoveVertical; ClientAPI.Write("Changed to Move Vertical Mode"); } } if (Input.GetMouseButtonDown (0) && !AtavismUiSystem.IsMouseOverFrame()) { if (GetBuildingState() == WorldBuildingState.PlaceItem) { List<int> effectPositions = itemBeingPlaced.GetEffectPositionsOfTypes("ClaimObject"); Dictionary<string, object> props = new Dictionary<string, object>(); props.Add("claim", activeClaim.id); props.Add("gameObject", itemBeingPlaced.itemEffectValues[effectPositions[0]]); props.Add("loc", currentReticle.transform.position); props.Add("orient", currentReticle.transform.rotation); props.Add("itemID", itemBeingPlaced.templateId); props.Add("itemOID", itemBeingPlaced.ItemId); NetworkAPI.SendExtensionMessage(ClientAPI.GetPlayerOid(), false, "voxel.PLACE_CLAIM_OBJECT", props); ClientAPI.Write("Send place claim object message"); ClearCurrentReticle(true); itemBeingPlaced = null; SetBuildingState(WorldBuildingState.Standard); AtavismCursor.Instance.ChangeWorldBuilderState(false); } else if (GetBuildingState() == WorldBuildingState.MoveItem) { Dictionary<string, object> props = new Dictionary<string, object>(); props.Add("action", "save"); props.Add("claimID", activeClaim.id); props.Add("objectID", objectBeingEdited.GetComponent<ClaimObject>().ID); props.Add("loc", objectBeingEdited.transform.position); props.Add("orient", objectBeingEdited.transform.rotation); NetworkAPI.SendExtensionMessage(ClientAPI.GetPlayerOid(), false, "voxel.EDIT_CLAIM_OBJECT", props); //SetBuildingState(WorldBuildingState.EditItem); SetBuildingState(WorldBuildingState.Standard); ClearCurrentReticle(false); itemBeingPlaced = null; objectBeingEdited.GetComponent<ClaimObject>().Selected = false; objectBeingEdited = null; } } } }