예제 #1
0
        /// <summary>
        /// Erzeugt eine neue Instanz der Klasse Simulation.
        /// </summary>
        public Simulation(IResourceManager resourceManager, IExtensionResolver extensionResolver, IGameService service)
        {
            ResourceManager        = resourceManager;
            simulationSubscription = resourceManager.UpdateHub.Subscribe(this, DefaultChannels.Simulation);
            entityNotificationPool = TypeContainer.Get <IPool <EntityNotification> >();


            this.extensionResolver = extensionResolver;
            State      = SimulationState.Ready;
            UniverseId = Guid.Empty;
            Service    = service;

            Components = new ComponentList <SimulationComponent>(
                ValidateAddComponent, ValidateRemoveComponent, null, null);

            extensionResolver.ExtendSimulation(this);
        }
예제 #2
0
        /// <summary>
        /// Instanziert einen neuen local Chunk Cache.
        /// </summary>
        /// <param name="globalCache">Referenz auf global Chunk Cache</param>
        /// <param name="dimensions">Größe des Caches in Zweierpotenzen</param>
        /// <param name="range">Gibt die Range in alle Richtungen an.</param>
        public LocalChunkCache(IGlobalChunkCache globalCache, int dimensions, int range)
        {
            if (1 << dimensions < (range * 2) + 1)
                throw new ArgumentException("Range too big");

            
            semaphore = new LockSemaphore(1, 1);
            taskSemaphore = new LockSemaphore(1, 1);
            Planet = globalCache.Planet;
            this.globalCache = globalCache;
            this.range = range;

            limit = dimensions;
            mask = (1 << limit) - 1;
            chunkColumns = new IChunkColumn[(mask + 1) * (mask + 1)];
            logger = (TypeContainer.GetOrNull<ILogger>() ?? NullLogger.Default).As(typeof(LocalChunkCache));
        }
예제 #3
0
        /// <summary>
        /// Deserialisiert die Entität aus dem angegebenen BinaryReader.
        /// </summary>
        /// <param name="reader">Der BinaryWriter, mit dem gelesen wird.</param>
        public virtual void Deserialize(BinaryReader reader)
        {
            var count = reader.ReadInt32();

            for (int i = 0; i < count; i++)
            {
                var name = reader.ReadString();

                var type = Type.GetType(name);

                T component;

                if (!components.TryGetValue(type, out component))
                {
                    component = (T)TypeContainer.GetUnregistered(type);
                    //components.Add(type, component);
                    AddComponent(component);
                }

                component.Deserialize(reader);
            }
        }
예제 #4
0
 /// <summary>
 /// Erzeugt eine neue Player-Instanz an der Default-Position.
 /// </summary>
 public Player() : base()
 {
     entityNotificationPool = TypeContainer.Get <IPool <EntityNotification> >();
 }
예제 #5
0
        /// <summary>
        /// Deserialisiert die Chunksäule aus dem angegebenen Stream.
        /// </summary>
        /// <param name="stream">Quellstream</param>
        /// <param name="definitionManager">Der verwendete DefinitionManager</param>
        /// <param name="columnIndex">Die Position der Säule</param>
        /// <param name="planetId">Der Index des Planeten</param>
        public void Deserialize(BinaryReader reader)
        {
            var longIndex = reader.ReadByte() > 0;

            // Phase 1 (Column Meta: Heightmap, populated, chunkcount)
            Chunks = new Chunk[reader.ReadByte()]; // Chunk Count

            Populated = reader.ReadBoolean();      // Populated

            Index = new Index2(reader.ReadInt32(), reader.ReadInt32());
            int planetId = reader.ReadInt32();

            var resManager = TypeContainer.Get <IResourceManager>();

            Planet = resManager.GetPlanet(planetId);

            for (var y = 0; y < Chunk.CHUNKSIZE_Y; y++) // Heightmap
            {
                for (var x = 0; x < Chunk.CHUNKSIZE_X; x++)
                {
                    Heights[x, y] = reader.ReadUInt16();
                }
            }


            // Phase 2 (Block Definitionen)
            var types = new List <IDefinition>();
            var map   = new Dictionary <ushort, ushort>();

            int typecount = longIndex ? reader.ReadUInt16() : reader.ReadByte();

            for (var i = 0; i < typecount; i++)
            {
                var           typeName        = reader.ReadString();
                IDefinition[] definitions     = DefinitionManager.GetDefinitions().ToArray();
                IDefinition   blockDefinition = definitions.FirstOrDefault(d => d.GetType().FullName == typeName);
                types.Add(blockDefinition);

                map.Add((ushort)types.Count, (ushort)(Array.IndexOf(definitions, blockDefinition) + 1));
            }

            // Phase 3 (Chunk Infos)
            for (var c = 0; c < Chunks.Length; c++)
            {
                IChunk chunk = Chunks[c] = new Chunk(new Index3(Index, c), Planet);
                chunk.Changed += OnChunkChanged;
                chunk.SetColumn(this);

                for (var i = 0; i < chunk.Blocks.Length; i++)
                {
                    var typeIndex = longIndex ? reader.ReadUInt16() : reader.ReadByte();
                    chunk.MetaData[i] = 0;
                    if (typeIndex > 0)
                    {
                        chunk.Blocks[i] = map[typeIndex];

                        var definition = (IBlockDefinition)DefinitionManager.GetDefinitionByIndex(map[typeIndex]);

                        if (definition.HasMetaData)
                        {
                            chunk.MetaData[i] = reader.ReadInt32();
                        }
                    }
                }
            }
        }
