Move most palette-related stuff + nuke effect + Immobile trait to Mods.Common

This commit is contained in:
reaperrr
2014-11-09 04:17:02 +01:00
parent 961782cb21
commit 2043c31e05
15 changed files with 23 additions and 21 deletions

View File

@@ -0,0 +1,98 @@
#region Copyright & License Information
/*
* Copyright 2007-2014 The OpenRA Developers (see AUTHORS)
* This file is part of OpenRA, which is free software. It is made
* available to you under the terms of the GNU General Public License
* as published by the Free Software Foundation. For more information,
* see COPYING.
*/
#endregion
using System.Collections.Generic;
using System.Linq;
using OpenRA.Effects;
using OpenRA.GameRules;
using OpenRA.Graphics;
using OpenRA.Traits;
namespace OpenRA.Mods.Common.Effects
{
public class NukeLaunch : IEffect
{
readonly Player firedBy;
readonly Animation anim;
readonly string weapon;
readonly WPos ascendSource;
readonly WPos ascendTarget;
readonly WPos descendSource;
readonly WPos descendTarget;
readonly int delay;
readonly int turn;
WPos pos;
int ticks;
public NukeLaunch(Player firedBy, string weapon, WPos launchPos, WPos targetPos, WRange velocity, int delay, bool skipAscent)
{
this.firedBy = firedBy;
this.weapon = weapon;
this.delay = delay;
this.turn = delay / 2;
var offset = new WVec(WRange.Zero, WRange.Zero, velocity * turn);
ascendSource = launchPos;
ascendTarget = launchPos + offset;
descendSource = targetPos + offset;
descendTarget = targetPos;
anim = new Animation(firedBy.World, weapon);
anim.PlayRepeating("up");
pos = launchPos;
var weaponRules = firedBy.World.Map.Rules.Weapons[weapon.ToLowerInvariant()];
if (weaponRules.Report != null && weaponRules.Report.Any())
Sound.Play(weaponRules.Report.Random(firedBy.World.SharedRandom), pos);
if (skipAscent)
ticks = turn;
}
public void Tick(World world)
{
anim.Tick();
if (ticks == turn)
anim.PlayRepeating("down");
if (ticks <= turn)
pos = WPos.LerpQuadratic(ascendSource, ascendTarget, WAngle.Zero, ticks, turn);
else
pos = WPos.LerpQuadratic(descendSource, descendTarget, WAngle.Zero, ticks - turn, delay - turn);
if (ticks == delay)
Explode(world);
ticks++;
}
void Explode(World world)
{
world.AddFrameEndTask(w => w.Remove(this));
var weapon = world.Map.Rules.Weapons[this.weapon.ToLowerInvariant()];
weapon.Impact(Target.FromPos(pos), firedBy.PlayerActor, Enumerable.Empty<int>());
world.WorldActor.Trait<ScreenShaker>().AddEffect(20, pos, 5);
foreach (var a in world.ActorsWithTrait<NukePaletteEffect>())
a.Trait.Enable();
}
public IEnumerable<IRenderable> Render(WorldRenderer wr)
{
return anim.Render(pos, wr.Palette("effect"));
}
public float FractionComplete { get { return ticks * 1f / delay; } }
}
}

View File

