blob: e963740aeff8f477f5cb9a11898829dc398c10c8 (
plain)
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
|
using System.Numerics;
using LiteNetLib.Utils;
using OnekoOnline.Net;
using Raylib_cs;
namespace OnekoOnline;
class OnekoLocal : Oneko
{
public static Oneko? Instance;
static int Id => OnekoOnline.Client?.Id ?? -1;
OnekoAIState CurrentAIState;
readonly OnekoAIState[] AIStates;
OnekoAnimation Animation;
float updateTimer = 0f;
const float updateRate = 1f/5f;
int Frame = 0;
int FrameCounter = 0;
public OnekoLocal() : base()
{
Instance ??= this;
Name = OnekoOnline.Config.GetValue("NekoName", "Oneko");
AIStates = [new BoredState(this), new ChaseMouseState(this), new ItchyState(this)];
CurrentAIState = AIStates[0];
}
public override void Update(float delta)
{
updateTimer += delta;
if (updateTimer < updateRate) return;
OnekoUpdate(updateTimer);
updateTimer = 0f;
}
public void OnekoUpdate(float delta)
{
//AI STUFF
OnekoAIState HighestPriority = AIStates.MaxBy(state => state.GetPriority())!;
if (HighestPriority != CurrentAIState) {
CurrentAIState.ExitState();
CurrentAIState = HighestPriority;
CurrentAIState.StartState();
}
CurrentAIState.Update(delta);
FrameCounter = (FrameCounter+1)%(Animation.AnimSpeed+1);
Frame = (int)MathF.Round(FrameCounter/(float)Animation.AnimSpeed);
Sprite = Animation.GetFrame(Frame);
if (OnekoOnline.Client!.Connected) {
NetDataWriter writer = new();
writer.Put(new PacketInfo(PacketType.OnekoState, Id));
writer.Put(Position);
writer.Put(FrameId);
OnekoOnline.Client?.ConnectedServer.Send(writer, LiteNetLib.DeliveryMethod.Unreliable);
}
}
void MoveTowardsPosition(Vector2 TargetPosition)
{
Vector2 TargetDirection = (TargetPosition-Position).LimitLength(10f);
if (TargetDirection.Length() > 1) {
Animation = GetDirectionalRun(TargetDirection);
}
Position += TargetDirection;
}
protected abstract class OnekoAIState(OnekoLocal neko)
{
protected OnekoLocal Neko = neko;
public abstract int GetPriority();
public abstract void Update(float delta);
public virtual void StartState() {}
public virtual void ExitState() {}
protected bool IsCurrentState => Neko.CurrentAIState == this;
}
class BoredState(OnekoLocal neko) : OnekoAIState(neko)
{
float InactivityTimer = 0f;
public override int GetPriority() => 0;
public override void Update(float delta)
{
InactivityTimer += delta;
Neko.Animation = Idle;
if (InactivityTimer > 1.5f && InactivityTimer < 3f) Neko.Animation = Clean;
if (InactivityTimer > 5f) Neko.Animation = ScratchSelf;
if (InactivityTimer > 7f) Neko.Animation = Yawn;
if (InactivityTimer > 8f) Neko.Animation = Sleep;
}
public override void ExitState() => InactivityTimer = 0f;
}
class ChaseMouseState : OnekoAIState
{
Mouse? MouseToChase;
float AlertTimer = 0f;
public ChaseMouseState(OnekoLocal neko) : base(neko)
{
Mouse.Clicked += mouse => {
if (MouseToChase != mouse && Random.Shared.NextSingle() > 0.3f) {
AlertTimer = 0.5f;
MouseToChase = mouse;
}
};
}
public override int GetPriority()
{
MouseToChase ??= Mouse.AllMice.Where(m => m.Visible).MinBy(m => Vector2.Distance(m.Position, Neko.Position));
if (MouseToChase != null && MouseToChase.Visible && Vector2.Distance(MouseToChase.Position, Neko.Position) > 15) return 100;
return -1;
}
public override void Update(float delta)
{
if (AlertTimer < 1f) {
AlertTimer += delta;
Neko.Animation = Alert;
return;
}
if (MouseToChase is null || !MouseToChase.Visible) return;
Neko.MoveTowardsPosition(MouseToChase.Position);
}
public override void ExitState() {
MouseToChase = null;
AlertTimer = 0f;
}
}
class ItchyState(OnekoLocal neko) : OnekoAIState(neko)
{
float ItchTimer = -1f;
bool Cleaning = false;
public override int GetPriority()
{
if (ItchTimer > 0f || Random.Shared.NextSingle() > 0.997f) return 1000;
else return -1;
}
public override void StartState() {
ItchTimer = 1f + Random.Shared.NextSingle()*4f;
Cleaning = Random.Shared.NextSingle() > 0.5f;
}
public override void Update(float delta) {
ItchTimer -= delta;
Neko.Animation = Cleaning ? Clean : ScratchSelf;
}
}
}
|