예제 #6
0
        /// <summary>
        /// Serialisiert die Chunksäule in den angegebenen Stream.
        /// </summary>
        /// <param name="writer">Zielschreiber</param>
        /// <param name="definitionManager">Der verwendete DefinitionManager</param>
        public void Serialize(BinaryWriter writer)
        {
            // Definitionen sammeln
            var definitions = new List <IBlockDefinition>();

            for (var c = 0; c < Chunks.Length; c++)
            {
                IChunk chunk = Chunks[c];
                for (var i = 0; i < chunk.Blocks.Length; i++)
                {
                    if (chunk.Blocks[i] != 0)
                    {
                        var definition = (IBlockDefinition)DefinitionManager.GetDefinitionByIndex(chunk.Blocks[i]);
                        if (!definitions.Contains(definition))
                        {
                            definitions.Add(definition);
                        }
                    }
                }
            }

            var longIndex = definitions.Count > 254;

            writer.Write((byte)((longIndex) ? 1 : 0));

            // Schreibe Phase 1 (Column Meta: Heightmap, populated, chunkcount)
            writer.Write((byte)Chunks.Length); // Chunk Count
            writer.Write(Populated);           // Populated
            writer.Write(Index.X);
            writer.Write(Index.Y);
            writer.Write(Planet.Id);

            for (var y = 0; y < Chunk.CHUNKSIZE_Y; y++) // Heightmap
            {
                for (var x = 0; x < Chunk.CHUNKSIZE_X; x++)
                {
                    writer.Write((ushort)Heights[x, y]);
                }
            }

            // Schreibe Phase 2 (Block Definitionen)
            if (longIndex)
            {
                writer.Write((ushort)definitions.Count);
            }
            else
            {
                writer.Write((byte)definitions.Count);
            }

            foreach (IBlockDefinition definition in definitions)
            {
                writer.Write(definition.GetType().FullName);
            }

            // Schreibe Phase 3 (Chunk Infos)
            for (var c = 0; c < Chunks.Length; c++)
            {
                IChunk chunk = Chunks[c];
                for (var i = 0; i < chunk.Blocks.Length; i++)
                {
                    if (chunk.Blocks[i] == 0)
                    {
                        // Definition Index (Air)
                        if (longIndex)
                        {
                            writer.Write((ushort)0);
                        }
                        else
                        {
                            writer.Write((byte)0);
                        }
                    }
                    else
                    {
                        // Definition Index
                        var definition = (IBlockDefinition)DefinitionManager.GetDefinitionByIndex(chunk.Blocks[i]);

                        if (longIndex)
                        {
                            writer.Write((ushort)(definitions.IndexOf(definition) + 1));
                        }
                        else
                        {
                            writer.Write((byte)(definitions.IndexOf(definition) + 1));
                        }

                        // Meta Data
                        if (definition.HasMetaData)
                        {
                            writer.Write(chunk.MetaData[i]);
                        }
                    }
                }
            }
            var resManager = TypeContainer.Get <IResourceManager>();

            using (var lockObj = entitieSemaphore.Wait())
            {
                foreach (var entity in entities)
                {
                    resManager.SaveEntity(entity);
                }
            }
        }
예제 #7
0
 public EntityList(IChunkColumn column)
 {
     entities        = new List <Entity>();
     this.column     = column;
     resourceManager = TypeContainer.Get <IResourceManager>();
 }