Map-Zeichen-Logik in extra Klasse verschoben
This commit is contained in:
@@ -8,12 +8,10 @@ namespace WorldOfPeacecraft
|
||||
{
|
||||
class Gui : Form, IGui
|
||||
{
|
||||
private const int TileSize = 32;
|
||||
private const int EntitySize = 10;
|
||||
private const int ChatWidth = 300;
|
||||
private IBackend Backend;
|
||||
|
||||
private Panel Board = new Panel();
|
||||
private MapPanel MapPanel;
|
||||
private ChatPanel ChatPanel;
|
||||
|
||||
public Gui ()
|
||||
@@ -36,11 +34,11 @@ namespace WorldOfPeacecraft
|
||||
public void InitializeComponents ()
|
||||
{
|
||||
ChatPanel = new ChatPanel (Backend);
|
||||
MapPanel = new MapPanel (Backend);
|
||||
this.SuspendLayout();
|
||||
this.Size = new Size(400 + ChatWidth, 400);
|
||||
Board.Location = new Point(0,0);
|
||||
Board.Size = new Size(400, 400);
|
||||
Board.Paint += DoPaint;
|
||||
MapPanel.Location = new Point(0,0);
|
||||
MapPanel.Size = new Size(400, 400);
|
||||
ChatPanel.Location = new Point (400, 0);
|
||||
ChatPanel.Size = new Size (300, 400);
|
||||
this.DoubleBuffered = true;
|
||||
@@ -49,7 +47,7 @@ namespace WorldOfPeacecraft
|
||||
this.Name = "WorldOfPeacecraft";
|
||||
this.ShowIcon = false;
|
||||
|
||||
this.Controls.Add (Board);
|
||||
this.Controls.Add (MapPanel);
|
||||
this.Controls.Add (ChatPanel);
|
||||
|
||||
this.ResumeLayout();
|
||||
@@ -61,89 +59,15 @@ namespace WorldOfPeacecraft
|
||||
Backend.Stop ();
|
||||
}
|
||||
|
||||
public void DoPaint (object source, PaintEventArgs args)
|
||||
{
|
||||
BufferedGraphics buffer = BufferedGraphicsManager.Current.Allocate (Board.CreateGraphics (), Board.DisplayRectangle);
|
||||
Graphics g = buffer.Graphics;
|
||||
lock (Backend) {
|
||||
PaintMap (g);
|
||||
PaintEntities (g);
|
||||
}
|
||||
buffer.Render();
|
||||
}
|
||||
|
||||
public void PaintMap (Graphics g)
|
||||
{
|
||||
ITile[,] map = Backend.GetMap ();
|
||||
if (map != null) {
|
||||
for (int y = 0; y < map.GetLength(1); y++) {
|
||||
for (int x = 0; x < map.GetLength(0); x++) {
|
||||
PaintTile (g, map [x, y], x, y);
|
||||
}
|
||||
}
|
||||
}
|
||||
else {
|
||||
g.FillRectangle(new SolidBrush(Color.White), Board.DisplayRectangle);
|
||||
}
|
||||
}
|
||||
|
||||
public void PaintTile (Graphics g, ITile tile, int x, int y)
|
||||
{
|
||||
int posx = x * TileSize;
|
||||
int posy = y * TileSize;
|
||||
Color color;
|
||||
if (tile.IsHuntable ()) {
|
||||
color = Color.DarkGreen;
|
||||
} // Stupid parenthesis
|
||||
else if (tile.IsForest ()) {
|
||||
color = Color.Green;
|
||||
} else if (tile.IsWater ()) {
|
||||
color = Color.Blue;
|
||||
} else if (tile.IsWalkable ()) {
|
||||
color = Color.Yellow;
|
||||
} else if (tile.IsWall ()) {
|
||||
color = Color.DarkGray;
|
||||
} else {
|
||||
color = Color.Black;
|
||||
}
|
||||
g.FillRectangle(new SolidBrush(color), posx, posy, TileSize, TileSize);
|
||||
}
|
||||
|
||||
public void PaintEntities (Graphics g)
|
||||
{
|
||||
IEnumerable<IPositionable> dragons = Backend.GetDragons ();
|
||||
IEnumerable<IPositionable> players = Backend.GetPlayers ();
|
||||
foreach (IPositionable dragon in dragons) {
|
||||
PaintEntity (g, dragon);
|
||||
}
|
||||
foreach (IPositionable player in players) {
|
||||
PaintEntity (g, player);
|
||||
}
|
||||
}
|
||||
|
||||
public void PaintEntity (Graphics g, IPositionable entity)
|
||||
{
|
||||
int x = entity.GetX () * TileSize + TileSize / 2 - EntitySize / 2;
|
||||
int y = entity.GetY () * TileSize + TileSize / 2 - EntitySize / 2;
|
||||
g.FillRectangle (new SolidBrush (Color.Red), x, y, EntitySize, EntitySize);
|
||||
g.DrawRectangle (new Pen( new SolidBrush (Color.Black)), x, y, EntitySize, EntitySize);
|
||||
}
|
||||
|
||||
public void PerformRefresh ()
|
||||
{
|
||||
this.BeginInvoke(new MethodInvoker(delegate
|
||||
{
|
||||
ITile[,] map = Backend.GetMap();
|
||||
if (map == null) {
|
||||
return;
|
||||
}
|
||||
int mapWidth = (map.GetLength(0)) * TileSize;
|
||||
int mapHeight = (map.GetLength(1)) * TileSize;
|
||||
MapPanel.PerformLayout ();
|
||||
this.SuspendLayout();
|
||||
this.SetClientSizeCore(mapWidth + ChatWidth, mapHeight);
|
||||
Board.Size = new Size(mapWidth, mapHeight);
|
||||
ChatPanel.Location = new Point (mapWidth, 0);
|
||||
ChatPanel.Size = new Size (ChatWidth, mapHeight);
|
||||
this.SetClientSizeCore(MapPanel.Width + ChatWidth, MapPanel.Height);
|
||||
ChatPanel.Location = new Point (MapPanel.Width, 0);
|
||||
ChatPanel.Size = new Size (ChatWidth, MapPanel.Height);
|
||||
this.ResumeLayout();
|
||||
this.PerformLayout();
|
||||
ChatPanel.UpdateData ();
|
||||
|
||||
Reference in New Issue
Block a user