Added a new trait : Scale

This commit is contained in:
geckosoft
2010-10-28 23:41:20 +13:00
committed by Chris Forbes
parent e32e060d37
commit 9489196911
13 changed files with 99 additions and 27 deletions

View File

@@ -62,7 +62,8 @@ namespace OpenRA
Constrain(Y, min.Y, max.Y)); Constrain(Y, min.Y, max.Y));
} }
public static float2 operator *(float a, float2 b) { return new float2(a * b.X, a * b.Y); } public static float2 operator *(float a, float2 b) { return new float2(a * b.X, a * b.Y); }
public static float2 operator *(float2 b, float a) { return new float2(a * b.X, a * b.Y); }
public static float2 operator *( float2 a, float2 b ) { return new float2( a.X * b.X, a.Y * b.Y ); } public static float2 operator *( float2 a, float2 b ) { return new float2( a.X * b.X, a.Y * b.Y ); }
public static float2 operator /( float2 a, float2 b ) { return new float2( a.X / b.X, a.Y / b.Y ); } public static float2 operator /( float2 a, float2 b ) { return new float2( a.X / b.X, a.Y / b.Y ); }

View File

@@ -61,8 +61,8 @@ namespace OpenRA
// auto size from render // auto size from render
var firstSprite = TraitsImplementing<IRender>().SelectMany(x => x.Render(this)).FirstOrDefault(); var firstSprite = TraitsImplementing<IRender>().SelectMany(x => x.Render(this)).FirstOrDefault();
if (firstSprite.Sprite == null) return float2.Zero; if (firstSprite.Sprite == null) return float2.Zero;
return firstSprite.Sprite.size; return firstSprite.Sprite.size * firstSprite.Scale;
}); });
} }
@@ -110,7 +110,13 @@ namespace OpenRA
{ {
var si = Info.Traits.GetOrDefault<SelectableInfo>(); var si = Info.Traits.GetOrDefault<SelectableInfo>();
var size = Size.Value; var size = Size.Value;
/* apply scaling */
var scale = this.TraitOrDefault<Scale>();
if (scale != null && scale.Info.Value != 1)
size = size*scale.Info.Value;
var loc = CenterLocation - 0.5f * size; var loc = CenterLocation - 0.5f * size;
if (si != null && si.Bounds != null && si.Bounds.Length > 2) if (si != null && si.Bounds != null && si.Bounds.Length > 2)

View File

@@ -60,6 +60,11 @@ namespace OpenRA.Graphics
Game.Renderer.SpriteRenderer.DrawSprite( this, location, paletteIndex, this.size ); Game.Renderer.SpriteRenderer.DrawSprite( this, location, paletteIndex, this.size );
} }
public void DrawAt(float2 location, int paletteIndex, float scale)
{
Game.Renderer.SpriteRenderer.DrawSprite(this, location, paletteIndex, this.size * scale);
}
public void DrawAt( float2 location, int paletteIndex, float2 size ) public void DrawAt( float2 location, int paletteIndex, float2 size )
{ {
Game.Renderer.SpriteRenderer.DrawSprite( this, location, paletteIndex, size ); Game.Renderer.SpriteRenderer.DrawSprite( this, location, paletteIndex, size );

View File

@@ -99,10 +99,10 @@ namespace OpenRA.Graphics
if (world.OrderGenerator != null) if (world.OrderGenerator != null)
world.OrderGenerator.RenderBeforeWorld(this, world); world.OrderGenerator.RenderBeforeWorld(this, world);
foreach( var image in SpritesToRender() ) foreach (var image in SpritesToRender() )
image.Sprite.DrawAt( image.Pos, this.GetPaletteIndex( image.Palette ) ); image.Sprite.DrawAt(image.Pos, this.GetPaletteIndex(image.Palette), image.Scale);
uiOverlay.Draw(this, world); uiOverlay.Draw(this, world);
// added for contrails // added for contrails
foreach (var a in world.Actors) foreach (var a in world.Actors)

View File

@@ -222,6 +222,7 @@
<Compile Include="Server\IServerExtension.cs" /> <Compile Include="Server\IServerExtension.cs" />
<Compile Include="Server\NullServerExtension.cs" /> <Compile Include="Server\NullServerExtension.cs" />
<Compile Include="Traits\ValidateOrder.cs" /> <Compile Include="Traits\ValidateOrder.cs" />
<Compile Include="Traits\Scale.cs" />
<Compile Include="TraitDictionary.cs" /> <Compile Include="TraitDictionary.cs" />
<Compile Include="Traits\Activities\CancelableActivity.cs" /> <Compile Include="Traits\Activities\CancelableActivity.cs" />
<Compile Include="Traits\SharesCell.cs" /> <Compile Include="Traits\SharesCell.cs" />

View File

@@ -18,7 +18,7 @@ using OpenRA.Widgets;
namespace OpenRA.Server namespace OpenRA.Server
{ {
static class MasterServerQuery public static class MasterServerQuery
{ {
public static event Action<GameServer[]> OnComplete = _ => { }; public static event Action<GameServer[]> OnComplete = _ => { };
public static event Action<string> OnVersion = _ => { }; public static event Action<string> OnVersion = _ => { };
@@ -114,7 +114,7 @@ namespace OpenRA.Server
} }
} }
class GameServer public class GameServer
{ {
public readonly int Id = 0; public readonly int Id = 0;
public readonly string Name = null; public readonly string Name = null;

View File

@@ -0,0 +1,42 @@
using System.Collections.Generic;
namespace OpenRA.Traits
{
public class ScaleInfo : ITraitInfo
{
public readonly float Value = 1f; /* default */
public ScaleInfo() { } /* only because we have other ctors */
public object Create(ActorInitializer init) { return new Scale(init.self, this); }
}
public class Scale : IRenderModifier
{
Actor self;
public ScaleInfo Info { get; protected set; }
public Scale(Actor self, ScaleInfo info)
{
this.Info = info;
this.self = self;
}
public IEnumerable<Renderable> ModifyRender(Actor self, IEnumerable<Renderable> r)
{
var r2 = new List<Renderable>(r);
var r3 = new List<Renderable>();
for (int i = 0; i < r2.Count;i++)
{
var renderable = r2[i];
renderable.Scale = Info.Value;
r3.Add(renderable);
// yield return renderable;
}
return r3;
}
}
}

View File

@@ -145,22 +145,33 @@ namespace OpenRA.Traits
public readonly string Palette; public readonly string Palette;
public readonly int Z; public readonly int Z;
public readonly int ZOffset; public readonly int ZOffset;
public float Scale;
public Renderable(Sprite sprite, float2 pos, string palette, int z, int zOffset) public Renderable(Sprite sprite, float2 pos, string palette, int z, int zOffset)
{ {
Sprite = sprite; Sprite = sprite;
Pos = pos; Pos = pos;
Palette = palette; Palette = palette;
Z = z; Z = z;
ZOffset = zOffset; ZOffset = zOffset;
} Scale = 1f; /* default */
}
public Renderable(Sprite sprite, float2 pos, string palette, int z, int zOffset, float scale)
{
Sprite = sprite;
Pos = pos;
Palette = palette;
Z = z;
ZOffset = zOffset;
Scale = scale; /* default */
}
public Renderable(Sprite sprite, float2 pos, string palette, int z) public Renderable(Sprite sprite, float2 pos, string palette, int z)
: this(sprite, pos, palette, z, 0) { } : this(sprite, pos, palette, z, 0) { }
public Renderable WithPalette(string newPalette) { return new Renderable(Sprite, Pos, newPalette, Z, ZOffset); } public Renderable WithPalette(string newPalette) { return new Renderable(Sprite, Pos, newPalette, Z, ZOffset, Scale); }
public Renderable WithZOffset(int newOffset) { return new Renderable(Sprite, Pos, Palette, Z, newOffset); } public Renderable WithZOffset(int newOffset) { return new Renderable(Sprite, Pos, Palette, Z, newOffset, Scale); }
public Renderable WithPos(float2 newPos) { return new Renderable(Sprite, newPos, Palette, Z, ZOffset); } public Renderable WithPos(float2 newPos) { return new Renderable(Sprite, newPos, Palette, Z, ZOffset, Scale); }
} }
public interface ITraitInfo { object Create(ActorInitializer init); } public interface ITraitInfo { object Create(ActorInitializer init); }

View File

@@ -99,7 +99,13 @@ namespace OpenRA.Traits
public static Renderable Centered(Actor self, Sprite s, float2 location) public static Renderable Centered(Actor self, Sprite s, float2 location)
{ {
var pal = self.Owner == null ? "player0" : self.Owner.Palette; var pal = self.Owner == null ? "player0" : self.Owner.Palette;
var loc = location - 0.5f * s.size; var scale = self.TraitOrDefault<Scale>();
var scaleModifier = 1f;
if (scale != null)
scaleModifier = scale.Info.Value;
var loc = location - 0.5f * s.size * scaleModifier;
return new Renderable(s, loc.Round(), pal, (int)self.CenterLocation.Y); return new Renderable(s, loc.Round(), pal, (int)self.CenterLocation.Y);
} }

View File

@@ -6,7 +6,7 @@ using OpenRA.Traits;
namespace OpenRA.Mods.RA.Orders namespace OpenRA.Mods.RA.Orders
{ {
class DeployOrderTargeter : IOrderTargeter public class DeployOrderTargeter : IOrderTargeter
{ {
readonly Func<bool> useDeployCursor; readonly Func<bool> useDeployCursor;

View File

@@ -12,7 +12,7 @@ using System;
namespace OpenRA.Mods.RA.Orders namespace OpenRA.Mods.RA.Orders
{ {
class EnterOrderTargeter<T> : UnitTraitOrderTargeter<T> public class EnterOrderTargeter<T> : UnitTraitOrderTargeter<T>
{ {
readonly Func<Actor, bool> canTarget; readonly Func<Actor, bool> canTarget;
readonly Func<Actor, bool> useEnterCursor; readonly Func<Actor, bool> useEnterCursor;

View File

@@ -14,7 +14,7 @@ using OpenRA.Traits;
namespace OpenRA.Mods.RA.Orders namespace OpenRA.Mods.RA.Orders
{ {
class UnitOrderTargeter : IOrderTargeter public class UnitOrderTargeter : IOrderTargeter
{ {
readonly string cursor; readonly string cursor;
readonly bool targetEnemyUnits, targetAllyUnits; readonly bool targetEnemyUnits, targetAllyUnits;
@@ -52,7 +52,7 @@ namespace OpenRA.Mods.RA.Orders
} }
} }
class UnitTraitOrderTargeter<T> : UnitOrderTargeter public class UnitTraitOrderTargeter<T> : UnitOrderTargeter
{ {
public UnitTraitOrderTargeter( string order, int priority, string cursor, bool targetEnemyUnits, bool targetAllyUnits ) public UnitTraitOrderTargeter( string order, int priority, string cursor, bool targetEnemyUnits, bool targetAllyUnits )
: base( order, priority, cursor, targetEnemyUnits, targetAllyUnits ) : base( order, priority, cursor, targetEnemyUnits, targetAllyUnits )

View File

@@ -28,7 +28,7 @@ namespace OpenRA.Mods.RA
public object Create( ActorInitializer init ) { return new Passenger( init.self ); } public object Create( ActorInitializer init ) { return new Passenger( init.self ); }
} }
class Passenger : IIssueOrder, IResolveOrder, IOrderVoice public class Passenger : IIssueOrder, IResolveOrder, IOrderVoice
{ {
readonly Actor self; readonly Actor self;
public Passenger( Actor self ) { this.self = self; } public Passenger( Actor self ) { this.self = self; }