// Use this for initialization void Start () { // int width = 20; int height = 20; level.RegisterStructurePlacedCallBack (OnStructurePlacedOnSurface); // level.RegisterSurfaceChangedCallBack (OnSurfaceTerrainCreated); level.CreateEmptyLevel(width, height); //surfaceViews = new Views.Surfaces[width * height]; //Create Dictionary Maps surfaceModelViewMap = new Dictionary<Models.Surfaces, Surfaces>(); structureModelViewMap = new Dictionary<Models.Structures, Structures> (); for (int x = 0; x < level.Width; x++) { for (int y = 0; y < level.Height; y++) { //Get Surface Model Models.Surfaces surfaceModel = level.GetSurfaceAt (x, y); this.OnSurfaceTerrainCreated (surfaceModel); } } //Tell the level model about any changes to individual surfaces instead of registering with each surface level.RegisterSurfaceChangedCallBack (OnSurfaceTerrainChanged); }
/// <description> /// Surface Methods for all surface models in level view /// </description> public void OnSurfaceTerrainCreated(Models.Surfaces surfaceModel) { int x = surfaceModel.X; int y = surfaceModel.Y; GameObject gameObject = new GameObject (); Vector3 point = Utility.ConvertCartesianToIsometric (new Vector3 (x, y, 0), offset); surfaceModel.Terrain = Models.Surfaces.TerrainType.Empty; // Debug.Log ("Views.Level --> OnSurfaceTerrainCreated : Creating surface view for surface models"); //Create an object of Views.Surface class Views.Surfaces surfaceView = new Views.Surfaces(gameObject); surfaceView.GameObject.name = "Surface_" + x + "_" + y; surfaceView.GameObject.transform.SetParent (viewLevelInstance.transform, true); surfaceView.GameObject.AddComponent<SpriteRenderer> (); surfaceView.GameObject.GetComponent<SpriteRenderer> ().sprite = Empty; surfaceView.GameObject.GetComponent<SpriteRenderer> ().sortingOrder = Utility.SortingOrderNumber (level.Width, level.Height, x, y); surfaceView.SetPosition (point.x, point.y); //FIXME: for now all the sprite tiles will be empty surfaceModelViewMap.Add (surfaceModel, surfaceView); // surfaceModel.RegisterTerrainCallBack (OnSurfaceTerrainChanged); // Debug.Log ("Views.Level --> OnSurfaceTerrainCreated : SurfaceModel Created and displayed on screen " + surfaceModelViewMap.Count); }
/// <summary> /// Builds the structure. /// </summary> /// <param name="type">Type.</param> /// <param name="surfaceModel">Surface model.</param> protected void BuildStructure(Models.Structures.StructureType type, Models.Surfaces surfaceModel) { Debug.Log("Controllers.Mouse -> BuildStructure : building " + type); //TODO: currently it only checks if there is a structure, // in the future it should check for null values? or create a different system to remove structures levelModel.PlaceStructure(type, surfaceModel); }
/// <summary> /// Gets the surface under mouse. /// </summary> /// <returns>The surface under mouse.</returns> public Models.Surfaces GetSurfaceUnderMouse() { Vector3 point = Utility.ConvertIsometricToCartesian(GetMousePositionInWorld()); Models.Surfaces surfaceModel = Models.Levels.Instance.GetSurfaceAt((int)point.x, (int)point.y); return(surfaceModel); }
public void SurfaceChanged(Models.Surfaces surfaceModel) { Debug.Log("Models.Levels --> structure changed : something related to the surface changed updating surface"); //Check if surface existis ? if (SurfaceChangedCallBacks != null && surfaceModel != null) { SurfaceChangedCallBacks(surfaceModel); } }
// Update is called once per frame void Update() { surfaceModel = mouseController.GetSurfaceUnderMouse(); if (surfaceModel != null) { uiTextForSurface.text = "Terrain: " + surfaceModel.Terrain.ToString(); } else { uiTextForSurface.text = "Terrain: Empty"; } }
/// <summary> /// Gets the structure under mouse. /// </summary> /// <returns>The structure under mouse.</returns> public Models.Structures GetStructureUnderMouse() { Models.Structures structureModel; Models.Surfaces surfaceModel = GetSurfaceUnderMouse(); if (surfaceModel != null && surfaceModel.hasStructure() == true) { structureModel = GetSurfaceUnderMouse().Structure; return(structureModel); } else { return(null); } }
public void CreateEmptyLevel(int width = 10, int height = 10, string seed = "") { this.width = width; this.height = height; surfaceModels = new Models.Surfaces[Width * Height]; for (int x = 0; x < width; x++) { for (int y = 0; y < height; y++) { surfaceModels [x * Width + y] = new Models.Surfaces(this, x, y); surfaceModels [x * Width + y].Terrain = Models.Surfaces.TerrainType.Empty; surfaceModels [x * Width + y].RegisterTerrainCallBack(SurfaceChanged); } } //FIXME: // for now this function initializes the level and creates a prototype // of all the structures in this level for tracking purposes structurePrototypes = new Dictionary <Structures.StructureType, Structures> (); structurePrototypes.Add(Models.Structures.StructureType.Road, Models.Structures.CreateStructure(Models.Structures.StructureType.Road, 1f, //movement cost 1, // surfaces occupied in x dir 1, // surfaces occupied in y dir true //Links to neighbors )); structurePrototypes.Add(Models.Structures.StructureType.Wall, Models.Structures.CreateStructure(Models.Structures.StructureType.Wall, 0f, //movement cost 1, // surfaces occupied in x dir 1, // surfaces occupied in y dir true //Links to neighbors )); structurePrototypes.Add(Models.Structures.StructureType.House, Models.Structures.CreateStructure(Models.Structures.StructureType.House, 0f, //movement cost 2, // surfaces occupied in x dir 2, // surfaces occupied in y dir false //Links to neighbors )); Debug.Log("Models.Levels -> CreateEmptyLevel : created Structure prototypes "); }
/// <description> /// This subsection includes all functions related to surfaces listed as follows /// PlaceStructure(Models.Structure.StructureType, Models.Surfaces): /// creates and places a structure of type structuretype on surface. /// CheckForStructureConnections(int, int, Models.Structure.StructureType): /// chedk if the given structure type has connectable neighbors /// </description> public void PlaceStructure(Models.Structures.StructureType type, Models.Surfaces surfaceModel) { Debug.Log("Models.Levels -> placeStructure : trying to place structure of type : " + type); if (structurePrototypes.ContainsKey(type) == false) { Debug.Log("Models.Levels -> placeStructure : Cannot create structure of type " + type); return; } Models.Structures structureModel = Models.Structures.PlaceStructureOnSurface(structurePrototypes [type], surfaceModel); //either there are no callbacks or //structureModel returns null as there is already a structure on the surface if (StructurePlacedCallBacks != null && structureModel != null) { Debug.Log("Models.Levels -> placeStructure : callback for showing on screen"); StructurePlacedCallBacks(structureModel); } }
//Checks if the current position of a structure is valid //For structures larger than 1x1, it checks its neighbors as well //Function returns true only if all surfaces under consideration don'e have structures on them public bool isPositionValidOnSurface(Models.Surfaces surfaceModel) { for (int x = surfaceModel.X; x < surfaceModel.X + this.width; x++) { for (int y = surfaceModel.Y; y < surfaceModel.Y + this.height; y++) { Models.Surfaces surface = surfaceModel.Level.GetSurfaceAt(x, y); //Return false if the floor is empty if (surface.Terrain == Surfaces.TerrainType.Empty) { return(false); } //return false if there is already a structure on the surface if (surface.hasStructure() == true) { return(false); } } } return(true); }
public void OnSurfaceTerrainChanged(Models.Surfaces surfaceModel) { Debug.Log ("On surface changed"); if (surfaceModelViewMap.ContainsKey (surfaceModel) == false) { Debug.Log ("Views.Levels --> On surface Changed : surface model does not exist"); return; } Views.Surfaces surfaceView = surfaceModelViewMap [surfaceModel]; //FIXME: Major Re-Write Needed if (surfaceModel.Terrain == Models.Surfaces.TerrainType.Empty) { surfaceView.GameObject.GetComponent<SpriteRenderer> ().sprite = Empty; } else if (surfaceModel.Terrain == Models.Surfaces.TerrainType.Lake) { surfaceView.GameObject.GetComponent<SpriteRenderer> ().sprite = Lake[UnityEngine.Random.Range(0, Lake.Length)]; } else if (surfaceModel.Terrain == Models.Surfaces.TerrainType.Mountain) { surfaceView.GameObject.GetComponent<SpriteRenderer> ().sprite = Mountain[UnityEngine.Random.Range(0, Mountain.Length)]; } else if (surfaceModel.Terrain == Models.Surfaces.TerrainType.Plain) { surfaceView.GameObject.GetComponent<SpriteRenderer> ().sprite = Plain[UnityEngine.Random.Range(0, Plain.Length)]; } else { Debug.Log ("Views.Levels - Trying to assign sprite based on terrain type, but terrain type not found"); } }
// Update is called once per frame void Update() { //If user clicks on a button then do nothin on screen if (EventSystem.current.IsPointerOverGameObject()) { return; } currentMousePosition = GetMousePositionInWorld(); //TODO: right-click to cancel build mode if (Input.GetMouseButtonUp(1)) { isBuildModeActive = false; } //If the user clicks on screen (but not on the UI) then do the following // check if in building mode (??) // register startX and startY to register dragging if (Input.GetMouseButtonDown(0)) { startPoint = Utility.ConvertIsometricToCartesian(currentMousePosition); } //If the user releases the button after clicking on it then do the following // register endX and endY if (Input.GetMouseButtonUp(0)) { endPoint = Utility.ConvertIsometricToCartesian(currentMousePosition); if (startPoint.x > endPoint.x) { Utility.Swap(ref startPoint.x, ref endPoint.x); } if (startPoint.y > endPoint.y) { Utility.Swap(ref startPoint.y, ref endPoint.y); } //FIXME: the logic for this will need to be changed based on what is being built and where // or what is changing and where for (int x = (int)startPoint.x; x <= (int)endPoint.x; x++) { for (int y = (int)startPoint.y; y <= (int)endPoint.y; y++) { Models.Surfaces surfaceModel = Models.Levels.Instance.GetSurfaceAt(x, y); if (surfaceModel != null) { Debug.Log("Building on surface : " + surfaceModel.X + ", " + surfaceModel.Y); if (isBuildModeActive == true) { if (surfaceModel.Terrain == Models.Surfaces.TerrainType.Mountain) { Debug.Log("Mouse Controller -> Update : cannot build on mountains"); } else { BuildStructure(structureToBuild, surfaceModel); } } else { SetTerrainType(terrainToApply, surfaceModel); } } } } } //Track where the mouse ends up lastMousePosition = GetMousePositionInWorld(); }
/// <summary> /// Sets the type of the terrain. /// </summary> /// <param name="terrain">Terrain.</param> /// <param name="surfaceModel">Surface model.</param> protected void SetTerrainType(Models.Surfaces.TerrainType terrain, Models.Surfaces surfaceModel) { Debug.Log("Controllers.Mouse --> SetTerrainType : terrain " + terrain); surfaceModel.Terrain = terrain; }
public static Models.Structures PlaceStructureOnSurface(Models.Structures structureModel, Models.Surfaces surfaceModel) { if (structureModel.PositionValidationFunctions(surfaceModel) == false) { Debug.Log("Models.Structures --> place structure on surface : cannot place the structure here"); return(null); } Models.Structures structure = new Models.Structures(); structure.type = structureModel.type; structure.movemenCost = structureModel.movemenCost; structure.width = structureModel.width; structure.height = structureModel.height; structure.linksToNeighbor = structureModel.linksToNeighbor; structure.SurfaceModel = surfaceModel; if (surfaceModel.hasStructure()) { Console.Write("Models.Structure -> placeStructureOnSurface : surface has pre-existing structure on it"); return(null); } else { Debug.Log("Models.Structure -> placeStructureOnSurface : structure added to surface"); surfaceModel.PlaceStructure(structure); } int x = structure.SurfaceModel.X; int y = structure.SurfaceModel.Y; Models.Levels levelModel = Models.Levels.Instance; if (structure.linksToNeighbor == true) { //North if (x < levelModel.Width - 1 && levelModel.GetSurfaceAt(x + 1, y).Structure != null && levelModel.GetSurfaceAt(x + 1, y).Structure.Type == structure.type) { levelModel.GetSurfaceAt(x + 1, y).Structure.StructureCallBacks(levelModel.GetSurfaceAt(x + 1, y).Structure); } //East if (y > 0 && levelModel.GetSurfaceAt(x, y - 1).Structure != null && levelModel.GetSurfaceAt(x, y - 1).Structure.Type == structure.type) { levelModel.GetSurfaceAt(x, y - 1).Structure.StructureCallBacks(levelModel.GetSurfaceAt(x, y - 1).Structure); } //South if (x > 0 && levelModel.GetSurfaceAt(x - 1, y).Structure != null && levelModel.GetSurfaceAt(x - 1, y).Structure.Type == structure.type) { levelModel.GetSurfaceAt(x - 1, y).Structure.StructureCallBacks(levelModel.GetSurfaceAt(x - 1, y).Structure); } //West if (y < levelModel.Height - 1 && levelModel.GetSurfaceAt(x, y + 1).Structure != null && levelModel.GetSurfaceAt(x, y + 1).Structure.Type == structure.type) { levelModel.GetSurfaceAt(x, y + 1).Structure.StructureCallBacks(levelModel.GetSurfaceAt(x, y + 1).Structure); } } return(structure); }