Client LuaCsForBarotrauma
GrowToTargetState.cs
1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
5 using Microsoft.Xna.Framework;
6 
8 {
9  class GrowToTargetState: GrowIdleState
10  {
11  public readonly List<BallastFloraBranch> TargetBranches = new List<BallastFloraBranch>();
12  public readonly Item Target;
13 
14  private bool isFinished;
15 
16  public GrowToTargetState(BallastFloraBehavior behavior, BallastFloraBranch starter, Item target) : base(behavior)
17  {
18  Target = target;
19  TargetBranches.Add(starter);
20  }
21 
22  // do nothing
23  public override void Enter() { }
24 
25  public override ExitState GetState() => isFinished ? ExitState.Terminate : ExitState.Running;
26 
27  protected override void Grow()
28  {
29  if (TargetBranches.Any(b => b.Removed))
30  {
31  if (!Behavior.IgnoredTargets.ContainsKey(Target))
32  {
33  Behavior.IgnoredTargets.Add(Target, 10);
34  }
35 
36  isFinished = true;
37  return;
38  }
39 
40  if (Target == null || Target.Removed)
41  {
42  isFinished = true;
43  return;
44  }
45 
46  GrowTowardsTarget();
47  }
48 
49  private void GrowTowardsTarget()
50  {
51  bool succeeded = false;
52 
53  List<BallastFloraBranch> newList = new List<BallastFloraBranch>(TargetBranches);
54  foreach (BallastFloraBranch branch in newList)
55  {
56  if (branch.FailedGrowthAttempts > 8 || branch.DisconnectedFromRoot || !branch.CanGrowMore()) { continue; }
57 
58  // Get what side gets us closest to the target
59  TileSide side = GetClosestSide(branch, Target.WorldPosition);
60 
61  if (branch.IsSideBlocked(side)) { continue; }
62 
63  succeeded |= Behavior.TryGrowBranch(branch, side, out List<BallastFloraBranch> newBranches);
64  TargetBranches.AddRange(newBranches);
65 
66  foreach (BallastFloraBranch newBranch in newBranches)
67  {
68  Rectangle worldRect = newBranch.Rect;
69  worldRect.Location = Behavior.GetWorldPosition().ToPoint() + worldRect.Location;
70  if (Behavior.BranchContainsTarget(newBranch, Target))
71  {
72  Behavior.ClaimTarget(Target, newBranch);
73  isFinished = true;
74  return;
75  }
76  }
77  }
78 
79  if (!succeeded)
80  {
81  if (!Behavior.IgnoredTargets.ContainsKey(Target))
82  {
83  Behavior.IgnoredTargets.Add(Target, 1);
84  }
85 
86  isFinished = true;
87  }
88  }
89 
90  private TileSide GetClosestSide(VineTile tile, Vector2 targetPos)
91  {
92  var (distX, distY) = tile.Position + Behavior.GetWorldPosition() - targetPos;
93  int absDistX = (int) Math.Abs(distX), absDistY = (int) Math.Abs(distY);
94 
95  return absDistX > absDistY ? distX > 0 ? TileSide.Left : TileSide.Right : distY > 0 ? TileSide.Bottom : TileSide.Top;
96  }
97  }
98 }
GrowToTargetState(BallastFloraBehavior behavior, BallastFloraBranch starter, Item target)
readonly List< BallastFloraBranch > TargetBranches