git-svn-id: svn://svn.ijw.co.nz/svn/OpenRa@1285 993157c7-ee19-0410-b2c4-bb4e9862e678

This commit is contained in:
bob
2007-07-16 03:45:05 +00:00
parent ee8b3d82b9
commit 333a20d29e
10 changed files with 196 additions and 58 deletions

View File

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

View File

@@ -7,6 +7,6 @@ namespace OpenRa.Game
interface ISelectable
{
//Sprite CurrentCursor( int x, int y );
MoveOrder Order( int x, int y );
IOrder Order( int2 xy );
}
}

View File

@@ -60,9 +60,9 @@ namespace OpenRa.Game
foreach (TreeReference treeReference in map.Trees)
world.Add(new Tree(treeReference, treeCache, map));
world.Add(new Mcv(24 * new float2(5, 5), 3));
world.Add(new Mcv(24 * new float2(7, 5), 2));
Mcv mcv = new Mcv( 24 * new float2( 9, 5 ), 1 );
world.Add(new Mcv(new int2(5, 5), 3));
world.Add(new Mcv(new int2(7, 5), 2));
Mcv mcv = new Mcv( new int2( 9, 5 ), 1 );
myUnit = mcv;
world.Add( mcv );
@@ -91,7 +91,7 @@ namespace OpenRa.Game
{
int x = (int)( ( e.X + viewport.Location.X ) / 24 );
int y = (int)( ( e.Y + viewport.Location.Y ) / 24 );
myUnit.Order( x, y ).Apply();
myUnit.Order( new int2( x, y ) ).Apply();
}
}

View File

@@ -10,14 +10,19 @@ namespace OpenRa.Game
class Mcv : Actor, ISelectable
{
static Range<int>? mcvRange = null;
MoveOrder currentOrder = null;
int facing = 0;
float2 location;
int2 fromCell, toCell;
int moveFraction, moveFractionTotal;
public Mcv( float2 location, int palette )
delegate void TickFunc( World world, double t );
TickFunc currentOrder = null;
TickFunc nextOrder = null;
public Mcv( int2 cell, int palette )
{
this.location = location;
this.renderLocation = this.location - new float2( 12, 12 ); // HACK: display the mcv centered in it's cell
fromCell = toCell = cell;
float2 location = ( cell * 24 ).ToFloat2();
this.renderLocation = location - new float2( 12, 12 ); // HACK: display the mcv centered in it's cell
this.palette = palette;
if (mcvRange == null)
@@ -65,48 +70,106 @@ namespace OpenRa.Game
}
}
public void Accept(MoveOrder o)
const int Speed = 6;
public override void Tick( World world, double t )
{
currentOrder = o;
}
const float Speed = 48.0f;
public override void Tick( double t )
{
if( currentOrder == null )
return;
if( float2.WithinEpsilon( location, currentOrder.Destination, 1.0f ) )
if( currentOrder == null && nextOrder != null )
{
currentOrder = null;
return;
currentOrder = nextOrder;
nextOrder = null;
}
Range<float2> r = new Range<float2>(
new float2( -Speed * (float)t, -Speed * (float)t ),
new float2( Speed * (float)t, Speed * (float)t ) );
float2 d = ( currentOrder.Destination - location ).Constrain( r );
int desiredFacing = GetFacing( d );
int df = (desiredFacing - facing + 32) % 32;
if( df == 0 )
location += d;
else if( df > 16 )
facing = ( facing + 31 ) % 32;
else
facing = ( facing + 1 ) % 32;
renderLocation = location - new float2( 12, 12 ); // HACK: center mcv in it's cell
renderLocation.X = (float)Math.Round( renderLocation.X );
renderLocation.Y = (float)Math.Round( renderLocation.Y );
if( currentOrder != null )
currentOrder( world, t );
}
public MoveOrder Order( int x, int y )
public void AcceptMoveOrder( int2 destination )
{
return new MoveOrder( this, x, y );
nextOrder = delegate( World world, double t )
{
int speed = (int)( t * ( Speed * 100 ) );
if( nextOrder != null )
destination = toCell;
int desiredFacing = GetFacing( ( toCell - fromCell ).ToFloat2() );
if( facing != desiredFacing )
{
int df = ( desiredFacing - facing + 32 ) % 32;
if( df > 16 )
facing = ( facing + 31 ) % 32;
else
facing = ( facing + 1 ) % 32;
}
else
{
moveFraction += speed;
if( moveFraction >= moveFractionTotal )
{
moveFraction = 0;
moveFractionTotal = 0;
fromCell = toCell;
if( toCell == destination )
{
currentOrder = null;
}
else
{
int2 dir = destination - fromCell;
toCell = fromCell + new int2( Math.Sign( dir.X ), Math.Sign( dir.Y ) );
moveFractionTotal = ( dir.X != 0 && dir.Y != 0 ) ? 250 : 200;
}
}
}
float2 location;
if( moveFraction > 0 )
{
float frac = (float)moveFraction / moveFractionTotal;
location = 24 * ( ( 1 - frac ) * fromCell.ToFloat2() + frac * toCell.ToFloat2() );
}
else
location = 24 * fromCell.ToFloat2();
renderLocation = location - new float2( 12, 12 ); // HACK: center mcv in it's cell
renderLocation.X = (float)Math.Round( renderLocation.X );
renderLocation.Y = (float)Math.Round( renderLocation.Y );
};
}
public void AcceptDeployOrder()
{
nextOrder = delegate( World world, double t )
{
int desiredFacing = 12;
if( facing != desiredFacing )
{
int df = ( desiredFacing - facing + 32 ) % 32;
if( df > 16 )
facing = ( facing + 31 ) % 32;
else
facing = ( facing + 1 ) % 32;
}
else
{
world.AddFrameEndTask( delegate
{
world.Add( new Refinery( ( fromCell * 24 - new int2( 24, 24 ) ).ToFloat2(), palette ) );
} );
currentOrder = null;
}
};
}
public IOrder Order( int2 xy )
{
if( ( fromCell == toCell || moveFraction == 0 ) && fromCell == xy )
return new DeployMcvOrder( this );
else
return new MoveOrder( this, xy );
}
}
}

