public void Update() { RaycastHit raycast = LayoutManager.Instance.CurrentCamera.CurrentRaycast; if (!raycast.transform) { return; } OverlayMesh overlayMesh = raycast.transform.GetComponent <OverlayMesh>(); TileEntity tileEntity = raycast.transform.GetComponent <TileEntity>(); }
private void Update() { RaycastHit raycast = LayoutManager.Instance.CurrentCamera.CurrentRaycast; if (!raycast.transform) { return; } OverlayMesh overlayMesh = raycast.transform.GetComponent <OverlayMesh>(); TileEntity tileEntity = raycast.transform.GetComponent <TileEntity>(); int floor = 0; int x = -1; int y = -1; if (tileEntity && tileEntity.Valid) { floor = tileEntity.Floor; x = tileEntity.Tile.X; y = tileEntity.Tile.Y; } else if (overlayMesh) { floor = LayoutManager.Instance.CurrentCamera.Floor; x = Mathf.FloorToInt(raycast.point.x / 4f); y = Mathf.FloorToInt(raycast.point.z / 4f); } if (floor == 0 || floor == -1) { LayoutManager.Instance.TooltipText += "\n<color=red><b>It's not possible to place roofs on ground floor</b></color>"; return; } if (Input.GetMouseButton(0)) { RoofData data = GuiManager.Instance.RoofsList.SelectedValue as RoofData; GameManager.Instance.Map[x, y].SetRoof(data, floor); } else if (Input.GetMouseButton(1)) { GameManager.Instance.Map[x, y].SetRoof(null, floor); } if (Input.GetMouseButtonUp(0) || Input.GetMouseButtonUp(1)) { GameManager.Instance.Map.CommandManager.FinishAction(); } }
private void Update() { if (Input.GetMouseButtonUp(0) || Input.GetMouseButtonUp(1)) { GameManager.Instance.Map.CommandManager.FinishAction(); } RaycastHit raycast = LayoutManager.Instance.CurrentCamera.CurrentRaycast; if (!raycast.transform) { return; } OverlayMesh overlayMesh = raycast.transform.GetComponent <OverlayMesh>(); GroundMesh groundMesh = raycast.transform.GetComponent <GroundMesh>(); TileEntity tileEntity = raycast.transform.GetComponent <TileEntity>(); Wall wallEntity = tileEntity as Wall; bool automaticReverse = automaticReverseToggle.isOn; bool reverse = reverseToggle.isOn; int floor = 0; int x = -1; int y = -1; bool horizontal = false; bool propertiesNeedSaving = false; if (automaticReverse != Properties.Instance.WallAutomaticReverse) { Properties.Instance.WallAutomaticReverse = automaticReverse; propertiesNeedSaving = true; } if (reverse != Properties.Instance.WallReverse) { Properties.Instance.WallReverse = reverse; propertiesNeedSaving = true; } if (propertiesNeedSaving) { Properties.Instance.SaveProperties(); } if (wallEntity && wallEntity.Valid) { floor = tileEntity.Floor; if (LayoutManager.Instance.CurrentCamera.Floor == floor + 1) { floor++; } x = tileEntity.Tile.X; y = tileEntity.Tile.Y; EntityType type = tileEntity.Type; horizontal = (type == EntityType.Hwall || type == EntityType.Hfence); } else if (overlayMesh || groundMesh) { if (overlayMesh) { floor = LayoutManager.Instance.CurrentCamera.Floor; } else if (groundMesh) { floor = 0; } TileSelectionHit tileSelectionHit = TileSelection.PositionToTileSelectionHit(raycast.point, TileSelectionMode.Borders); TileSelectionTarget target = tileSelectionHit.Target; if (target == TileSelectionTarget.Nothing) { return; } x = tileSelectionHit.X; y = tileSelectionHit.Y; horizontal = (target == TileSelectionTarget.BottomBorder); } if (Input.GetMouseButton(0)) { Floor currentFloor = GameManager.Instance.Map[x, y].GetTileContent(floor) as Floor; bool shouldReverse = false; if (automaticReverse && horizontal) { Floor nearFloor = GameManager.Instance.Map[x, y - 1].GetTileContent(floor) as Floor; shouldReverse = currentFloor && !nearFloor; } else if (automaticReverse && !horizontal) { Floor nearFloor = GameManager.Instance.Map[x - 1, y].GetTileContent(floor) as Floor; shouldReverse = !currentFloor && nearFloor; } if (reverse) { shouldReverse = !shouldReverse; } WallData data = GuiManager.Instance.WallsTree.SelectedValue as WallData; if (horizontal) { GameManager.Instance.Map[x, y].SetHorizontalWall(data, shouldReverse, floor); } else { GameManager.Instance.Map[x, y].SetVerticalWall(data, shouldReverse, floor); } } else if (Input.GetMouseButton(1)) { if (floor != LayoutManager.Instance.CurrentCamera.Floor) { return; } if (horizontal) { GameManager.Instance.Map[x, y].SetHorizontalWall(null, false, floor); } else { GameManager.Instance.Map[x, y].SetVerticalWall(null, false, floor); } } }
private void Update() { if (Input.GetMouseButtonUp(0) || Input.GetMouseButtonUp(1)) { GameManager.Instance.Map.CommandManager.FinishAction(); } RaycastHit raycast = LayoutManager.Instance.CurrentCamera.CurrentRaycast; if (!raycast.transform) { return; } OverlayMesh overlayMesh = raycast.transform.GetComponent <OverlayMesh>(); TileEntity tileEntity = raycast.transform.GetComponent <TileEntity>(); int floor = 0; int x = -1; int y = -1; if (tileEntity && tileEntity.Valid) { floor = tileEntity.Floor; x = tileEntity.Tile.X; y = tileEntity.Tile.Y; } else if (overlayMesh) { floor = LayoutManager.Instance.CurrentCamera.Floor; x = Mathf.FloorToInt(raycast.point.x / 4f); y = Mathf.FloorToInt(raycast.point.z / 4f); } FloorData data = GuiManager.Instance.FloorsTree.SelectedValue as FloorData; if (data.Opening && (floor == 0 || floor == -1)) { LayoutManager.Instance.TooltipText += "\n<color=red><b>It's not possible to place openings/stairs on ground floor</b></color>"; return; } FloorOrientation orientation = FloorOrientation.Down; if (southToggle.isOn) { orientation = FloorOrientation.Down; } else if (westToggle.isOn) { orientation = FloorOrientation.Right; } else if (northToggle.isOn) { orientation = FloorOrientation.Up; } else if (eastToggle.isOn) { orientation = FloorOrientation.Left; } if (Input.GetMouseButton(0)) { GameManager.Instance.Map[x, y].SetFloor(data, orientation, floor); } else if (Input.GetMouseButton(1)) { GameManager.Instance.Map[x, y].SetFloor(null, orientation, floor); } }
private void Update() { if (currentTool != ToolType.MaterialsCalculator) { // we need to react to actions on map only when calculating materials return; } RaycastHit raycast = LayoutManager.Instance.CurrentCamera.CurrentRaycast; if (!raycast.transform) { return; } OverlayMesh overlayMesh = raycast.transform.GetComponent <OverlayMesh>(); if (!overlayMesh) { return; } if (Input.GetMouseButtonDown(0)) { int floor = LayoutManager.Instance.CurrentCamera.Floor; int x = Mathf.FloorToInt(raycast.point.x / 4f); int y = Mathf.FloorToInt(raycast.point.z / 4f); Map map = GameManager.Instance.Map; Tile clickedTile = map[x, y]; if (buildingAllLevelsMaterialsToggle.isOn) { BuildingsSummary surfaceGroundSummary = new BuildingsSummary(map, 0); Materials materials = new Materials(); Building building = surfaceGroundSummary.GetBuildingAtTile(clickedTile); if (building == null) { ShowMaterialsWindow("No valid building on clicked tile"); return; } foreach (TileSummary tileSummary in building.AllTiles) { Tile tile = map[tileSummary.X, tileSummary.Y]; materials.Add(tile.CalculateTileMaterials(tileSummary.TilePart)); } StringBuilder summary = new StringBuilder(); summary.Append("Carpentry needed: ").Append(building.GetCarpentryRequired()).AppendLine(); summary.Append("Total tiles: ").Append(building.TilesCount).AppendLine(); summary.AppendLine(); summary.Append(materials); ShowMaterialsWindow(summary.ToString()); if (Debug.isDebugBuild) { Debug.Log(building.CreateSummary()); } } else if (buildingCurrentLevelMaterialsToggle.isOn) { BuildingsSummary surfaceGroundSummary = new BuildingsSummary(map, floor); Materials materials = new Materials(); Building building = surfaceGroundSummary.GetBuildingAtTile(clickedTile); if (building == null) { ShowMaterialsWindow("No valid building on clicked tile"); return; } foreach (TileSummary tileSummary in building.AllTiles) { Tile tile = map[tileSummary.X, tileSummary.Y]; materials.Add(tile.CalculateFloorMaterials(floor, tileSummary.TilePart)); } StringBuilder summary = new StringBuilder(); if (floor == 0 || floor == -1) { summary.Append("Carpentry needed: ").Append(building.GetCarpentryRequired()).AppendLine(); } else { summary.AppendLine("To calculate carpentry needed, please use this option on a ground floor"); } summary.Append("Rooms on this level: ").Append(building.RoomsCount).AppendLine(); summary.Append("Tiles on this level: ").Append(building.TilesCount).AppendLine(); summary.AppendLine(); summary.Append(materials); ShowMaterialsWindow(summary.ToString()); if (Debug.isDebugBuild) { Debug.Log(building.CreateSummary()); } } else if (roomCurrentLevelMaterialsToggle.isOn) { BuildingsSummary surfaceGroundSummary = new BuildingsSummary(map, floor); Materials materials = new Materials(); Room room = surfaceGroundSummary.GetRoomAtTile(clickedTile); if (room == null) { ShowMaterialsWindow("No valid room on clicked tile"); return; } foreach (TileSummary tileSummary in room.Tiles) { Tile tile = map[tileSummary.X, tileSummary.Y]; materials.Add(tile.CalculateFloorMaterials(floor, tileSummary.TilePart)); } StringBuilder summary = new StringBuilder(); summary.Append("Tiles in this room: ").Append(room.Tiles.Count).AppendLine(); summary.AppendLine(); summary.Append(materials); ShowMaterialsWindow(summary.ToString()); if (Debug.isDebugBuild) { Debug.Log(room.CreateSummary()); } } } }
public void Initialize(int width, int height, OverlayMesh newOverlayMesh) { gameObject.layer = LayerMasks.GroundLayer; if (groundTexturesArray == null) { int groundTexturesCount = CalculateGroundTexturesCount(); if (Properties.Web) { groundTexturesArray = new IndexedTextureArray <TextureReference>(GroundTexturesWidth, GroundTexturesHeight, groundTexturesCount, WebGroundTexturesFormat); } else { groundTexturesArray = new IndexedTextureArray <TextureReference>(GroundTexturesWidth, GroundTexturesHeight, groundTexturesCount, DefaultGroundTexturesFormat); } } gameObject.layer = LayerMasks.GroundLayer; if (!material) { material = new Material(GraphicsManager.Instance.TerrainMaterial); material.mainTexture = groundTexturesArray.TextureArray; } if (!meshRenderer) { meshRenderer = gameObject.AddComponent <MeshRenderer>(); } if (!meshFilter) { meshFilter = gameObject.AddComponent <MeshFilter>(); } if (!meshCollider) { meshCollider = gameObject.AddComponent <MeshCollider>(); } meshRenderer.sharedMaterial = material; Width = width; Height = height; overlayMesh = newOverlayMesh; slopesArray = new int[Width + 1, Height + 1]; dataArray = new GroundData[Width, Height]; directionsArray = new RoadDirection[Width, Height]; RenderMesh = new Mesh(); RenderMesh.name = "ground render mesh"; meshFilter.sharedMesh = RenderMesh; ColliderMesh = new Mesh(); ColliderMesh.name = "ground collider mesh"; meshCollider.sharedMesh = ColliderMesh; InitializeRenderMesh(); InitializeColliderMesh(); needsVerticesUpdate = true; needsUvUpdate = true; }
private void Update() { bool snapToGrid = snapToGridToggle.isOn; bool rotationSnapping = rotationSnappingToggle.isOn; float rotationEditSensitivity = 1; float.TryParse(rotationSensitivityInput.text, NumberStyles.Any, CultureInfo.InvariantCulture, out rotationEditSensitivity); bool propertiesNeedSaving = false; if (Math.Abs(rotationEditSensitivity - Properties.Instance.DecorationRotationSensitivity) > float.Epsilon) { Properties.Instance.DecorationRotationSensitivity = rotationEditSensitivity; propertiesNeedSaving = true; } if (snapToGrid != Properties.Instance.DecorationSnapToGrid) { Properties.Instance.DecorationSnapToGrid = snapToGrid; propertiesNeedSaving = true; } if (rotationSnapping != Properties.Instance.DecorationRotationSnapping) { Properties.Instance.DecorationRotationSnapping = rotationSnapping; propertiesNeedSaving = true; } if (propertiesNeedSaving) { Properties.Instance.SaveProperties(); } RaycastHit raycast = LayoutManager.Instance.CurrentCamera.CurrentRaycast; if (!raycast.transform) { if (ghostObject) { ghostObject.SetActive(false); } return; } DecorationData data = (DecorationData)GuiManager.Instance.ObjectsTree.SelectedValue; bool dataChanged = data != lastFrameData; lastFrameData = data; if (!data) { return; } OverlayMesh overlayMesh = raycast.transform.GetComponent <OverlayMesh>(); GroundMesh groundMesh = raycast.transform.GetComponent <GroundMesh>(); TileEntity tileEntity = raycast.transform.GetComponent <TileEntity>(); Material ghostMaterial = GraphicsManager.Instance.GhostMaterial; if (dataChanged) { CoroutineManager.Instance.QueueCoroutine(data.Model.CreateOrGetModel(ghostMaterial, OnGhostCreated)); return; } if (!ghostObject) { return; } int targetFloor = LayoutManager.Instance.CurrentCamera.Floor; if (tileEntity && tileEntity.Valid && tileEntity.GetType() == typeof(Floor)) { targetFloor = tileEntity.Floor; } if (data.CenterOnly || data.Tree || data.Bush) { targetFloor = 0; } Map map = GameManager.Instance.Map; if (!placingDecoration) { position = CalculateCorrectedPosition(raycast.point, data, snapToGrid); targetedTile = null; if (overlayMesh) { int tileX = Mathf.FloorToInt(position.x / 4f); int tileY = Mathf.FloorToInt(position.z / 4f); targetedTile = map[tileX, tileY]; position.y = map.GetInterpolatedHeight(position.x, position.z); if (data.Floating) { position.y = Mathf.Max(position.y, 0); } else { float floorHeight = 3f; position.y += targetFloor * floorHeight; } } else if (tileEntity && tileEntity.Valid) { targetedTile = tileEntity.Tile; } } bool canPlaceNewObject = overlayMesh || groundMesh || (tileEntity && tileEntity.Valid && tileEntity.GetType() == typeof(Floor)); if (canPlaceNewObject || placingDecoration) { ghostObject.gameObject.SetActive(true); ghostObject.transform.position = position; } else { ghostObject.gameObject.SetActive(false); } bool placementAllowed = true; Vector2 position2d = new Vector2(position.x, position.z); IEnumerable <Decoration> nearbyDecorations = GetAllNearbyDecorations(targetedTile); foreach (Decoration decoration in nearbyDecorations) { Vector3 decorationPosition3d = decoration.transform.position; Vector2 decorationPosition2d = new Vector2(decorationPosition3d.x, decorationPosition3d.z); float distance = Vector2.Distance(position2d, decorationPosition2d); if (distance < minimumPlacementGap) { placementAllowed = false; break; } } ToggleGhostPropertyBlock(placementAllowed ? allowedGhostPropertyBlock : disabledGhostPropertyBlock); if (Input.GetMouseButtonDown(0) && placementAllowed) { placingDecoration = true; dragStartPos = LayoutManager.Instance.CurrentCamera.MousePosition; } if (Input.GetMouseButton(0) && placingDecoration) { Vector2 dragEndPos = LayoutManager.Instance.CurrentCamera.MousePosition; Vector2 difference = dragEndPos - dragStartPos; rotation = -difference.x * rotationEditSensitivity; if (rotationSnapping) { rotation = Mathf.Round(rotation / 45f) * 45f; } ghostObject.transform.localRotation = Quaternion.Euler(0, rotation, 0); } if (Input.GetMouseButtonUp(0) && placingDecoration) { float decorationPositionX = position.x - targetedTile.X * 4f; float decorationPositionY = position.z - targetedTile.Y * 4f; Vector2 decorationPosition = new Vector2(decorationPositionX, decorationPositionY); targetedTile.SetDecoration(data, decorationPosition, rotation * Mathf.Deg2Rad, targetFloor, data.Floating); map.CommandManager.FinishAction(); placingDecoration = false; ghostObject.transform.localRotation = Quaternion.identity; } if (Input.GetMouseButtonDown(1)) { placingDecoration = false; ghostObject.transform.localRotation = Quaternion.identity; } if (Input.GetMouseButtonDown(1) && !placingDecoration) { IEnumerable <Decoration> decorationsOnTile = targetedTile.GetDecorations(); foreach (Decoration decoration in decorationsOnTile) { targetedTile.SetDecoration(null, decoration.Position, decoration.Rotation, targetFloor); } map.CommandManager.FinishAction(); } }