ConstructionYard.cs!

git-svn-id: svn://svn.ijw.co.nz/svn/OpenRa@1286 993157c7-ee19-0410-b2c4-bb4e9862e678
This commit is contained in:
chrisf
2007-07-16 10:29:03 +00:00
parent 333a20d29e
commit 58be529e32
13 changed files with 146 additions and 205 deletions

View File

@@ -13,6 +13,6 @@ namespace OpenRa.Game
public float2 renderLocation; public float2 renderLocation;
public int palette; public int palette;
public abstract Sprite[] CurrentImages { get; } public abstract Sprite[] CurrentImages { get; }
public abstract void Tick( World world, double t ); public virtual void Tick(World world, double t) { }
} }
} }

View File

@@ -1,75 +0,0 @@
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
namespace OpenRa.Game
{
public static class Clock
{
[DllImport("kernel32.dll")]
static extern bool QueryPerformanceCounter(out long value);
[DllImport("kernel32.dll")]
static extern bool QueryPerformanceFrequency(out long frequency);
static int frameCount = 0;
static long frequency;
static long lastTime;
static double frameTime = 0;
static double totalTime = 0;
const int FramerateUpdateFrequency = 10;
static int lastFrameRate = 0;
static int lastFrameCount = 0;
static double nextFrameRateUpdateTime = 0;
static Clock()
{
QueryPerformanceFrequency(out frequency);
QueryPerformanceCounter(out lastTime);
}
public static void Reset()
{
totalTime = 0;
}
public static void StartFrame()
{
long time;
QueryPerformanceCounter(out time);
frameTime = (double)(time - lastTime) / (double)frequency;
totalTime += frameTime;
lastTime = time;
frameCount++;
if (totalTime > nextFrameRateUpdateTime)
{
nextFrameRateUpdateTime += (1.0 / FramerateUpdateFrequency);
const int OldFramerateWeight = 20;
const int NewFramerateWeight = 1;
int newFrameRate = (frameCount - lastFrameCount) * FramerateUpdateFrequency;
lastFrameRate = (lastFrameRate * OldFramerateWeight + NewFramerateWeight * newFrameRate)
/ (OldFramerateWeight + NewFramerateWeight);
lastFrameCount = frameCount;
}
}
public static double Time
{
get { return totalTime; }
}
public static int FrameRate
{
get { return lastFrameRate; }
}
public static double FrameTime
{
get { return frameTime; }
}
}
}

View File

@@ -0,0 +1,38 @@
using System;
using System.Collections.Generic;
using System.Text;
using BluntDirectX.Direct3D;
namespace OpenRa.Game
{
class ConstructionYard : Actor
{
static Range<int> normalSequence = UnitSheetBuilder.GetUnit("fact");
static Range<int> makeSequence = UnitSheetBuilder.GetUnit("factmake");
Range<int> sequence = makeSequence;
int frame = -1;
public ConstructionYard(float2 location, int palette)
{
this.renderLocation = location;
this.palette = palette;
}
public override Sprite[] CurrentImages
{
get
{
if ((sequence.Start == makeSequence.Start) && ++frame >= sequence.End - sequence.Start)
{
frame = 0;
sequence = normalSequence;
}
return new Sprite[] { UnitSheetBuilder.sprites[sequence.Start + frame] };
}
}
public override void Tick(World world, double t) { }
}
}

View File