@@ -61,6 +61,7 @@
<Compile Include="Effects\GravityBomb.cs" />
<Compile Include="Effects\LaserZap.cs" />
<Compile Include="Effects\Missile.cs" />
<Compile Include="Effects\NukeLaunch.cs" />
<Compile Include="Effects\PowerdownIndicator.cs" />
<Compile Include="Effects\RallyPoint.cs" />
<Compile Include="Effects\Smoke.cs" />
@@ -85,7 +86,11 @@
<Compile Include="Orders\DeployOrderTargeter.cs" />
<Compile Include="Orders\EnterAlliedActorTargeter.cs" />
<Compile Include="Orders\UnitOrderTargeter.cs" />
<Compile Include="PaletteFromFile.cs" />
<Compile Include="PaletteEffects\CloakPaletteEffect.cs" />
<Compile Include="PaletteEffects\LightPaletteRotator.cs" />
<Compile Include="PaletteEffects\MenuPaletteEffect.cs" />
<Compile Include="PaletteEffects\NukePaletteEffect.cs" />
<Compile Include="PaletteEffects\WaterPaletteRotation.cs" />
<Compile Include="RallyPoint.cs" />
<Compile Include="ServerTraits\ColorValidator.cs" />
<Compile Include="ServerTraits\LobbyCommands.cs" />
@@ -93,6 +98,7 @@
<Compile Include="ServerTraits\MasterServerPinger.cs" />
<Compile Include="ServerTraits\PlayerPinger.cs" />
<Compile Include="Traits\BlocksBullets.cs" />
<Compile Include="Traits\Immobile.cs" />
<Compile Include="Traits\JamsMissiles.cs" />
<Compile Include="Traits\ProvidesRadar.cs" />
<Compile Include="Traits\Power\AffectedByPowerOutage.cs" />
@@ -145,6 +151,10 @@
<Compile Include="World\CreateMPPlayers.cs" />
<Compile Include="World\MPStartLocations.cs" />
<Compile Include="World\MPStartUnits.cs" />
<Compile Include="World\PaletteFromCurrentTileset.cs" />
<Compile Include="World\PaletteFromFile.cs" />
<Compile Include="World\PaletteFromRGBA.cs" />
<Compile Include="World\PlayerPaletteFromCurrentTileset.cs" />
<Compile Include="World\PlayMusicOnMapLoad.cs" />
<Compile Include="World\RadarPings.cs" />
<Compile Include="World\ResourceClaim.cs" />

View File

@@ -0,0 +1,50 @@
#region Copyright & License Information
/*
* Copyright 2007-2014 The OpenRA Developers (see AUTHORS)
* This file is part of OpenRA, which is free software. It is made
* available to you under the terms of the GNU General Public License
* as published by the Free Software Foundation. For more information,
* see COPYING.
*/
#endregion
using System.Collections.Generic;
using System.Drawing;
using OpenRA.Graphics;
using OpenRA.Traits;
namespace OpenRA.Mods.Common
{
public class CloakPaletteEffectInfo : TraitInfo<CloakPaletteEffect> { }
public class CloakPaletteEffect : IPaletteModifier, ITick
{
float t = 0;
string paletteName = "cloak";
Color[] colors = {
Color.FromArgb(55, 205, 205, 220),
Color.FromArgb(120, 205, 205, 230),
Color.FromArgb(192, 180, 180, 255),
Color.FromArgb(178, 205, 250, 220),
};
public void AdjustPalette(IReadOnlyDictionary<string, MutablePalette> b)
{
var i = (int)t;
var p = b[paletteName];
for (var j = 0; j < colors.Length; j++)
{
var k = (i + j) % 16 + 0xb0;
p.SetColor(k, colors[j]);
}
}
public void Tick(Actor self)
{
t += 0.25f;
if (t >= 256) t = 0;
}
}
}

View File

@@ -0,0 +1,56 @@
#region Copyright & License Information
/*
* Copyright 2007-2014 The OpenRA Developers (see AUTHORS)
* This file is part of OpenRA, which is free software. It is made
* available to you under the terms of the GNU General Public License
* as published by the Free Software Foundation. For more information,
* see COPYING.
*/
#endregion
using System.Collections.Generic;
using System.Linq;
using OpenRA.Graphics;
using OpenRA.Traits;
namespace OpenRA.Mods.Common
{
[Desc("Palette effect used for blinking \"animations\" on actors.")]
class LightPaletteRotatorInfo : ITraitInfo
{
public readonly string[] ExcludePalettes = { };
public object Create(ActorInitializer init) { return new LightPaletteRotator(this); }
}
class LightPaletteRotator : ITick, IPaletteModifier
{
float t = 0;
public void Tick(Actor self)
{
t += .5f;
}
readonly LightPaletteRotatorInfo info;
public LightPaletteRotator(LightPaletteRotatorInfo info)
{
this.info = info;
}
public void AdjustPalette(IReadOnlyDictionary<string, MutablePalette> palettes)
{
foreach (var pal in palettes)
{
if (info.ExcludePalettes.Contains(pal.Key))
continue;
var rotate = (int)t % 18;
if (rotate > 9)
rotate = 18 - rotate;
pal.Value.SetColor(0x67, pal.Value.GetColor(230 + rotate));
}
}
}
}

View File

