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

This commit is contained in:
(no author)
2007-07-23 05:25:57 +00:00
parent d4f72cb5c3
commit a6502022e9
24 changed files with 839 additions and 76 deletions

View File

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

View File

@@ -18,7 +18,7 @@ namespace OpenRa.Game
animation.PlayThen( "make", delegate { animation.Play( "idle" ); } );
}
public override void Tick( World world, double t )
public override void Tick( Game game, double t )
{
animation.Tick( t );
}

View File

@@ -1,40 +0,0 @@
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using OpenRa.FileFormats;
namespace OpenRa.Game
{
class Folder : IFolder
{
readonly string path;
public Folder(string path) { this.path = path; }
public Stream GetContent(string filename)
{
try { return File.OpenRead(path + filename); }
catch { throw new FileNotFoundException("File not found", filename); }
}
}
static class FileSystem
{
static List<IFolder> mountedFolders = new List<IFolder>();
public static void Mount(IFolder folder)
{
mountedFolders.Add(folder);
}
public static Stream Open(string filename)
{
foreach (IFolder folder in mountedFolders)
try { return folder.GetContent(filename); }
catch { }
throw new FileNotFoundException("File not found", filename);
}
}
}

36
OpenRa.Game/Game.cs Normal file
View File

@@ -0,0 +1,36 @@
using System;
using System.Collections.Generic;
using System.Text;
using OpenRa.FileFormats;
namespace OpenRa.Game
{
class Game
{
public readonly World world;
public readonly Map map;
public readonly SpriteRenderer SpriteRenderer;
public readonly TreeCache treeCache;
public readonly TerrainRenderer terrain;
public readonly Viewport viewport;
public readonly PathFinder pathFinder;
public Game( string mapName, Renderer renderer, int2 clientSize )
{
SheetBuilder.Initialize( renderer.Device );
map = new Map( new IniFile( FileSystem.Open( mapName ) ) );
FileSystem.Mount( new Package( "../../../" + map.Theater + ".mix" ) );
viewport = new Viewport( clientSize, new float2( map.Size ), renderer );
terrain = new TerrainRenderer( renderer, map, viewport );
world = new World( renderer, viewport );
treeCache = new TreeCache( renderer.Device, map );
foreach( TreeReference treeReference in map.Trees )
world.Add( new Tree( treeReference, treeCache, map ) );
pathFinder = new PathFinder( map, terrain.tileSet );
}
}
}

View File

@@ -13,15 +13,10 @@ namespace OpenRa.Game
class MainWindow : Form
{
readonly Renderer renderer;
readonly Map map;
//readonly Map map;
Package TileMix;
World world;
TreeCache treeCache;
TerrainRenderer terrain;
Sidebar sidebar;
Viewport viewport;
Game game;
public readonly Sidebar sidebar;
static Size GetResolution(Settings settings)
{
@@ -44,42 +39,29 @@ namespace OpenRa.Game
Visible = true;
bool windowed = !settings.GetValue("fullscreeen", false);
renderer = new Renderer(this, GetResolution(settings), windowed);
map = new Map(new IniFile(FileSystem.Open(settings.GetValue("map", "scm12ea.ini"))));
viewport = new Viewport(new float2(ClientSize), new float2(map.Size), renderer);
SheetBuilder.Initialize(renderer.Device);
FileSystem.Mount(TileMix = new Package("../../../" + map.Theater + ".mix"));
renderer.SetPalette(new HardwarePalette(renderer.Device, map));
terrain = new TerrainRenderer(renderer, map, TileMix, viewport);
world = new World(renderer, viewport);
treeCache = new TreeCache(renderer.Device, map, TileMix);
foreach (TreeReference treeReference in map.Trees)
world.Add(new Tree(treeReference, treeCache, map));
game = new Game( settings.GetValue( "map", "scm12ea.ini" ), renderer, new int2( ClientSize ) );
SequenceProvider.ForcePrecache();
world.Add( new Mcv( new int2( 5, 5 ), 3 ) );
world.Add( new Mcv( new int2( 7, 5 ), 2 ) );
game.world.Add( new Mcv( new int2( 5, 5 ), 3 ) );
game.world.Add( new Mcv( new int2( 7, 5 ), 2 ) );
Mcv mcv = new Mcv( new int2( 9, 5 ), 1 );
world.myUnit = mcv;
world.Add( mcv );
world.Add( new Refinery( new int2( 7, 5 ), 2 ) );
game.world.myUnit = mcv;
game.world.Add( mcv );
game.world.Add( new Refinery( new int2( 7, 5 ), 2 ) );
sidebar = new Sidebar(Race.Soviet, renderer, viewport);
sidebar = new Sidebar(Race.Soviet, renderer, game.viewport);
PathFinder.Instance = new PathFinder( map, terrain.tileSet );
renderer.SetPalette( new HardwarePalette( renderer.Device, game.map ) );
}
internal void Run()
{
while (Created && Visible)
{
viewport.DrawRegions();
game.viewport.DrawRegions( game );
Application.DoEvents();
}
}
@@ -93,9 +75,8 @@ namespace OpenRa.Game
if (e.Button == MouseButtons.Left)
{
int x = (int)( ( e.X + viewport.Location.X ) / 24 );
int y = (int)( ( e.Y + viewport.Location.Y ) / 24 );
world.myUnit.Order( new int2( x, y ) ).Apply();
float2 xy = ( 1 / 24.0f ) * ( new float2( e.Location ) + game.viewport.Location );
game.world.myUnit.Order( new int2( (int)xy.X, (int)xy.Y ) ).Apply();
}
}
@@ -106,7 +87,7 @@ namespace OpenRa.Game
if (e.Button == MouseButtons.Right)
{
float2 p = new float2(e.Location);
viewport.Scroll(lastPos - p);
game.viewport.Scroll(lastPos - p);
lastPos = p;
}
}

