Convert ScreenShaker to world coords.

This commit is contained in:
Paul Chote
2013-07-20 14:59:01 +12:00
parent 0c35e49239
commit ddd1314613
4 changed files with 20 additions and 17 deletions

View File

@@ -14,47 +14,50 @@ using System.Linq;
namespace OpenRA.Traits namespace OpenRA.Traits
{ {
class ScreenShakerInfo : TraitInfo<ScreenShaker> {} public class ScreenShakerInfo : TraitInfo<ScreenShaker> { }
public class ScreenShaker : ITick public class ScreenShaker : ITick
{ {
int ticks = 0;
List<ShakeEffect> shakeEffects = new List<ShakeEffect>(); List<ShakeEffect> shakeEffects = new List<ShakeEffect>();
int ticks = 0;
public void Tick (Actor self) public void Tick(Actor self)
{ {
if(shakeEffects.Any()) if (shakeEffects.Any())
{ {
Game.viewport.Scroll(GetScrollOffset(), true); Game.viewport.Scroll(GetScrollOffset(), true);
shakeEffects.RemoveAll(t => t.ExpiryTime == ticks); shakeEffects.RemoveAll(t => t.ExpiryTime == ticks);
} }
ticks++; ticks++;
} }
public void AddEffect(int time, float2 position, int intensity) public void AddEffect(int time, WPos position, int intensity)
{ {
shakeEffects.Add(new ShakeEffect { ExpiryTime = ticks + time, Position = position, Intensity = intensity }); shakeEffects.Add(new ShakeEffect { ExpiryTime = ticks + time, Position = position, Intensity = intensity });
} }
float2 GetScrollOffset() float2 GetScrollOffset()
{ {
int xFreq = 4;
int yFreq = 5;
return GetIntensity() * new float2( return GetIntensity() * new float2(
(float) Math.Sin((ticks*2*Math.PI)/xFreq) , (float)Math.Sin((ticks * 2 * Math.PI) / 4),
(float) Math.Cos((ticks*2*Math.PI)/yFreq)); (float)Math.Cos((ticks * 2 * Math.PI) / 5));
} }
float GetIntensity() float GetIntensity()
{ {
var cp = Game.viewport.CenterLocation; var cp = ((PPos)Game.viewport.CenterLocation.ToInt2()).ToWPos(0);
var intensity = Game.CellSize * Game.CellSize * 100 * shakeEffects.Sum( var intensity = 100 * 1024 * 1024 * shakeEffects.Sum(
e => e.Intensity / (e.Position - cp).LengthSquared); e => (float)e.Intensity / (e.Position - cp).LengthSquared);
return Math.Min(intensity, 10); return Math.Min(intensity, 10);
} }
} }
class ShakeEffect { public int ExpiryTime; public float2 Position; public int Intensity; } struct ShakeEffect
{
public int ExpiryTime;
public WPos Position;
public int Intensity;
}
} }

View File

@@ -29,7 +29,7 @@ namespace OpenRA.Mods.RA.Buildings
public void Killed(Actor self, AttackInfo e) public void Killed(Actor self, AttackInfo e)
{ {
self.World.WorldActor.Trait<ScreenShaker>().AddEffect(Info.Intensity, self.CenterLocation.ToFloat2(), 1); self.World.WorldActor.Trait<ScreenShaker>().AddEffect(Info.Intensity, self.CenterPosition, 1);
} }
} }
} }

View File

@@ -67,7 +67,7 @@ namespace OpenRA.Mods.RA.Effects
{ {
world.AddFrameEndTask(w => w.Remove(this)); world.AddFrameEndTask(w => w.Remove(this));
Combat.DoExplosion(firedBy.PlayerActor, weapon, pos); Combat.DoExplosion(firedBy.PlayerActor, weapon, pos);
world.WorldActor.Trait<ScreenShaker>().AddEffect(20, PPos.FromWPos(pos).ToFloat2(), 5); world.WorldActor.Trait<ScreenShaker>().AddEffect(20, pos, 5);
foreach (var a in world.ActorsWithTrait<NukePaletteEffect>()) foreach (var a in world.ActorsWithTrait<NukePaletteEffect>())
a.Trait.Enable(); a.Trait.Enable();

View File

@@ -129,7 +129,7 @@ namespace OpenRA.Mods.RA.Missions
.OrderBy(a => (startJeep.CenterPosition - a.CenterPosition).LengthSquared) .OrderBy(a => (startJeep.CenterPosition - a.CenterPosition).LengthSquared)
.First(); .First();
Combat.DoExplosion(bridge, "Demolish", bridge.CenterPosition); Combat.DoExplosion(bridge, "Demolish", bridge.CenterPosition);
world.WorldActor.Trait<ScreenShaker>().AddEffect(15, bridge.CenterLocation.ToFloat2(), 6); world.WorldActor.Trait<ScreenShaker>().AddEffect(15, bridge.CenterPosition, 6);
bridge.Kill(bridge); bridge.Kill(bridge);
})); }));
} }