@@ -60,17 +60,30 @@ namespace OpenRa.Game
foreach (TreeReference treeReference in map.Trees) foreach (TreeReference treeReference in map.Trees)
world.Add(new Tree(treeReference, treeCache, map)); world.Add(new Tree(treeReference, treeCache, map));
PrecacheStructure("proc");
PrecacheStructure("fact");
PrecacheUnit("mcv");
world.Add(new Mcv(new int2(5, 5), 3)); world.Add(new Mcv(new int2(5, 5), 3));
world.Add(new Mcv(new int2(7, 5), 2)); world.Add(new Mcv(new int2(7, 5), 2));
Mcv mcv = new Mcv( new int2( 9, 5 ), 1 ); Mcv mcv = new Mcv( new int2( 9, 5 ), 1 );
myUnit = mcv; myUnit = mcv;
world.Add( mcv ); world.Add( mcv );
world.Add(new Refinery(24 * new float2(5, 7), 1));
sidebar = new Sidebar(Race.Soviet, renderer, viewport); sidebar = new Sidebar(Race.Soviet, renderer, viewport);
} }
void PrecacheStructure(string name)
{
UnitSheetBuilder.GetUnit(name);
UnitSheetBuilder.GetUnit(name + "make");
}
void PrecacheUnit(string name)
{
UnitSheetBuilder.GetUnit(name);
}
internal void Run() internal void Run()
{ {
while (Created && Visible) while (Created && Visible)

View File

@@ -9,7 +9,8 @@ namespace OpenRa.Game
{ {
class Mcv : Actor, ISelectable class Mcv : Actor, ISelectable
{ {
static Range<int>? mcvRange = null; static Range<int> mcvRange = UnitSheetBuilder.GetUnit("mcv");
int facing = 0; int facing = 0;
int2 fromCell, toCell; int2 fromCell, toCell;
int moveFraction, moveFractionTotal; int moveFraction, moveFractionTotal;
@@ -18,28 +19,16 @@ namespace OpenRa.Game
TickFunc currentOrder = null; TickFunc currentOrder = null;
TickFunc nextOrder = null; TickFunc nextOrder = null;
public Mcv( int2 cell, int palette ) public Mcv(int2 cell, int palette)
{ {
fromCell = toCell = cell; fromCell = toCell = cell;
float2 location = ( cell * 24 ).ToFloat2(); // HACK: display the mcv centered in it's cell;
this.renderLocation = location - new float2( 12, 12 ); // HACK: display the mcv centered in it's cell renderLocation = (cell * 24).ToFloat2() - new float2(12, 12);
this.palette = palette; this.palette = palette;
if (mcvRange == null)
mcvRange = UnitSheetBuilder.AddUnit("mcv");
} }
static float2[] fvecs; static float2[] fvecs = Util.MakeArray<float2>(32,
delegate(int i) { return -float2.FromAngle(i / 16.0f * (float)Math.PI); });
static Mcv()
{
fvecs = new float2[32];
for (int i = 0; i < 32; i++)
{
float angle = i / 16.0f * (float)Math.PI;
fvecs[i] = new float2(-(float)Math.Sin(angle), -(float)Math.Cos(angle));
}
}
int GetFacing(float2 d) int GetFacing(float2 d)
{ {
@@ -49,7 +38,7 @@ namespace OpenRa.Game
int highest = -1; int highest = -1;
float highestDot = -1.0f; float highestDot = -1.0f;
for (int i = 0; i < 32; i++) for (int i = 0; i < fvecs.Length; i++)
{ {
float dot = float2.Dot(fvecs[i], d); float dot = float2.Dot(fvecs[i], d);
if (dot > highestDot) if (dot > highestDot)
@@ -64,10 +53,7 @@ namespace OpenRa.Game
public override Sprite[] CurrentImages public override Sprite[] CurrentImages
{ {
get get { return new Sprite[] { UnitSheetBuilder.sprites[facing + mcvRange.Start] }; }
{
return new Sprite[] { UnitSheetBuilder.sprites[facing + mcvRange.Value.Start] };
}
} }
const int Speed = 6; const int Speed = 6;
@@ -95,13 +81,7 @@ namespace OpenRa.Game
int desiredFacing = GetFacing( ( toCell - fromCell ).ToFloat2() ); int desiredFacing = GetFacing( ( toCell - fromCell ).ToFloat2() );
if( facing != desiredFacing ) if( facing != desiredFacing )
{ Turn(desiredFacing);
int df = ( desiredFacing - facing + 32 ) % 32;
if( df > 16 )
facing = ( facing + 31 ) % 32;
else
facing = ( facing + 1 ) % 32;
}
else else
{ {
moveFraction += speed; moveFraction += speed;
@@ -112,13 +92,11 @@ namespace OpenRa.Game
fromCell = toCell; fromCell = toCell;
if( toCell == destination ) if( toCell == destination )
{
currentOrder = null; currentOrder = null;
}
else else
{ {
int2 dir = destination - fromCell; int2 dir = destination - fromCell;
toCell = fromCell + new int2( Math.Sign( dir.X ), Math.Sign( dir.Y ) ); toCell = fromCell + dir.Sign();
moveFractionTotal = ( dir.X != 0 && dir.Y != 0 ) ? 250 : 200; moveFractionTotal = ( dir.X != 0 && dir.Y != 0 ) ? 250 : 200;
} }
} }
@@ -126,39 +104,36 @@ namespace OpenRa.Game
float2 location; float2 location;
if( moveFraction > 0 ) if( moveFraction > 0 )
{ location = 24 * float2.Lerp(fromCell.ToFloat2(), toCell.ToFloat2(),
float frac = (float)moveFraction / moveFractionTotal; (float)moveFraction / moveFractionTotal);
location = 24 * ( ( 1 - frac ) * fromCell.ToFloat2() + frac * toCell.ToFloat2() );
}
else else
location = 24 * fromCell.ToFloat2(); location = 24 * fromCell.ToFloat2();
renderLocation = location - new float2( 12, 12 ); // HACK: center mcv in it's cell renderLocation = location - new float2( 12, 12 ); // HACK: center mcv in it's cell
renderLocation.X = (float)Math.Round( renderLocation.X ); renderLocation = renderLocation.Round();
renderLocation.Y = (float)Math.Round( renderLocation.Y );
}; };
} }
void Turn(int desiredFacing)
{
int df = (desiredFacing - facing + 32) % 32;
facing = (facing + (df > 16 ? 31 : 1)) % 32;
}
public void AcceptDeployOrder() public void AcceptDeployOrder()
{ {
nextOrder = delegate( World world, double t ) nextOrder = delegate( World world, double t )
{ {
int desiredFacing = 12; int desiredFacing = 12;
if( facing != desiredFacing ) if (facing != desiredFacing)
{ Turn(desiredFacing);
int df = ( desiredFacing - facing + 32 ) % 32;
if( df > 16 )
facing = ( facing + 31 ) % 32;
else
facing = ( facing + 1 ) % 32;
}
else else
{ {
world.AddFrameEndTask( delegate world.AddFrameEndTask(delegate
{ {
world.Add( new Refinery( ( fromCell * 24 - new int2( 24, 24 ) ).ToFloat2(), palette ) ); world.Add(new ConstructionYard((fromCell * 24 - new int2(24, 24)).ToFloat2(), palette));
} ); });
currentOrder = null; currentOrder = null;
} }
}; };

View File

@@ -41,7 +41,7 @@
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Compile Include="Actor.cs" /> <Compile Include="Actor.cs" />
<Compile Include="Clock.cs" /> <Compile Include="ConstructionYard.cs" />
<Compile Include="float2.cs" /> <Compile Include="float2.cs" />
<Compile Include="int2.cs" /> <Compile Include="int2.cs" />
<Compile Include="ISelectable.cs" /> <Compile Include="ISelectable.cs" />

View File

@@ -9,13 +9,10 @@ namespace OpenRa.Game
{ {
class Refinery : Actor class Refinery : Actor
{ {
static Range<int>? refineryRange = null; static Range<int> sequence = UnitSheetBuilder.GetUnit("proc");
public Refinery(float2 location, int palette) public Refinery(float2 location, int palette)
{ {
if (refineryRange == null)
refineryRange = UnitSheetBuilder.AddUnit("proc");
this.renderLocation = location; this.renderLocation = location;
this.palette = palette; this.palette = palette;
} }
@@ -26,10 +23,8 @@ namespace OpenRa.Game
{ {
get get
{ {
return new Sprite[] { UnitSheetBuilder.sprites[refineryRange.Value.Start + GetFrame()] }; return new Sprite[] { UnitSheetBuilder.sprites[sequence.Start + GetFrame()] };
} }
} }
public override void Tick( World world, double t ) { }
} }
} }

View File

@@ -22,15 +22,9 @@ namespace OpenRa.Game
} }
} }
public bool Contains(string key) public bool Contains(string key) { return settings.ContainsKey(key); }
{
return settings.ContainsKey(key);
}
public string GetValue(string key, string defaultValue) public string GetValue(string key, string defaultValue) { return Contains(key) ? settings[key] : defaultValue; }
{
return Contains(key) ? settings[key] : defaultValue;
}
public int GetValue(string key, int defaultValue) public int GetValue(string key, int defaultValue)
{ {

View File

@@ -15,11 +15,7 @@ namespace OpenRa.Game
} }
Sprite[] currentImages; Sprite[] currentImages;
public override Sprite[] CurrentImages
{
get { return currentImages; }
}
public override void Tick( World world, double t ) { } public override Sprite[] CurrentImages { get { return currentImages; } }
} }
} }

