public void BuildMap() { if (sourceImage != null) { IDictionary<string, Model.Terrain> terrainDictionary = new Dictionary<string, Model.Terrain>() { { "sea", new Model.Terrain("Sea", 0) }, { "plain", new Model.Terrain("Plain", 1) }, { "mountain", new Model.Terrain("Moutain", 3) }, }; IList<MapTile> tileCollection = sourceImage.GetPixels32().Select(color => { if (color == Color.blue) return new MapTile(terrainDictionary["sea"]); if (color == Color.green) return new MapTile(terrainDictionary["plain"]); if (color.Equals((Color32)Color.gray)) return new MapTile(terrainDictionary["mountain"]); return new MapTile(new Model.Terrain("NULL", 0)); }).ToList(); map = new Model.Map(sourceImage.width, sourceImage.height, tileCollection); } else { IList<MapTile> tileCollection = new List<MapTile>(100 * 100); for (int i = 0; i < 100 * 100; i++) tileCollection.Add(new MapTile(new Model.Terrain("NULL", 0))); map = new Model.Map(100, 100, tileCollection); } BuildMesh(); }
public static void loadMap(ContentManager content, string mapName, out Model.Map map) { LoadResult loadResult = loader.load(content, mapName); int height = loadResult.Height; int width = loadResult.Width; int wallLayerLocation = 0; // generate walls Layer wallLayer = loadResult.Layers[wallLayerLocation]; MapTile[,] layerTiles = wallLayer.Tiles; List <Wall> walls = new List <Wall>(); BasePathFinder.TypeOfSpace[,] aiSpaceTypes = new BasePathFinder.TypeOfSpace[height, width]; foreach (var mapTile in layerTiles) { if (mapTile != null) { Vector2 newPosition = Vector2.Add(mapTile.WorldPosition, new Vector2(Constants.TILE_SIZE) / 2); BoundingBox boundingBox = CollisionGenerationUtils.generateBoundingBoxesForTexture(mapTile.Texture, newPosition); walls.Add(new Wall(content, newPosition, mapTile.Texture, boundingBox)); aiSpaceTypes[mapTile.Index.Y, mapTile.Index.X] = BasePathFinder.TypeOfSpace.Unwalkable; } } // load the AI for the map BasePathFinder.TypeOfSpace defaultSpaceType = (BasePathFinder.TypeOfSpace)Activator.CreateInstance(typeof(BasePathFinder.TypeOfSpace)); for (int y = 0; y < height; y++) { for (int x = 0; x < width; x++) { if (aiSpaceTypes[y, x] == defaultSpaceType) { aiSpaceTypes[y, x] = BasePathFinder.TypeOfSpace.Walkable; } } } AIManager.getInstance().init(height, width); AIManager.getInstance().Board = aiSpaceTypes; map = new Model.Map(walls); }
private async Task <Model.MapStory> JSONtoModels(Windows.Storage.StorageFile file) { Model.Story storyModel = new Model.Story(); Model.Map mapModel = new Model.Map(); var dialog = new MessageDialog(""); if (file != null) { string data = await Windows.Storage.FileIO.ReadTextAsync(file); JsonObject jsonObj = new JsonObject(); jsonObj = JsonObject.Parse(data); MapStory.Story.Title = jsonObj.GetNamedString("title"); MapStory.Story.Description = jsonObj.GetNamedString("description"); MapStory.Story.Author = jsonObj.GetNamedString("author"); //JsonObject, to access Json Api for Interactable Object. JsonObject interactableObjects = new JsonObject(); interactableObjects = jsonObj.GetNamedObject("interactableobjects"); //Multiple Interactable Objects usually. Has to dismantle and save them all into objects of the major Model: MapStory Model. foreach (var obj in interactableObjects) { Model.InteractableObject interactableObjectModel = new Model.InteractableObject(); //Interactable Object (characters, enemies, bosses, friends, talking chair, etc) interactableObjectModel.Name = obj.Key; JsonObject jsonInteracatbleObject = new JsonObject(); jsonInteracatbleObject = JsonObject.Parse(obj.Value.ToString()); // Each interactable Objects has multiple branches, options, states, feelings etc... foreach (var branch in jsonInteracatbleObject) { //Branch has a name. Model.Branch branchModel = new Model.Branch(); branchModel.Messages = new List <Model.Message>(); branchModel.Name = branch.Key; JsonObject jsonObjBranch = new JsonObject(); jsonObjBranch = JsonObject.Parse(branch.Value.ToString()); //BranchModel added into the InteractableObjectModel. This process will repeatedly for the other branches. interactableObjectModel.Branches.Add(branchModel); //var dialogJson is not really a true JsonObject, it has only 2 properties (Key and Value). //Those values have to be converted to Strings from a so called: "KeyValuePair". foreach (var dialogJson in jsonObjBranch) { JsonObject dialogJsonObject = JsonObject.Parse(dialogJson.Value.ToString()); if (dialogJsonObject.ContainsKey("question")) { Model.Question questionModel = new Model.Question(); questionModel.Choices = new List <Model.Choice>(); questionModel.Name = dialogJsonObject.GetNamedValue("name").ToString(); questionModel.Text = dialogJsonObject.GetNamedValue("question").ToString(); if (dialogJsonObject.ContainsKey("emote")) { questionModel.Emote = dialogJsonObject.GetNamedValue("emote").ToString(); } JsonObject jsonObjChoices = new JsonObject(); if (JsonObject.TryParse(dialogJsonObject.GetNamedValue("choices").ToString(), out jsonObjChoices)) { foreach (var choiceJson in jsonObjChoices) { JsonObject jsonObjectChoice = JsonObject.Parse(choiceJson.Value.ToString()); Model.Choice choiceModel = new Model.Choice(); choiceModel.Name = jsonObjectChoice.GetNamedValue("name").ToString(); //Recommend to have this... if it's empty, make a notification in the GUI. if (jsonObjChoices.ContainsKey("description")) { choiceModel.Description = jsonObjectChoice.GetNamedValue("description").ToString(); } //Target is optional. if (jsonObjectChoice.ContainsKey("target")) { choiceModel.Function = jsonObjectChoice.GetNamedValue("target").ToString(); } //Branch is optional... what's the real difference tho? if (jsonObjectChoice.ContainsKey("branch")) { choiceModel.Branch = jsonObjectChoice.GetNamedValue("branch").ToString(); } questionModel.Choices.Add(choiceModel); } branchModel.Messages.Add(questionModel); } else { MessageDialog msgbox = new MessageDialog("Error! There should be Choices jsonObject in the question JsonObject."); await msgbox.ShowAsync(); } } else { Model.Message message = new Model.Message(); if (dialogJsonObject.ContainsKey("name")) { message.Name = dialogJsonObject.GetNamedValue("name").ToString(); } else { message.Name = "No name found?!"; } if (dialogJsonObject.ContainsKey("message")) { message.Text = dialogJsonObject.GetNamedValue("message").ToString(); } else { message.Text = ""; } if (dialogJsonObject.ContainsKey("emote")) { message.Emote = dialogJsonObject.GetNamedValue("emote").ToString(); } branchModel.Messages.Add(message); } if (int.TryParse(dialogJson.Key, out int dialognumber)) { } else { } } //has chain of messages that the player has to go through. //continue here -> //extra info: go through another foreach. This time for the real dialogs. } //Interactable Object is filled... now it gets added into the MapStory. this.MapStory.Story.InteractableObjects.Add(interactableObjectModel); } } else { dialog = new MessageDialog("Error, file is null!"); await dialog.ShowAsync(); } if (MapStory != null) { return(this.MapStory); } else { dialog = new MessageDialog("Error, MapStory is null!"); await dialog.ShowAsync(); MapStory = new Model.MapStory(); return(MapStory); } }
public void Initialize(ITime time) { _LineRenderer = GetComponent <LineRenderer>(); ModelMap = new Model.Map(new XmlMap(_MapFile.text), time); _Tiles = new List <Tile>(); for (var y = 0; y < ModelMap.Height.Value; ++y) { for (var x = 0; x < ModelMap.Width.Value; ++x) { var pos = new Position(x, y); var tile = Instantiate(_InteractionTile, new Vector3(x, y, 0), Quaternion.identity, transform); tile.Initialize(ModelMap[pos], _UIManager); bool isWaterTile = false; Ground groundPrefab; switch (ModelMap[pos].Ground) { case Model.Ground.Water _: groundPrefab = _Water; isWaterTile = true; break; case Model.Ground.Grass _: groundPrefab = _Grass; break; case Model.Ground.Mountain _: groundPrefab = _Mountain; break; default: throw new System.Exception("todo"); } var ground = Instantiate(groundPrefab, new Vector3(x, y, 0), Quaternion.identity, tile.transform); if (!isWaterTile) { AddWaterTransitions(ground, pos); } switch (ModelMap[pos].Object) { case null: break; case Model.MapObject.Property.Castle modelCastle: var castle = tile.InstantiateMapObject(_Castle); castle.Initialize(modelCastle, _GameManager, _UIManager); break; case Model.MapObject.Property.House modelHouse: var house = tile.InstantiateMapObject(_House); house.Initialize(modelHouse, _GameManager, _UIManager); break; case Model.MapObject.Bridge _: tile.InstantiateMapObject(_Bridge); break; case Model.MapObject.Road _: tile.InstantiateMapObject(ChooseRoadPiece(pos)); break; case Model.MapObject.Forest _: tile.InstantiateMapObject(_Forest); break; default: throw new System.Exception("todo"); } _Tiles.Add(tile); } } }