示例#1
0
        public Game(String userId, String username, Config config, Legend legend)
        {
            cachedEntities = new HashSet <Entity>();
            this.config    = config;
            active         = true;
            this.userId    = userId;
            this.username  = username;
            this.legend    = legend;

            BsonDocument userData = legend.GetUserData(this.userId);

            if (userData == null)
            {
                userData = new BsonDocument(config.defaultUser);
                userData.Set("user", this.userId);
                legend.InsertUserData(userData);
            }

            Inventory inventory = new Inventory();

            inventory.ItemAddedToInventory += new Inventory.ItemAddedToInventoryHandler(ItemAddedToInventory);
            inventory.ItemModified         += new Inventory.ItemModifiedHandler(ItemModified);

            foreach (BsonValue itemValue in userData["inventory"].AsBsonArray)
            {
                Item item = LegendDB.DecodeItem(itemValue.AsBsonDocument, config.baseItems);
                inventory.AddItem(item, false);
            }

            Dictionary <string, Flag> flags = new Dictionary <string, Flag>();

            foreach (var flagPair in userData["flags"].AsBsonDocument)
            {
                string flagKey   = flagPair.Name;
                Flag   flagValue = LegendDB.DecodeFlag(flagPair.Value);
                flags[flagKey] = flagValue;
            }

            player = new Player(
                userData.GetValue("sprite").ToString(),
                userData.GetValue("pos_x").ToInt32(),
                userData.GetValue("pos_y").ToInt32(),
                userData.GetValue("inventory_size").ToInt32(),
                inventory,
                flags,
                legend,
                this
                );

            inventory.guid = player.uuid;

            foreach (var item in player.inventory.items)
            {
                Console.WriteLine("{0}: [{1} / {2}] \"{3}\" x{4} ", item.GetName(), item.GetSprite(), item.GetItemType(), item.GetDescription(), item.GetQuantity());
            }
        }
