/// <summary>
 /// Generate a new local NetworkEntity at this host.
 /// </summary>
 public NetworkEntity()
 {
     nm = LKernel.Get<NetworkManager>();
     local = true;
     owner = null;
     _GlobalID = nm.AssignGlobalID();
     _Selection = "Twilight Sparkle";
     _Name = String.Format("Ponefag{0}", _GlobalID);
 }
 /// <summary>
 /// Generate a new remote NetworkEntity at this host.
 /// </summary>
 public NetworkEntity(Connection parent)
 {
     nm = LKernel.Get<NetworkManager>();
     local = false;
     owner = parent;
     _GlobalID = nm.AssignGlobalID();
     _Name = String.Format("Ponefag{0}", _GlobalID);
     _Selection = "Twilight Sparkle";
 }
 /// <summary>
 /// Create a new NetworkEntity that is fully designated by a remote host.
 /// </summary>
 public NetworkEntity(Connection parent, int globalid, string name, string selection, bool islocal)
 {
     nm = LKernel.Get<NetworkManager>();
     if (parent != null) { owner = parent; }
     local = islocal;
     _GlobalID = globalid;
     _Selection = selection;
     _Name = name;
 }
 public PonykartPacket(Commands type, byte[] contents, Connection c, bool isVolatile)
 {
     // Outgoing
     Contents = contents;
     Timestamp = System.DateTime.Now.Ticks;
     _Type = (Int32)type;
     Owner = c;
     Source = PacketSource.Local;
     Volatile = isVolatile;
 }
 public PonykartPacket(Commands type, string contents, Connection c, bool isVolatile)
 {
     // Outgoing
     Contents = System.Text.ASCIIEncoding.ASCII.GetBytes(contents);
     Timestamp = System.DateTime.Now.Ticks;
     _Type = (Int32)type;
     Owner = c;
     Source = PacketSource.Local;
     Volatile = isVolatile;
 }
 public PonykartPacket ToPKPacket(Connection owner)
 {
     return new PonykartPacket(Type, Contents, owner, Volatile);
 }
 public void SetOrigin(Connection receiver)
 {
     Owner = receiver;
 }
        /// <summary>
        /// Changes this entity's properties according to the given xml schema
        /// </summary>
        public static bool PerformChange(string contents, Connection sender)
        {
            var AsXML = XElement.Parse(contents);

            var Property = AsXML.Attribute("Property").Value;
            var Value = AsXML.Attribute("Value").Value;
            var ID = int.Parse(AsXML.Attribute("Id").Value);

            NetworkEntity target = LKernel.Get<NetworkManager>().Players.Find((e) => e.GlobalID == ID);
            if (target == null) { return false; }
            if (LKernel.Get<NetworkManager>().NetworkType == NetworkTypes.Host &&  target.owner != sender) { return false; }
            switch (Property) {
                case "Selection":
                    target._Selection = Value;
                    break;
                case "Name":
                    target._Name = Value;
                    break;
                default:
                    return false;
            }
            return true;
        }
        /// <summary>
        /// Perform the given location change if valid.
        /// </summary>
        public static void DeserializeLocations(string contents, Connection sender)
        {
            var AsXML = XElement.Parse(contents);

            // anonymous types!
            var Karts = (from x in AsXML.Elements("Kart")
                         select new {
                             IDStr = x.Attribute("Id").Value,
                             PositionStr = x.Attribute("Pos").Value,
                             SpeedStr = x.Attribute("Vel").Value,
                             OrientationStr = x.Attribute("Or").Value
                         }).ToDictionary((a) => Int32.Parse(a.IDStr));
            foreach (NetworkEntity ne in LKernel.Get<NetworkManager>().Players) {
                if (Karts.ContainsKey(ne.GlobalID) && !ne.local) {
                    if (ne.owner == sender || ne.nm.NetworkType == NetworkTypes.Client) {
                        try {
                            var Kart = Karts[ne.GlobalID];
                            var PosList = Kart.PositionStr.Split(' ').Select((s) => float.Parse(s)).ToList();
                            var SpeedList = Kart.SpeedStr.Split(' ').Select((s) => float.Parse(s)).ToList();
                            var OrList = Kart.OrientationStr.Split(' ').Select((s) => float.Parse(s)).ToList();
                            var Pos = new Mogre.Vector3(PosList[0], PosList[1], PosList[2]);
                            var Speed = new Mogre.Vector3(SpeedList[0], SpeedList[1], SpeedList[2]);
                            var Or = new Mogre.Quaternion(OrList[0], OrList[1], OrList[2], OrList[3]);
                            ne.player.Kart.SetState(Pos, Speed, Or);
                        } catch (Exception e) { }
                    }
                }
            }
            return;
        }
        /// <summary>
        /// Turn a string representing a player into a NetworkEntity
        /// </summary>
        public static NetworkEntity Deserialize(string contents, Connection parent, bool local)
        {
            var AsXML = XElement.Parse(contents);

            var Name = AsXML.Attribute("Name").Value;
            var ID = int.Parse(AsXML.Attribute("Id").Value);
            var Selection = AsXML.Attribute("Selection").Value;

            return new NetworkEntity(parent, ID, Name, Selection, local);
        }