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

View File

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

View File

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

View File

@@ -4,6 +4,9 @@ namespace WorldOfPeacecraft
{ {
private int Score; 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) public Player (int id, int posX, int posY, string desc, bool busy, int score) : base(id, posX, posY, desc, busy)
{ {
this.SetScore(score); this.SetScore(score);
@@ -24,6 +27,16 @@ namespace WorldOfPeacecraft
return Desc; 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() public override string ToString()
{ {
return "Player: " + Id + " " + Coord.X + " " + Coord.Y + " " + Desc + " " + Busy + " " + Score; return "Player: " + Id + " " + Coord.X + " " + Coord.Y + " " + Desc + " " + Busy + " " + Score;