Map-Zeichen-Logik in extra Klasse verschoben

This commit is contained in:
2014-05-02 09:46:44 +02:00
parent 2048f344c1
commit 849d764c92
3 changed files with 114 additions and 91 deletions

View File

@@ -9,27 +9,26 @@
<OutputType>Exe</OutputType>
<RootNamespace>inf3</RootNamespace>
<AssemblyName>inf3</AssemblyName>
<TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|x86' ">
<DebugSymbols>True</DebugSymbols>
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>False</Optimize>
<Optimize>false</Optimize>
<OutputPath>bin\Debug</OutputPath>
<DefineConstants>DEBUG;</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<PlatformTarget>x86</PlatformTarget>
<ConsolePause>False</ConsolePause>
<ConsolePause>false</ConsolePause>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|x86' ">
<DebugType>none</DebugType>
<Optimize>True</Optimize>
<Optimize>true</Optimize>
<OutputPath>bin\Release</OutputPath>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<PlatformTarget>x86</PlatformTarget>
<ConsolePause>False</ConsolePause>
<ConsolePause>false</ConsolePause>
</PropertyGroup>
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
<ItemGroup>
@@ -80,6 +79,7 @@
<Compile Include="src\Gui\ChatInputBox.cs" />
<Compile Include="src\Gui\ChatOutputBox.cs" />
<Compile Include="src\Gui\IChatMessage.cs" />
<Compile Include="src\Gui\MapPanel.cs" />
</ItemGroup>
<ItemGroup />
<ItemGroup>

View File

@@ -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 ();

99
src/Gui/MapPanel.cs Normal file
View File

@@ -0,0 +1,99 @@
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
namespace WorldOfPeacecraft
{
public class MapPanel : Panel
{
private const int TileSize = 32;
private const int EntitySize = 10;
private IBackend Backend;
public MapPanel (IBackend backend)
{
Backend = backend;
this.Paint += DoPaint;
}
protected override void OnLayout (LayoutEventArgs levent)
{
ITile[,] map = Backend.GetMap ();
if (map != null) {
int mapWidth = (map.GetLength(0)) * TileSize;
int mapHeight = (map.GetLength(1)) * TileSize;
this.SetClientSizeCore(mapWidth, mapHeight);
}
base.OnLayout (levent);
}
public void DoPaint (object source, PaintEventArgs args)
{
BufferedGraphics buffer = BufferedGraphicsManager.Current.Allocate (this.CreateGraphics (), this.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), this.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);
}
}
}