Skip to content

Instantly share code, notes, and snippets.

@Donnotron666
Created October 31, 2017 23:04
Show Gist options
  • Save Donnotron666/c5b66dc5d512a8741eba0c51d451f483 to your computer and use it in GitHub Desktop.
Save Donnotron666/c5b66dc5d512a8741eba0c51d451f483 to your computer and use it in GitHub Desktop.
using Client.Game.Utils;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UnityEngine;
namespace Client.Game.Actors.Controllers {
public class VectorConsumer {
public Vector2 Vector;
//seconds to zero
public float Duration;
protected float accum;
protected float lastAlpha;
public VectorConsumer(Vector2 motion, float time) {
Vector = motion;
Duration = time;
}
public VectorConsumer() {
}
public float TimeLeft {
get {
return Duration - accum;
}
}
public virtual Vector2 Consume(float dt) {
if (accum < Duration) {
accum += dt;
float alpha = Easing.ExponentialEaseOut(accum/Duration);
float diffAlpha = alpha - lastAlpha;
lastAlpha = alpha;
var move = Vector * diffAlpha;
Vector -= move;
return move;
}
return Vector2.zero;
}
public bool IsComplete
{
get
{
return accum >= Duration;
}
}
public static VectorConsumer operator +(VectorConsumer a, VectorConsumer b) {
return new VectorConsumer(a.Vector + b.Vector, Mathf.Max(a.TimeLeft, b.TimeLeft));
}
}
public class ShakeConsumer : VectorConsumer
{
private float magnitude;
private Vector3 Mask;
public ShakeConsumer(float magnitude, float duration, Vector3 mask)
{
this.magnitude = magnitude;
Duration = duration;
this.Mask = mask;
}
public ShakeConsumer(){ }
public override Vector2 Consume(float dt)
{
if(accum < Duration)
{
accum += dt;
//using a sine wave means that the pattern definitely has a phase, and extreme shake won't actually move the object too much, rather, it'll just get vibrated to death.
return new Vector2(
Mathf.Sin(accum * 200) * magnitude * Mask.x,
Mathf.Sin(accum * accum * 200) * magnitude * Mask.y);
} else
{
magnitude = 0f;
}
return Vector3.zero;
}
public static ShakeConsumer operator +(ShakeConsumer a, ShakeConsumer b)
{
return new ShakeConsumer(a.magnitude + b.magnitude, Mathf.Max(a.TimeLeft, b.TimeLeft), Vector3.Max(a.Mask, b.Mask));
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment