59 lines
2 KiB
C#
59 lines
2 KiB
C#
|
|
using System;
|
||
|
|
using System.Diagnostics;
|
||
|
|
using Microsoft.Xna.Framework;
|
||
|
|
using MonoGameLibrary.Graphics;
|
||
|
|
|
||
|
|
namespace FNAF_Clone.GUI;
|
||
|
|
|
||
|
|
public class JumpscareUIElement : UIElement {
|
||
|
|
private int twitchHorizontal;
|
||
|
|
private int twitchVertical;
|
||
|
|
private Point positionDefault;
|
||
|
|
private Random random;
|
||
|
|
private float defaultScaleMultiplier;
|
||
|
|
private float twitchScale;
|
||
|
|
|
||
|
|
private bool playing = false;
|
||
|
|
private Stopwatch stopwatch = new();
|
||
|
|
private int duration;
|
||
|
|
|
||
|
|
public JumpscareUIElement(TextureRegion texture, Point positionDefault, int twitchHorizontal, int twitchVertical, float defaultScaleMultiplier, float twitchScale, int durationMs = 2000, Action afterStop = null) : base(texture, positionDefault) {
|
||
|
|
this.twitchHorizontal = twitchHorizontal;
|
||
|
|
this.twitchVertical = twitchVertical;
|
||
|
|
this.positionDefault = positionDefault;
|
||
|
|
random = new Random();
|
||
|
|
this.defaultScaleMultiplier = defaultScaleMultiplier;
|
||
|
|
ScaleMultiplier = defaultScaleMultiplier;
|
||
|
|
this.twitchScale = twitchScale;
|
||
|
|
duration = durationMs;
|
||
|
|
Active = false;
|
||
|
|
Visible = false;
|
||
|
|
AfterStop = afterStop;
|
||
|
|
}
|
||
|
|
|
||
|
|
// public JumpscareUIElement(UIElement element) : base(element.GetTextures(), element.Bounds.Item1) {}
|
||
|
|
|
||
|
|
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();
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
// ScaleMultiplier = defaultScaleMultiplier + (float)(random.NextDouble() * twitchScale * new[]{-1, 1}[random.Next(2)]);
|
||
|
|
}
|
||
|
|
|
||
|
|
private Action AfterStop;
|
||
|
|
}
|