View File

@@ -14,6 +14,8 @@ namespace OpenRa.Game
public static readonly List<Sprite> sprites = new List<Sprite>(); public static readonly List<Sprite> sprites = new List<Sprite>();
static Dictionary<string, Range<int>> sequences = new Dictionary<string, Range<int>>();
static ShpReader Load(string filename) static ShpReader Load(string filename)
{ {
foreach( Package p in new Package[] { unitsPackage, otherUnitsPackage } ) foreach( Package p in new Package[] { unitsPackage, otherUnitsPackage } )
@@ -23,14 +25,25 @@ namespace OpenRa.Game
throw new NotImplementedException(); throw new NotImplementedException();
} }
public static Range<int> AddUnit( string name ) public static Range<int> GetUnit(string name)
{
Range<int> result;
if (sequences.TryGetValue(name, out result))
return result;
return AddUnit(name);
}
static Range<int> AddUnit( string name )
{ {
int low = sprites.Count; int low = sprites.Count;
ShpReader reader = Load(name + ".shp"); ShpReader reader = Load(name + ".shp");
foreach (ImageHeader h in reader) foreach (ImageHeader h in reader)
sprites.Add(SheetBuilder.Add(h.Image, reader.Size)); sprites.Add(SheetBuilder.Add(h.Image, reader.Size));
return new Range<int>(low, sprites.Count - 1); Range<int> sequence = new Range<int>(low, sprites.Count - 1);
sequences.Add(name, sequence);
return sequence;
} }
} }
} }

