Observer-modell

This commit is contained in:
2014-05-15 13:40:29 +02:00
parent 0c50f3b801
commit 42fcc8a1f0
4 changed files with 29 additions and 15 deletions

View File

@@ -19,6 +19,7 @@ namespace WorldOfPeacecraft
private int SelfId;
private AutoResetEvent InitializedEvent = new AutoResetEvent(false);
private bool initialized = false;
private Pathwalker Pathwalker = new Pathwalker();
public Backend (IGui gui)
{
@@ -40,6 +41,7 @@ namespace WorldOfPeacecraft
public void SetSelfId (int id)
{
this.SelfId = id;
Players[SelfId].LocationChanged += OnPlayerMoved;
}
public IEnumerable<IEntity> GetDragons ()
@@ -151,22 +153,21 @@ namespace WorldOfPeacecraft
public void MoveTo (int x, int y)
{
if (Map.GetTiles () [x, y].IsWalkable ()) {
Thread thread = new Thread (() => WalkTo (x, y));
thread.IsBackground = true;
thread.Start ();
}
// Hier müsste man locken
Pathwalker.Stop();
LinkedList<Coordinate> path = Pathfinder.FindPath (Players [SelfId].Coord, new Coordinate (x, y), Map);
Pathwalker.SetCoords (path);
}
private void WalkTo (int x, int y)
public void MoveStep ()
{
LinkedList<Coordinate> path = Pathfinder.FindPath (Players [SelfId].Coord, new Coordinate (x, y), Map);
Pathwalker walker = new Pathwalker ();
walker.SetCoords (path);
while (walker.HasMoreSteps()) {
SenderBuffer.AddLine("ask:" + walker.NextStep());
Thread.Sleep(250);
}
if (Pathwalker.HasMoreSteps())
SenderBuffer.AddLine("ask:" + Pathwalker.NextStep());
}
public void OnPlayerMoved (int x, int y)
{
MoveStep();
}
public void RefreshGui ()

View File

@@ -41,7 +41,7 @@ namespace WorldOfPeacecraft
return Coord.X;
}
public void SetPos (int x, int y)
public virtual void SetPos (int x, int y)
{
SetX (x);
SetY (y);

View File

@@ -292,8 +292,8 @@ namespace WorldOfPeacecraft
{
Player self = MapPlayer (playerBlock);
lock (Backend) {
Backend.SetSelfId(self.GetId());
Backend.SetPlayer(self);
Backend.SetSelfId(self.GetId());
}
Backend.RefreshGui ();
}

View File

@@ -4,6 +4,9 @@ namespace WorldOfPeacecraft
{
private int Score;
public delegate void LocationChangedEventHandler (int x, int y);
public event LocationChangedEventHandler LocationChanged;
public Player (int id, int posX, int posY, string desc, bool busy, int score) : base(id, posX, posY, desc, busy)
{
this.SetScore(score);
@@ -24,6 +27,16 @@ namespace WorldOfPeacecraft
return Desc;
}
public override void SetPos (int x, int y)
{
bool positionChanged = false;
if (x != GetX() || y != GetY ())
positionChanged = true;
base.SetPos (x, y);
if (LocationChanged != null)
LocationChanged(x, y);
}
public override string ToString()
{
return "Player: " + Id + " " + Coord.X + " " + Coord.Y + " " + Desc + " " + Busy + " " + Score;