Ejemplo n.º 1
0
        public static bool SaveNPCInfoAsINI(string pathName, Level level)
        {
            bool result = false;

            if (level.Entities.Count > 0)
            {
                using (StreamWriter writer = new StreamWriter(pathName))
                {
                    int counter = 0;
                    foreach (Entity entity in level.Entities)
                    {
                        if (entity.Tag == LevelConstants.TagNPC)
                        {
                            counter++;
                            writer.WriteLine("[" + counter + "]");

                            writer.WriteLine(string.Format("name={0}", entity.ObjectName));
                        }
                    }
                    writer.Flush();
                    writer.Close();
                }
            }

            return result;
        }
Ejemplo n.º 2
0
        public static bool SaveLevelAsINI(string pathName, Level level)
        {
            bool result = false;

            if (level.Entities.Count > 0)
            {
                using (StreamWriter writer = new StreamWriter(pathName))
                {
                    // Save the Render settings
                    writer.WriteLine("[rendersettings]");
                    writer.WriteLine(string.Format("{0}={1}", "fogActive", level.Fog.active));
                    writer.WriteLine(string.Format("{0}={1},{2},{3},{4}", "fogColor", level.Fog.color.x, level.Fog.color.y, level.Fog.color.z, level.Fog.color.w));
                    writer.WriteLine(string.Format("{0}={1}", "fogDensity", level.Fog.density));
                    writer.WriteLine(string.Format("{0}={1}", "fogStartDistance", level.Fog.startDistance));
                    writer.WriteLine(string.Format("{0}={1}", "fogEndDistance", level.Fog.endDistance));
                    writer.WriteLine(string.Format("{0}=\"{1}\"", "fogMode", level.Fog.fogMode));
                    writer.WriteLine();

                    writer.WriteLine("[lightmaps]");
                    writer.WriteLine(string.Format("lightmapsCount={0}", level.Lightmap.lightmaps.Count));
                    writer.WriteLine(string.Format("lightmapsMode=\"{0}\"", level.Lightmap.lightmapsMode));
                    int counter = 0;
                    foreach (LightmapDataInfo lmData in level.Lightmap.lightmaps)
                    {
                        writer.WriteLine(string.Format("near_{0}=\"{1}\"", counter, lmData.nearLightmap));
                        writer.WriteLine(string.Format("far_{0}=\"{1}\"", counter++, lmData.farLightmap));
                    }
                    writer.WriteLine();

                    // Save the entities
                    counter = 0;
                    foreach (Entity entity in level.Entities)
                    {
                        counter ++;
                        writer.WriteLine("[" + counter + "]");
                        writer.WriteLine("objectName=\"" + entity.ObjectName + "\"");
                        writer.WriteLine(string.Format("position={0},{1},{2}", entity.Position.x, entity.Position.y, entity.Position.z));
                        writer.WriteLine(string.Format("rotation={0},{1},{2}", entity.Rotation.x, entity.Rotation.y, entity.Rotation.z));
                        writer.WriteLine(string.Format("tag=\"{0}\"", entity.Tag));
                        writer.WriteLine(string.Format("lightmapIndex={0}", entity.LightmapIndex));
                        writer.WriteLine(string.Format("lightmapTilingOffset={0},{1},{2},{3}", entity.LightmapTilingOffset.x, entity.LightmapTilingOffset.y, entity.LightmapTilingOffset.z, 
                            entity.LightmapTilingOffset.w));
                        writer.WriteLine();
                    }

                    writer.Flush();
                    writer.Close();
                }
            }

            return result;
        }
Ejemplo n.º 3
0
 public static void Serialize(string filepath, Level level)
 {
     using (var file = File.Create(filepath))
     {
         Serializer.Serialize(file, level);
         file.Flush();
     }
 }
Ejemplo n.º 4
0
        public static byte[] Serialize(Level level)
        {
            MemoryStream stream = new MemoryStream();

            Serializer.Serialize<Level>(stream, level);
                
            stream.Seek(0, SeekOrigin.Begin);

            return stream.ToArray();
        }
Ejemplo n.º 5
0
        public void SerializeLevel(string levelName, string userid, Level level, ref BsonDocument bsonDoc)
        {
            bsonDoc.Add("userid", userid);
            bsonDoc.Add("name", levelName);
            bsonDoc.Add("world_size_x", level.WorldSize[0].ToString());
            bsonDoc.Add("world_size_y", level.WorldSize[1].ToString());
            bsonDoc.Add("interest_area_x", level.InterestArea[0].ToString());
            bsonDoc.Add("interest_area_y", level.InterestArea[1].ToString());

            int index = 1;

            BsonDocument entities = new BsonDocument();

            foreach (Entity entity in level.Entities)
            {
                BsonDocument obj = new BsonDocument();

                obj.Add("objectName", entity.ObjectName);
                obj.Add("asset_file", entity.FilePath);
                obj.Add("position_x", entity.Position.x.ToString());
                obj.Add("position_y", entity.Position.y.ToString());
                obj.Add("position_z", entity.Position.z.ToString());
                obj.Add("rotation_x", entity.Rotation.x.ToString());
                obj.Add("rotation_y", entity.Rotation.y.ToString());
                obj.Add("rotation_z", entity.Rotation.z.ToString());
                obj.Add("scale_x", entity.Scale.x.ToString());
                obj.Add("scale_y", entity.Scale.y.ToString());
                obj.Add("scale_z", entity.Scale.z.ToString());

                obj.Add("tag", entity.Tag);

                entities.Add(string.Format("{0}", index++), obj);
            }

            bsonDoc.Add("assets", entities);
        }