View File

@@ -35,7 +35,7 @@ namespace OpenRa.Game
public static Vertex MakeVertex(float2 o, float2 uv, Sprite r, int palette) public static Vertex MakeVertex(float2 o, float2 uv, Sprite r, int palette)
{ {
return new Vertex( return new Vertex(
Lerp( o, o + new float2(r.bounds.Size), uv ), float2.Lerp( o, o + new float2(r.bounds.Size), uv ),
r.MapTextureCoords(uv), r.MapTextureCoords(uv),
EncodeVertexAttributes(r.channel, palette)); EncodeVertexAttributes(r.channel, palette));
} }
@@ -45,11 +45,13 @@ namespace OpenRa.Game
return (1 - t) * a + t * b; return (1 - t) * a + t * b;
} }
static float2 Lerp(float2 a, float2 b, float2 t) public static T[] MakeArray<T>(int count, Converter<int, T> f)
{ {
return new float2( T[] result = new T[count];
Lerp(a.X, b.X, t.X), for (int i = 0; i < count; i++)
Lerp(a.Y, b.Y, t.Y)); result[i] = f(i);
return result;
} }
static float2[] uv = static float2[] uv =

View File

@@ -20,14 +20,30 @@ namespace OpenRa.Game
public PointF ToPointF() { return new PointF(X, Y); } public PointF ToPointF() { return new PointF(X, Y); }
public static float2 operator +(float2 a, float2 b) 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) { return new float2(-a.X, -a.Y); }
static float Lerp(float a, float b, float t) { return (1 - t) * a + t * b; }
public static float2 Lerp(float2 a, float2 b, float t)
{ {
return new float2(a.X + b.X, a.Y + b.Y); return new float2(
Lerp(a.X, b.X, t),
Lerp(a.Y, b.Y, t));
} }
public static float2 operator -(float2 a, float2 b) public static float2 Lerp(float2 a, float2 b, float2 t)
{ {
return new float2(a.X - b.X, a.Y - b.Y); return new float2(
Lerp(a.X, b.X, t.X),
Lerp(a.Y, b.Y, t.Y));
}
public static float2 FromAngle(float a)
{
return new float2((float)Math.Sin(a), (float)Math.Cos(a));
} }
public float2 Constrain(Range<float2> r) public float2 Constrain(Range<float2> r)
@@ -55,21 +71,8 @@ namespace OpenRa.Game
return Math.Abs(d.X) < e && Math.Abs(d.Y) < e; return Math.Abs(d.X) < e && Math.Abs(d.Y) < e;
} }
static float Sign(float f) public float2 Sign() { return new float2(Math.Sign(X), Math.Sign(Y)); }
{ public static float Dot(float2 a, float2 b) { return a.X * b.X + a.Y * b.Y; }
if (f > 0) return 1; public float2 Round() { return new float2((float)Math.Round(X), (float)Math.Round(Y)); }
if (f < 0) return -1;
return 0;
}
public float2 Sign()
{
return new float2(Sign(X), Sign(Y));
}
public static float Dot(float2 a, float2 b)
{
return a.X * b.X + a.Y * b.Y;
}
} }
} }

