Esempio n. 1
0
        public IEnumerator JumpCoroutine(Unit unit, Point pointDestination)
        {
            Logcat.I($"Jump Coroutine, initial position {unit.GetPosition()}, final position {pointDestination}");
            Vector3 destination = PointConverter.ToVector(pointDestination);

            destination.y = unit.Height;

            float JumpProgress = 0;
            var   startPos     = unit.transform.position;

            while (JumpProgress <= 1.0)
            {
                JumpProgress += Time.deltaTime / jumpingTime;
                var height = Mathf.Sin(Mathf.PI * JumpProgress) * jumpingHeight;
                if (height < 0f)
                {
                    height = 0f;
                }
                unit.transform.position = Vector3.Lerp(startPos, destination, JumpProgress) + Vector3.up * height;
                yield return(null);
            }

            unit.transform.position = destination;
            PlacementHelper.Move(unit, pointDestination, new MovementActionValidator());
        }
Esempio n. 2
0
        public IEnumerator LerpMovementPath(MonoBehaviour caller, Unit unit, List <Point> path)
        {
            Logcat.I($"Lerp Movement Path moving unit {unit.GetPosition()}");
            path.ForEach(p => Logcat.I($"Lerp Path {p}"));

            for (int i = 1; i < path.Count; i++)
            {
                yield return(LerpMovement(unit, unit.gameObject.transform.position, PointConverter.ToVector(path[i])));
            }
            yield return(null);
        }
Esempio n. 3
0
        public void UndoMovement()
        {
            if (this.selectedUnit == null)
            {
                return;
            }

            Logcat.I(this, $"Undo movement. Original position {this.originalPosition} final position {this.selectedUnit.GetPosition()}");
            PlacementHelper.Move(this.selectedUnit, this.originalPosition, new MoveUnitValidator(this.selectedUnit, this.originalPosition));
            this.undoButton.gameObject.SetActive(false);
            this.RevertStats();
            this.itemSelected.Value = default;
        }
Esempio n. 4
0
        private void RevertStats()
        {
            if (this.selectedUnit == null)
            {
                return;
            }

            Logcat.I(this, $"RevertStats {this.selectedUnit.UnitName}");
            Action action = this.selectedUnit.ActionsHandler.GetActions()[0];

            action?.IsActive(false);
            action?.IsEnabled(true);
            this.selectedUnit = null;
        }
Esempio n. 5
0
        private void OnUnitSelected(Unit unit)
        {
            if (unit == null)
            {
                return;
            }

            if (this.selectedUnit == null || this.selectedUnit != unit)
            {
                this.selectedUnit     = unit;
                this.originalPosition = this.selectedUnit.GetPosition();
                this.undoButton.gameObject.SetActive(false);
                Logcat.I(this, $"OnActionSelected Selected Unit's position {this.originalPosition}");
            }
        }
Esempio n. 6
0
        public static bool Remove(Unit unit, IValidator validator)
        {
            if (unit == null || validator == null || !validator.IsValid())
            {
                Logcat.I(null, "unable to remove");
                return(false);
            }

            unit?.Health.DeadByOneHit();
            unit?.GetComponent <EnemyUnit>()?.NotifyDyingEvent();
            unit?.GetComponent <PlayerUnit>()?.NotifyDyingEvent();
            unit?.UnitsMap.Remove(unit.GetPosition());
            MonoBehaviour.Destroy(unit.gameObject);
            return(true);
        }
Esempio n. 7
0
        public static Unit Add(Transform parent, Unit unit, Point point, IValidator validator)
        {
            if (unit == null || !validator.IsValid())
            {
                Logcat.I($"Invalid position {point}");
                return(null);
            }

            Unit instance = MonoBehaviour.Instantiate(unit, new Vector3((float)point.x, point.y + unit.Height, (float)point.z), unit.transform.rotation);

            instance.SetPosition(point);
            instance.transform.parent = parent;
            instance.UnitsMap.Add(point, instance);
            return(instance);
        }
Esempio n. 8
0
        private IEnumerator LerpMovement(Unit unit, Vector3 initialPosition, Vector3 finalPosition)
        {
            Logcat.I($"Lerp Movement initial position {initialPosition} final position {finalPosition}");
            this.FlipUnit(unit, initialPosition, finalPosition);
            float elapsedTime = 0;

            unit.StepsFx?.Play(unit.transform.position);
            unit.transform.position = initialPosition;

            while (elapsedTime < timePerTile)
            {
                float xPosition = Mathf.Lerp(unit.transform.position.x, finalPosition.x, elapsedTime / timePerTile);
                float zPosition = Mathf.Lerp(unit.transform.position.z, finalPosition.z, elapsedTime / timePerTile);
                unit.transform.position = new Vector3(xPosition, unit.gameObject.transform.position.y, zPosition);
                elapsedTime            += Time.deltaTime;
                yield return(new WaitForEndOfFrame());
            }
            PlacementHelper.Move(unit, PointConverter.ToPoint(finalPosition), new MovementActionValidator());
            yield return(null);
        }
Esempio n. 9
0
        private void Update()
        {
            if (Input.GetMouseButtonDown(0))
            {
                // Check if the mouse was clicked over a UI element
                if (EventSystem.current.IsPointerOverGameObject())
                {
                    Logcat.I(this, "Clicked on the UI");
                    return;
                }

                Ray ray = this.mainCamera.ScreenPointToRay(Input.mousePosition);
                if (Physics.Raycast(ray, out var hit))
                {
                    MonoBehaviour[] list = hit.collider.gameObject.GetComponents <MonoBehaviour>();
                    foreach (MonoBehaviour item in list)
                    {
                        if (item is ISelectable)
                        {
                            this.itemSelected.Value = ((ISelectable)item).GetPosition();
                            Logcat.I(this, $"Position: {((ISelectable)item).GetPosition()}");
                            break;
                        }
                    }
                }
                else
                {
                    CancelledAction?.Invoke();
                    Logcat.I(this, $"Cancelled action event throwed");
                }
            }
            else if (Input.GetMouseButtonDown(1))
            {
                CancelledAction?.Invoke();
                Logcat.I(this, $"Cancelled action event throwed");
            }
        }