OneNightDuel/ONDClient/GUI/JumpscareUIElement.cs

51 lines
1.6 KiB
C#
Raw Permalink Normal View History

using System;
using System.Diagnostics;
using Microsoft.Xna.Framework;
using MonoGameLibrary.Graphics;
namespace ONDClient.GUI;
public class JumpscareUIElement : UIElement {
private int twitchHorizontal;
private int twitchVertical;
private Point positionDefault;
private Random random;
private bool playing = false;
private Stopwatch stopwatch = new();
private int duration;
public JumpscareUIElement(TextureRegion texture, Point positionDefault, int twitchHorizontal, int twitchVertical, float defaultScaleMultiplier, int durationMs = 2000, Action afterStop = null) : base(texture, positionDefault) {
this.twitchHorizontal = twitchHorizontal;
this.twitchVertical = twitchVertical;
this.positionDefault = positionDefault;
random = new Random();
ScaleMultiplier = defaultScaleMultiplier;
duration = durationMs;
Active = false;
Visible = false;
AfterStop = afterStop;
}
public void Play() {
playing = true;
Active = true;
Visible = true;
stopwatch.Start();
}
public override void Update() {
if (!playing) return;
SetPosition(new(positionDefault.X + random.Next(-twitchHorizontal, twitchHorizontal), positionDefault.Y + random.Next(-twitchVertical, twitchVertical)));
if (stopwatch.ElapsedMilliseconds >= duration){
playing = false;
Active = false;
Visible = false;
if (AfterStop != null){
AfterStop();
}
}
}
private Action AfterStop;
}