Ejemplo n.º 6
0
        public void SavePrivateLevel(string levelName, string userid, Level level)
        {
            MongoServer server = MongoServer.Create(DbSettings.ConnectionString);

            MongoDatabase db = server.GetDatabase(DbConstants.DbNameGame, Credentials, new SafeMode(true));
            MongoCollection<BsonDocument> col = db.GetCollection(DbConstants.CollectionNameRooms);

            var query = Query.And(
                Query.EQ("userid", userid),
                Query.EQ("name", levelName));

            var doc = col.FindOne(query);

            if (doc == null)
            {
                doc = new BsonDocument();
            }

            SerializeLevel(levelName, userid, level, ref doc);

            col.Insert(doc);

            server.Disconnect();
        }
Ejemplo n.º 7
0
        public Level LoadPrivateLevel(string levelName, string userid)
        {
            Level level = null;

            MongoServer server = MongoServer.Create(DbSettings.ConnectionString);

            MongoDatabase db = server.GetDatabase(DbConstants.DbNameGame, Credentials, new SafeMode(true));
            MongoCollection<BsonDocument> col = db.GetCollection(DbConstants.CollectionNameRooms);

            var query = Query.And(
                Query.EQ("userid", userid),
                Query.EQ("name", levelName));

            var bsonDoc = col.FindOne(query);

            if (bsonDoc != null)
            {
                level = new Level();

                List<Entity> entities = GetEntities(bsonDoc);
                level.Entities = entities;

                level.WorldSize = new float[2] { float.Parse(bsonDoc["world_size_x"].AsString), float.Parse(bsonDoc["world_size_y"].AsString) };
                level.InterestArea = new float[2] { float.Parse(bsonDoc["interest_area_x"].AsString), float.Parse(bsonDoc["interest_area_y"].AsString) };
            }

            server.Disconnect();

            return level;
        }
Ejemplo n.º 8
0
        /// <summary>
        /// Get the level instance.
        /// </summary>
        /// <param name="levelName">The name of level.</param>
        /// <returns>Level instance.</returns>
        public Level GetLevel(string levelName)
        {
            var level = new Level();

            try
            {
                MongoServer server = MongoServer.Create(DbSettings.ConnectionString);
                var db = server.GetDatabase(DbConstants.DbNameAssets, Credentials, new SafeMode(true));
                MongoCollection<BsonDocument> mongColl = db.GetCollection(DbConstants.CollectionNameLevel);
                var query = new QueryDocument(DbConstants.LevelFieldName, levelName);
                var bsonDoc = mongColl.FindOne(query);

                level.Path = bsonDoc.Contains(DbConstants.LevelFieldAssetPath) ? bsonDoc[DbConstants.LevelFieldAssetPath].AsString : "";
                level.WorldSize = new float[2] { float.Parse(bsonDoc[DbConstants.LevelFieldWorldSizeX].AsString), float.Parse(bsonDoc[DbConstants.LevelFieldWorldSizeX].AsString) };
                level.InterestArea = new float[2] { float.Parse(bsonDoc[DbConstants.LevelFieldInterestAreaX].AsString), float.Parse(bsonDoc[DbConstants.LevelFieldInterestAreaY].AsString) };
                level.Entities = GetEntities(bsonDoc);

                level.Skybox = bsonDoc.Contains(DbConstants.LevelFieldSkybox) ? level.Skybox = bsonDoc[DbConstants.LevelFieldSkybox].AsString : "";
                level.Audio = bsonDoc.Contains(DbConstants.LevelFieldAudioFile) ? level.Audio = bsonDoc[DbConstants.LevelFieldAudioFile].AsString : "";

                // Load fog information
                level.Fog = new Fog();
                level.Fog.fogMode = bsonDoc.Contains(DbConstants.LevelFieldFogMode) ? bsonDoc[DbConstants.LevelFieldFogMode].AsString : "ExponentialSquared";
                level.Fog.active = bsonDoc.Contains(DbConstants.LevelFieldFogActive) ? bsonDoc[DbConstants.LevelFieldFogActive].AsBoolean : false;
                level.Fog.density = bsonDoc.Contains(DbConstants.LevelFieldFogDensity) ? (float)bsonDoc[DbConstants.LevelFieldFogDensity].AsDouble : 0.01f;
                level.Fog.startDistance = bsonDoc.Contains(DbConstants.LevelFieldFogStartDistance) ? (float)bsonDoc[DbConstants.LevelFieldFogStartDistance].AsDouble : 0f;
                level.Fog.endDistance = bsonDoc.Contains(DbConstants.LevelFieldFogEndDistance) ? (float)bsonDoc[DbConstants.LevelFieldFogEndDistance].AsDouble : 300f;
                float r = bsonDoc.Contains(DbConstants.LevelFieldFogColorR) ? r = (float)bsonDoc[DbConstants.LevelFieldFogColorR].AsDouble : 0.5f;
                float g = bsonDoc.Contains(DbConstants.LevelFieldFogColorG) ? g = (float)bsonDoc[DbConstants.LevelFieldFogColorG].AsDouble : 0.5f;
                float b = bsonDoc.Contains(DbConstants.LevelFieldFogColorB) ? b = (float)bsonDoc[DbConstants.LevelFieldFogColorB].AsDouble : 0.5f;
                float a = bsonDoc.Contains(DbConstants.LevelFieldFogColorA) ? a = (float)bsonDoc[DbConstants.LevelFieldFogColorA].AsDouble : 1f;
                level.Fog.color = new float4(r, g, b, a);

                BsonDocument lightmapsBson = bsonDoc.Contains("lightmaps") ? bsonDoc["lightmaps"].AsBsonDocument : null;

                level.Lightmap = GetLightmaps(lightmapsBson);

                server.Disconnect();
            }
            catch (Exception ex)
            {
                log.Error("DbManager.GetLevel(): " + ex.Message);
            }

            return level;
        }