public ReflectorController SpawnReflector(GridElementController container, ReflectorType type) { var prefab = ItemsMap.Reflectors.FirstOrDefault((p) => p.Type == type).Prefab; var reflector = Instantiate(prefab, container.transform, false); return(reflector); }
public AbsorberController SpawnAbsorber(GridElementController container, AbsorberType type) { var prefab = ItemsMap.Absorbers.FirstOrDefault((p) => p.Type == type).Prefab; var absorber = Instantiate(prefab, container.transform, false); return(absorber); }
private void RefreshTargetTile(GridController grid, GridElementController elem, Vector2?gridPosition = null) { var ray = HandleUtility.GUIPointToWorldRay(Event.current.mousePosition); gridPosition = gridPosition ?? grid.RaycastGrid(ray); targetTile = gridPosition == null ? new GridTile(0, 0) : grid.GetGridTile(gridPosition.Value); isSuitableTile = grid.CanPlaceIn(elem, targetTile); }
private void ResetEntityToSpawn() { if (placeControlId == GUIUtility.hotControl) { GUIUtility.hotControl = 0; } var levelController = ((LevelController)target); levelController.EntityToSpawn = null; if (entityToSpawnGridElementContainer != null) { DestroyImmediate(entityToSpawnGridElementContainer.gameObject); entityToSpawnGridElementContainer = null; } }
public GameObject InstantiateEntity(GridElementController container, LevelEntity entity) { switch (entity.Type) { case EntityType.Emitter: return(SpawnEmitter(container, entity.EmitterType).gameObject); case EntityType.Absorber: return(SpawnAbsorber(container, entity.AbsorberType).gameObject); case EntityType.Reflector: return(SpawnReflector(container, entity.ReflectorType).gameObject); default: throw new Exception($"Unknown entity type {entity.Type}."); } }
private static void DrawGizmos(GridElementController target, GizmoType gizmoType) { // Only for convenient grid tile selection var grid = target.GetComponentInParent <GridController>(); if (grid == null) { return; } var gridPos = grid.WorldToGrid(target.transform.position); var rect = grid.GetTileRect(grid.GetGridTile(gridPos)); var a = grid.GridToWorld(new Vector2(rect.xMin, rect.yMin)); var b = grid.GridToWorld(new Vector2(rect.xMax, rect.yMax)); var p = grid.GridToWorld(rect.center) + new Vector3(0, 0, 0); var s = new Vector3(b.x - a.x, 0, b.z - a.z); Gizmos.color = new Color(0, 0, 0, 0); Gizmos.DrawCube(p, s); }
private bool CanBePlaced() { bool canBagBePlaced = true; _matchedGridElements = new List <GridElementController>(); bool isOffsetFound = false; Vector2 nearestGridOffset = new Vector2(); foreach (Collider2D bagAnchor in bagAnchors) { bool canAnchorBePlaced = false; foreach (Collider2D gridElementCollider in _gridElements) { GridElementController gridElement = gridElementCollider.GetComponent <GridElementController>(); if (!gridElement.isOccupied && bagAnchor.IsTouching(gridElementCollider)) { if (!isOffsetFound) { isOffsetFound = true; nearestGridOffset = gridElementCollider.transform.position - bagAnchor.transform.position; } canAnchorBePlaced = true; _matchedGridElements.Add(gridElement); } } canBagBePlaced = canBagBePlaced && canAnchorBePlaced; } if (canBagBePlaced) { _nearestGridOffset = nearestGridOffset; } return(canBagBePlaced); }
private void RenderEntityPreview() { var level = ((LevelController)target); placeControlId = EditorGUIUtility.GetControlID(FocusType.Keyboard); if (level.EntityToSpawn != null && entityToSpawnGridElementContainer == null) { var grid = level.GetComponent <GridController>(); var ray = HandleUtility.GUIPointToWorldRay(Event.current.mousePosition); var tile = grid.RaycastTile(ray); if (tile == null) { tile = new GridTile(0, 0); } entityToSpawnGridElementContainer = level.CreateGridElement(tile.Value); entityToSpawnGridElementContainer.tag = "EditorOnly"; entityToSpawnGridElementContainer.name = $"Temporal_{entityToSpawnGridElementContainer.name}"; level.InstantiateEntity(entityToSpawnGridElementContainer, level.EntityToSpawn); EditorUtilities.ApplyMaterialRecursively(entityToSpawnGridElementContainer.gameObject, (Material)EditorGUIUtility.Load("Materials/temp_grid_item.mat")); GUIUtility.hotControl = placeControlId; } if (entityToSpawnGridElementContainer != null && GUIUtility.hotControl == placeControlId) { var grid = level.GetComponent <GridController>(); var ray = HandleUtility.GUIPointToWorldRay(Event.current.mousePosition); var tile = grid.RaycastTile(ray); var isSuitableTile = tile == null ? false : grid.CanPlaceIn(entityToSpawnGridElementContainer, tile.Value); if (Event.current.type == EventType.MouseMove) { if (tile != null && isSuitableTile) { entityToSpawnGridElementContainer.Tile = tile.Value; level.EntityToSpawn.Tile = tile.Value; grid.Layout(); } Event.current.Use(); } else if (Event.current.type == EventType.Repaint || Event.current.type == EventType.Layout) { if (tile != null) { var rectColor = isSuitableTile ? new Color(0, 1, 0, 0.1f) : new Color(1, 0, 0, 0.3f); var outlineColor = isSuitableTile ? new Color(0, 1, 0, 0.5f) : new Color(1, 0, 0, 0.7f); var rect = grid.GetWorldSpacedTileRect(tile.Value); Handles.DrawSolidRectangleWithOutline(rect, rectColor, outlineColor); } } else if (Event.current.type == EventType.KeyDown) { if (Event.current.keyCode == KeyCode.Delete || Event.current.keyCode == KeyCode.Escape) { ResetEntityToSpawn(); Event.current.Use(); } } } if (Event.current.type == EventType.MouseDown && Event.current.button == 0 && level.EntityToSpawn != null) { level.SpawnEntity(level.EntityToSpawn); ResetEntityToSpawn(); Event.current.Use(); } }
private void MovingHandle(GridElementController elem) { var grid = elem.GetComponentInParent <GridController>(); if (grid == null) { return; } var id = GUIUtility.GetControlID(FocusType.Passive); switch (Event.current.type) { case EventType.Layout: case EventType.Repaint: var gridPos = grid.WorldToGrid(elem.transform.position); if (GUIUtility.hotControl == id) { var ray = HandleUtility.GUIPointToWorldRay(Event.current.mousePosition); gridPos = grid.RaycastGrid(ray) ?? gridPos; } var isMoving = GUIUtility.hotControl == id; var tile = grid.GetGridTile(gridPos); var tileRect = grid.GetWorldSpacedTileRect(tile); var rectColor = isSuitableTile ? new Color(0, 1, 0, 0.1f) : new Color(1, 0, 0, 0.3f); var outlineColor = isSuitableTile ? new Color(0, 1, 0, 0.5f) : new Color(1, 0, 0, 0.7f); var innerRectColor = isSuitableTile ? (isMoving ? new Color(1, 1, 0, 0.1f) : rectColor) : new Color(1, 0, 0, 0.3f); var innerRectOutlineColor = isSuitableTile ? (isMoving ? new Color(1, 1, 0, 0.5f) : outlineColor) : new Color(1, 0, 0, 0.7f); var handlePos = grid.GridToWorld(gridPos); var size = 4; var innerRectSize = size * 0.5f; Handles.DrawSolidRectangleWithOutline(tileRect, rectColor, outlineColor); Handles.color = new Color(1, 1, 1, 1); Handles.color = innerRectColor; Handles.DrawSolidDisc(grid.GridToWorld(gridPos), Vector3.up, innerRectSize); Handles.color = innerRectOutlineColor; Handles.CircleHandleCap(id, grid.GridToWorld(gridPos), Quaternion.Euler(90, 0, 0), innerRectSize, Event.current.type); // Handles.DrawSolidRectangleWithOutline(CreateTileRect(handlePos, innerRectSize), innerRectColor, innerRectOutlineColor); Handles.color = innerRectOutlineColor; Handles.ArrowHandleCap(id, handlePos + Vector3.forward * innerRectSize, Quaternion.identity, size, Event.current.type); Handles.ArrowHandleCap(id, handlePos + Vector3.right * innerRectSize, Quaternion.Euler(0, 90, 0), size, Event.current.type); Handles.ArrowHandleCap(id, handlePos + Vector3.back * innerRectSize, Quaternion.Euler(0, 180, 0), size, Event.current.type); Handles.ArrowHandleCap(id, handlePos + Vector3.left * innerRectSize, Quaternion.Euler(0, 270, 0), size, Event.current.type); break; case EventType.MouseDown: if (HandleUtility.nearestControl == id && Event.current.button == 0) { GUIUtility.hotControl = id; RefreshTargetTile(grid, elem); Event.current.Use(); } break; case EventType.MouseUp: if (GUIUtility.hotControl == id) { GUIUtility.hotControl = 0; RefreshTargetTile(grid, elem, grid.WorldToGrid(elem.transform.position)); Event.current.Use(); } break; case EventType.MouseDrag: if (GUIUtility.hotControl == id) { RefreshTargetTile(grid, elem); if (isSuitableTile) { elem.X = targetTile.X; elem.Y = targetTile.Y; grid.Layout(); } Event.current.Use(); } break; } }