/// <summary> /// Initializes a new instance of the <see cref="Platform"/> class. /// </summary> /// <param name="description">The platform description.</param> /// <param name="physicsWorld">The physics world.</param> /// <param name="spriteBatch">The sprite batch to use for rendering.</param> /// <param name="contentManager">The game's content manager.</param> public Platform(PlatformDescription description, ref World physicsWorld, SpriteBatch spriteBatch, ContentManager contentManager) { this.spriteOffsets = new List<Vector2>(); this.sprite = new Sprite(); this.InitializeAndLoadSprites(spriteBatch, contentManager, description); this.SetUpPhysics(ref physicsWorld, description); }
/// <summary> /// Draws a platform to the screen. /// </summary> /// <param name="platformDescription">The platform description to use draw.</param> private void DrawPlatform(PlatformDescription platformDescription) { Camera2D.Draw(this.platformSprite, platformDescription.Position, 0.0f); if (platformDescription.Length > this.platformSprite.Width) { int count = 0; // Useable area on middle sprites (removing rounded ends) float offset = this.platformSprite.Width - this.platformSprite.Height; float halfLeftOver = (platformDescription.Length - offset) * 0.5f; // Leftover greater than useable area on end sprites while (halfLeftOver > this.platformSprite.Width - (this.platformSprite.Height / 2.0f)) { count++; Camera2D.Draw(this.platformSprite, new Vector2(platformDescription.Position.X + (offset * count), platformDescription.Position.Y), 0.0f); Camera2D.Draw(this.platformSprite, new Vector2(platformDescription.Position.X - (offset * count), platformDescription.Position.Y), 0.0f); halfLeftOver -= offset; } // Fill in ends if (halfLeftOver > 0.0f) { Camera2D.Draw(this.platformSprite, platformDescription.Position + new Vector2((platformDescription.Length / 2.0f) - (this.platformSprite.Width / 2.0f), 0.0f), 0.0f); Camera2D.Draw(this.platformSprite, platformDescription.Position + new Vector2(-(platformDescription.Length / 2.0f) + (this.platformSprite.Width / 2.0f), 0.0f), 0.0f); } } }
/// <summary> /// Initializes a new instance of the <see cref="Platform"/> class. /// </summary> /// <param name="description">The platform description.</param> /// <param name="physicsWorld">The physics world.</param> /// <param name="spriteBatch">The sprite batch to use for rendering.</param> /// <param name="contentManager">The game's content manager.</param> public Platform(PlatformDescription description, ref World physicsWorld, SpriteBatch spriteBatch, ContentManager contentManager) { this.spriteOffsets = new List <Vector2>(); this.sprite = new Sprite(); this.InitializeAndLoadSprites(spriteBatch, contentManager, description); this.SetUpPhysics(ref physicsWorld, description); }
/// <summary> /// Adds a platform to the level. /// </summary> /// <param name="length">The length of the platform.</param> /// <param name="position">The position of the platform.</param> public void AddPlatform(float length, Vector2 position) { PlatformDescription platformDescription = new PlatformDescription(); platformDescription.Length = length; platformDescription.Position = position; this.platformDescriptions.Add(platformDescription); }
/* * private void AddPlatform(IPAddress ip, short port, bool autofocus) * { * if (ip.Equals(IPAddress.None) || ip.Equals(IPAddress.Any) || ip.Equals(IPAddress.Loopback)) * return; * * Task.Run(() => * { * PingReply reply = new Ping().Send(ip, 16); * * if (reply.Status == IPStatus.Success) * { * String name = reply.Address.ToString(); * * try * { * IPHostEntry entry = Dns.GetHostEntry(reply.Address); * if (entry != null) * name = entry.HostName; * } * catch (SocketException) { } * * Application.Current.Dispatcher.BeginInvoke(new Action(() => * { * var newPlatform = new PlatformDescription() { Name = name, IP = reply.Address, Port = port, Icon = GetIconByComputerName(name) }; * * bool needAdd = true; * * foreach (PlatformDescription platform in platforms) * { * if (platform.IP.Equals(reply.Address) && platform.Port == port) * { * newPlatform = platform; * needAdd = false; * break; * } * } * * if (needAdd) * platforms.Add(newPlatform); * * if (autofocus) * comboBox.SelectedItem = newPlatform; * })); * } * }); * } * * * private void ScanNetworkForCompatibleDevices(IPAddress startAddress) * { * byte[] address = startAddress.GetAddressBytes(); * byte originalIndex = address[address.Length-1]; * for (byte i = 0; i < 255; ++i) * { * if (i != originalIndex) * { * address[address.Length-1] = i; * IPAddress ip = new IPAddress(address); * * AddPlatform(new IPAddress(address), PlatformDescription.DEFAULT_PORT, false); * } * } * } */ private void comboBox_SelectionChanged(object sender, SelectionChangedEventArgs e) { PlatformDescription desc = comboBox.SelectedItem as PlatformDescription; if (desc != null) { IP.Text = desc.IP.ToString(); Port.Text = desc.Port.ToString(); DetailedIP.Visibility = desc != null && desc.Detailed ? Visibility.Visible : Visibility.Collapsed; } }
private void Port_TextChanged(object sender, TextChangedEventArgs e) { PlatformDescription desc = comboBox.SelectedItem as PlatformDescription; if (desc != null) { short port = 0; if (short.TryParse(Port.Text, out port)) { desc.Port = port; } } }
private void IP_TextChanged(object sender, TextChangedEventArgs e) { PlatformDescription desc = comboBox.SelectedItem as PlatformDescription; if (desc != null) { IPAddress address = null; if (IPAddress.TryParse(IP.Text, out address)) { desc.IP = address; } } }
/// <summary> /// Initializes and loads the textures for the sprites in a platform object. /// </summary> /// <param name="spriteBatch">The sprite batch to use for rendering the sprites.</param> /// <param name="contentManager">The content manager to use for loading the sprites.</param> /// <param name="description">The description of the platform object.</param> private void InitializeAndLoadSprites(SpriteBatch spriteBatch, ContentManager contentManager, PlatformDescription description) { this.sprite.InitializeAndLoad(spriteBatch, contentManager, EntityConstants.SpritesFolderPath + EntityConstants.Platform); this.spriteOffsets.Add(Vector2.Zero); if (description.Length > this.sprite.Width) { int count = 0; // Useable area on middle sprites (removing rounded ends) float offset = this.sprite.Width - this.sprite.Height; float halfLeftOver = (description.Length - offset) * 0.5f; // Leftover greater than useable area on end sprites while (halfLeftOver > this.sprite.Width - (this.sprite.Height / 2.0f)) { count++; this.spriteOffsets.Add(new Vector2(offset * count, 0.0f)); this.spriteOffsets.Add(new Vector2(-offset * count, 0.0f)); halfLeftOver -= offset; } // Fill in ends if (halfLeftOver > 0.0f) { this.spriteOffsets.Add(new Vector2((description.Length / 2.0f) - (this.sprite.Width / 2.0f), 0.0f)); this.spriteOffsets.Add(new Vector2(-(description.Length / 2.0f) + (this.sprite.Width / 2.0f), 0.0f)); } } }
/// <summary> /// Sets up the physics body for a platform. /// </summary> /// <param name="physicsWorld">The physics world to create the body in.</param> /// <param name="description">The description of the platform.</param> private void SetUpPhysics(ref World physicsWorld, PlatformDescription description) { this.body = BodyFactory.CreateRoundedRectangle(physicsWorld, ConvertUnits.ToSimUnits(description.Length), ConvertUnits.ToSimUnits(this.sprite.Height), ConvertUnits.ToSimUnits(this.sprite.Height / 2.0f), ConvertUnits.ToSimUnits(this.sprite.Height / 2.0f), 8, 3.0f, ConvertUnits.ToSimUnits(description.Position)); this.body.CollisionCategories = EntityConstants.PlatformCategory; }