View File

@@ -7,45 +7,32 @@ namespace OpenRa.Game
{ {
struct int2 struct int2
{ {
public int X; public int X,Y;
public int Y;
public int2( int x, int y ) { this.X = x; this.Y = y; } public int2( int x, int y ) { this.X = x; this.Y = y; }
public int2( Point p ) { X = p.X; Y = p.Y; } public int2( Point p ) { X = p.X; Y = p.Y; }
public int2( Size p ) { X = p.Width; Y = p.Height; } public int2( Size p ) { X = p.Width; Y = p.Height; }
public static int2 operator +( int2 a, int2 b ) public static int2 operator +(int2 a, int2 b) { return new int2(a.X + b.X, a.Y + b.Y); }
{ public static int2 operator -(int2 a, int2 b) { return new int2(a.X - b.X, a.Y - b.Y); }
return new int2( a.X + b.X, a.Y + b.Y ); public static int2 operator *(int a, int2 b) { return new int2(a * b.X, a * b.Y); }
} public static int2 operator *(int2 b, int a) { return new int2(a * b.X, a * b.Y); }
public static int2 operator -( int2 a, int2 b ) public float2 ToFloat2() { return new float2(X, Y); }
{
return new int2( a.X - b.X, a.Y - b.Y );
}
public static int2 operator *( int a, int2 b ) public static bool operator ==(int2 me, int2 other) { return (me.X == other.X && me.Y == other.Y); }
{ public static bool operator !=(int2 me, int2 other) { return !(me == other); }
return new int2( a * b.X, a * b.Y );
}
public static int2 operator *( int2 b, int a )
{
return new int2( a * b.X, a * b.Y );
}
public float2 ToFloat2() public int2 Sign() { return new int2(Math.Sign(X), Math.Sign(Y)); }
{ public override int GetHashCode() { return X.GetHashCode() ^ Y.GetHashCode(); }
return new float2( X, Y );
}
public static bool operator ==( int2 me, int2 other ) public override bool Equals(object obj)
{ {
return ( me.X == other.X && me.Y == other.Y ); if (obj == null)
} return false;
public static bool operator !=( int2 me, int2 other ) int2 o = (int2)obj;
{ return o == this;
return !( me == other );
} }
} }
} }