public Sprite SpriteForStructure(Models.Structures structureModel) { int x, y; //Currently a hack to get string name from structure.structureType string spriteName = structureModel.Type + ""; if (structureModel.LinksToNeighbor == false) { //Since the object does not bother with connectivity to its neighbors it is assumed to have only one sprite //later iterations can randomize this to some extent return SpriteNameMap [spriteName + "_1"]; } //Get the coordinates for the surface on which the structure is created x = structureModel.SurfaceModel.X; y = structureModel.SurfaceModel.Y; //Get spritename based on links to neighbors spriteName += "_"; spriteName += level.CheckForStructureConnections (x, y, structureModel.Type); if (SpriteNameMap.ContainsKey (spriteName) == false) { Debug.Log ("Views.Levels --> SpriteForStructure : Cannot find sprite with name " + spriteName); //FIXME: Should return a placeholder of somekind or a null value? return null; } else { return SpriteNameMap [spriteName]; } }
public bool PlaceStructure(Models.Structures structureModel) { if (structureModel == null) { //Removing object from surface this.structure = null; return(true); } if (structureModel.isPositionValidOnSurface(structureModel.SurfaceModel) == false) { Debug.Log("Models.Surfaces --> PlaceStructureOnSurface : structure at " + X + ", " + Y + " already exists"); return(false); } else { for (int a = X; a < X + structureModel.Width; a++) { for (int b = Y; b < Y + structureModel.Height; b++) { level.GetSurfaceAt(a, b).structure = structureModel; } } //FIXME: placeholder cause the compiler throws a tantrum return(true); } }
/// <description> /// Methods for all structures on surfaces in the level view /// </description> //TODO: once a new structure is created, check its drawing order compared to all other structures allready placed. // then rearrange all the structures and place at correct position. public void OnStructurePlacedOnSurface(Models.Structures structureModel) { Console.Write ("Views.Levels -> OnStructurePlacedOnSurface : placing structure view"); //Create Game Object GameObject gameObject = new GameObject(); gameObject.name = "Structure_" + structureModel.SurfaceModel.X + "_" + structureModel.SurfaceModel.Y; //Set Parent? //Views.Surfaces surfaceView = surfaceModelViewMap[structureModel.SurfaceModel]; gameObject.transform.SetParent (this.transform, true); Views.Structures structureView = new Views.Structures (viewLevelInstance, gameObject); /* * Structure view currently statically assigned road tile */ //load structure sprite based on its string name in the structure name sprite map structureView.GameObject.AddComponent<SpriteRenderer> ().sprite = SpriteForStructure(structureModel); structureView.GameObject.GetComponent<SpriteRenderer> ().sortingOrder = Utility.SortingOrderNumber (level.Width, level.Height, structureModel.SurfaceModel.X, structureModel.SurfaceModel.Y); //Position the structure on to its surface Vector3 point = Utility.ConvertCartesianToIsometric (new Vector3 (structureModel.SurfaceModel.X, structureModel.SurfaceModel.Y, 0), offset); structureView.SetPosition (point.x, point.y, point.z); //When this is added check for its position, and then basically redraw all the tiles where overlap may occur. structureModelViewMap.Add (structureModel, structureView); structureModel.RegisterStructureChangesCallBack (OnStructureChanges); Console.Write ("Views.Levels -> OnStructurePlacedOnSurface : structure view placed"); }
public void OnStructureChanges(Models.Structures structureModel) { //TODO: implement this when a placed structure needs to expand, gets damaged, or destroyed // Console.Write("Views.Levels -> OnStructureChanges : Currently unimplementd"); if (structureModelViewMap.ContainsKey (structureModel) == false) { Debug.Log ("Views.Levels --> OnstructureChanges : something went horribly wrong, cant find the structure model in the model-view map"); } else { Views.Structures structureView = structureModelViewMap[structureModel]; structureView.GameObject.GetComponent<SpriteRenderer> ().sprite = SpriteForStructure (structureModel); } }
static public Models.Structures CreateStructure(StructureType type, float movementCost = 1f, int width = 1, int height = 1, bool linksToNeighbor = false) { Models.Structures structure = new Models.Structures(); structure.type = type; structure.movemenCost = movementCost; structure.width = width; structure.height = height; structure.linksToNeighbor = linksToNeighbor; structure.PositionValidationFunctions = structure.isPositionValidOnSurface; return(structure); }
/// <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); } }
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); }