-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
78 lines (71 loc) · 2.28 KB
/
Copy pathProgram.cs
File metadata and controls
78 lines (71 loc) · 2.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
public class ActionDamage : ActionNode
{
private IPlayer _player;
private int _damage;
public ActionDamage(int damage) : base(null)
{
_damage = damage;
}
public override NodeStatus Execute()
{
if (_player == null)
{
var player = BehaviorTree?.Blackboard.GetValue<IPlayer>("Player");
if (player == null) throw new Exception("Player not found in blackboard.");
_player = player;
}
_player.Health -= _damage;
Console.WriteLine($"Player damaged for {_damage}. Remaining health: {_player.Health}");
return _player.Health <= 0 ? NodeStatus.Failure : NodeStatus.Success;
}
}
public class PlayerConditionNode : ConditionNode
{
private IPlayer _player;
private Func<IPlayer, bool> _playerCondition;
public PlayerConditionNode(Func<IPlayer, bool> playerCondition) : base(null)
{
_playerCondition = playerCondition;
}
public override NodeStatus Execute()
{
if (_player == null)
{
var player = BehaviorTree?.Blackboard.GetValue<IPlayer>("Player");
if (player == null) throw new Exception("Player not found in blackboard.");
_player = player;
}
bool result = _playerCondition(_player);
return result ? NodeStatus.Success : NodeStatus.Failure;
}
}
public class Program
{
public static void Main(string[] args)
{
BehaviorTree _behaviorTree = new BehaviorTree(
new SelectorNode(new List<Node>
{
new SequenceNode(new List<Node>
{
new PlayerConditionNode(player => player.Health > 50),
new ActionDamage(20),
}),
new SequenceNode(new List<Node>
{
new ActionNode(() => {
Console.WriteLine("Not harming player, health is low.");
return NodeStatus.Success;
})
}),
})
);
_behaviorTree.Blackboard.SetValue<IPlayer>("Player", new DummyPLayer(100));
var steps = 5;
for (int i = 0; i < steps; i++)
{
_behaviorTree.Tick();
}
}
}