Exemplo n.º 1
0
 public static SelectArea GetInstance()
 {
     if (Instance == null)
     {
         Instance = new SelectArea();
     }
     return(Instance);
 }
Exemplo n.º 2
0
        private void ReceiveCommands()
        {
            this.HeroQueue = this.Battlefield.GetAllHeroes();
            this.CommandStackNormal.Push(this.Battlefield.CreateMemento());
            this.RenderBattleField();

            String CommandInput;

            String[] Tokens;

            while (true)
            {
                CommandInput = Console.ReadLine();
                Tokens       = CommandInput.Split(' ');
                switch (Tokens[0])
                {
                case "move":
                    if (Tokens.Length != 2)
                    {
                        Console.WriteLine("Invalid syntax");
                        continue;
                    }
                    List <(int, int)> Coordinates = new List <(int, int)>();
                    int start_x = this.HeroQueue.First().GetCoordinates()[0];
                    int start_y = this.HeroQueue.First().GetCoordinates()[1];

                    if (Tokens[1].Split(',').Count() != 2)
                    {
                        Console.WriteLine("Invalid syntax");
                        continue;
                    }
                    int end_x = int.Parse(Tokens[1].Split(',')[0]);
                    int end_y = int.Parse(Tokens[1].Split(',')[1]);

                    int x_incerement = start_x < end_x ? 1 : -1;
                    int y_incerement = start_y < end_y ? 1 : -1;

                    int i = start_x;
                    int j = start_y;
                    while (i != end_x)
                    {
                        Coordinates.Add((i, j));
                        i += x_incerement;
                    }
                    while (j != end_y)
                    {
                        Coordinates.Add((i, j));
                        j += y_incerement;
                    }
                    Coordinates.Add((i, j));
                    Console.WriteLine(Coordinates.Count + " coordinates");

                    Command MoveCommand = new CommandMove(Coordinates);

                    lock (Refreshlock)
                    {
                        RefreshBattleField = true;
                        Monitor.PulseAll(Refreshlock);
                    }
                    this.ExecuteCommand(MoveCommand);
                    lock (Refreshlock)
                    {
                        RefreshBattleField = false;
                    }
                    Thread.Sleep(500);
                    this.RenderBattleField();
                    break;

                case "skill":
                    if (Tokens.Length != 3)
                    {
                        Console.WriteLine("Invalid syntax - need 3 params");
                        continue;
                    }

                    int SkillID = -1;
                    if (!int.TryParse(Tokens[1], out SkillID))
                    {
                        Console.WriteLine("Invalid syntax - param 1 must be integer");
                        continue;
                    }

                    Skill Skill = HeroQueue.First().GetSkill(SkillID);
                    if (Skill == null)
                    {
                        Console.WriteLine("Skill with id " + SkillID + " does not exist");
                        continue;
                    }

                    if (Skill.Passive)
                    {
                        Console.WriteLine("Cannot actively use passive skill");
                        continue;
                    }

                    ITriggerBehaviour TargetingStrategy = null;
                    switch (Tokens[2])
                    {
                    case "one":
                        TargetingStrategy = SelectOneTarget.GetInstance();
                        break;

                    case "auto":
                        TargetingStrategy = SelectAutoTarget.GetInstance();
                        break;

                    case "area":
                        TargetingStrategy = SelectArea.GetInstance();
                        break;

                    case "self":
                        TargetingStrategy = SelectSelf.GetInstance();
                        break;

                    default:
                        break;
                    }
                    if (TargetingStrategy == null)
                    {
                        Console.WriteLine("Invalid target strategy option");
                        continue;
                    }

                    Command UseSkillCommand = new CommandUseSkill(Skill, Battlefield.GetField(HeroQueue.First().GetCoordinates()[0], HeroQueue.First().GetCoordinates()[1]), TargetingStrategy);
                    this.ExecuteCommand(UseSkillCommand);
                    this.RenderBattleField();
                    break;

                case "gather":
                    Command GatherCommand = new CommandGather(this.HeroQueue.First());
                    this.ExecuteCommand(GatherCommand);
                    this.RenderBattleField();
                    break;

                case "end":
                    this.CommandStackNormal.Clear();
                    this.CommandStackNormal.Push(this.Battlefield.CreateMemento());
                    this.CommandStackReverse.Clear();

                    HeroInterface Temp = this.HeroQueue.First();
                    Temp.RestoreTurn();
                    this.HeroQueue.RemoveFirst();
                    this.HeroQueue.AddLast(Temp);

                    this.RenderBattleField();

                    break;

                case "undo":
                    if (this.CommandStackNormal.Count > 1)
                    {
                        this.CommandStackReverse.Push(this.CommandStackNormal.Pop());
                        this.Battlefield.Restore(this.CommandStackNormal.Peek());
                        this.RenderBattleField();
                    }
                    break;

                case "redo":
                    if (this.CommandStackReverse.Any())
                    {
                        this.CommandStackNormal.Push(this.CommandStackReverse.Pop());
                        this.Battlefield.Restore(this.CommandStackNormal.Peek());
                        this.RenderBattleField();
                    }
                    break;

                case "refresh":
                    this.RenderBattleField();
                    break;

                case "exit":
                    this.Over = true;
                    break;

                default:
                    Console.WriteLine("Unknown command");
                    break;
                }

                if (Over)
                {
                    lock (Refreshlock)
                    {
                        Monitor.PulseAll(Refreshlock);
                    }
                    break;
                }
            }

            ObserverFactory.GetInstance().Finish();
        }