Esempio n. 1
0
        /// <summary>
        /// Creates a data binding between this property and another which
        /// have a different type.
        /// </summary>
        /// <typeparam name="T2">Target property type.</typeparam>
        /// <param name="property">Binding target property.</param>
        /// <param name="outAdapter">Data conversion function.</param>
        public void Bind <T2>(Bindable <T2> property, Func <T, T2> outAdapter)
        {
            if (outAdapter == null)
            {
                throw new InvalidOperationException("No binding adapter provided.");
            }

            if (!AllowedDirections.HasFlag(BindingDirection.Read))
            {
                throw new InvalidOperationException("This binding direction is not allowed.");
            }

            // - Get a weak reference of the property
            var propRef = new WeakReference <Bindable <T2> >(property);

            var bindingAction = new Action(() =>
            {
                // - Check if the target property is still alive
                if (!propRef.TryGetTarget(out Bindable <T2> prop))
                {
                    Bindings.Remove(propRef);
                    return;
                }

                // - Update value
                prop.Value = outAdapter.Invoke(InternalValue);
            });

            Bindings.Add(propRef, bindingAction);
        }
Esempio n. 2
0
        // Loop for any user move actions untill hitting treasure or loosing the whole score or hitting x for exit
        public bool AskToMove()
        {
            PrintInfo();

            string question = "Please Enter ";

            if (AllowedDirections["N"])
            {
                question += " [N] for North,";
            }
            if (AllowedDirections["S"])
            {
                question += " [S] for South,";
            }
            if (AllowedDirections["W"])
            {
                question += " [W] for West,";
            }
            if (AllowedDirections["E"])
            {
                question += " [E] for East,";
            }
            question += " [X] for Exit : ";

            Helpers.ConsoleReadChar(question, out var dir);
            if (dir == 'X' || dir == 'x')
            {
                return(false);
            }
            if (AllowedDirections.TryGetValue(dir.ToString().ToUpper(), out var allowed) && allowed)
            {
                Move(CurrentRoom, dir);
                VisitedMaze.ConsolePrint(CurrentRoom);
                if (Won)
                {
                    Console.WriteLine("Conguratulations you found the Treasure.");
                    return(false);
                }
                if (PlayerScore <= 0)
                {
                    Console.WriteLine("Your score is 0, Your Total Steps are {0}, Game Over.", PlayerSteps);
                    return(false);
                }
            }
            else
            {
                Console.WriteLine("Wrong Input");
            }

            return(true);
        }
Esempio n. 3
0
        private void OnTileClicked(int tileValue)
        {
            int index = 0;

            while (field[index] != tileValue)
            {
                index++;
            }

            AllowedDirections direction = GetMovingDirection(index);

            if (direction == AllowedDirections.None)
            {
                return;
            }

            List <int> indices = GetMovingIndices(index, direction);

            foreach (var i in indices)
            {
                switch (direction)
                {
                case AllowedDirections.Up:
                    MoveTile(i, i - Core.SIDE_SIZE);
                    break;

                case AllowedDirections.Right:
                    MoveTile(i, i + 1);
                    break;

                case AllowedDirections.Down:
                    MoveTile(i, i + Core.SIDE_SIZE);
                    break;

                case AllowedDirections.Left:
                    MoveTile(i, i - 1);
                    break;

                case AllowedDirections.None:
                    Debug.LogError("You are trying to move tiles that can't be moved. How did you end up in there?");
                    break;
                }
            }

            if (puzzle.WinConditionPredicate(field))
            {
                Core.Instance.FSM.Event(FSM.Events.PuzzleSolved);
            }
        }
Esempio n. 4
0
        private List <int> GetMovingIndices(int index, AllowedDirections direction)
        {
            List <int> result       = new List <int>();
            int        currentIndex = index;

            switch (direction)
            {
            case AllowedDirections.Up:
                while (currentIndex >= 0 && field[currentIndex] != 0)
                {
                    result.Add(currentIndex);
                    currentIndex -= Core.SIDE_SIZE;
                }
                break;

            case AllowedDirections.Right:
                while (currentIndex % Core.SIDE_SIZE < Core.SIZE && field[currentIndex] != 0)
                {
                    result.Add(currentIndex);
                    currentIndex++;
                }
                break;

            case AllowedDirections.Down:
                while (currentIndex < Core.SIZE && field[currentIndex] != 0)
                {
                    result.Add(currentIndex);
                    currentIndex += Core.SIDE_SIZE;
                }
                break;

            case AllowedDirections.Left:
                while (currentIndex % Core.SIDE_SIZE >= 0 && field[currentIndex] != 0)
                {
                    result.Add(currentIndex);
                    currentIndex--;
                }
                break;
            }
            result.Reverse();
            return(result);
        }
Esempio n. 5
0
        public override Direction CalculateMoveDirection()
        {
            if (_ChangeDirectionTime < 0 || !AllowedDirections.Contains(_MoveDirection))
            {
                if (AllowedDirections.Count > 0)
                {
                    do
                    {
                        _MoveDirection = RandomUtils.GetRandomDirection();
                    } while (!AllowedDirections.Contains(_MoveDirection));
                }
                else
                {
                    _MoveDirection = Direction.None;
                }
                SetChangeDirectionTime();
            }

            return(_MoveDirection);
        }
