static CommandType GetPossibleCommands(RaycastObjectData raycastObjectData) { CommandType possibleCommands = CommandType.NONE; if (raycastObjectData.type == RaycastObjectType.TERRAIN) { if (raycastObjectData.tile is TerrainTile terrainTile) { possibleCommands |= CommandType.MOVE; possibleCommands |= CommandType.CRAFT; if (terrainTile.terrainType == TerrainType.GRASS || terrainTile.terrainType == TerrainType.DIRT) { possibleCommands |= CommandType.PLANT_FUEL; } } } else if (raycastObjectData.type == RaycastObjectType.COLLIDER_OBJECT) { possibleCommands |= CommandType.MOVE; IInteractable[] interactables = raycastObjectData.colliderObject.GetComponents <IInteractable>(); foreach (IInteractable interactable in interactables) { possibleCommands |= interactable.PossibleCommands(); } } if (possibleCommands != CommandType.NONE) { possibleCommands ^= CommandType.NONE; } return(possibleCommands); }
void Update() { if (!LevelManager.Instance.playing) { return; } // When we click down and the panel is open, the next button up has // potential to close the current open panel if (Input.GetMouseButtonDown(0) || Input.GetMouseButtonDown(1)) { if (panelOpen) { _closeOnNextButtonUp = true; } } if (EventSystem.current.IsPointerOverGameObject()) { return; } // If we reach time to open a new panel, then the next button up // should not close the panel if (Input.GetMouseButton(0) || Input.GetMouseButton(1)) { _inputPositionOnClickDown = Input.mousePosition; _currentHoldDuration += Time.deltaTime; if (_currentHoldDuration >= minimumHoldDuration && !_currentlyHolding) { if (panelOpen) { OnCommandPanelClose?.Invoke(); } _currentlyHolding = true; _closeOnNextButtonUp = false; RaycastHit2D hit = Physics2D.GetRayIntersection(Camera.main.ScreenPointToRay(_inputPositionOnClickDown), Single.PositiveInfinity, collisionMask); // Find out what we are using commands on RaycastObjectData raycastObjectData = RaycastObjectIdentifier.IdentifyRaycastObject(hit, ignoreMask, _inputPositionOnClickDown); if (raycastObjectData.type == RaycastObjectType.NONE) { return; } // Determine what commands are possible for the object / tile CommandType possibleCommands = GetPossibleCommands(raycastObjectData); Vector3 groundPosition = Camera.main.ScreenToWorldPoint(_inputPositionOnClickDown); if (raycastObjectData.type == RaycastObjectType.TERRAIN) { OnCommandPanelOpen?.Invoke(groundPosition, null, possibleCommands); } else if (raycastObjectData.type == RaycastObjectType.COLLIDER_OBJECT) { OnCommandPanelOpen?.Invoke(groundPosition, hit.transform, possibleCommands); } } } if (Input.GetMouseButtonUp(0) || Input.GetMouseButtonUp(1)) { _currentlyHolding = false; _currentHoldDuration = 0; if (_closeOnNextButtonUp) { _closeOnNextButtonUp = false; OnCommandPanelClose?.Invoke(); } else if (!panelOpen) { RaycastHit2D hit = Physics2D.GetRayIntersection(Camera.main.ScreenPointToRay(_inputPositionOnClickDown), Single.PositiveInfinity, collisionMask); if (hit.transform == null) { Vector3 groundPosition = Camera.main.ScreenToWorldPoint(_inputPositionOnClickDown); _commandManager.AddCommand(new MovePositionCommand(groundPosition)); } else if (hit.transform.gameObject.layer != LayerMask.NameToLayer("UI")) { _commandManager.AddCommand(new MoveAndInteractCommand(hit.transform, null)); } } } }