View File

@@ -4,17 +4,17 @@ using System.Text;
namespace OpenRa.Game
{
class MoveOrder
interface IOrder
{
void Apply();
}
class MoveOrder : IOrder
{
public readonly Mcv Unit;
public readonly float2 Destination;
public readonly int2 Destination;
public MoveOrder( Mcv unit, int x, int y )
: this( unit, new float2( x * 24, y * 24 ) )
{
}
public MoveOrder(Mcv unit, float2 destination)
public MoveOrder(Mcv unit, int2 destination)
{
this.Unit = unit;
this.Destination = destination;
@@ -22,7 +22,22 @@ namespace OpenRa.Game
public void Apply()
{
Unit.Accept( this );
Unit.AcceptMoveOrder( Destination );
}
}
class DeployMcvOrder : IOrder
{
public Mcv Unit;
public DeployMcvOrder( Mcv unit )
{
this.Unit = unit;
}
public void Apply()
{
Unit.AcceptDeployOrder();
}
}
}

View File

@@ -43,6 +43,7 @@
<Compile Include="Actor.cs" />
<Compile Include="Clock.cs" />
<Compile Include="float2.cs" />
<Compile Include="int2.cs" />
<Compile Include="ISelectable.cs" />
<Compile Include="MoveOrder.cs" />
<Compile Include="Region.cs" />

View File

@@ -30,6 +30,6 @@ namespace OpenRa.Game
}
}
public override void Tick(double t) { }
public override void Tick( World world, double t ) { }
}
}

View File

@@ -20,6 +20,6 @@ namespace OpenRa.Game
get { return currentImages; }
}
public override void Tick(double t){}
public override void Tick( World world, double t ) { }
}
}

View File

@@ -11,6 +11,7 @@ namespace OpenRa.Game
class World
{
List<Actor> actors = new List<Actor>();
List<Action<World>> frameEndActions = new List<Action<World>>();
SpriteRenderer spriteRenderer;
Renderer renderer;
Viewport viewport;
@@ -24,6 +25,7 @@ namespace OpenRa.Game
}
public void Add(Actor a) { actors.Add(a); }
public void AddFrameEndTask( Action<World> a ) { frameEndActions.Add( a ); }
double lastTime = Environment.TickCount / 1000.0;
@@ -37,7 +39,7 @@ namespace OpenRa.Game
foreach (Actor a in actors)
{
a.Tick( dt );
a.Tick( this, dt );
Sprite[] images = a.CurrentImages;
@@ -51,6 +53,12 @@ namespace OpenRa.Game
spriteRenderer.DrawSprite(image, a.renderLocation, a.palette);
}
foreach( Action<World> a in frameEndActions )
{
a( this );
}
frameEndActions.Clear();
spriteRenderer.Flush();
}
}

51
OpenRa.Game/int2.cs Normal file
View File

@@ -0,0 +1,51 @@
using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
namespace OpenRa.Game
{
struct int2
{
public int X;
public int 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( Size p ) { X = p.Width; Y = p.Height; }
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 );
}
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 float2 ToFloat2()
{
return new float2( X, Y );
}
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 );
}
}
}