void SyncSectorCoords(SectorWCWrapper wrapper) { wrapper.sector.bounds.x = Mathf.Min((int)wrapper.renderer.GetPosition(0).x, (int)wrapper.renderer.GetPosition(2).x); wrapper.sector.bounds.y = Mathf.Max((int)wrapper.renderer.GetPosition(0).y, (int)wrapper.renderer.GetPosition(2).y); wrapper.sector.bounds.w = Mathf.Abs((int)wrapper.renderer.GetPosition(2).x - (int)wrapper.renderer.GetPosition(0).x); wrapper.sector.bounds.h = Mathf.Abs((int)wrapper.renderer.GetPosition(2).y - (int)wrapper.renderer.GetPosition(0).y); }
void TranslateSector(SectorWCWrapper sector, Vector2 offset) { for (int i = 0; i < 4; i++) { sector.renderer.SetPosition(i, sector.originalRendererPos[i] + offset); } SyncSectorCoords(sector); }
void RemoveSector(SectorWCWrapper sector) { Destroy(sector.renderer.gameObject); if (sectors.Contains(sector)) { sectors.Remove(sector); } }
// revert or destroy pending sector if it exists void RemovePendingSector() { if (currentSector != null) { if (lastSectorPos != null) { for (int i = 0; i < 4; i++) { currentSector.renderer.SetPosition(i, lastSectorPos[i]); } SyncSectorCoords(currentSector); currentSector = null; lastSectorPos = null; } else { sectors.Remove(currentSector); Destroy(currentSector.renderer.gameObject); currentSector = null; } } }
void PollSectors() { if (!Input.GetKey(KeyCode.LeftShift) && translatingSectors.Count > 0) { FinalizeTranslation(); translatingSectors.Clear(); return; } if (Input.GetMouseButtonDown(0)) { foreach (SectorWCWrapper sector in sectors) { LineRenderer renderer = sector.renderer; if (CheckMouseContainsSector(renderer)) { origPos = GetSectorOriginalPosition(renderer); lastSectorPos = new Vector3[4]; for (int i = 0; i < 4; i++) { lastSectorPos[i] = renderer.GetPosition(i); } //renderer.SetPosition(0, origPos); if (Input.GetKey(KeyCode.LeftShift) && Time.time - doubleClickTimer < 0.2F) { if (!translatingSectors.Contains(sector)) { translatingSectors.Add(sector); } else { translatingSectors.Remove(sector); } doubleClickTimer = 0; } else { currentSector = sector; } doubleClickTimer = Time.time; break; } } sectorStoredMousePos = Input.mousePosition; if (Input.GetKey(KeyCode.LeftShift)) { foreach (var sector in translatingSectors) { for (int i = 0; i < 4; i++) { sector.originalRendererPos[i] = sector.renderer.GetPosition(i); } } sectorTranslationStoredPos = CalcSectorPos(); } else { if (currentSector == null) { currentSector = new SectorWCWrapper(); currentSector.sector = ScriptableObject.CreateInstance <Sector>(); currentSector.sector.backgroundSpawns = new Sector.BackgroundSpawn[0]; currentSector.sector.hasMusic = true; // sectors have music by default in WC currentSector.sector.backgroundColor = GetDefaultColor((Sector.SectorType) 0); currentSector.sector.rectangleEffectSkin = (RectangleEffectSkin) PlayerPrefs.GetInt("WCSectorPropertyDisplay_defaultParticles", 0); currentSector.sector.backgroundTileSkin = (BackgroundTileSkin) PlayerPrefs.GetInt("WCSectorPropertyDisplay_defaultTiles", 0); var renderer = currentSector.renderer = Instantiate(borderPrefab).GetComponent <LineRenderer>(); renderer.GetComponentInChildren <WorldCreatorSectorRepScript>().sector = currentSector.sector; lastSectorPos = null; origPos = CalcSectorPos(); renderer.SetPosition(0, origPos); renderer.SetPosition(1, origPos); renderer.SetPosition(2, origPos); renderer.SetPosition(3, origPos); SyncSectorCoords(currentSector); } } } else if (Input.GetMouseButtonUp(0)) { if (Input.GetKey(KeyCode.LeftShift)) { FinalizeTranslation(); } if ((Vector2)Input.mousePosition == sectorStoredMousePos) { foreach (SectorWCWrapper sector in sectors) { LineRenderer renderer = sector.renderer; if (CheckMouseContainsSector(renderer) && !Input.GetKey(KeyCode.LeftShift)) { sectorPropertyDisplay.DisplayProperties(sector.sector); currentSector = null; return; } } } if (currentSector != null) { if (!CheckForSectorOverlap(currentSector.renderer) && CheckSectorSize(currentSector.renderer)) { if (lastSectorPos == null) { sectors.Add(currentSector); } } else if (lastSectorPos != null) // invalid position for current sector { for (int i = 0; i < 4; i++) { currentSector.renderer.SetPosition(i, lastSectorPos[i]); } SyncSectorCoords(currentSector); lastSectorPos = null; } else // delete sector { RemoveSector(currentSector); } currentSector = null; // reset reference } } else if (Input.GetMouseButton(0)) { if (Input.GetKey(KeyCode.LeftShift)) { foreach (var sector in translatingSectors) { TranslateSector(sector, CalcSectorPos() - sectorTranslationStoredPos); if (CheckForSectorOverlap(sector.renderer)) { if (sector.renderer.sortingOrder < sortLayerNum) { sector.renderer.sortingOrder = ++sortLayerNum; } sector.renderer.startColor = sector.renderer.endColor = Color.red; } } } else if (currentSector != null && (Vector2)Input.mousePosition != sectorStoredMousePos) { var renderer = currentSector.renderer; renderer.SetPosition(0, origPos); renderer.SetPosition(1, new Vector3(origPos.x, CalcSectorPos().y, 0)); renderer.SetPosition(2, CalcSectorPos()); renderer.SetPosition(3, new Vector3(CalcSectorPos().x, origPos.y, 0)); SyncSectorCoords(currentSector); // check for overlap if (CheckForSectorOverlap(renderer)) { if (renderer.sortingOrder < sortLayerNum) { renderer.sortingOrder = ++sortLayerNum; } renderer.startColor = renderer.endColor = Color.red; } } } else if (Input.GetMouseButtonUp(1) && !Input.GetKey(KeyCode.LeftShift)) { foreach (SectorWCWrapper sector in sectors) { var renderer = sector.renderer; renderer.startColor = renderer.endColor = Color.white; if (CheckMouseContainsSector(renderer)) { Destroy(renderer.gameObject); if (sectors.Contains(sector)) { sectors.Remove(sector); currentSector = null; return; } } } } }
bool CheckMouseContainsSector(SectorWCWrapper sector) { return(CheckMouseContainsSector(sector.renderer) && sector.sector.dimension == currentDim); }