示例#2
0
        public void LoadConfig()
        {
            /*
             * GET Config & locations of other files
             * */
            JObject configJSON         = JObject.Parse(File.ReadAllText(@"config/config.json"));  //----
            String  worldLocation      = "config/" + configJSON.GetValue("world_map").ToString(); //----
            String  bumpLocation       = "config/" + configJSON.GetValue("bump_map").ToString();
            String  portalsLocation    = "config/" + configJSON.GetValue("portals").ToString();   //----
            String  entitiesLocation   = "config/" + configJSON.GetValue("entities").ToString();
            String  dialogueLocation   = "config/" + configJSON.GetValue("dialogue").ToString();
            String  itemsLocation      = "config/" + configJSON.GetValue("items").ToString(); //----
            String  encountersLocation = "config/" + configJSON.GetValue("encounters").ToString();
            String  enemiesLocation    = "config/" + configJSON.GetValue("enemies").ToString();
            String  tilesLocation      = "config/" + configJSON.GetValue("tiles").ToString(); //----

            /*
             * Build config values into a config object
             * */

            BsonDocument defaultUser     = BsonDocument.Parse(configJSON.GetValue("default_user").ToString());
            IPAddress    ip              = IPAddress.Parse(configJSON.GetValue("ip").ToString());
            int          port            = configJSON.GetValue("port").ToObject <int>();
            int          chatRadius      = configJSON.GetValue("chat_radius").ToObject <int>();
            int          entityDistanceX = configJSON.GetValue("entity_distance_x").ToObject <int>();
            int          entityDistanceY = configJSON.GetValue("entity_distance_y").ToObject <int>();
            int          tickRate        = configJSON.GetValue("tick_rate").ToObject <int>();
            int          interactRange   = configJSON.GetValue("interact_range").ToObject <int>();
            float        tickFrequency   = 1000.0f / tickRate;

            timer.Interval = tickFrequency;

            /*
             * Load base items
             * */

            JObject itemsJSON = JObject.Parse(File.ReadAllText(itemsLocation));

            Dictionary <String, BaseItem> baseItems = new Dictionary <String, BaseItem>();

            foreach (var itemPair in itemsJSON)
            {
                string   itemId    = itemPair.Key;
                JToken   itemToken = itemPair.Value;
                BaseItem item      = LegendDB.DecodeBaseItem(BsonDocument.Parse(itemToken.ToString()), itemId);
                baseItems[itemId] = item;
            }

            Dictionary <String, Dialogue> dialogue = new Dictionary <String, Dialogue>();


            config = new Config()
            {
                defaultUser     = defaultUser,
                ip              = ip,
                port            = port,
                chatRadius      = chatRadius,
                entityDistanceX = entityDistanceX,
                entityDistanceY = entityDistanceY,
                interactRange   = interactRange,
                tickRate        = tickRate,
                baseItems       = baseItems,
                dialogue        = dialogue
            };

            /*
             * Connect to database
             */
            String mongoServer   = configJSON.GetValue("db_server").ToString();
            int    mongoPort     = configJSON.GetValue("db_port").ToObject <int>();
            String mongoUser     = configJSON.GetValue("db_user").ToString();
            String mongoPassword = configJSON.GetValue("db_password").ToString();
            String mongoDatabase = configJSON.GetValue("db_database").ToString();

            MongoClientSettings mongoSettings = new MongoClientSettings
            {
                Server     = new MongoServerAddress(mongoServer, mongoPort),
                Credential = MongoCredential.CreateCredential(mongoDatabase, mongoUser, mongoPassword)
            };

            mongoClient    = new MongoClient(mongoSettings);
            db             = mongoClient.GetDatabase(mongoDatabase);
            userCollection = db.GetCollection <BsonDocument>("users");

            /*
             * Get Portals
             * */
            Dictionary <Position, Position> portals = new Dictionary <Position, Position>();

            JArray portalsJArray = JArray.Parse(File.ReadAllText(portalsLocation));

            foreach (var portalToken in portalsJArray)
            {
                JObject  portal = portalToken.ToObject <JObject>();
                var      fromX  = portal.GetValue("pos_x").ToObject <int>();
                var      fromY  = portal.GetValue("pos_y").ToObject <int>();
                var      toX    = portal.GetValue("to_x").ToObject <int>();
                var      toY    = portal.GetValue("to_y").ToObject <int>();
                Position from   = new Position(fromX, fromY);
                Position to     = new Position(toX, toY);
                portals[from] = to;
            }

            /*
             * Get Tiles
             * */
            JObject tilesJSON = JObject.Parse(File.ReadAllText(tilesLocation));

            List <JToken>           tilesList    = tilesJSON.GetValue("tiles").ToObject <List <JToken> >();
            Dictionary <Color, int> colorMap     = new Dictionary <Color, int>();
            Dictionary <Color, int> bumpColorMap = new Dictionary <Color, int>
            {
                [Color.FromArgb(0, 0, 0)]       = 0,
                [Color.FromArgb(255, 255, 255)] = 1,
                [Color.FromArgb(255, 0, 0)]     = 2
            };

            for (var i = 0; i < tilesList.Count; i++)
            {
                var   tileDocument = tilesList[i];
                Color color        = System.Drawing.ColorTranslator.FromHtml(tileDocument.ToObject <JObject>().GetValue("color").ToString());
                colorMap[color] = i;
            }


            /*
             * Get world
             * */

            Bitmap worldImage  = new Bitmap(worldLocation);
            Bitmap bumpImage   = new Bitmap(bumpLocation);
            int    worldHeight = worldImage.Height;
            int    worldWidth  = worldImage.Width;

            int[,] worldData = new int[worldHeight, worldWidth];
            int[,] bumpData  = new int[worldHeight, worldWidth];

            for (int y = 0; y < worldHeight; y++)
            {
                for (int x = 0; x < worldWidth; x++)
                {
                    Color cell     = worldImage.GetPixel(x, y);
                    int   colIndex = 0;
                    if (colorMap.ContainsKey(cell))
                    {
                        colIndex = colorMap[cell];
                    }
                    worldData[y, x] = colIndex;

                    Color bumpCell     = bumpImage.GetPixel(x, y);
                    int   bumpColIndex = 0;
                    if (bumpColorMap.ContainsKey(bumpCell))
                    {
                        bumpColIndex = bumpColorMap[bumpCell];
                    }
                    bumpData[y, x] = bumpColIndex;
                }
            }
            world = new World(worldData, bumpData, worldHeight, worldWidth, portals);

            /*
             * Load Entities
             * */

            JArray entitiesJArray = JArray.Parse(File.ReadAllText(entitiesLocation));

            foreach (var entityToken in entitiesJArray)
            {
                JObject entityObject = entityToken.ToObject <JObject>();
                int     posX         = entityObject.GetValue("pos_x").ToObject <int>();
                int     posY         = entityObject.GetValue("pos_y").ToObject <int>();
                int     facing       = entityObject.GetValue("facing").ToObject <int>();
                string  sprite       = entityObject.GetValue("sprite").ToString();
                string  entityType   = entityObject.GetValue("type").ToString();
                if (entityType == "npc")
                {
                    string dialogueKey = entityObject.GetValue("dialogue").ToString();
                    NPC    npc         = new NPC(sprite, posX, posY, (FACING)facing, dialogueKey, this);
                }
            }

            /*
             * Load Dialogue
             * */

            JObject dialogueJson = JObject.Parse(File.ReadAllText(dialogueLocation));

            foreach (var dialoguePair in dialogueJson)
            {
                string   dialogueKey    = dialoguePair.Key;
                JToken   dialogueToken  = dialoguePair.Value;
                Dialogue singleDialogue = LegendDB.DecodeDialogue(BsonDocument.Parse(dialogueToken.ToString()), config);
                dialogue[dialogueKey] = singleDialogue;
            }
        }