Skip to content

Commit

Permalink
use new c# features
Browse files Browse the repository at this point in the history
  • Loading branch information
Jnnshschl committed Jan 7, 2024
1 parent b381acb commit e146b06
Show file tree
Hide file tree
Showing 196 changed files with 892 additions and 1,675 deletions.
24 changes: 6 additions & 18 deletions AmeisenBotX.BehaviorTree/Objects/Annotator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,11 @@ namespace AmeisenBotX.BehaviorTree.Objects
/// BehaviorTree Node that executes a node before executing annother node. Use this to update
/// stuff before executing a node.
/// </summary>
public class Annotator : INode
public class Annotator(INode annotationNode, INode child) : INode
{
public Annotator(INode annotationNode, INode child) : base()
{
AnnotationNode = annotationNode;
Child = child;
}

public INode AnnotationNode { get; set; }
public INode AnnotationNode { get; set; } = annotationNode;

This comment has been minimized.

Copy link
@player1175

player1175 Apr 13, 2024

bad


public INode Child { get; set; }
public INode Child { get; set; } = child;

public BtStatus Execute()
{
Expand All @@ -30,17 +24,11 @@ public INode GetNodeToExecute()
}
}

public class Annotator<T> : INode<T>
public class Annotator<T>(INode<T> annotationNode, INode<T> child) : INode<T>
{
public Annotator(INode<T> annotationNode, INode<T> child) : base()
{
AnnotationNode = annotationNode;
Child = child;
}

public INode<T> AnnotationNode { get; set; }
public INode<T> AnnotationNode { get; set; } = annotationNode;

public INode<T> Child { get; set; }
public INode<T> Child { get; set; } = child;

public BtStatus Execute(T blackboard)
{
Expand Down
30 changes: 8 additions & 22 deletions AmeisenBotX.BehaviorTree/Objects/DualSelector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,13 @@ namespace AmeisenBotX.BehaviorTree.Objects
/// Special selector that executes nodeNone when input is 0|0, nodeA when input is 1|0, nodeB
/// when input is 0|1 and nodeBoth when input is 1|1.
/// </summary>
public class DualSelector : IComposite
public class DualSelector(Func<bool> conditionA, Func<bool> conditionB, INode nodeNone, INode nodeA, INode nodeB, INode nodeBoth) : IComposite
{
public DualSelector(Func<bool> conditionA, Func<bool> conditionB, INode nodeNone, INode nodeA, INode nodeB, INode nodeBoth) : base()
{
ConditionA = conditionA;
ConditionB = conditionB;
Children = new INode[] { nodeNone, nodeA, nodeB, nodeBoth };
}

public INode[] Children { get; }
public INode[] Children { get; } = [nodeNone, nodeA, nodeB, nodeBoth];

public Func<bool> ConditionA { get; }
public Func<bool> ConditionA { get; } = conditionA;

public Func<bool> ConditionB { get; }
public Func<bool> ConditionB { get; } = conditionB;

public BtStatus Execute()
{
Expand All @@ -35,20 +28,13 @@ public INode GetNodeToExecute()
}
}

public class DualSelector<T> : IComposite<T>
public class DualSelector<T>(Func<T, bool> conditionA, Func<T, bool> conditionB, INode<T> nodeNone, INode<T> nodeA, INode<T> nodeB, INode<T> nodeBoth) : IComposite<T>
{
public DualSelector(Func<T, bool> conditionA, Func<T, bool> conditionB, INode<T> nodeNone, INode<T> nodeA, INode<T> nodeB, INode<T> nodeBoth) : base()
{
ConditionA = conditionA;
ConditionB = conditionB;
Children = new INode<T>[] { nodeNone, nodeA, nodeB, nodeBoth };
}

public INode<T>[] Children { get; }
public INode<T>[] Children { get; } = [nodeNone, nodeA, nodeB, nodeBoth];

public Func<T, bool> ConditionA { get; }
public Func<T, bool> ConditionA { get; } = conditionA;

public Func<T, bool> ConditionB { get; }
public Func<T, bool> ConditionB { get; } = conditionB;

public BtStatus Execute(T blackboard)
{
Expand Down
18 changes: 4 additions & 14 deletions AmeisenBotX.BehaviorTree/Objects/Inverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,9 @@ namespace AmeisenBotX.BehaviorTree.Objects
/// <summary>
/// Inverts the status of a node. Ongoing state will remain Onging.
/// </summary>
public class Inverter : INode
public class Inverter(INode child) : INode
{
public Inverter(INode child) : base()
{
Child = child;
}

public INode Child { get; }
public INode Child { get; } = child;

public BtStatus Execute()
{
Expand All @@ -36,14 +31,9 @@ public INode GetNodeToExecute()
}
}

public class Inverter<T> : INode<T>
public class Inverter<T>(INode<T> child) : INode<T>
{
public Inverter(INode<T> child) : base()
{
Child = child;
}

public INode<T> Child { get; }
public INode<T> Child { get; } = child;

public BtStatus Execute(T blackboard)
{
Expand Down
18 changes: 4 additions & 14 deletions AmeisenBotX.BehaviorTree/Objects/Leaf.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,9 @@

namespace AmeisenBotX.BehaviorTree.Objects
{
public class Leaf : INode
public class Leaf(Func<BtStatus> behaviorTreeAction) : INode
{
public Leaf(Func<BtStatus> behaviorTreeAction) : base()
{
BehaviorTreeAction = behaviorTreeAction;
}

public Func<BtStatus> BehaviorTreeAction { get; set; }
public Func<BtStatus> BehaviorTreeAction { get; set; } = behaviorTreeAction;

public BtStatus Execute()
{
Expand All @@ -23,14 +18,9 @@ public INode GetNodeToExecute()
}
}

public class Leaf<T> : INode<T>
public class Leaf<T>(Func<T, BtStatus> behaviorTreeAction) : INode<T>
{
public Leaf(Func<T, BtStatus> behaviorTreeAction) : base()
{
BehaviorTreeAction = behaviorTreeAction;
}

public Func<T, BtStatus> BehaviorTreeAction { get; set; }
public Func<T, BtStatus> BehaviorTreeAction { get; set; } = behaviorTreeAction;

public BtStatus Execute(T blackboard)
{
Expand Down
24 changes: 6 additions & 18 deletions AmeisenBotX.BehaviorTree/Objects/Selector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,11 @@ namespace AmeisenBotX.BehaviorTree.Objects
/// <summary>
/// Executes a when input is true and b when it is false.
/// </summary>
public class Selector : IComposite
public class Selector(Func<bool> condition, INode nodeA, INode nodeB) : IComposite
{
public Selector(Func<bool> condition, INode nodeA, INode nodeB) : base()
{
Condition = condition;
Children = new INode[] { nodeA, nodeB };
}

public INode[] Children { get; }
public INode[] Children { get; } = [nodeA, nodeB];

public Func<bool> Condition { get; }
public Func<bool> Condition { get; } = condition;

public BtStatus Execute()
{
Expand All @@ -29,17 +23,11 @@ public INode GetNodeToExecute()
}
}

public class Selector<T> : IComposite<T>
public class Selector<T>(Func<T, bool> condition, INode<T> nodeA, INode<T> nodeB) : IComposite<T>
{
public Selector(Func<T, bool> condition, INode<T> nodeA, INode<T> nodeB) : base()
{
Condition = condition;
Children = new INode<T>[] { nodeA, nodeB };
}

public INode<T>[] Children { get; }
public INode<T>[] Children { get; } = [nodeA, nodeB];

public Func<T, bool> Condition { get; }
public Func<T, bool> Condition { get; } = condition;

public BtStatus Execute(T blackboard)
{
Expand Down
18 changes: 4 additions & 14 deletions AmeisenBotX.BehaviorTree/Objects/Sequence.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,9 @@ namespace AmeisenBotX.BehaviorTree.Objects
/// Executes a sequence of nodes until all nodes returned success. If a node fails or the
/// sequence finished, it gets resetted.
/// </summary>
public class Sequence : IComposite
public class Sequence(params INode[] children) : IComposite
{
public Sequence(params INode[] children)
{
Children = children;
}

public INode[] Children { get; }
public INode[] Children { get; } = children;

public int Counter { get; private set; }

Expand Down Expand Up @@ -55,14 +50,9 @@ public INode GetNodeToExecute()
}
}

public class Sequence<T> : IComposite<T>
public class Sequence<T>(params INode<T>[] children) : IComposite<T>
{
public Sequence(params INode<T>[] children)
{
Children = children;
}

public INode<T>[] Children { get; }
public INode<T>[] Children { get; } = children;

public int Counter { get; private set; }

Expand Down
24 changes: 6 additions & 18 deletions AmeisenBotX.BehaviorTree/Objects/Waterfall.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,13 @@ namespace AmeisenBotX.BehaviorTree.Objects
/// Special selector that runs the first node where the condition returns true. If none returned
/// true, the fallbackNode will be executed
/// </summary>
public class Waterfall : IComposite
public class Waterfall(INode fallbackNode, params (Func<bool> condition, INode node)[] conditionNodePairs) : IComposite
{
public Waterfall(INode fallbackNode, params (Func<bool> condition, INode node)[] conditionNodePairs) : base()
{
FallbackNode = fallbackNode;
ConditionNodePairs = conditionNodePairs;
}

public INode[] Children { get; }

public (Func<bool> condition, INode node)[] ConditionNodePairs { get; }
public (Func<bool> condition, INode node)[] ConditionNodePairs { get; } = conditionNodePairs;

public INode FallbackNode { get; }
public INode FallbackNode { get; } = fallbackNode;

public BtStatus Execute()
{
Expand All @@ -40,19 +34,13 @@ public INode GetNodeToExecute()
}
}

public class Waterfall<T> : IComposite<T>
public class Waterfall<T>(INode<T> fallbackNode, params (Func<T, bool> condition, INode<T> node)[] conditionNodePairs) : IComposite<T>
{
public Waterfall(INode<T> fallbackNode, params (Func<T, bool> condition, INode<T> node)[] conditionNodePairs) : base()
{
FallbackNode = fallbackNode;
ConditionNodePairs = conditionNodePairs;
}

public INode<T>[] Children { get; }

public (Func<T, bool> condition, INode<T> node)[] ConditionNodePairs { get; }
public (Func<T, bool> condition, INode<T> node)[] ConditionNodePairs { get; } = conditionNodePairs;

public INode<T> FallbackNode { get; }
public INode<T> FallbackNode { get; } = fallbackNode;

public BtStatus Execute(T blackboard)
{
Expand Down
12 changes: 3 additions & 9 deletions AmeisenBotX.BehaviorTree/Tree.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,19 +80,13 @@ public BtStatus Tick()
}
}

public class Tree
public class Tree(INode node, bool resumeOngoingNodes = false)
{
public Tree(INode node, bool resumeOngoingNodes = false)
{
RootNode = node;
ResumeOngoingNodes = resumeOngoingNodes;
}

public INode OngoingNode { get; private set; }

public bool ResumeOngoingNodes { get; set; }
public bool ResumeOngoingNodes { get; set; } = resumeOngoingNodes;

public INode RootNode { get; set; }
public INode RootNode { get; set; } = node;

public BtStatus Tick()
{
Expand Down
4 changes: 3 additions & 1 deletion AmeisenBotX.Common/Keyboard/Enums/KeyCode.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;

#pragma warning disable CA1069
namespace AmeisenBotX.Common.Keyboard.Enums
{
[Flags]
Expand Down Expand Up @@ -200,4 +201,5 @@ public enum KeyCode // todo: rewrite, messy
Z = 90,
Zoom = 0xfb
}
}
}
#pragma warning restore CA1069
24 changes: 12 additions & 12 deletions AmeisenBotX.Common/Keyboard/KeyboardHook.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ namespace AmeisenBotX.Common.Keyboard
/// <summary>
/// This is a global keyboard hook used to manage hotkeys.
/// </summary>
public class KeyboardHook
public partial class KeyboardHook
{
public KeyboardHook()
{
Expand Down Expand Up @@ -56,7 +56,7 @@ public void Disable()
{
if (HookPtr != nint.Zero)
{
UnhookWindowsHookEx(HookPtr);
_ = UnhookWindowsHookEx(HookPtr);
}
}

Expand All @@ -74,20 +74,20 @@ public void Enable()
}
}

[DllImport("user32", SetLastError = true)]
private static extern int CallNextHookEx(nint hHook, int nCode, nint wParam, ref LowLevelKeyboardInput lParam);
[LibraryImport("user32")]
private static partial int CallNextHookEx(nint hHook, int nCode, nint wParam, ref LowLevelKeyboardInput lParam);

[DllImport("user32", SetLastError = true)]
private static extern short GetKeyState(KeyCode nVirtKey);
[LibraryImport("user32")]
private static partial short GetKeyState(KeyCode nVirtKey);

[DllImport("kernel32", CharSet = CharSet.Auto, SetLastError = true)]
private static extern nint GetModuleHandle(string lpModuleName);
[LibraryImport("kernel32", EntryPoint = "GetModuleHandleA", StringMarshalling = StringMarshalling.Utf16)]
private static partial nint GetModuleHandle(string lpModuleName);

[DllImport("user32", SetLastError = true)]
private static extern nint SetWindowsHookEx(int idHook, LowLevelKeyboardProc lpfn, nint hMod, int dwThreadId);
[LibraryImport("user32", EntryPoint = "SetWindowsHookExA")]
private static partial nint SetWindowsHookEx(int idHook, LowLevelKeyboardProc lpfn, nint hMod, int dwThreadId);

[DllImport("user32", SetLastError = true)]
private static extern int UnhookWindowsHookEx(nint hHook);
[LibraryImport("user32")]
private static partial int UnhookWindowsHookEx(nint hHook);

private int LowLevelKeyboardCallback(int nCode, nint wParam, ref LowLevelKeyboardInput lParam)
{
Expand Down
4 changes: 1 addition & 3 deletions AmeisenBotX.Common/Math/BotMath.cs
Original file line number Diff line number Diff line change
Expand Up @@ -118,9 +118,7 @@ public static double SlopeGradientAngle(Vector3 startPoint, Vector3 endPoint)
// calculates the arctan to get the radians (arctan(alpha) = rise / run)
double radAngle = System.Math.Atan(slope / 100);
// converts the radians in degrees
double degAngle = radAngle * 180 / System.Math.PI;

return degAngle;
return radAngle * 180 / System.Math.PI;
}
}
}
Loading

0 comments on commit e146b06

Please sign in to comment.