@@ -0,0 +1,100 @@
#region Copyright & License Information
/*
* Copyright 2007-2014 The OpenRA Developers (see AUTHORS)
* This file is part of OpenRA, which is free software. It is made
* available to you under the terms of the GNU General Public License
* as published by the Free Software Foundation. For more information,
* see COPYING.
*/
#endregion
using System.Collections.Generic;
using System.Drawing;
using OpenRA.Graphics;
using OpenRA.Traits;
namespace OpenRA.Mods.Common
{
[Desc("Fades the world from/to black at the start/end of the game, and can (optionally) desaturate the world")]
public class MenuPaletteEffectInfo : ITraitInfo
{
[Desc("Time (in ticks) to fade between states")]
public readonly int FadeLength = 10;
[Desc("Effect style to fade to during gameplay. Accepts values of None or Desaturated.")]
public readonly MenuPaletteEffect.EffectType Effect = MenuPaletteEffect.EffectType.None;
[Desc("Effect style to fade to when opening the in-game menu. Accepts values of None, Black or Desaturated.")]
public readonly MenuPaletteEffect.EffectType MenuEffect = MenuPaletteEffect.EffectType.None;
public object Create(ActorInitializer init) { return new MenuPaletteEffect(this); }
}
public class MenuPaletteEffect : IPaletteModifier, ITickRender, IWorldLoaded
{
public enum EffectType { None, Black, Desaturated }
public readonly MenuPaletteEffectInfo Info;
int remainingFrames;
EffectType from = EffectType.Black;
EffectType to = EffectType.Black;
public MenuPaletteEffect(MenuPaletteEffectInfo info) { Info = info; }
public void Fade(EffectType type)
{
remainingFrames = Info.FadeLength;
from = to;
to = type;
}
public void TickRender(WorldRenderer wr, Actor self)
{
if (remainingFrames > 0)
remainingFrames--;
}
static Color ColorForEffect(EffectType t, Color orig)
{
switch (t)
{
case EffectType.Black:
return Color.FromArgb(orig.A, Color.Black);
case EffectType.Desaturated:
var lum = (int)(255 * orig.GetBrightness());
return Color.FromArgb(orig.A, lum, lum, lum);
default:
case EffectType.None:
return orig;
}
}
public void AdjustPalette(IReadOnlyDictionary<string, MutablePalette> palettes)
{
if (to == EffectType.None && remainingFrames == 0)
return;
foreach (var pal in palettes.Values)
{
for (var x = 0; x < Palette.Size; x++)
{
var orig = pal.GetColor(x);
var t = ColorForEffect(to, orig);
if (remainingFrames == 0)
pal.SetColor(x, t);
else
{
var f = ColorForEffect(from, orig);
pal.SetColor(x, Exts.ColorLerp((float)remainingFrames / Info.FadeLength, t, f));
}
}
}
}
public void WorldLoaded(World w, WorldRenderer wr)
{
Fade(Info.Effect);
}
}
}

View File

@@ -0,0 +1,55 @@
#region Copyright & License Information
/*
* Copyright 2007-2014 The OpenRA Developers (see AUTHORS)
* This file is part of OpenRA, which is free software. It is made
* available to you under the terms of the GNU General Public License
* as published by the Free Software Foundation. For more information,
* see COPYING.
*/
#endregion
using System.Collections.Generic;
using System.Drawing;
using OpenRA.Graphics;
using OpenRA.Traits;
namespace OpenRA.Mods.Common
{
[Desc("Apply palette full screen rotations during atom bomb explosions. Add this to the world actor.")]
class NukePaletteEffectInfo : TraitInfo<NukePaletteEffect> { }
public class NukePaletteEffect : IPaletteModifier, ITick
{
const int nukeEffectLength = 20;
int remainingFrames;
public void Enable()
{
remainingFrames = nukeEffectLength;
}
public void Tick(Actor self)
{
if (remainingFrames > 0)
remainingFrames--;
}
public void AdjustPalette(IReadOnlyDictionary<string, MutablePalette> palettes)
{
if (remainingFrames == 0)
return;
var frac = (float)remainingFrames / nukeEffectLength;
foreach (var pal in palettes)
{
for (var x = 0; x < Palette.Size; x++)
{
var orig = pal.Value.GetColor(x);
var white = Color.FromArgb(orig.A, 255, 255, 255);
pal.Value.SetColor(x, Exts.ColorLerp(frac, orig, white));
}
}
}
}
}

View File

