diff options
Diffstat (limited to 'OnekoLocal.cs')
| -rw-r--r-- | OnekoLocal.cs | 55 |
1 files changed, 51 insertions, 4 deletions
diff --git a/OnekoLocal.cs b/OnekoLocal.cs index 00eaf82..c33dd66 100644 --- a/OnekoLocal.cs +++ b/OnekoLocal.cs @@ -11,6 +11,17 @@ class OnekoLocal : Oneko 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; @@ -20,19 +31,55 @@ class OnekoLocal : Oneko Name = Client.UserName; } - public override void OnekoUpdate() + 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.OnekoTargetPosition, Id)); - writer.Put(TargetPosition); + 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; - base.OnekoUpdate(); + public abstract int GetPriority(); + public abstract void Update(float delta); + protected bool IsCurrentState => Neko.CurrentAIState == this; } }
\ No newline at end of file |
