void Start() { float currentLowest = 100f; float currentHighest = -100f; foreach (Transform colObj in transform) { foreach (Transform child in colObj.transform) { LevelObject lvlObj = child.GetComponent <LevelObject> (); float objLow = lvlObj.transform.position.y - (lvlObj.GetComponent <Renderer> ().bounds.size.y / 2); float objHigh = lvlObj.transform.position.y + (lvlObj.GetComponent <Renderer> ().bounds.size.y / 2); lvlObj.Subscribe(this); if (currentLowest > objLow) { currentLowest = objLow; } if (currentHighest < objHigh) { currentHighest = objHigh; } } } patternHeight = currentHighest - currentLowest; }
private IEnumerator AnimateMove(GameObject parent, float xPosition) { parent.GetComponent <ContentSizeFitter>().enabled = false; parent.GetComponent <LayoutGroup>().enabled = false; for (int i = 0; i < parent.transform.childCount; i++) { LevelObject o = parent.transform.GetChild(i).GetComponent <LevelObject>(); RectTransform rt = o.GetComponent <RectTransform>(); o.MoveTo(new Vector2(rt.anchoredPosition.x + xPosition, rt.anchoredPosition.y)); yield return(new WaitForSeconds(0.05f)); } yield return(new WaitForSeconds(0.5f)); }
// add an object to the level. this step of reference is needed for later deconstruction of the level and serialization public void DeleteObject(LevelObject levelObj) { if (LevelPlacer._instance != null) { switch (levelObj.objectType) { case LevelObject.ObjectType.turret: turrets.Remove((Turret)levelObj); break; case LevelObject.ObjectType.attractor: attractors.Remove((Attractor)levelObj); break; case LevelObject.ObjectType.portal: portals.Remove((Portal)levelObj); levelObj.GetComponent <Portal>().Unlink(); break; case LevelObject.ObjectType.speedStrip: speedStrips.Remove((SpeedStrip)levelObj); break; case LevelObject.ObjectType.bouncer: bouncers.Remove((Bouncer)levelObj); break; default: //Debug.Log("Wasnt able to add the levelobject to the LevelDataMono of type " + lo.objectType); break; } //ProgressManager.GetProgress().unlocks.inventory.Add(levelObj.objectType, 1); DestroyImmediate(levelObj.gameObject); UndoManager.AddUndoPoint(); } else { Debug.LogError("LevelPlacer needed to add an object to the level."); } }
public void SetData(string jsonString) { LevelData data = JsonUtility.FromJson <LevelData>(jsonString); //Before we instantiate the new level we should delete the old one foreach (LevelObject obj in FindObjectsOfType <LevelObject>()) { Undo.DestroyObjectImmediate(obj.gameObject); } //DestroyImmediate(obj.gameObject); foreach (ObjectData objData in data.objectList) { //We try to load the object from our Resources folder based on the name stored in the Json file LevelObject objPrefab = Resources.Load <LevelObject>(objData.name); //Safety check in case the prefab doesnt exist in our Resources folder if (objPrefab == null) { Debug.LogError(objData.name + " doesnt exist!"); continue; } LevelObject objClone = Instantiate(objPrefab); Undo.RegisterCreatedObjectUndo(objClone.gameObject, "Created level"); //Unity automatically puts 'clone' behind any cloned object. We want to make sure that this is not the case objClone.name = objData.name; objClone.transform.position = objData.position; objClone.transform.rotation = objData.rotation; objClone.transform.localScale = objData.scale; Spawner s = objClone.GetComponent <Spawner>(); if (s != null) { s.team = objData.team; } } }
// checks for double clicks and selectes/deselects objects accordingly private void ClickHandler(Vector3 position) { bool objectSelected = false; if (LevelEditor.editorMode == LevelEditor.EditorMode.select) { // double click if (Time.time - doubleClickTime < doubleClickDelay && doubleClickTime > 0) { LevelObject levelObject = GetLevelObjectAt(position); if (levelObject != null) { Debug.Log("SELECTED OBJ " + levelObject.name); LevelEditor.SetSelectedObject(levelObject); objectSelected = true; } // the raycast didn't select any levelobjects if (!objectSelected) { Vector3 localPos = LevelPlacer.generatedLevel.moveArea.transform.InverseTransformPoint(position); bool clickInsideMesh = VertHelper.IsInsideMesh(LevelPlacer.generatedLevel.moveArea.meshFilter.mesh, Vector3.zero, localPos); // we double clicked inside the mesh => select the mesh if (clickInsideMesh) { LevelEditor.SetSelectedObject(LevelPlacer.generatedLevel.moveArea); objectSelected = true; } } } } else if (LevelEditor.editorMode == LevelEditor.EditorMode.edit) { if (Time.time - doubleClickTime < doubleClickDelay && doubleClickTime > 0) { LevelObject levelObject = GetLevelObjectAt(position); if (levelObject != null) { // same object got selected again, deselect it if (levelObject == LevelEditor.selectedObject) { LevelEditor.SetSelectedObject(null); objectSelected = true; } // a new object gets selected, replace the old selection with the new one. else if (levelObject != LevelEditor.selectedObject) { LevelEditor.SetSelectedObject(levelObject); objectSelected = true; } } // the raycast didn't select any levelobjects if (!objectSelected) { Vector3 localPos = LevelPlacer.generatedLevel.moveArea.transform.InverseTransformPoint(position); bool clickInsideMesh = VertHelper.IsInsideMesh(LevelPlacer.generatedLevel.moveArea.meshFilter.mesh, Vector3.zero, localPos); // we double clicked inside the mesh => select the mesh if (clickInsideMesh) { if (LevelEditor.selectedObject.objectType != LevelObject.ObjectType.moveArea) LevelEditor.SetSelectedObject(LevelPlacer.generatedLevel.moveArea); else LevelEditor.SetSelectedObject(null); objectSelected = true; } } } Debug.Log(LevelEditor.selectedObject); // there was no double click detected, if a movearea is selected, try to add vertices if (LevelEditor.selectedObject != null && LevelEditor.selectedObject.objectType == LevelObject.ObjectType.moveArea && !Handle.vertGettingSelected) { Vector3 screenPosition = Camera.main.WorldToScreenPoint(position); bool isInIgnoreArea = screenPosition.y < Camera.main.pixelHeight * ignoreBottomScreenPercent; if (!isInIgnoreArea) if (VertHandler._instance.VertexAdd(position)) { SoundManager.ButtonClicked(); } } } else if (LevelEditor.editorMode == LevelEditor.EditorMode.place) { // snapping Vector3 snapPos = VertHelper.Snap(position, false); LevelObject.ObjectType objectType = UILevelObject.currentSelectedObject.objectType; Vector3 screenPosition = Camera.main.WorldToScreenPoint(snapPos); bool isInIgnoreArea = screenPosition.y < Camera.main.pixelHeight * ignoreBottomScreenPercent; // the snapping found a valid snap position around the given position if (snapPos != Vector3.zero && !isInIgnoreArea) { Debug.Log("Buttom screen percent " + ignoreBottomScreenPercent + " screenPos.y " + screenPosition.y); // return to selection mode and deselect the inventory item LevelEditor.SetSelectedObject(LevelPlacer.generatedLevel.AddObject(objectType, snapPos)); UILevelObject.onItemSelect.Invoke(null); } } else if (LevelEditor.editorMode == LevelEditor.EditorMode.portalLink) { if (Time.time - doubleClickTime < doubleClickDelay && doubleClickTime > 0) { LevelObject levelObject = GetLevelObjectAt(position); if (levelObject != null) { if (levelObject.objectType == LevelObject.ObjectType.portal) { Portal p = levelObject.GetComponent<Portal>(); UIPortalMenu.SelectLinkPortal(p); } } } } doubleClickTime = Time.time; }
void Update() { if (!currentButton) { return; } var worldPos = Camera.main.ScreenToWorldPoint(Input.mousePosition); worldPos.z = 0; bool overlap = false; if (currentHeldObject) { var halfWidth = currentHeldObject.GetOffset(); //halfWidth.x *= currentHeldObject.transform.localScale.x; //halfWidth.y *= currentHeldObject.transform.localScale.y; worldPos.x -= worldPos.x % gridSize.x; worldPos.y -= worldPos.y % gridSize.y; worldPos += halfWidth; currentHeldObject.transform.position = worldPos; if (Input.GetMouseButtonUp(0) && currentTool == Tool.Move) { currentHeldObject = null; } for (int i = 0; i < levelObjects.Count; i++) { //TODO fix error here var c2D = currentHeldObject.bc; if (c2D.bounds.Intersects(levelObjects[i].bc.bounds)) { overlap = true; break; } } var objColor = Color.green; if (overlap || !bounds) { objColor = Color.red; } var sr = new [] { currentHeldObject.GetComponent <SpriteRenderer>() }; if (sr[0] == null) { sr = currentHeldObject.GetComponentsInChildren <SpriteRenderer>(); } for (int i = 0; i < sr.Length; i++) { sr[i].color = objColor; } } if (!overlap && currentTool == Tool.Create && !EventSystem.current.IsPointerOverGameObject() && Input.GetMouseButtonDown(0)) { var o = Instantiate(currentButton.createdObject); o.Init(this); o.transform.position = worldPos; levelObjects.Add(o); } }
void Update() { //Debug.Log(Camera.main.ScreenToWorldPoint(Input.mousePosition)); // Only allow map editing if the game is not simulating if (!isSimulating && mapEditMode) { // If a mouse click is detected while in edit mode if (Input.GetMouseButtonDown(0) && GUIUtility.hotControl == 0) { // We get any levelObject at the position from where the mouse is clicked LevelObject objectAtMousePosition = GetObjectAtMousePosition(); // This handles the levelObjectPlacement if (objectToSpawn != ObjectType.None && // We check if we have something to spawn CurrentMode == LevelEditorMode.PlacementMode && // We also check if the current mode is objectPlacement objectAtMousePosition == null) // Finally we check if there is an object at the current position { // If there is none, then continue the object placement HandleLevelObjectPlacement(); } // This handles the picking of level Objects for editing else if (objectAtMousePosition != null && CurrentMode == LevelEditorMode.PickToEditMode && CurrentMode != LevelEditorMode.PickToLinkMode) // If there is an object { // We open the attribute window OpenAttributeWindow(objectAtMousePosition); } // This handles the picking of level objects for linking else if (CurrentMode == LevelEditorMode.PickToLinkMode) { objectToDisplay.GetComponent <Switch>().PlaceInLinkedObjectsListAtIndex(objectAtMousePosition); CurrentMode = LevelEditorMode.EditingMode; } } // Pressing the right mouse button deletes the object at mouse position else if (Input.GetMouseButtonDown(1) && GUIUtility.hotControl == 0) { HandleLevelObjectDeletion(); } // Pressing the middle mouse button edits the object at mouse position else if (Input.GetMouseButtonDown(2) && GUIUtility.hotControl == 0) { // We get any levelObjsect at the position from where the mouse is clicked LevelObject objectAtMousePosition = GetObjectAtMousePosition(); OpenAttributeWindow(objectAtMousePosition); } } if (Registry.debugConfig.isCameraOrthographic) { return; } if (mapEditMode) { Camera.main.orthographic = true; originMarkerRenderer.enabled = true; } else { Camera.main.orthographic = false; originMarkerRenderer.enabled = false; } }