@@ -0,0 +1,64 @@
#region Copyright & License Information
/*
* Copyright 2007-2014 The OpenRA Developers (see AUTHORS)
* This file is part of OpenRA, which is free software. It is made
* available to you under the terms of the GNU General Public License
* as published by the Free Software Foundation. For more information,
* see COPYING.
*/
#endregion
using System.Collections.Generic;
using System.Linq;
using OpenRA.Graphics;
using OpenRA.Traits;
namespace OpenRA.Mods.Common
{
[Desc("Palette effect used for sprinkle \"animations\" on terrain tiles.")]
class WaterPaletteRotationInfo : ITraitInfo
{
public readonly string[] ExcludePalettes = { };
public object Create(ActorInitializer init) { return new WaterPaletteRotation(init.world, this); }
}
class WaterPaletteRotation : ITick, IPaletteModifier
{
float t = 0;
readonly WaterPaletteRotationInfo info;
readonly World world;
public WaterPaletteRotation(World world, WaterPaletteRotationInfo info)
{
this.world = world;
this.info = info;
}
public void Tick(Actor self) { t += .25f; }
uint[] temp = new uint[7]; /* allocating this on the fly actually hurts our profile */
public void AdjustPalette(IReadOnlyDictionary<string, MutablePalette> palettes)
{
var rotate = (int)t % 7;
if (rotate == 0)
return;
foreach (var kvp in palettes)
{
if (info.ExcludePalettes.Contains(kvp.Key))
continue;
var palette = kvp.Value;
for (var i = 0; i < 7; i++)
temp[(rotate + i) % 7] = palette[world.TileSet.WaterPaletteRotationBase + i];
for (var i = 0; i < 7; i++)
palette[world.TileSet.WaterPaletteRotationBase + i] = temp[i];
}
}
}
}

View File

@@ -0,0 +1,58 @@
#region Copyright & License Information
/*
* Copyright 2007-2014 The OpenRA Developers (see AUTHORS)
* This file is part of OpenRA, which is free software. It is made
* available to you under the terms of the GNU General Public License
* as published by the Free Software Foundation. For more information,
* see COPYING.
*/
#endregion
using System.Collections.Generic;
using OpenRA.Primitives;
using OpenRA.Traits;
namespace OpenRA.Mods.Common
{
class ImmobileInfo : ITraitInfo, IOccupySpaceInfo
{
public readonly bool OccupiesSpace = true;
public object Create(ActorInitializer init) { return new Immobile(init, this); }
}
class Immobile : IOccupySpace, ISync, INotifyAddedToWorld, INotifyRemovedFromWorld
{
[Sync] readonly CPos location;
[Sync] readonly WPos position;
readonly IEnumerable<Pair<CPos, SubCell>> occupied;
public Immobile(ActorInitializer init, ImmobileInfo info)
{
location = init.Get<LocationInit, CPos>();
position = init.world.Map.CenterOfCell(location);
if (info.OccupiesSpace)
occupied = new [] { Pair.New(TopLeft, SubCell.FullCell) };
else
occupied = new Pair<CPos, SubCell>[0];
}
public CPos TopLeft { get { return location; } }
public WPos CenterPosition { get { return position; } }
public IEnumerable<Pair<CPos, SubCell>> OccupiedCells() { return occupied; }
public void AddedToWorld(Actor self)
{
self.World.ActorMap.AddInfluence(self, this);
self.World.ActorMap.AddPosition(self, this);
self.World.ScreenMap.Add(self);
}
public void RemovedFromWorld(Actor self)
{
self.World.ActorMap.RemoveInfluence(self, this);
self.World.ActorMap.RemovePosition(self, this);
self.World.ScreenMap.Remove(self);
}
}
}

View File