View File

@@ -16,13 +16,14 @@ namespace OpenRa.Game
public void AcceptDeployOrder()
{
nextOrder = delegate( World world, double t )
nextOrder = delegate( Game game, double t )
{
int desiredFacing = 12;
if( facing != desiredFacing )
Turn( desiredFacing );
else
{
World world = game.world;
world.AddFrameEndTask( delegate
{
world.Remove( this );

View File

@@ -43,7 +43,7 @@
<Compile Include="Actor.cs" />
<Compile Include="Animation.cs" />
<Compile Include="Building.cs" />
<Compile Include="FileSystem.cs" />
<Compile Include="Game.cs" />
<Compile Include="Harvester.cs" />
<Compile Include="Log.cs" />
<Compile Include="PathFinder.cs" />

View File

@@ -8,8 +8,6 @@ namespace OpenRa.Game
{
class PathFinder
{
public static PathFinder Instance;
double[ , ] passableCost = new double[ 128, 128 ];
Map map;
@@ -28,7 +26,7 @@ namespace OpenRa.Game
// returns estimate to destination, 0.0 is cell is dest
public delegate double DestinationFunc( int2 cell );
public List<int2> FindUnitPath( World world, Unit unit, DestinationFunc estimator )
public List<int2> FindUnitPath( Unit unit, DestinationFunc estimator )
{
int2 startLocation = unit.Location + map.Offset;
@@ -58,10 +56,7 @@ namespace OpenRa.Game
cellInfo[ here.X, here.Y ].Seen = true;
if( estimator( here - offset ) == 0.0 )
{
Log.Write( "{0}, {1}", seenCount, impassableCount );
return MakePath( cellInfo, here, offset );
}
foreach( int2 d in directions )
{
@@ -106,7 +101,6 @@ namespace OpenRa.Game
pathNode = cellInfo[ pathNode.X, pathNode.Y ].Path;
}
Log.Write( "Path Length: {0}", ret.Count );
return ret;
}

View File

@@ -10,7 +10,7 @@ namespace OpenRa.Game
{
Point location;
Size size;
MethodInvoker drawFunction;
Action<Game> drawFunction;
static Size MakeSize(Viewport v, DockStyle d, int size)
{
@@ -29,7 +29,7 @@ namespace OpenRa.Game
}
}
public static Region Create(Viewport v, DockStyle d, int size, MethodInvoker f)
public static Region Create(Viewport v, DockStyle d, int size, Action<Game> f)
{
Size s = MakeSize(v, d, size);
@@ -48,17 +48,17 @@ namespace OpenRa.Game
}
}
Region(Point location, Size size, MethodInvoker drawFunction)
Region(Point location, Size size, Action<Game> drawFunction)
{
this.location = location;
this.size = size;
this.drawFunction = drawFunction;
}
public void Draw(Renderer renderer, Viewport viewport)
public void Draw(Renderer renderer, Game game)
{
renderer.Device.EnableScissor(location.X, location.Y, size.Width, size.Height);
drawFunction();
drawFunction( game );
renderer.Device.DisableScissor();
}
}

View File

@@ -5,6 +5,7 @@ using System.Windows.Forms;
using System.Drawing;
using BluntDirectX.Direct3D;
using System.IO;
using OpenRa.FileFormats;
namespace OpenRa.Game
{

View File

@@ -2,6 +2,7 @@ using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using OpenRa.FileFormats;
namespace OpenRa.Game
{

View File

@@ -55,7 +55,7 @@ namespace OpenRa.Game
DrawSprite(blank, ref p);
}
public void Paint()
public void Paint( Game game )
{
float2 buildPos = viewport.Location + new float2(viewport.Size.X - 128, 0);
float2 unitPos = viewport.Location + new float2(viewport.Size.X - 64, 0);

View File

@@ -20,14 +20,14 @@ namespace OpenRa.Game
Renderer renderer;
Map map;
public TerrainRenderer(Renderer renderer, Map map, Package tilePackage, Viewport viewport)
public TerrainRenderer(Renderer renderer, Map map, Viewport viewport)
{
this.renderer = renderer;
this.viewport = viewport;
viewport.AddRegion(Region.Create(viewport, DockStyle.Left, viewport.Width - 128, Draw));
this.map = map;
tileSet = new TileSet(tilePackage, map.TileSuffix);
tileSet = new TileSet( map.TileSuffix );
Dictionary<TileReference, Sprite> tileMapping =
new Dictionary<TileReference, Sprite>();
@@ -58,7 +58,7 @@ namespace OpenRa.Game
indexBuffer.SetData( indices.ToArray() );
}
void Draw()
void Draw( Game game )
{
int indicesPerRow = map.Width * 6;
int verticesPerRow = map.Width * 4;

View File

@@ -11,7 +11,7 @@ namespace OpenRa.Game
{
Dictionary<string, Sprite> trees = new Dictionary<string, Sprite>();
public TreeCache(GraphicsDevice device, Map map, Package package)
public TreeCache(GraphicsDevice device, Map map)
{
foreach (TreeReference r in map.Trees)
{
@@ -20,7 +20,7 @@ namespace OpenRa.Game
string filename = r.Image + "." + map.Theater.Substring(0, 3);
ShpReader reader = new ShpReader(package.GetContent(filename));
ShpReader reader = new ShpReader( FileSystem.Open( filename ) );
trees.Add(r.Image, SheetBuilder.Add(reader[0].Image, reader.Size));
}
}

View File

@@ -12,7 +12,7 @@ namespace OpenRa.Game
protected int2 fromCell, toCell;
protected int moveFraction, moveFractionTotal;
protected delegate void TickFunc( World world, double t );
protected delegate void TickFunc( Game game, double t );
protected TickFunc currentOrder = null;
protected TickFunc nextOrder = null;
@@ -54,7 +54,7 @@ namespace OpenRa.Game
const int Speed = 6;
public override void Tick( World world, double t )
public override void Tick( Game game, double t )
{
animation.Tick( t );
if( currentOrder == null && nextOrder != null )
@@ -64,12 +64,12 @@ namespace OpenRa.Game
}
if( currentOrder != null )
currentOrder( world, t );
currentOrder( game, t );
}
public void AcceptMoveOrder( int2 destination )
{
nextOrder = delegate( World world, double t )
nextOrder = delegate( Game game, double t )
{
int speed = (int)( t * ( Speed * 100 ) );
@@ -92,7 +92,7 @@ namespace OpenRa.Game
currentOrder = null;
else
{
List<int2> res = PathFinder.Instance.FindUnitPath( world, this, PathFinder.DefaultEstimator( destination ) );
List<int2> res = game.pathFinder.FindUnitPath( this, PathFinder.DefaultEstimator( destination ) );
if( res.Count != 0 )
{
toCell = res[ res.Count - 1 ];

View File

@@ -37,7 +37,7 @@ namespace OpenRa.Game
regions.Add(r);
}
public void DrawRegions()
public void DrawRegions(Game game)
{
float2 r1 = new float2(2, -2) / Size;
float2 r2 = new float2(-1, 1);
@@ -45,7 +45,7 @@ namespace OpenRa.Game
renderer.BeginFrame(r1, r2, scrollPosition);
foreach (Region region in regions)
region.Draw(renderer, this);
region.Draw(renderer, game);
renderer.EndFrame();
}

View File

@@ -29,7 +29,7 @@ namespace OpenRa.Game
double lastTime = Environment.TickCount / 1000.0;
void Draw()
void Draw( Game game )
{
double t = Environment.TickCount / 1000.0;
double dt = t - lastTime;
@@ -39,7 +39,7 @@ namespace OpenRa.Game
foreach (Actor a in actors)
{
a.Tick( this, dt );
a.Tick( game, dt );
Sprite[] images = a.CurrentImages;
float2 loc = a.RenderLocation;