Files
OpenRA/OpenRa.Game/Traits/World/ScreenShaker.cs
2010-02-27 19:40:57 +13:00

67 lines
1.4 KiB
C#

using System;
using System.Linq;
using OpenRa.Traits;
using System.Collections.Generic;
using OpenRa.FileFormats;
namespace OpenRa.Traits
{
class ScreenShakerInfo : ITraitInfo
{
public object Create( Actor self ) { return new ScreenShaker( self ); }
}
public class ScreenShaker : ITick
{
static int ticks = 0;
static List<Tuple<int, float2, int>> shakeEffects = new List<Tuple<int, float2, int>>();
public ScreenShaker (Actor self){}
public void Tick (Actor self)
{
Game.viewport.Scroll(getScrollOffset());
UpdateList();
ticks++;
}
void UpdateList()
{
var toRemove = new List<Tuple<int, float2, int>>();
shakeEffects.RemoveAll(t => t.a == ticks);
foreach(Tuple<int, float2, int> t in toRemove){
shakeEffects.Remove(t);
}
}
public static void RegisterShakeEffect(int time, float2 position, int intensity)
{
shakeEffects.Add(Tuple.New(ticks + time, position, intensity));
}
public float2 getScrollOffset()
{
int xFreq = 4;
int yFreq = 5;
return GetIntensity() * new float2(
(float) Math.Sin((ticks*2*Math.PI)/xFreq) ,
(float) Math.Cos((ticks*2*Math.PI)/yFreq));
}
public float GetIntensity()
{
var cp = Game.viewport.Location
+ .5f * new float2(Game.viewport.Width, Game.viewport.Height);
var intensity = 24 * 24 * 100 * shakeEffects.Sum(
e => e.c / (e.b - cp).LengthSquared);
return Math.Min(intensity, 10);
}
}
}