Exemple #1
0
 public void AddTree(Vector2D position, Team team)
 {
     var newTree = new Tree(position, team);
     trees.Add(newTree);
     gameScene.Add(newTree);
     UpdateBars();
     new Command(newTree.TryToUpgrade).Add(new MouseHoldTrigger(newTree.DrawArea)).Add(
         new TouchHoldTrigger(newTree.DrawArea));
     new Command(
         (start, end, dragDone) => MoveGhostsFromTreeToTree(start, end, dragDone, newTree)).Add(
             new MouseDragTrigger()).Add(new TouchDragTrigger());
 }
Exemple #2
0
 private void OnClickSelectTree()
 {
     new Command(Command.Click, position =>
     {
         var nearestTree = FindNearestTree(null, position);
         if (nearestTree != null)
             selection.DrawArea = Rectangle.FromCenter(nearestTree.Center,
                 new Size(nearestTree.Size.Height));
         if (nearestTree == lastSelectedTree || Time.Total - lastSelectedTreeSoundTime < 0.2f)
             return;
         ContentLoader.Load<Sound>("MalletSwing").Play(0.15f);
         lastSelectedTreeSoundTime = Time.Total;
         lastSelectedTree = nearestTree;
     });
 }
Exemple #3
0
 private void SendWave(Tree startTree, Tree targetTree)
 {
     ContentLoader.Load<Sound>("GhostWaveStart").Play(0.5f);
     var ghostsToSend = Math.Min(startTree.NumberOfGhosts, 5);
     startTree.NumberOfGhosts -= ghostsToSend;
     UpdateBars();
     if (targetTree == null)
         return;
     var wave = new GhostWave(startTree.Center, targetTree.Center, ghostsToSend,
         startTree.Team.ToColor());
     wave.Attacker = startTree.Team;
     wave.TargetReached += (attacker, waveSize) =>
     {
         targetTree.Attack((Team)attacker, waveSize);
         UpdateBars();
     };
 }
Exemple #4
0
 private void MoveGhostsFromTreeToTree(Vector2D start, Vector2D end, bool dragDone,
     Tree startTree)
 {
     if (start.DistanceTo(startTree.DrawArea.Center) > 0.04f)
         return;
     var targetTree = FindNearestTree(startTree, end);
     arrow.IsVisible = false;
     if (startTree.Team != MainMenu.PlayerTeam || targetTree == startTree ||
         MainMenu.State != GameState.Game || startTree.Center.DistanceTo(end) < 0.05f)
         return;
     arrow.Dispose();
     arrow = Effects.CreateArrow(startTree.Center, targetTree.Center);
     arrow.Color = startTree.Team.ToColor();
     arrow.IsVisible = !dragDone;
     if ((dragDone && Time.Total - lastWaveSend > TimeBetweenWaves ||
         !dragDone && Time.CheckEvery(TimeBetweenWaves)) && startTree.NumberOfGhosts >= 1)
     {
         lastWaveSend = Time.Total;
         SendWave(startTree, targetTree);
     }
 }
Exemple #5
0
 private void HandleAi(Tree tree)
 {
     var nearestFreeTree = FindNearestTreeFreeTree(tree);
     var nearestTree = FindNearestTreeWeakTree(tree);
     if (tree.NumberOfGhosts > 10 && nearestFreeTree != null)
         SendWave(tree, nearestFreeTree);
     else if (tree.NumberOfGhosts > 15 && nearestTree != null)
         SendWave(tree, nearestTree);
     else if (tree.NumberOfGhosts > 25)
         SendWave(tree, trees[Randomizer.Current.Get(0, trees.Count)]);
 }
Exemple #6
0
 private Tree FindNearestTreeWeakTree(Tree sourceTree)
 {
     float nearestDistance = float.MaxValue;
     Tree nearestTree = null;
     foreach (var tree in trees)
         if (tree.Team != Team.None && tree.Team != sourceTree.Team &&
             tree.NumberOfGhosts < sourceTree.NumberOfGhosts)
         {
             float treeDistance = tree.Center.DistanceTo(sourceTree.Center);
             if (treeDistance > nearestDistance)
                 continue;
             nearestDistance = treeDistance;
             nearestTree = tree;
         }
     return nearestTree;
 }
Exemple #7
0
 private Tree FindNearestTreeFreeTree(Tree sourceTree)
 {
     float nearestDistance = float.MaxValue;
     Tree nearestTree = null;
     foreach (var tree in trees)
         if (tree.Team == Team.None)
         {
             float treeDistance = tree.Center.DistanceTo(sourceTree.Center);
             if (treeDistance > nearestDistance)
                 continue;
             nearestDistance = treeDistance;
             nearestTree = tree;
         }
     return nearestTree;
 }
Exemple #8
0
 private Tree FindNearestTree(Tree previousTree, Vector2D target)
 {
     float nearestDistance = float.MaxValue;
     Tree nearestTree = null;
     foreach (var tree in trees)
         if (tree != previousTree)
         {
             float treeDistance = tree.Center.DistanceTo(target);
             if (previousTree != null)
                 treeDistance +=
                     Math.Abs(previousTree.Center.RotationTo(target) -
                         previousTree.Center.RotationTo(tree.Center)) / 500.0f;
             if (treeDistance > nearestDistance)
                 continue;
             nearestDistance = treeDistance;
             nearestTree = tree;
         }
     return nearestTree;
 }