git-svn-id: svn://svn.ijw.co.nz/svn/OpenRa@1339 993157c7-ee19-0410-b2c4-bb4e9862e678
This commit is contained in:
@@ -68,5 +68,7 @@ namespace OpenRa
|
||||
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; }
|
||||
public float2 Round() { return new float2((float)Math.Round(X), (float)Math.Round(Y)); }
|
||||
|
||||
public override string ToString() { return string.Format("({0},{1})", X, Y); }
|
||||
}
|
||||
}
|
||||
|
||||
@@ -33,5 +33,8 @@ namespace OpenRa
|
||||
int2 o = (int2)obj;
|
||||
return o == this;
|
||||
}
|
||||
|
||||
public static readonly int2 Zero = new int2(0, 0);
|
||||
public Point ToPoint() { return new Point(X, Y); }
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,6 +2,8 @@ using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
using OpenRa.FileFormats;
|
||||
using BluntDirectX.Direct3D;
|
||||
using System.Drawing;
|
||||
|
||||
namespace OpenRa.Game
|
||||
{
|
||||
@@ -65,6 +67,7 @@ namespace OpenRa.Game
|
||||
public void Tick()
|
||||
{
|
||||
viewport.DrawRegions(this);
|
||||
Queue<Packet> stuffFromOtherPlayers = network.Tick(); // todo: actually use the orders!
|
||||
}
|
||||
|
||||
public void Issue(IOrder order)
|
||||
|
||||
@@ -66,12 +66,12 @@ namespace OpenRa.Game
|
||||
}
|
||||
}
|
||||
|
||||
float2 lastPos;
|
||||
int2 lastPos;
|
||||
|
||||
protected override void OnMouseDown(MouseEventArgs e)
|
||||
{
|
||||
base.OnMouseDown(e);
|
||||
lastPos = new float2(e.Location);
|
||||
lastPos = new int2(e.Location);
|
||||
|
||||
if (e.Button == MouseButtons.Left)
|
||||
foreach (Region region in game.viewport.Regions)
|
||||
@@ -85,7 +85,7 @@ namespace OpenRa.Game
|
||||
|
||||
if (e.Button == MouseButtons.Right)
|
||||
{
|
||||
float2 p = new float2(e.Location);
|
||||
int2 p = new int2(e.Location);
|
||||
game.viewport.Scroll(lastPos - p);
|
||||
lastPos = p;
|
||||
}
|
||||
|
||||
@@ -18,7 +18,7 @@ namespace OpenRa.Game
|
||||
int currentFrame = 0;
|
||||
|
||||
public int CurrentFrame { get { return currentFrame; } }
|
||||
public int RemainingNetSyncTime { get { return Math.Min(0, Environment.TickCount - nextSyncTime); } }
|
||||
public int RemainingNetSyncTime { get { return Math.Max(0, nextSyncTime - Environment.TickCount); } }
|
||||
|
||||
Queue<Packet> incomingPackets = new Queue<Packet>();
|
||||
|
||||
@@ -36,11 +36,9 @@ namespace OpenRa.Game
|
||||
Packet packet = Packet.FromReceivedData(sender, data);
|
||||
|
||||
lock (this)
|
||||
{
|
||||
if (currentFrame <= packet.Frame)
|
||||
incomingPackets.Enqueue(packet);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
receiveThread.IsBackground = true;
|
||||
@@ -76,7 +74,9 @@ namespace OpenRa.Game
|
||||
if (p.Frame == currentFrame)
|
||||
toProcess.Enqueue(p);
|
||||
}
|
||||
|
||||
++currentFrame;
|
||||
nextSyncTime = Environment.TickCount + netSyncInterval;
|
||||
}
|
||||
|
||||
return toProcess;
|
||||
|
||||
@@ -68,6 +68,7 @@
|
||||
<Compile Include="Renderer.cs" />
|
||||
<Compile Include="Settings.cs" />
|
||||
<Compile Include="Sidebar.cs" />
|
||||
<Compile Include="SidebarItem.cs" />
|
||||
<Compile Include="Sprite.cs" />
|
||||
<Compile Include="SpriteRenderer.cs" />
|
||||
<Compile Include="SpriteSheetBuilder.cs" />
|
||||
|
||||
@@ -25,19 +25,19 @@ namespace OpenRa.Game
|
||||
|
||||
Action drawFunction;
|
||||
MouseEventHandler mouseHandler;
|
||||
RectangleF rect;
|
||||
Rectangle rect;
|
||||
|
||||
static float2 MakeSize(Viewport v, DockStyle d, float size)
|
||||
static int2 MakeSize(Viewport v, DockStyle d, int size)
|
||||
{
|
||||
switch (d)
|
||||
{
|
||||
case DockStyle.Top:
|
||||
case DockStyle.Bottom:
|
||||
return new float2(v.Width, size);
|
||||
return new int2(v.Width, size);
|
||||
|
||||
case DockStyle.Left:
|
||||
case DockStyle.Right:
|
||||
return new float2(size, v.Height);
|
||||
return new int2(size, v.Height);
|
||||
|
||||
default:
|
||||
throw new NotImplementedException();
|
||||
@@ -46,42 +46,39 @@ namespace OpenRa.Game
|
||||
|
||||
public void Clicked(MouseEventArgs e)
|
||||
{
|
||||
mouseHandler(this, e);
|
||||
mouseHandler(this, new MouseEventArgs(e.Button, e.Clicks, e.X - rect.Left, e.Y - rect.Top, e.Delta));
|
||||
}
|
||||
|
||||
public static Region Create(Viewport v, DockStyle d, float size, Action f, MouseEventHandler m)
|
||||
public static Region Create(Viewport v, DockStyle d, int size, Action f, MouseEventHandler m)
|
||||
{
|
||||
float2 s = MakeSize(v, d, size);
|
||||
int2 s = MakeSize(v, d, size);
|
||||
|
||||
switch (d)
|
||||
{
|
||||
case DockStyle.Top:
|
||||
case DockStyle.Left:
|
||||
return new Region(new float2(0,0), s, v, f, m);
|
||||
return new Region(int2.Zero, s, v, f, m);
|
||||
|
||||
case DockStyle.Right:
|
||||
case DockStyle.Bottom:
|
||||
return new Region(new float2( v.Width - s.X, v.Height - s.Y ), s, v, f, m);
|
||||
return new Region(new int2( v.Width - s.X, v.Height - s.Y ), s, v, f, m);
|
||||
|
||||
default:
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
}
|
||||
|
||||
Region(float2 location, float2 size, Viewport viewport, Action drawFunction, MouseEventHandler mouseHandler)
|
||||
Region(int2 location, int2 size, Viewport viewport, Action drawFunction, MouseEventHandler mouseHandler)
|
||||
{
|
||||
this.location = location;
|
||||
this.size = size;
|
||||
this.drawFunction = drawFunction;
|
||||
this.viewport = viewport;
|
||||
this.mouseHandler = mouseHandler;
|
||||
rect = new RectangleF(location.ToPointF(), new SizeF(size.ToPointF()));
|
||||
rect = new Rectangle(location.X, location.Y, size.X, size.Y);
|
||||
}
|
||||
|
||||
public bool Contains(float2 point)
|
||||
{
|
||||
return rect.Contains(point.ToPointF());
|
||||
}
|
||||
public bool Contains(int2 point) { return rect.Contains(point.ToPoint()); }
|
||||
|
||||
public void Draw(Renderer renderer)
|
||||
{
|
||||
|
||||
@@ -14,24 +14,20 @@ namespace OpenRa.Game
|
||||
{
|
||||
TechTree.TechTree techTree;
|
||||
|
||||
SpriteRenderer spriteRenderer;
|
||||
SpriteRenderer spriteRenderer, clockRenderer;
|
||||
Sprite blank;
|
||||
Game game;
|
||||
readonly Region region;
|
||||
|
||||
public Region Region
|
||||
{
|
||||
get { return region; }
|
||||
}
|
||||
Animation clockAnimation = new Animation("clock");
|
||||
|
||||
public Region Region { get { return region; } }
|
||||
public float Width { get { return spriteWidth * 2; } }
|
||||
|
||||
Dictionary<string, Sprite> sprites = new Dictionary<string,Sprite>();
|
||||
const float spriteWidth = 64, spriteHeight = 48;
|
||||
|
||||
public float Width
|
||||
{
|
||||
get { return spriteWidth * 2; }
|
||||
}
|
||||
const int spriteWidth = 64, spriteHeight = 48;
|
||||
|
||||
List<SidebarItem> items = new List<SidebarItem>();
|
||||
|
||||
public Sidebar( Race race, Renderer renderer, Game game )
|
||||
{
|
||||
@@ -42,17 +38,20 @@ namespace OpenRa.Game
|
||||
techTree.CurrentRace = race;
|
||||
techTree.Build("FACT", true);
|
||||
spriteRenderer = new SpriteRenderer(renderer, false);
|
||||
clockRenderer = new SpriteRenderer(renderer, true);
|
||||
|
||||
LoadSprites("buildings.txt");
|
||||
LoadSprites("units.txt");
|
||||
|
||||
blank = SheetBuilder.Add(new Size((int)spriteWidth, (int)spriteHeight), 16);
|
||||
|
||||
clockAnimation.PlayRepeating("idle");
|
||||
}
|
||||
|
||||
public void Build(string key)
|
||||
public void Build(SidebarItem item)
|
||||
{
|
||||
if (string.IsNullOrEmpty(key)) return;
|
||||
game.world.orderGenerator = new PlaceBuilding( 1, key.ToLowerInvariant() );
|
||||
if (item != null)
|
||||
game.world.orderGenerator = new PlaceBuilding( 1, item.techTreeItem.tag.ToLowerInvariant() );
|
||||
}
|
||||
|
||||
void LoadSprites(string filename)
|
||||
@@ -76,53 +75,54 @@ namespace OpenRa.Game
|
||||
DrawSprite(blank, ref p);
|
||||
}
|
||||
|
||||
void Paint()
|
||||
void PopulateItemList()
|
||||
{
|
||||
float2 buildPos = region.Location + new float2(region.Size.X - spriteWidth * 2, 0);
|
||||
float2 unitPos = region.Location + new float2(region.Size.X - spriteWidth, 0);
|
||||
int buildPos = 0, unitPos = 0;
|
||||
|
||||
items.Clear();
|
||||
|
||||
foreach (Item i in techTree.BuildableItems)
|
||||
{
|
||||
Sprite sprite;
|
||||
if (!sprites.TryGetValue(i.tag, out sprite)) continue;
|
||||
|
||||
items.Add(new SidebarItem(sprite, i, i.IsStructure ? buildPos : unitPos));
|
||||
|
||||
if (i.IsStructure)
|
||||
DrawSprite( sprite, ref buildPos );
|
||||
buildPos += spriteHeight;
|
||||
else
|
||||
DrawSprite( sprite, ref unitPos );
|
||||
unitPos += spriteHeight;
|
||||
}
|
||||
}
|
||||
|
||||
Fill( region.Location.Y + region.Size.Y, buildPos );
|
||||
Fill( region.Location.Y + region.Size.Y, unitPos );
|
||||
void Paint()
|
||||
{
|
||||
PopulateItemList(); // todo: do this less often, just when things actually change!
|
||||
|
||||
spriteRenderer.Flush();
|
||||
foreach (SidebarItem i in items)
|
||||
i.Paint(spriteRenderer, region.Location);
|
||||
|
||||
spriteRenderer.Flush(); //todo: fix filling
|
||||
|
||||
clockRenderer.DrawSprite( clockAnimation.Images[0], region.Location, 0 );
|
||||
clockAnimation.Tick(1);
|
||||
|
||||
clockRenderer.Flush();
|
||||
}
|
||||
|
||||
public string FindSpriteAtPoint(float2 point)
|
||||
public SidebarItem GetItem(float2 point)
|
||||
{
|
||||
float y1 = 0, y2 = 0;
|
||||
foreach (Item i in techTree.BuildableItems)
|
||||
{
|
||||
RectangleF rect;
|
||||
if (i.IsStructure)
|
||||
{
|
||||
rect = new RectangleF(region.Location.X, region.Location.Y + y1, spriteWidth, spriteHeight);
|
||||
y1 += 48;
|
||||
}
|
||||
else
|
||||
{
|
||||
rect = new RectangleF(region.Location.X + spriteWidth, region.Location.Y + y2, spriteWidth, spriteHeight);
|
||||
y2 += 48;
|
||||
}
|
||||
if (rect.Contains(point.ToPointF())) return i.tag;
|
||||
}
|
||||
foreach (SidebarItem i in items)
|
||||
if (i.Clicked(point))
|
||||
return i;
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
void MouseHandler(object sender, MouseEventArgs e)
|
||||
{
|
||||
float2 point = new float2(e.Location);
|
||||
Build(FindSpriteAtPoint(point));
|
||||
Build(GetItem(point));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
37
OpenRa.Game/SidebarItem.cs
Normal file
37
OpenRa.Game/SidebarItem.cs
Normal file
@@ -0,0 +1,37 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
using OpenRa.TechTree;
|
||||
|
||||
namespace OpenRa.Game
|
||||
{
|
||||
class SidebarItem
|
||||
{
|
||||
public readonly Item techTreeItem;
|
||||
public readonly float2 location;
|
||||
readonly Sprite sprite;
|
||||
|
||||
public SidebarItem(Sprite s, Item item, int y)
|
||||
{
|
||||
this.techTreeItem = item;
|
||||
this.sprite = s;
|
||||
location = new float2(item.IsStructure ? 0 : 64, y);
|
||||
}
|
||||
|
||||
public bool Clicked(float2 p)
|
||||
{
|
||||
if (p.X < location.X || p.Y < location.Y)
|
||||
return false;
|
||||
|
||||
if (p.X > location.X + 64 || p.Y > location.Y + 48)
|
||||
return false;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public void Paint(SpriteRenderer renderer, float2 offset)
|
||||
{
|
||||
renderer.DrawSprite(sprite, location + offset, 0);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -130,4 +130,10 @@
|
||||
<sequence name="harvest7" start="88" length="8"/>
|
||||
</unit>
|
||||
|
||||
<!-- build clock - hacked in -->
|
||||
|
||||
<unit name="clock">
|
||||
<sequence name="idle" start="0" length="*"/>
|
||||
</unit>
|
||||
|
||||
</sequences>
|
||||
|
||||
Reference in New Issue
Block a user