diff --git a/src/Backend.cs b/src/Backend.cs index cee9537..e07dc0f 100644 --- a/src/Backend.cs +++ b/src/Backend.cs @@ -1,6 +1,6 @@ using System.Net.Sockets; using System.Collections.Generic; -using WorldOfPeacecraft; +using System.Threading; namespace WorldOfPeacecraft { @@ -16,6 +16,7 @@ namespace WorldOfPeacecraft private Map Map; private Buffer SenderBuffer; private IGui Gui; + private int SelfId; public Backend (IGui gui) { @@ -30,9 +31,15 @@ namespace WorldOfPeacecraft Rec = new Receiver (Client, receiverBuffer); Send = new Sender (Client, SenderBuffer); SenderBuffer.AddLine ("get:map"); + SenderBuffer.AddLine ("get:me"); SenderBuffer.AddLine ("get:ents"); } + public void SetSelfId (int id) + { + this.SelfId = id; + } + public IEnumerable GetDragons () { return Dragons.Values; @@ -139,6 +146,22 @@ namespace WorldOfPeacecraft SenderBuffer.AddLine("ask:mv:rgt"); } + public void MoveTo (int x, int y) + { + new Thread(() => WalkTo(x,y)).Start(); + } + + private void WalkTo (int x, int y) + { + LinkedList 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(500); + } + } + public void RefreshGui() { Gui.PerformRefresh(); @@ -157,3 +180,4 @@ namespace WorldOfPeacecraft } } } + diff --git a/src/Gui/IBackend.cs b/src/Gui/IBackend.cs index 2cda33a..bde4612 100644 --- a/src/Gui/IBackend.cs +++ b/src/Gui/IBackend.cs @@ -17,10 +17,10 @@ namespace WorldOfPeacecraft void SendCommand (string command); void moveUp(); - void moveDown(); void moveLeft(); void moveRight(); + void MoveTo(int x, int y); void StartThreads(); diff --git a/src/Gui/MapPanel.cs b/src/Gui/MapPanel.cs index 5f7cd92..1c19755 100644 --- a/src/Gui/MapPanel.cs +++ b/src/Gui/MapPanel.cs @@ -50,7 +50,9 @@ namespace WorldOfPeacecraft protected override void OnMouseClick (MouseEventArgs e) { base.OnMouseClick (e); - //TODO: Things. + int targetX = e.X / TileSize; + int targetY = e.Y / TileSize; + Backend.MoveTo (targetX, targetY); } public void DoPaint (object source, PaintEventArgs args) diff --git a/src/Parser.cs b/src/Parser.cs index 5bf5f21..7d3bed9 100644 --- a/src/Parser.cs +++ b/src/Parser.cs @@ -119,7 +119,7 @@ namespace WorldOfPeacecraft ProcessChallenge (block); break; case MessPlayer: - ProcessPlayer (block); + ProcessPlayerSelf (block); break; case MessYourid: ProcessYourid (block); @@ -287,6 +287,15 @@ namespace WorldOfPeacecraft Challenge c = new Challenge(id, type, accepted); } + private void ProcessPlayerSelf (Block playerBlock) + { + Player self = MapPlayer (playerBlock); + lock (Backend) { + Backend.SetSelfId(self.GetId()); + Backend.SetPlayer(self); + } + Backend.RefreshGui (); + } private void ProcessPlayer (Block playerBlock) { diff --git a/src/Pathwalker.cs b/src/Pathwalker.cs index da694ca..501cfe5 100644 --- a/src/Pathwalker.cs +++ b/src/Pathwalker.cs @@ -13,6 +13,11 @@ namespace WorldOfPeacecraft Coords = null; } + public bool HasMoreSteps() + { + return Coords != null && Coords.Count >= 2; + } + public string NextStep () { if (Coords == null) { @@ -30,13 +35,13 @@ namespace WorldOfPeacecraft } string command; if (src.X > dst.X) { - command = "mv:dwn"; - } else if (src.X < dst.X) { - command = "mv:up"; - } else if (src.Y > dst.Y) { command = "mv:lft"; - } else if (src.Y < dst.Y) { + } else if (src.X < dst.X) { command = "mv:rgt"; + } else if (src.Y > dst.Y) { + command = "mv:up"; + } else if (src.Y < dst.Y) { + command = "mv:dwn"; } else { command = NextStep (); }