some fixes to shake code

This commit is contained in:
Chris Forbes
2010-02-27 19:40:57 +13:00
parent 5db5829bb6
commit 5dc240d390
2 changed files with 19 additions and 24 deletions

View File

@@ -94,7 +94,7 @@ namespace OpenRa.Traits
{
if (e.DamageState == DamageState.Dead)
{
ScreenShaker.RegisterShakeEffect(10, self.Location.ToFloat2()*new float2(24, 24), 3);
ScreenShaker.RegisterShakeEffect(10, self.CenterLocation, 1);
Sound.Play("kaboom22.aud");
}
}

View File

@@ -1,5 +1,6 @@
using System;
using System;
using System.Linq;
using OpenRa.Traits;
using System.Collections.Generic;
using OpenRa.FileFormats;
@@ -13,8 +14,8 @@ namespace OpenRa.Traits
public class ScreenShaker : ITick
{
int ticks = 0;
private static List<Tuple<int, float2, int>> shakeEffects = new List<Tuple<int, float2, int>>();
static int ticks = 0;
static List<Tuple<int, float2, int>> shakeEffects = new List<Tuple<int, float2, int>>();
public ScreenShaker (Actor self){}
@@ -25,18 +26,10 @@ namespace OpenRa.Traits
ticks++;
}
private void UpdateList()
void UpdateList()
{
var toRemove = new List<Tuple<int, float2, int>>();
for (int i = 0; i < shakeEffects.Count; i++){
var tuple = shakeEffects[i];
tuple.a = tuple.a - 1;
shakeEffects[i] = tuple;
if (tuple.a == 0)
toRemove.Add(tuple);
}
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);
@@ -45,7 +38,7 @@ namespace OpenRa.Traits
public static void RegisterShakeEffect(int time, float2 position, int intensity)
{
shakeEffects.Add(Tuple.New<int, float2, int>(time, position, intensity));
shakeEffects.Add(Tuple.New(ticks + time, position, intensity));
}
public float2 getScrollOffset()
@@ -53,17 +46,19 @@ namespace OpenRa.Traits
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));
return GetIntensity() * new float2(
(float) Math.Sin((ticks*2*Math.PI)/xFreq) ,
(float) Math.Cos((ticks*2*Math.PI)/yFreq));
}
public float GetIntensity()
{
float intensity = 0;
foreach(Tuple<int, float2, int> tuple in shakeEffects)
{
intensity += (24*24*100*tuple.c)/( (tuple.b.X - Game.viewport.Location.X) * (tuple.b.X - Game.viewport.Location.X)
+ (tuple.b.Y - Game.viewport.Location.Y) * (tuple.b.Y - Game.viewport.Location.Y) );
}
{
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);
}