Esempio n. 1
0
        // returns whether this character could step to given position (ignoring available action points)
        public bool CanStepToPosition(Position position, Game game)
        {
            if (!game.GetMap().IsPositionWithin(position))
            {
                return(false);
            }
            if (Position.GetDistance(position) != 1)
            {
                return(false);
            }

            if (!game.GetMap().IsPositionPermeable(position))
            {
                return(false);
            }

            SquareType squareType = game.GetMap().GetSquareTypeAt(position);

            if (squareType != null)
            {
                if (!squareType.AllowsCharacter(this))
                {
                    return(false);
                }
            }

            return(true);
        }
Esempio n. 2
0
        //After an object is selected, the current player can do some action with it (or they can do basic actions)
        public void DoAction(string actionName, params string[] actionParameters)
        {
            if (SelectedObject != null)
            {
                Log.Write($"Player [{CurrentPlayerNo}]: do '{actionName}' with object '{SelectedObject.Id}'");
            }
            else
            {
                Log.Write($"Player [{CurrentPlayerNo}]: do '{actionName}({string.Join(", ",actionParameters)})'");
            }



            if (actionName == BasicActions.Movement)
            {
                Character character = CurrentPlayer.Character;

                Position newPosition = new Position(character.Position.X, character.Position.Y);

                if (actionParameters[0] == MovementParameters.Norht)
                {
                    newPosition.Y -= 1;
                }
                else if (actionParameters[0] == MovementParameters.East)
                {
                    newPosition.X += 1;
                }
                else if (actionParameters[0] == MovementParameters.South)
                {
                    newPosition.Y += 1;
                }
                else if (actionParameters[0] == MovementParameters.West)
                {
                    newPosition.X -= 1;
                }

                // check if new position is within map borders
                if (!Map.IsPositionWithin(newPosition))
                {
                    throw new GameException("Invalid movement");
                }

                // if no special square at target position, or it has no associated action, the movement cost is the basic movement cost
                int MovementCost = Config.CharacterConfig.MovementActionPoints;

                // check square restrictions at new position
                SquareType squareType = Map.GetSquareTypeAt(newPosition);
                if (squareType != null)
                {
                    if (!squareType.AllowsCharacter(character))
                    {
                        throw new GameException("This character is not allowed to step on this square");
                    }
                    MovementCost = Map.GetMovementCost(squareType, Config);
                }

                if (MovementCost > CurrentPlayer.AvailableActionPoints)
                {
                    throw new GameException("Too few action points for movement");
                }

                character.Position = newPosition;
                CurrentPlayer.AvailableActionPoints -= MovementCost;

                // if character stepped on a square, we may have to call an action
                Map.ExecuteSquareAction(squareType, character, this);
            }
            else if (actionName == BasicActions.EndTurn)
            {
                NextPlayer();
            }
            else if (SelectedObject != null)
            {
                //Get action points to be removed on a successful action
                int actionPointsToRemove = SelectedObject.Scheme.GetFunctionByName(actionName).ActionPoints;

                if (actionPointsToRemove > CurrentPlayer.AvailableActionPoints)
                {
                    throw new GameException("Too few action points");
                }

                actionPointsToRemove = SelectedObject.ExecuteAction(actionName, CurrentPlayer.Character, this);

                CurrentPlayer.AvailableActionPoints -= actionPointsToRemove;
            }
            else
            {
                throw new GameException("Non-basic actions can only be applied to objects");
            }
        }