void Update() { UnitUnderlay unitUnderlay; Field field; // Mouse Down if ((unitUnderlay = ray.Selector <UnitUnderlay>(MouseAction.Down)) != null) { Unit unitPrototype = unitUnderlay.transform.GetComponentInChildren <Unit>(); GameObject unitGO = Instantiate(unitPrototype.gameObject); unitHeld = unitGO.GetComponent <Unit>(); unitHeld.transform.localScale = new Vector3(1, 1, 1); } if ((field = ray.Selector <Field>(MouseAction.Down)) != null && !field.IsEmpty()) { unitHeld = field.unit; deleteZone.alpha = 1; } // Mouse Held if (Input.GetMouseButton(0) && unitHeld != null) { RaycastHit hit = ray.GetRaycastHit(); Vector3 dir = (cam.transform.position - hit.point).normalized; unitHeld.transform.position = hit.point + dir * 5f; } // Mouse Up if ((field = ray.Selector <Field>(MouseAction.Up)) && unitHeld) { if (field.ID < 21) { if (CreatingUnit(unitHeld)) { CreateUnitOn(field); } else { MoveUnitTo(field); } } else { DestroyHeldUnit(); } unitHeld = null; deleteZone.alpha = 0; } else if (Input.GetMouseButtonUp(0)) { if (unitHeld != null) { if (CreatingUnit(unitHeld)) { DestroyHeldUnit(); } else { unitHeld.transform.position = unitHeld.field.transform.position; } unitHeld = null; deleteZone.alpha = 0; } } }
private void UnitOptionsFlow() { Field fieldHit; // 1) Selecting if (fieldHit = ray.FieldWithUnitSelector(MouseAction.Down)) // Button Down { unitSelected = fieldHit.unit; unitValidity = CheckUnitValidity(unitSelected); if (unitValidity.Valid) { HighlightUnits(player, false); Invoke("HighlightLegalMoves", 0.0f); drag.SetArrowStart(fieldHit); OnUnitSelected?.Invoke(unitSelected); } return; } // 2) Holding -> Only for Drag animation if (fieldHit = ray.Selector <Field>(MouseAction.Held)) { if (unitSelected == null) { return; } if (!unitValidity.Valid) { return; } drag.SetArrowTarget(fieldHit); return; } else if (Input.GetMouseButton(0)) { drag.Pause(); return; } // 3) Releasing if (Input.GetMouseButtonUp(0) && unitSelected != null) { UnhighlightLegalMoves(); } if (fieldHit = ray.Selector <Field>(MouseAction.Up)) { if (fieldHit != null && unitSelected != null) { Action action = new Action(unitSelected.field, fieldHit); if (unitValidity.Valid && IsMoveLegal(unitSelected, fieldHit)) { // Making the actual Move player.MakeAction(action); } else if (fieldHit != unitSelected.field) { // Error message printing Validity worseValidity = unitValidity.GetIfWorse(action.Move.CheckValidity(board)); if (worseValidity.PrintIfInvalid()) { InfoText.Show(unitSelected.unitName + " can't move like that"); } HighlightUnits(player, true); } else { HighlightUnits(player, true); } } } if (Input.GetMouseButtonUp(0)) { drag.Stop(); OnUnitDeselected?.Invoke(unitSelected); unitSelected = null; unitValidity = null; if (fieldHit == null) { HighlightUnits(player, true); } } }