Esempio n. 6
0
        public override Direction CalculateMoveDirection()
        {
            if (Keyboard.GetState().IsKeyDown(Keys.A))
            {
                DrawDirection = Direction.Left;
                if (AllowedDirections.Contains(Direction.Left))
                {
                    return(Direction.Left);
                }
            }
            if (Keyboard.GetState().IsKeyDown(Keys.S))
            {
                DrawDirection = Direction.Down;
                if (AllowedDirections.Contains(Direction.Down))
                {
                    return(Direction.Down);
                }
            }
            if (Keyboard.GetState().IsKeyDown(Keys.W))
            {
                DrawDirection = Direction.Up;
                if (AllowedDirections.Contains(Direction.Up))
                {
                    return(Direction.Up);
                }
            }
            if (Keyboard.GetState().IsKeyDown(Keys.D))
            {
                DrawDirection = Direction.Right;
                if (AllowedDirections.Contains(Direction.Right))
                {
                    return(Direction.Right);
                }
            }

            return(Direction.None);
        }
Esempio n. 7
0
        /// <summary>
        /// Creates a data binding between this property and the specified one.
        /// </summary>
        /// <param name="property">Binding target property.</param>
        /// <param name="direction">Binding direction.</param>
        public void Bind(Bindable <T> property, BindingDirection direction = BindingDirection.Both)
        {
            bool invalidOperation = false;

            invalidOperation = invalidOperation || direction.HasFlag(BindingDirection.Read) && !AllowedDirections.HasFlag(BindingDirection.Write);
            invalidOperation = invalidOperation || direction.HasFlag(BindingDirection.Write) && !AllowedDirections.HasFlag(BindingDirection.Read);

            if (invalidOperation)
            {
                throw new InvalidOperationException("This binding direction is not allowed");
            }

            // - Get a weak reference of the property
            var propRef = new WeakReference <Bindable <T> >(property);

            if (direction.HasFlag(BindingDirection.Write))
            {
                var bindingAction = new Action(() =>
                {
                    // - Check if the target property is still alive
                    if (!propRef.TryGetTarget(out Bindable <T> prop))
                    {
                        Bindings.Remove(propRef);
                        return;
                    }

                    // - Update value
                    prop.Value = InternalValue;
                });

                Bindings.Add(propRef, bindingAction);
            }

            // - If we want to read the target property, delegate binding process
            if (direction.HasFlag(BindingDirection.Read))
            {
                property.Bind(this, BindingDirection.Write);
            }
        }
Esempio n. 8
0
        public override Direction CalculateMoveDirection()
        {
            //Set current cell Visit
            MapInfo.SetVisitTime(position);

            //Check visible user. If can see user "inform" all other tanks
            Vector2 userPos = MapInfo.GetUserVisiblePosition(position);

            if (userPos != Vector2.Zero)
            {
                MapInfo.SetUserPosition(userPos);
                UserVisibleDirection = AIUtils.GetMaxDirection(GetPosition - userPos);
            }
            else
            {
                UserVisibleDirection = Direction.None;
            }

            //Get User position. if == current lost user
            Vector2 userTankPosition = MapInfo.GetUserPosition();

            if (userTankPosition == position)
            {
                LostUserPosition();
            }

            if (MapInfo.UserDetected)
            {
                Vector2 difference = this.GetPosition - userTankPosition;
                if (SpriteUtils.IsYAxisFromDirection(_MoveDirection) && SpriteUtils.CoordinateEqualToZero((int)difference.Y) ||
                    !SpriteUtils.IsYAxisFromDirection(_MoveDirection) && SpriteUtils.CoordinateEqualToZero((int)difference.X))
                {
                    _MoveDirection = Direction.None;
                }

                if (!AllowedDirections.Contains(_MoveDirection))
                {
                    //Smart Algo Start
                    Direction maxD = AIUtils.GetMaxDirection(this.GetPosition - userTankPosition);
                    if (AllowedDirections.Contains(maxD))
                    {
                        _MoveDirection = maxD;
                    }
                    else
                    {
                        Direction minD = AIUtils.GetMinDirection(this.GetPosition - userTankPosition);
                        if (AllowedDirections.Contains(minD))
                        {
                            _MoveDirection = minD;
                        }
                        else
                        {
                            if (CurrentMissle == null || CurrentMissle.State == SpriteState.Destroyed)
                            {
                                _MoveDirection = maxD;
                            }
                            else
                            {
                                if (AllowedDirections.Count > 0)
                                {
                                    do
                                    {
                                        _MoveDirection = RandomUtils.GetRandomDirection();
                                    } while (!AllowedDirections.Contains(_MoveDirection));
                                }
                                else
                                {
                                    _MoveDirection = Direction.None;
                                }
                            }
                        }
                    }
                }
                //Smart Algo End
            }
            else
            {
                if (!AllowedDirections.Contains(_MoveDirection) || movedDistance > Default.CellSetting.CellSize.X)
                {
                    movedDistance  = 0;
                    _MoveDirection = MapInfo.GetOldestDirection(position, AllowedDirections);
                }
                movedDistance += speedValue;
            }


            return(_MoveDirection);
        }
Esempio n. 9
0
 public override void Update(Microsoft.Xna.Framework.GameTime gameTime)
 {
     // Increment to next frame
     base.Update(gameTime);
     if (UserVisibleDirection == DrawDirection || MoveDirection == Direction.None || !AllowedDirections.Contains(MoveDirection))
     {
         Fire();
     }
 }
Esempio n. 10
0
 public override void Update(Microsoft.Xna.Framework.GameTime gameTime, Microsoft.Xna.Framework.Rectangle clientBounds)
 {
     base.Update(gameTime, clientBounds);
     if (UserVisibleDirection == DrawDirection || MoveDirection != Direction.None && !AllowedDirections.Contains(MoveDirection))
     {
         Fire();
     }
 }