@@ -0,0 +1,45 @@
#region Copyright & License Information
/*
* Copyright 2007-2014 The OpenRA Developers (see AUTHORS)
* This file is part of OpenRA, which is free software. It is made
* available to you under the terms of the GNU General Public License
* as published by the Free Software Foundation. For more information,
* see COPYING.
*/
#endregion
using OpenRA.FileSystem;
using OpenRA.Graphics;
using OpenRA.Traits;
namespace OpenRA.Mods.Common
{
[Desc("Loads the palette specified in the tileset definition")]
class PaletteFromCurrentTilesetInfo : ITraitInfo
{
[Desc("internal palette name")]
public readonly string Name = null;
[Desc("Map listed indices to shadow. Ignores previous color.")]
public readonly int[] ShadowIndex = { };
public readonly bool AllowModifiers = true;
public object Create(ActorInitializer init) { return new PaletteFromCurrentTileset(init.world, this); }
}
class PaletteFromCurrentTileset : ILoadsPalettes
{
readonly World world;
readonly PaletteFromCurrentTilesetInfo info;
public PaletteFromCurrentTileset(World world, PaletteFromCurrentTilesetInfo info)
{
this.world = world;
this.info = info;
}
public void LoadPalettes(WorldRenderer wr)
{
wr.AddPalette(info.Name, new ImmutablePalette(GlobalFileSystem.Open(world.TileSet.Palette), info.ShadowIndex), info.AllowModifiers);
}
}
}

View File

@@ -0,0 +1,57 @@
#region Copyright & License Information
/*
* Copyright 2007-2014 The OpenRA Developers (see AUTHORS)
* This file is part of OpenRA, which is free software. It is made
* available to you under the terms of the GNU General Public License
* as published by the Free Software Foundation. For more information,
* see COPYING.
*/
#endregion
using System.Linq;
using OpenRA.Graphics;
using OpenRA.Traits;
namespace OpenRA.Mods.Common
{
[Desc("Creates a single color palette without any base palette file.")]
class PaletteFromRGBAInfo : ITraitInfo
{
[Desc("internal palette name")]
public readonly string Name = null;
[Desc("If defined, load the palette only for this tileset.")]
public readonly string Tileset = null;
[Desc("red color component")]
public readonly int R = 0;
[Desc("green color component")]
public readonly int G = 0;
[Desc("blue color component")]
public readonly int B = 0;
[Desc("alpha channel (transparency)")]
public readonly int A = 255;
public readonly bool AllowModifiers = true;
public object Create(ActorInitializer init) { return new PaletteFromRGBA(init.world, this); }
}
class PaletteFromRGBA : ILoadsPalettes
{
readonly World world;
readonly PaletteFromRGBAInfo info;
public PaletteFromRGBA(World world, PaletteFromRGBAInfo info)
{
this.world = world;
this.info = info;
}
public void LoadPalettes(WorldRenderer wr)
{
// Enable palette only for a specific tileset
if (info.Tileset != null && info.Tileset.ToLowerInvariant() != world.Map.Tileset.ToLowerInvariant())
return;
var c = (uint)((info.A << 24) | (info.R << 16) | (info.G << 8) | info.B);
wr.AddPalette(info.Name, new ImmutablePalette(Enumerable.Range(0, Palette.Size).Select(i => (i == 0) ? 0 : c)), info.AllowModifiers);
}
}
}

View File

@@ -0,0 +1,46 @@
#region Copyright & License Information
/*
* Copyright 2007-2014 The OpenRA Developers (see AUTHORS)
* This file is part of OpenRA, which is free software. It is made
* available to you under the terms of the GNU General Public License
* as published by the Free Software Foundation. For more information,
* see COPYING.
*/
#endregion
using OpenRA.FileSystem;
using OpenRA.Graphics;
using OpenRA.Traits;
namespace OpenRA.Mods.Common
{
class PlayerPaletteFromCurrentTilesetInfo : ITraitInfo
{
[Desc("internal palette name")]
public readonly string Name = null;
[Desc("Map listed indices to shadow.")]
public readonly int[] ShadowIndex = { };
[Desc("Apply palette rotatotors or not.")]
public readonly bool AllowModifiers = true;
public object Create(ActorInitializer init) { return new PlayerPaletteFromCurrentTileset(init.world, this); }
}
class PlayerPaletteFromCurrentTileset : ILoadsPalettes
{
readonly World world;
readonly PlayerPaletteFromCurrentTilesetInfo info;
public PlayerPaletteFromCurrentTileset(World world, PlayerPaletteFromCurrentTilesetInfo info)
{
this.world = world;
this.info = info;
}
public void LoadPalettes(WorldRenderer wr)
{
var filename = world.TileSet.PlayerPalette ?? world.TileSet.Palette;
wr.AddPalette(info.Name, new ImmutablePalette(GlobalFileSystem.Open(filename), info.ShadowIndex), info.AllowModifiers);
}
}
}