Esempio n. 1
0
        internal static void InitializeTypes()
        {
            Constructors = new SortedList <string, ConstructorInfo>();
            var Instances = new SortedList <string, NetworkedEntity>();

            foreach (Type t in Assembly.GetEntryAssembly().GetTypes())
            {
                if (t.IsSubclassOf(typeof(NetworkedEntity)) && !t.IsAbstract)
                {
                    ConstructorInfo c = t.GetConstructor(Type.EmptyTypes);
                    if (c != null)
                    {
                        NetworkedEntity ent = c.Invoke(Type.EmptyTypes) as NetworkedEntity;
                        if (Constructors.ContainsKey(ent.NetworkedType))
                        {
                            Console.Error.WriteLine("Duplicate network type detected - {1} and {2} both use the same NetworkedType: {0}", ent.NetworkedType, c.DeclaringType.Name, Constructors[ent.NetworkedType].DeclaringType.Name);
                        }
                        else
                        {
                            Constructors.Add(ent.NetworkedType, c);
                            Instances.Add(ent.NetworkedType, ent);
                        }
                    }
                }
            }

            NetworkTableHash = GetNetworkTableHash(Instances.Values);
        }
Esempio n. 2
0
        protected virtual void Initialize()
        {
            NetworkedEntity.InitializeTypes();

#if NET_INFO
            NetInfo = new NetworkInfo();
#endif

            dt            = TickInterval = TimeSpan.FromSeconds(1.0 / 33);
            nextFrameTime = DateTime.Now;
            FrameTime     = lastFrameTime = DateTime.Now.Subtract(dt);
        }
Esempio n. 3
0
        private void ScheduleCreation(ushort entityID, string type, InboundMessage m, uint tick)
        {
            NetworkedEntity ent = NetworkedEntity.Create(type, entityID);

            if (ent == null)
            {
                Console.Error.WriteLine("Snapshot tried to create unrecognised entity type: " + type);
                return;
            }
            ent.ReadSnapshot(m, tick, false);
            Creations.Add(ent);
        }
Esempio n. 4
0
        internal static NetworkedEntity Create(string type, ushort entityID)
        {
            ConstructorInfo c;

            if (!Constructors.TryGetValue(type, out c))
            {
                return(null);
            }

            NetworkedEntity ent = c.Invoke(Type.EmptyTypes) as NetworkedEntity;

            ent.EntityID = entityID;
            NetworkedEntities[entityID] = ent;
            return(ent);
        }
Esempio n. 5
0
 private void Apply()
 {
     foreach (var id in Deletions)
     {
         NetworkedEntity ent = NetworkedEntity.NetworkedEntities[id];
         if (ent != null)
         {
             ent.Delete();
         }
     }
     foreach (var ent in Creations)
     {
         ent.Initialize();
     }
 }
Esempio n. 6
0
        protected internal virtual bool MessageReceived(InboundMessage m)
        {
            switch ((EngineMessage)m.Type)
            {
            case EngineMessage.InitialData:
            {
                byte[] hash = m.ReadBytes(16);
                if (!NetworkedEntity.CheckNetworkTableHash(hash))
                {
                    Console.Error.WriteLine("Network table doesn't match server's");
                    Disconnect();
                    return(true);
                }

                CurrentTick = m.ReadUInt() - (uint)(LerpDelay.TotalSeconds / TickInterval.TotalSeconds);

                string name = m.ReadString();
                Name.ForceValue(name);
                Console.WriteLine("My name, corrected by server: " + name);

                byte numOthers = m.ReadByte();

                Console.WriteLine("There {0} {1} other {2} connected to this server{3}",
                                  numOthers == 1 ? "is" : "are",
                                  numOthers == 0 ? "no" : numOthers.ToString(),
                                  numOthers == 1 ? "client" : "clients",
                                  numOthers == 0 ? string.Empty : ":"
                                  );

                for (int i = 0; i < numOthers; i++)
                {
                    string otherName = m.ReadString();
                    Console.WriteLine(" * " + otherName);
                }

                ushort num = m.ReadUShort();
                for (int i = 0; i < num; i++)
                {
                    Variable var = Variable.Get(m.ReadString());
                    string   val = m.ReadString();
                    if (var != null)
                    {
                        var.ForceValue(val);
                    }
                }

                nextFrameTime  = DateTime.Now;
                FullyConnected = true;
                return(true);
            }

            case EngineMessage.ClientConnected:
            {
                string clientName = m.ReadString();

                Console.WriteLine(clientName + " joined the game");
                return(true);
            }

            case EngineMessage.ClientNameChange:
            {
                string newName = m.ReadString();

                if (m.ReadBool())
                {        // it's me!
                    //this is a cheeky hack, to avoid having to add a new variable just to stop the callback sending it's message again
                    FullyConnected = false;
                    Name.ForceValue(newName);
                    FullyConnected = true;
                    Console.WriteLine("You changed your name to {0}", newName);
                }
                else
                {        // someone else
                    string oldName = m.ReadString();
                    Console.WriteLine("{0} changed name to {1}", oldName, newName);
                }
                return(true);
            }

            case EngineMessage.Snapshot:
            {
                Snapshot.Read(m);
                return(true);
            }

            case EngineMessage.VariableChange:
            {
                string name = m.ReadString();
                string val  = m.ReadString();

                Variable var = Variable.Get(name);
                if (var != null)
                {
                    var.ForceValue(val);
                }
                Variable.WriteChange(name, val);
                return(true);
            }

            default:
                return(false);
            }
        }