summaryrefslogtreecommitdiff
path: root/OnekoLocal.cs
blob: df54ff11d7edb4b7b272a8fa2ae82582bff2b76a (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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
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 override int DrawSort => (int)(Position.Y - (Size.Y/2.5f));

    public OnekoLocal() : base()
    {
        Instance ??= this;

        Name = OnekoOnline.Config.GetValue("NekoName", "Oneko");

        AIStates = [
            new BoredState(this), new ChaseMouseState(this), new ItchyState(this),
            new SleepState(this), new RunAwayFromCatState(this), new RandomSpotState(this),
            new ChaseOtherCatState(this), new YawnState(this)
        ];
        CurrentAIState = AIStates[0];
    }

    public override void Update(float delta)
    {
        updateTimer += delta;
        if (updateTimer < updateRate) return;

        OnekoUpdate(updateTimer);
        updateTimer = 0f;
    }

    public override void Draw()
    {
        #if DEBUG
        Raylib.DrawText($"Playfullness {OnekoAIState.Playfullness}", 0, 210, 10, Color.White);
        Raylib.DrawText($"Energy {OnekoAIState.Energy}", 0, 220, 10, Color.White);
        Raylib.DrawText($"State {CurrentAIState.GetType().Name}", 0, 230, 10, Color.White);
        #endif

        base.Draw();
    }

    public void OnekoUpdate(float delta)
    {
        //AI STUFF
        if (Random.Shared.NextSingle() > 0.997f) OnekoAIState.Playfullness = Random.Shared.NextSingle();
        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 == true) {
            NetDataWriter writer = new();
            writer.Put(new PacketInfo(PacketType.OnekoState, Id));
            writer.Put(Position);
            writer.Put(FrameId);
            OnekoOnline.Client?.ConnectedServer.Send(writer, LiteNetLib.DeliveryMethod.Unreliable);
        }

        Position = Vector2.Clamp(Position, Vector2.Zero, new(OnekoOnline.WindowX, OnekoOnline.WindowY));
    }

    void MoveTowardsPosition(Vector2 TargetPosition)
    {
        if (TargetPosition.Round() == Position) {
            Animation = Idle;
            return;
        }

        Vector2 TargetDirection = (TargetPosition-Position).LimitLength(8+(OnekoAIState.Energy*4f));
        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;

        //Mood
        private static float _playfulness = 0.8f;
        public static float Playfullness {
            get => _playfulness;
            set => _playfulness = Math.Clamp(value, 0f, 1f);
        }

        private static float _energy = 0.8f;
        public static float Energy {
            get => _energy;
            set => _energy = Math.Clamp(value, 0f, 1f);
        }
    }

    class BoredState(OnekoLocal neko) : OnekoAIState(neko)
    {
        float InactivityTimer = 0f;

        public override int GetPriority() => 0;

        public override void Update(float delta)
        {
            InactivityTimer += delta;
            Energy -= delta/150f;

            Neko.Animation = Idle;
            if (InactivityTimer > 1.5f && InactivityTimer < 3f) Neko.Animation = Clean;
            if (InactivityTimer > 5f && InactivityTimer < 6.5f) Neko.Animation = ScratchSelf;
            if (InactivityTimer > 10f) {
                if (Energy < 0.7f) SleepState.Sleeping = true;
                else RandomSpotState.NewSpotNow = true;
            }
        }

        public override void ExitState() => InactivityTimer = 0f;
    }

    class SleepState : OnekoAIState
    {
        float SleepTimer = 0f;
        public static bool Sleeping;

        public SleepState(OnekoLocal neko) : base(neko)
        {
            Mouse.Clicked += mouse => {
                if (Sleeping && Energy > 0.25f && mouse is MouseLocal) Sleeping = false;
            };
        }

        public override int GetPriority()
        {
            if (Energy == 0f) Sleeping = true;
            if (Energy > 0.5f && Sleeping) return 1;
            if (Sleeping) return 900;
            else return -1;
        }

        public override void Update(float delta)
        {
            SleepTimer += delta;
            Energy += delta/60f;

            if (SleepTimer < 1f) Neko.Animation = Yawn;
            else Neko.Animation = Sleep;
        }

        public override void ExitState() {
            SleepTimer = 0f;
            Sleeping = false;
        }
    }

    class RunAwayFromCatState(OnekoLocal neko) : OnekoAIState(neko)
    {
        Oneko? ClosestNeko;
        float RandOffset;

        public override int GetPriority()
        {
            if (AllNekos.Count < 2) return -1;
            //If a cat is too close, run!
            ClosestNeko = AllNekos.Where(neko => neko != Neko && Vector2.Distance(neko.Position, Neko.Position) < 30).FirstOrDefault();
            if (ClosestNeko != null) return 5;
            return -1;
        }

        public override void StartState() => RandOffset = Random.Shared.NextSingle() * 100f;

        public override void Update(float delta)
        {
            if (ClosestNeko is null) return;
            Energy -= delta/60f;
            Playfullness += delta/60f;
            Vector2 Direction = Raymath.Vector2Rotate(Directions.Up, DateTime.Now.Second+RandOffset);
            Neko.MoveTowardsPosition(ClosestNeko.Position + Direction*100);
        }
    }

    class ChaseOtherCatState(OnekoLocal neko) : OnekoAIState(neko)
    {
        Oneko? Victim;
        DateTime LastChase = DateTime.UnixEpoch;
        float RandOffset;

        public override int GetPriority()
        {
            //If feeling playful, chase another cat!
            if (Victim != null && LastChase.AddSeconds(Energy > 0.5 ? 24 : 12) > DateTime.Now) return 80;
            if (Playfullness < 0.7f || AllNekos.Count < 2 || LastChase.AddMinutes(Energy > 0.5 ? 2 : 5) > DateTime.Now) return -1;
            Victim = AllNekos.Where(neko => neko != Neko).MinBy(neko => Random.Shared.NextSingle());
            if (Victim != null) return 80;
            return -1;
        }

        public override void StartState() {
            RandOffset = Random.Shared.NextSingle() * 100f;
            LastChase = DateTime.Now;
        }

        public override void Update(float delta)
        {
            if (Victim is null) return;
            Playfullness -= delta/40f;
            Energy -= delta/60f;
            Neko.MoveTowardsPosition(Victim.Position + Raymath.Vector2Rotate(Directions.Down, DateTime.Now.Second+RandOffset)*28);
        }
    }

    class ChaseMouseState : OnekoAIState
    {
        Mouse? MouseToChase;
        Vector2 MousePosition;
        float AlertTimer = 0f;

        public ChaseMouseState(OnekoLocal neko) : base(neko)
        {
            Mouse.Clicked += mouse => {
                Playfullness += 0.03f;
                if (MouseToChase != mouse && Random.Shared.NextSingle() > 0.3f) {
                    AlertTimer = 0.5f;
                    MouseToChase = mouse;
                }
            };
        }

        public override int GetPriority()
        {
            if (Playfullness < 0.5f) return -1;
            MouseToChase ??= Mouse.AllMice.Where(m => m.Visible).MinBy(m => Vector2.Distance(m.Position, Neko.Position));
            if (MouseToChase is null || !MouseToChase.Visible) return -1;
            if (MouseToChase.Position == MousePosition && !IsCurrentState) return -1;
            if (Vector2.Distance(MouseToChase.Position, Neko.Position) > 30) return (int)(Playfullness*150f);
            return -1;
        }

        public override void Update(float delta)
        {
            if (AlertTimer < 1f) {
                AlertTimer += delta;
                Neko.Animation = Alert;
                return;
            }

            if (MouseToChase is null || !MouseToChase.Visible) return;
            MousePosition = MouseToChase.Position;
            Neko.MoveTowardsPosition(MousePosition + (Raymath.Vector2Rotate(Directions.Down, Id*2f) * 20f));

            Energy -= delta/60f;
            Playfullness -= delta/30f;
        }

        public override void ExitState() {
            MouseToChase = null;
            AlertTimer = 0f;
        }
    }

    class RandomSpotState(OnekoLocal neko) : OnekoAIState(neko)
    {
        DateTime NextInvestigation = DateTime.Now;
        Vector2 Spot;
        float AlertTimer = 0f;
        public static bool NewSpotNow = false;

        public override int GetPriority()
        {
            if (NewSpotNow) {
                NewSpotNow = false;
                return 50;
            }
            if (NextInvestigation < DateTime.Now || (Energy > 0.75f && Random.Shared.NextSingle() > 0.99f)) return 50;
            return -1;
        }

        public override void StartState()
        {
            Spot = new(Random.Shared.NextSingle()*OnekoOnline.WindowX, Random.Shared.NextSingle()*OnekoOnline.WindowY);
        }

        public override void Update(float delta)
        {
            if (AlertTimer < 1f) {
                AlertTimer += delta;
                Neko.Animation = Alert;
                return;
            }
            Neko.MoveTowardsPosition(Spot);
            Energy -= delta/60f;

            if (Vector2.Distance(Spot, Neko.Position) < 10)
                NextInvestigation = DateTime.Now.AddSeconds(Random.Shared.NextSingle() * 120);
        }

        public override void ExitState() => AlertTimer = 0f;
    }

    class ItchyState(OnekoLocal neko) : OnekoAIState(neko)
    {
        float ItchTimer = -1f;
        bool Cleaning = false;

        public override int GetPriority()
        {
            if (Neko.CurrentAIState is SleepState && Energy < 0.5f) return -1;
            if (ItchTimer > 0f || Random.Shared.NextSingle() > 0.995f) return 1000;
            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;
        }
    }

    class YawnState(OnekoLocal neko) : OnekoAIState(neko)
    {
        float YawnTimer = -1f;

        public override int GetPriority()
        {
            if (Energy < 0.25f && Neko.CurrentAIState is not SleepState && (YawnTimer > 0f || Random.Shared.NextSingle() > 0.98f)) return 2000;
            else return -1;
        }

        public override void StartState() => YawnTimer = 2f;

        public override void Update(float delta) {
            YawnTimer -= delta;
            Energy -= delta/60f;
            Neko.Animation = Yawn;
        }
    }
}