//Start initial textBlock public TextBlock setTextBlockToMap(Prajurit prajurit) { /*Start add text to soldier*/ TextBlock textBlock = new TextBlock(); textBlock.Text = prajurit.nomerUrut + ""; textBlock.Tag = prajurit.nomerUrut; textBlock.Background = new SolidColorBrush(convertCharToColor(prajurit.group[0])); textBlock.Width = 30; textBlock.FontSize = 20; /*Position TextBlock*/ int quadrant = (int)prajurit.heading / 90; if (prajurit.heading == 0) { textBlock.Margin = new Thickness(-120, 0, 0, 0); //Menghadap kanan atas } else if (prajurit.heading == 1) { textBlock.Margin = new Thickness(-120, -40, 0, 0); //Menghadap kanan bawah } else if (prajurit.heading == 2) { textBlock.Margin = new Thickness(50, -20, 0, 0); //Menghadap kiri bawah } else if (prajurit.heading == 3) { textBlock.Margin = new Thickness(5, 50, 0, 0); //Menghadap kiri atas } /*End add text to soldier*/ textBlock.TextAlignment = TextAlignment.Center; return(textBlock); }
// Show accuracy in map private Ellipse createAccuracyCircle(Prajurit prajurit) { Ellipse ellipse = new Ellipse(); ellipse.Fill = new SolidColorBrush(Color.FromArgb(20, 255, 0, 0)); ellipse.Stroke = Brushes.Red; return(ellipse); }
private void updateIfExist(Prajurit prajurit) { MapLayer.SetPosition(prajurit.pushpin, prajurit.location); /* start update textblock and accuracy */ setPositionText(prajurit); MapLayer.SetPosition(prajurit.assignedText, prajurit.location); updateAccuracyCircle(prajurit); MapLayer.SetPosition(prajurit.assignedAccuracy, prajurit.location); /* end update textblock and accuracy */ }
private void updateToShow(Prajurit prajurit) { prajurit.pushpin.Visibility = Visibility.Visible; MapLayer.SetPosition(prajurit.pushpin, prajurit.location); prajurit.assignedText.Visibility = Visibility.Visible; MapLayer.SetPosition(prajurit.assignedText, prajurit.location); prajurit.assignedAccuracy.Visibility = Visibility.Visible; MapLayer.SetPosition(prajurit.assignedAccuracy, prajurit.location); //MapLayer.SetZIndex(prajurit.pushpin, -1000); }
// update accuracy in map private void updateAccuracyCircle(Prajurit prajurit) { double meterPerPixel = (Math.Cos(prajurit.location.Latitude * Math.PI / 180) * 2 * Math.PI * 6378137) / (256 * Math.Pow(2, map.ZoomLevel)); double pixelRadius = prajurit.accuracy / meterPerPixel; prajurit.assignedAccuracy.Height = 2 * pixelRadius; prajurit.assignedAccuracy.Width = 2 * pixelRadius; double left = -(int)pixelRadius; double top = -(int)pixelRadius; prajurit.assignedAccuracy.Margin = new Thickness(left, top, 0, 0); }
private void draw(Prajurit prajurit) { ToolTipService.SetToolTip(prajurit.pushpin, prajurit.nama); /* start add textblock to layer */ MapLayer.SetPosition(prajurit.assignedText, prajurit.location); map.Children.Add(prajurit.assignedText); /* end add textblock to layer */ /* start add accuracy to layer */ MapLayer.SetPosition(prajurit.assignedAccuracy, prajurit.location); map.Children.Add(prajurit.assignedAccuracy); /* end add accuracy to layer */ /* start add prajurit to layer */ MapLayer.SetPosition(prajurit.pushpin, prajurit.location); map.Children.Add(prajurit.pushpin); /* end add prajurit to layer */ }
public ControlTemplate setTemplateStatePosture(Prajurit prajurit) { ControlTemplate ct = new ControlTemplate(); switch (prajurit.posture) { case Prajurit.Posture.STAND: if (prajurit.state == Prajurit.State.NORMAL) { ct = (ControlTemplate)Application.Current.Resources["pushpinStand"]; } else if (prajurit.state == Prajurit.State.SHOOT) { ct = (ControlTemplate)Application.Current.Resources["pushpinStandShoot"]; } else if (prajurit.state == Prajurit.State.HIT) { ct = (ControlTemplate)Application.Current.Resources["pushpinStandHit"]; } else if (prajurit.state == Prajurit.State.DEAD) { ct = (ControlTemplate)Application.Current.Resources["pushpinStandDead"]; } break; case Prajurit.Posture.CRAWL: if (prajurit.state == Prajurit.State.NORMAL) { ct = (ControlTemplate)Application.Current.Resources["pushpinCrawl"]; } else if (prajurit.state == Prajurit.State.SHOOT) { ct = (ControlTemplate)Application.Current.Resources["pushpinCrawlShoot"]; } else if (prajurit.state == Prajurit.State.HIT) { ct = (ControlTemplate)Application.Current.Resources["pushpinCrawlHit"]; } else if (prajurit.state == Prajurit.State.DEAD) { ct = (ControlTemplate)Application.Current.Resources["pushpinCrawlDead"]; } break; } return(ct); }
public void updateMap(Prajurit prajurit) { map.Dispatcher.Invoke((Action)(() => { // Create a push pin if not yet done if (prajurit.pushpin == null && prajurit.location != null) { prajurit.pushpin = new Image(); /*Default prajurits and initialization*/ prajurit.pushpin.Source = new BitmapImage(new Uri("img/stand-hit.png", UriKind.Relative)); prajurit.pushpin.Height = 60; prajurit.pushpin.Width = 60; prajurit.pushpin.RenderTransformOrigin = new Point(0.5, 0.5); prajurit.pushpin.Margin = new Thickness(-prajurit.pushpin.Height / 2, -prajurit.pushpin.Width / 2, 0, 0); prajurit.assignedText = setTextBlockToMap(prajurit); prajurit.assignedAccuracy = createAccuracyCircle(prajurit); updateAccuracyCircle(prajurit); draw(prajurit); if (prajurit.group.Equals("A") && checkA.Equals(false)) { updateToHide(prajurit); } else if (prajurit.group.Equals("B") && checkA.Equals(false)) { updateToHide(prajurit); } } // Update and draw the push pin if available if (prajurit.pushpin != null) { // Note: Bing Maps fix to force update. Hopefully it would work String newImg = setPositionPrajurit(prajurit); prajurit.pushpin.Source = new BitmapImage(new Uri(newImg, UriKind.Relative)); prajurit.pushpin.RenderTransform = new RotateTransform(prajurit.heading % 360); updateIfExist(prajurit); } // Refresh map, if map is ready. if (map.ActualHeight > 0 && map.ActualWidth > 0) { map.SetView(map.BoundingRectangle); } })); }
//set image prajurit public String setPositionPrajurit(Prajurit prajurit) { String img = ""; switch (prajurit.posture) { case Prajurit.Posture.STAND: if (prajurit.state == Prajurit.State.NORMAL) { img = "img/stand.png"; } else if (prajurit.state == Prajurit.State.SHOOT) { img = "img/stand-shoot.png"; } else if (prajurit.state == Prajurit.State.HIT) { img = "img/stand-hit.png"; } else if (prajurit.state == Prajurit.State.DEAD) { img = "img/stand-dead.png"; } break; case Prajurit.Posture.CRAWL: if (prajurit.state == Prajurit.State.NORMAL) { img = "img/crawl.png"; } else if (prajurit.state == Prajurit.State.SHOOT) { img = "img/crawl-shoot.png"; } else if (prajurit.state == Prajurit.State.HIT) { img = "img/crawl-hit.png"; } else if (prajurit.state == Prajurit.State.DEAD) { img = "img/crawl-dead.png"; } break; } return(img); }
//update position textBlock public void setPositionText(Prajurit p) { int quadrant = (int)p.heading / 90; if (p.heading == 0) { p.assignedText.Margin = new Thickness(-120, 0, 0, 0); //Menghadap kanan atas } else if (p.heading == 1) { p.assignedText.Margin = new Thickness(-120, -40, 0, 0); //Menghadap kanan bawah } else if (p.heading == 2) { p.assignedText.Margin = new Thickness(50, -20, 0, 0); //Menghadap kiri bawah } else if (p.heading == 3) { p.assignedText.Margin = new Thickness(5, 50, 0, 0); //Menghadap kiri atas } p.assignedText.Background = new SolidColorBrush(convertCharToColor(p.group[0])); }
public virtual void handlePacket(IPAddress address, JSONPacket inPacket, bool updateUI) { try { this.recorder.record(address, inPacket.ToString()); String type = inPacket.getParameter("type"); if (type.Equals("register")) { if (inPacket.getParameter("gameid").Equals(gameId)) { if (state == State.REGISTRATION) { int nomerUrut = Prajurit.findPrajuritIndexByNomerInduk(prajurits, inPacket.getParameter("nomerInduk")); if (nomerUrut == -1) { // Register nomerUrut = prajurits.Count + 1; Prajurit newPrajurit = new Prajurit(nomerUrut, inPacket.getParameter("nomerInduk"), address, "A", null); prajuritDatabase.retrieveNameFromDatabase(newPrajurit); prajurits.Add(newPrajurit); if (updateUI) { parent.refreshTable(); } } else { nomerUrut = nomerUrut + 1; } // Confirm JSONPacket outPacket = new JSONPacket("confirm"); outPacket.setParameter("androidId", "" + nomerUrut); communication.send(address, outPacket); } else { parent.writeLog(LogLevel.Warn, "Registration from " + address + " is ignored as we are not in registration phase"); } } else { parent.writeLog(LogLevel.Warn, "Registration from " + address + " with game id " + inPacket.getParameter("gameid") + " is ignored"); } } else if (type.Equals("event/update")) { if (inPacket.getParameter("gameid").Equals(gameId)) { if (state != State.IDLE) { int index = Int32.Parse(inPacket.getParameter("androidId")) - 1; Prajurit prajurit = prajurits[index]; prajurit.setLocation(inPacket.getParameter("location")); prajurit.heading = Double.Parse(inPacket.getParameter("heading")); prajurit.accuracy = Int32.Parse(inPacket.getParameter("accuracy")); string[] prajuritState = inPacket.getParameter("state").Split('/'); prajurit.state = (prajuritState[0].Equals("alive") ? Prajurit.State.NORMAL : Prajurit.State.DEAD); prajurit.posture = (prajuritState[1].Equals("stand") ? Prajurit.Posture.STAND : Prajurit.Posture.CRAWL); if (inPacket.getParameter("action").Equals("hit")) { int idSenjata = Int32.Parse(inPacket.getParameter("idsenjata")); int counter = Int32.Parse(inPacket.getParameter("counter")); if (state == State.REGISTRATION) { // Registration phase, senjata assign to prajurit. Senjata newSenjata = new Senjata(idSenjata, prajurit, counter, this.initialAmmo); prajurit.senjata = newSenjata; senjatas.Add(newSenjata.idSenjata, newSenjata); } else if (state == State.EXERCISE) { Senjata senjataPenembak = senjatas[idSenjata]; senjataPenembak.currentCounter = counter; if (senjataPenembak.getRemainingAmmo() > 0) { prajurit.state = Prajurit.State.HIT; communication.send(prajurit.ipAddress, new JSONPacket("killed")); } } parent.refreshTable(); } else if (inPacket.getParameter("action").Equals("shoot")) { prajurit.state = Prajurit.State.SHOOT; } if (updateUI) { parent.mapDrawer.updateMap(prajurit); parent.refreshTable(); } } else { parent.writeLog(LogLevel.Warn, "Update event is ignored when state is idle."); } } else { parent.writeLog(LogLevel.Warn, "Event update from " + address + " with game id " + inPacket.getParameter("gameid") + " is ignored"); } } else if (type.Equals("ping")) { // Reply with pong, and let the rest of parameters be the same. inPacket.setParameter("type", "pong"); communication.send(address, inPacket); } else if (type.Equals("pantau/register")) { if (inPacket.getParameter("gameid").Equals(gameId)) { JSONPacket outPacket = new JSONPacket("pantau/confirm"); if (state == State.REGISTRATION && prajurits.Count == 0) { watchers.Add(address); outPacket.setParameter("status", "ok"); outPacket.setParameter("gameid", gameId); outPacket.setParameter("ammo", "" + initialAmmo); } else { outPacket.setParameter("status", "Tidak bisa pantau karena sudah ada prajurit yang bergabung atau latihan sudah dimulai."); } communication.send(address, outPacket, UDPCommunication.IN_PORT); parent.writeLog(LogLevel.Info, address + " joined as watcher"); } else { parent.writeLog(LogLevel.Warn, "Watch request from " + address + " is ignored, as the game id " + inPacket.getParameter("gameid") + " mismatched"); } } else if (type.Equals("pantau/state")) { if (inPacket.getParameter("state").StartsWith("START")) { string[] tokens = inPacket.getParameter("state").Split('/'); for (int i = 0; i < tokens[1].Length; i++) { prajurits[i].group = "" + tokens[1][i]; } startExercise(); } else if (inPacket.getParameter("state").Equals("STOP")) { stopExercise(false); } else { parent.writeLog(LogLevel.Error, "Unknown state set: " + inPacket.getParameter("state")); } } else { parent.writeLog(LogLevel.Error, "Unknown type: " + type); } } catch (Exception e) { parent.writeLog(LogLevel.Error, "Unhandled exception: " + e); } }