summaryrefslogtreecommitdiff
path: root/OnekoLocal.cs
blob: 249c5d1db7825946605068cb5820c18c2e0fa006 (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
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;

    Vector2 TargetPosition = Vector2.Zero;
    float InactivityTimer = 0f;

    OnekoAnimation Animation;
    float updateTimer = 0f;
    const float updateRate = 1f/5f;
    int Frame = 0;
    int FrameCounter = 0;

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

        Client.UserConnected += OnekoNet.SpawnNetNeko;

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

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

        OnekoUpdate(updateTimer);
        updateTimer = 0f;
    }

    public void OnekoUpdate(float delta)
    {
        InactivityTimer += delta;

        Mouse? NearestMouse = Mouse.AllMice.Where(m => m.Visible).MinBy(m => Vector2.Distance(m.Position, Position));
        if (NearestMouse != null) TargetPosition = NearestMouse.Position;
        else TargetPosition = new Vector2(Math.Clamp((Id+1)*40, 20, 300), 240/2);

        Vector2 TargetDirection = (TargetPosition-Position).LimitLength(10f);
        if (TargetDirection.Length() > 1) {
            Animation = GetDirectionalRun(TargetDirection);
            InactivityTimer = 0f;
        }
        else Animation = Idle;
        Position += TargetDirection;

        if (InactivityTimer > 3f) Animation = ScratchSelf;
        if (InactivityTimer > 5f) Animation = Yawn;
        if (InactivityTimer > 6f) Animation = Sleep;

        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);
        }
    }

    protected abstract class OnekoAIState(OnekoLocal neko)
    {
        protected OnekoLocal Neko = neko;

        public abstract int GetPriority();
        public abstract void Update(float delta);
        protected bool IsCurrentState => Neko.CurrentAIState == this;
    }
}