public static void DoRoomFloodFill(InstalledObject _sourceInObj) { //_sourceInObj is the oject that may split 2 rooms or fully enclose on (e.g. a wall) //check NESW neighbour of the Installed Object's tile and do flood fills from them World world = _sourceInObj.Tile.World; Room oldRoom = _sourceInObj.Tile.room; //Try to build new rooms starting for each direction foreach (Tile t in _sourceInObj.Tile.GetNeighbours()) { ActualFloodFill(t, oldRoom); } //the source tile has a wall or something, which is not part of the room _sourceInObj.Tile.room = null; oldRoom.tiles.Remove(_sourceInObj.Tile); //If the Object was installed in a room (which should always be true if we consider outside a big room), delete that room and assign all tiles to outside, //then we will fill and create a new room (or rooms) to put the tiles back in if (/*_sourceInObj.Tile.room*/ oldRoom != world.GetOutsideRoom()) { //at this point oldRoom should no longer have tiles left in it, so in practice this Deletion should only need to remove the room form the world's list if (oldRoom.tiles.Count > 0) { Debug.LogError("Room -- DoRoomFloodFill: 'oldRoom' still has tiles assinged to it. This is wrong!"); } world.DeleteRoom(oldRoom); } }
public static void Door_UpdateAction(InstalledObject _inObj, float _deltaTime) { //if (Debug.isDebugBuild) // Debug.Log("Door_UpdateAction: " + _inObj.inObjParameters["openness"]); if (_inObj.GetParameter("is_opening") >= 1) { _inObj.ChangeParameter("openness", _deltaTime * 4); if (_inObj.GetParameter("openness") >= 1) { _inObj.SetParameter("is_opening", 0); } } else { _inObj.ChangeParameter("openness", -_deltaTime * 4); } _inObj.SetParameter("openness", Mathf.Clamp01(_inObj.GetParameter("openness"))); if (_inObj.cbOnChanged != null) { _inObj.cbOnChanged(_inObj); } }
public void OnInstalledObjectCreated(InstalledObject obj) { GameObject instObj = new GameObject(); _installedObjectGameObjectMap.Add(obj, instObj); instObj.name = obj.ObjectType + "_" + obj.Tile.X + "_" + obj.Tile.Y; instObj.transform.position = new Vector3(obj.Tile.X + (obj.Width - 1) / 2f, obj.Tile.Y + (obj.Height - 1) / 2f, 0); instObj.transform.SetParent(this.transform, true); if (obj.ObjectType == "Door") { Tile north = _world.GetTileAt(obj.Tile.X, obj.Tile.Y + 1); Tile south = _world.GetTileAt(obj.Tile.X, obj.Tile.Y - 1); if (north != null && south != null && north.InstalledObject != null && south.InstalledObject != null && north.InstalledObject.ObjectType == "Wall" && south.InstalledObject.ObjectType == "Wall") { instObj.transform.rotation = Quaternion.Euler(0, 0, 90); } } instObj.AddComponent <SpriteRenderer>().sprite = GetSpriteForInstalledObject(obj); instObj.GetComponent <SpriteRenderer>().sortingLayerName = "InstalledObjects"; //instObj.GetComponent<SpriteRenderer>().color = obj.Tint; obj.RegisterOnInstalledObjectChangedCallback(OnInstalledObjectChanged); }
void OnInstalledObjectChanged(InstalledObject obj) { GameObject inst_go; if (!_installedObjectGameObjectMap.TryGetValue(obj, out inst_go)) { Debug.LogError("_installedObjectGameObjectMap doesn't contain the installed object!"); return; } inst_go.GetComponent <SpriteRenderer>().sprite = GetSpriteForInstalledObject(obj); //inst_go.GetComponent<SpriteRenderer>().color = obj.Tint; //Change Door sprite alpha to simulate opening/closing if (obj.ObjectType == "Door") { if (obj.GetParameter("openness") < 0.1f) { inst_go.GetComponent <SpriteRenderer>().DOFade(1f, 0.25f); } else if (obj.GetParameter("openness") < 0.5f) { inst_go.GetComponent <SpriteRenderer>().DOFade(0.66f, 0.25f); } else if (obj.GetParameter("openness") < 0.9f) { inst_go.GetComponent <SpriteRenderer>().DOFade(0.33f, 0.25f); } else { inst_go.GetComponent <SpriteRenderer>().DOFade(0f, 0.25f); } } }
void OnInstalledObjectChanged(InstalledObject installedObject) { // Make sure the InstalledObject graphics are updated when an adjacent thing is added. if (!installedObject.installed) { return; } if (installedObjectGameObjectMap.TryGetValue(installedObject, out var obj)) { SpriteRenderer spriteRenderer = obj.GetComponent <SpriteRenderer>(); // TODO remove duplication with the OnInstalledObjectPlaced method. if (!installedObject.installed) { spriteRenderer.color = new Color(1f, 1f, 1f, 0.3f); } else { spriteRenderer.color = new Color(1f, 1f, 1f, 1f); } spriteRenderer.sprite = GetSpriteForInstalledObject(installedObject); spriteRenderer.sortingLayerName = "InstalledObject"; } else { Debug.LogError("OnInstalledObjectChanged - InstalledObject is not present in the " + "installedObjectsGameObjectMap."); } }
public void PlaceInstalledObject(Vector3Int tile_position, string buildModeObjectType) { Debug.Log("PLACING:" + buildModeObjectType); InstalledObject object_to_place = World.InstalledObjectPrototypes[buildModeObjectType].Clone(); object_to_place = InstalledObject.PlaceInstance(object_to_place, tile_position); TileBase tile = GameObject.FindObjectOfType <InstalledObjectSpriteController> ().GetTileBase(buildModeObjectType, tile_position); if (tile == null) { Debug.LogError("Something went wrong"); } else { tilemapFoundation.SetTile(tile_position, tile); tilemapFoundation.GetTile(tile_position).name = buildModeObjectType; World.InvalidateTileGraph(); if (object_to_place != null) { if (buildModeObjectType.Contains("Wall") || buildModeObjectType.Contains("Door") || buildModeObjectType.Contains("Window")) { if (World.foundationGameMap.ContainsKey(tile_position) && buildModeObjectType.Contains("Door") && World.foundationGameMap[tile_position].objectType.Contains("Wall")) { World.foundationGameMap.Remove(tile_position); } World.foundationGameMap.Add(tile_position, object_to_place); } } } }
//TODO: enforce global uniqueness of objectType OR change description //TODO: once InstalledObjects handle their own sprites, log a warning when sprites are not the same size as // their associated InstalledObject prototypes /// <summary> /// Creates a prototype of the InstalledObject class. /// <para></para> /// NOTE: The caller is currently responsible for making sure that the width and height are not swapped, /// which will not log an error and will not result in any default behavior. Once InstalledObjects handle /// their own sprites, this incorrect use will be logged as a warning, but it will not be corrected, since /// it's possible that the caller will want to have behavior associated with the object outside of the /// assigned sprite. /// </summary> /// <param name="objectType"> /// A string representing the type of the object. Currently, the caller is responsible for ensuring that the /// string is unique, but that will be enforced down the line. /// </param> /// <param name="movementCost"> /// A list of movement cost changes for tiles under or around the InstalledObject sprite. These values will /// be multiplied by the movementCost for the underlying Tile. A movementCost of 0 makes the Tile unpathable. /// Passing an empty list or a list of the wrong length (i.e. list.Count!=width*height) will create a list /// of the correct length with all values set to 0f, and the latter action will log an error. /// </param> /// <param name="width"> /// The number of tiles wide the InstalledObject should be. This is 1-indexed. Defaults to 1. /// </param> /// <param name="height"> /// The number of tiles high the InstalledObject should be. This is 1-indexed. Defaults to 1. /// </param> public static InstalledObject CreatePrototype(string objectType, List <float> movementCost, int width = 1, int height = 1) { InstalledObject io = new InstalledObject(); // If passed an empty list, make object unpathable // else if the list length is unreconcileable with the width and height, log an error and make the object unpathable if (movementCost.Count == 0) { Debug.LogWarning("InstalledObject.CreatePrototype has been called with an empty list. Tiles are defaulting to be unpathable."); io.movementCost = new List <float> (); for (int i = 0; i < width * height; i++) { io.movementCost.Add(0f); } } else if (movementCost.Count != width * height) { Debug.LogError("InstalledObject Prototype Constructor for " + objectType + " has been called incorrectly. Tiles are defaulting to be unpathable."); io.movementCost = new List <float> (); for (int i = 0; i < width * height; i++) { io.movementCost.Add(0f); } } else //I'm a dumbass - spent 20 minutes trying to figure out where my nullArgumentExceptions were coming from. { io.movementCost = movementCost; } io.ObjectType = objectType; io.Width = width; io.Height = height; return(io); }
public static void Door_UpdateAction(InstalledObject installedObject, float deltaTime) { // If the door isOpening is 'true' open the door a little bit more if (installedObject.GetParameter("isOpening") >= 1) { installedObject.ChangeParameter("OpenValue", (deltaTime * 4)); // If door is fully opened, close it again (right away) if (installedObject.GetParameter("OpenValue") >= 1) { installedObject.SetParameter("isOpening", 0); } } // Close door again else { installedObject.ChangeParameter("OpenValue", (deltaTime * -4)); } // Clamp value between 0 & 1 installedObject.SetParameter("OpenValue", Mathf.Clamp01(installedObject.GetParameter("OpenValue"))); // Call the callback if there is any if (installedObject.cb_OnChanged != null) { installedObject.cb_OnChanged(installedObject); } }
void OnInstalledObjectCreated(InstalledObject installedObject) { Debug.Log("Installed Object was placed."); AudioClip audioClip = Resources.Load <AudioClip>("Audio/" + installedObject.installedObjectType + "_OnCreated"); PlaySound(audioClip, Camera.main.transform.position); }
void ShowInstalledObjectSpriteAtTile(string objectType, Tile tile) { GameObject go = new GameObject(); go.transform.SetParent(_cursorGameObjectsContainer, true); _dragPreviewGameObjects.Add(go); SpriteRenderer renderer = go.AddComponent <SpriteRenderer>(); renderer.sprite = _iOSC.GetSpriteForInstalledObject(objectType); renderer.sortingLayerName = "Jobs"; if (_world.IsInstalledObjectPlacementValid(objectType, tile)) { renderer.color = new Color(0.5f, 1f, 0.5f, 0.25f); } else { renderer.color = new Color(1f, 0.5f, 0.5f, 0.25f); } InstalledObject prototype = _world.InstalledObjectPrototypes[objectType]; go.transform.position = new Vector3(tile.X + (prototype.Width - 1) / 2f, tile.Y + (prototype.Height - 1) / 2, 0); }
public void OnInstalledObjectCreated(InstalledObject _inObj) { //create a visual GameObject linked to this data GameObject inObj_GO = new GameObject(); //add the Object/GameObject pair to dictionary installedObjectGameObjectMap.Add(_inObj, inObj_GO); inObj_GO.name = _inObj.ObjectType + "_" + _inObj.Tile.X + "," + _inObj.Tile.Y; inObj_GO.transform.position = new Vector3(_inObj.Tile.X, _inObj.Tile.Y, 0); inObj_GO.transform.SetParent(transform, true); if (_inObj.ObjectType == "Door") { //By default, door graphic is made for walls to east/west, let check to see if there are walls north/south, and if so, rotate the GO Tile northTile = world.GetTileAt(_inObj.Tile.X, _inObj.Tile.Y + 1); Tile southTile = world.GetTileAt(_inObj.Tile.X, _inObj.Tile.Y - 1); if (northTile != null && southTile != null && northTile.InstalledObject != null && southTile.InstalledObject != null && northTile.InstalledObject.ObjectType == "Wall" && southTile.InstalledObject.ObjectType == "Wall") { inObj_GO.gameObject.transform.rotation = Quaternion.Euler(new Vector3(0, 0, 90)); } } //add a sprite renderer SpriteRenderer sr = inObj_GO.AddComponent <SpriteRenderer>(); sr.sprite = GetSpriteForInstalledObject(_inObj); sr.sortingLayerName = "InstalledObjects"; //register callback so our GameObject gets updated _inObj.RegisterOnChangedCallback(OnInstalledObjectChanged); }
void InitializeInstalledObjectPrototypesDictionary() { _installedObjectPrototypes = new Dictionary <string, InstalledObject>(); _installedObjectJobPrototypes = new Dictionary <string, Job>(); InstalledObject wallPrototype = new InstalledObject("Wall", 0, 1, 1, true, true); _installedObjectPrototypes.Add("Wall", wallPrototype); _installedObjectJobPrototypes.Add("Wall", new Job(null, "Wall", InstalledObjectAction.OnInstalledObjectJobCompleted, 1f, new LooseObject[] { new LooseObject("SteelPlate_", 5, 0) })); InstalledObject doorPrototype = new InstalledObject("Door", 1, 1, 1, false, true); _installedObjectPrototypes.Add("Door", doorPrototype); _installedObjectPrototypes["Door"].SetParameter("openness", 0); _installedObjectPrototypes["Door"].SetParameter("openingState", 0); _installedObjectPrototypes["Door"].RegisterUpdateAction(InstalledObjectAction.Door_UpdateAction); _installedObjectPrototypes["Door"]._checkEnterableState = InstalledObjectAction.Door_EnterableState; InstalledObject stockPilePrototype = new InstalledObject("Stockpile", 1, 1, 1, true, false); _installedObjectPrototypes.Add("Stockpile", stockPilePrototype); _installedObjectPrototypes["Stockpile"].RegisterUpdateAction(InstalledObjectAction.Stockpile_UpdateAction); _installedObjectPrototypes["Stockpile"].Tint = new Color(1f, 0f, 0f, 1f); _installedObjectJobPrototypes.Add("Stockpile", new Job(null, "Stockpile", InstalledObjectAction.OnInstalledObjectJobCompleted, -1f, null)); InstalledObject o2GeneratorPrototype = new InstalledObject("O2Generator", 10, 2, 2, false, false); _installedObjectPrototypes.Add("O2Generator", o2GeneratorPrototype); _installedObjectPrototypes["O2Generator"].RegisterUpdateAction(InstalledObjectAction.OxygenGenerator_UpdateAction); }
void onInstalledObjectRemoved(InstalledObject obj) { GameObject obj_go = installedObjectGameObjectMap[obj]; installedObjectGameObjectMap.Remove(obj); Destroy(obj_go); }
public static InstalledObject PlacePrototype(InstalledObject proto, Tile tile) { if (!CheckPlacementValidity(proto.installedObjectType, tile)) { return(null); } InstalledObject installedObject = new InstalledObject(); installedObject.installedObjectType = proto.installedObjectType; installedObject.linksToNeighbour = proto.linksToNeighbour; installedObject.width = proto.width; installedObject.height = proto.height; installedObject.movementCost = proto.movementCost; installedObject.installed = proto.installed; installedObject.tile = tile; // TODO: This assumes that the object is a 1x1 object. if (installedObject.tile.PlaceObject(installedObject) == false) { // If we can't place object here for some reason. return(null); } tile.pendingInstalledObjectJob = null; // Get rid of the job. InformNeighbours(installedObject); return(installedObject); }
void onInstalledObjectChanged(InstalledObject obj) { // Get gameObject GameObject obj_go = installedObjectGameObjectMap[obj]; // Update sprite string spriteKey; if (obj.ruleTile) { spriteKey = obj.type + getRuleTileForInstalledObject(obj); } else if (obj.stages != 0) { spriteKey = obj.type + "_" + obj.Stage; } else { spriteKey = obj.type; } SpriteRenderer sr = obj_go.GetComponent <SpriteRenderer>(); if (installedObjectSprites.ContainsKey(spriteKey) == false) { Debug.LogError("Couldn't find sprite for : " + spriteKey); sr.sprite = null; return; } sr.sprite = installedObjectSprites[spriteKey]; }
string getRuleTileForInstalledObject(InstalledObject obj) { string ruleTile = "_"; int x = obj.tile.x; int y = obj.tile.y; Tile t; t = world.GetTileAt(x, y + 1); if (t != null && t.installedObject != null && t.installedObject.type == obj.type) { ruleTile += "N"; } t = world.GetTileAt(x + 1, y); if (t != null && t.installedObject != null && t.installedObject.type == obj.type) { ruleTile += "E"; } t = world.GetTileAt(x, y - 1); if (t != null && t.installedObject != null && t.installedObject.type == obj.type) { ruleTile += "S"; } t = world.GetTileAt(x - 1, y); if (t != null && t.installedObject != null && t.installedObject.type == obj.type) { ruleTile += "W"; } if (ruleTile == "_") { ruleTile += "0"; } return(ruleTile); }
/// <summary> /// Copy constructor, protected so only callable from this class (and derived classes) /// </summary> /// <param name="other">The object to copy</param> protected InstalledObject(InstalledObject other) { ObjectType = other.ObjectType; MovementCost = other.MovementCost; width = other.width; height = other.height; color = other.color; IsLinkedToNeighbour = other.IsLinkedToNeighbour; RoomEnclosure = other.RoomEnclosure; // Will make a copy of the dictionary form the 'other' installedObject. // So that in the future each installedObject can add and remove installedObjectParameters // Will make a copy of the updateActions form the 'other' installedObject. // So that in the future each installedObject can add and remove updateActions installedObjectParameters = new Dictionary <string, float>(other.installedObjectParameters); jobs = new List <Job>(); // If 'other' has updateActions, copy those if (other.updateActions != null) { updateActions = (Action <InstalledObject, float>)other.updateActions.Clone(); } // Add 'IsEnterable' function this.IsEnterable = other.IsEnterable; }
public void OnInstalledObjectCreated(InstalledObject obj) { //Create a visual GameObject linked to this data //Debug.Log("OnInstalledObjectCreated"); // FIXME: No multitile objects or rotation GameObject obj_go = new GameObject(); //Add our InstalledObject GO pair to the Dictionary installedObjectGameObjectMap.Add(obj, obj_go); obj_go.name = obj.objectType + "_" + obj.tile.X + "_" + obj.tile.Y; obj_go.transform.position = new Vector3(obj.tile.X, obj.tile.Y, 0); obj_go.transform.SetParent(this.transform, true); //obj_go.layer = 11; //Add a Sprite renderer and set the sprite based off the base map. SpriteRenderer obj_sr = obj_go.AddComponent <SpriteRenderer>(); obj_sr.sprite = GetSpriteForInstalledObject(obj); obj_sr.sortingLayerName = "InstalledObjects"; //Register a callback so that the GameObject gets updated whenever the object chages obj.RegisterInstalledObjectChangedCallback(OnInstalledObjectChanged); }
public void UninstallObject() { if (HasObject) { InstalledObject.OnUninstalled(); InstalledObject = null; } }
void onInstalledObjectCreated(InstalledObject obj) { GameObject obj_go = new GameObject(); installedObjectGameObjectMap.Add(obj, obj_go); obj_go.name = obj.type + "_" + obj.tile.x + "_" + obj.tile.y; float z; if (obj.layer == "Background") { z = 1; } else { z = (float)obj.tile.y / height; } obj_go.transform.position = new Vector3(obj.tile.x, obj.tile.y, z); obj_go.transform.SetParent(transform, true); if (obj.movementCost == 0) { BoxCollider2D bc = obj_go.AddComponent <BoxCollider2D>(); bc.offset = new Vector2(0.5f, 0.5f); bc.size = new Vector2(0.75f, 0.75f); } string spriteKey; if (obj.ruleTile) { spriteKey = obj.type + getRuleTileForInstalledObject(obj); } else if (obj.stages != 0) { spriteKey = obj.type + "_" + obj.Stage; } else { spriteKey = obj.type; } SpriteRenderer sr = obj_go.AddComponent <SpriteRenderer>(); if (installedObjectSprites.ContainsKey(spriteKey) == false) { Debug.LogError("Couldn't find sprite for : " + spriteKey); sr.sprite = null; return; } sr.sprite = installedObjectSprites[spriteKey]; sr.sortingLayerName = "InstalledObjects"; obj.RegisterInstalledObjectChangedCB(onInstalledObjectChanged); }
public static EnterableState Door_EnterableState(InstalledObject obj) { obj.SetParameter("openingState", 1); if (obj.GetParameter("openness") >= 1) { return(EnterableState.Yes); } return(EnterableState.Soon); }
void changeObjectSprite(GameObject go, InstalledObject iObject) { switch(iObject.iObject) { case InstalledObject.ObjectType.treePine: go.GetComponent<SpriteRenderer>().sprite = treePine; break; } }
public static InstalledObject CreatePrototype(string objectType, float movementCost = 1f, int width = 1, int height = 1) { InstalledObject obj = new InstalledObject(); obj.ObjectType = objectType; obj.MovementCost = movementCost; obj.Width = width; obj.Height = height; return(obj); }
public static void OxygenGenerator_UpdateAction(InstalledObject obj, float deltaTime) { if (obj.Tile.Room.GetGasAmount("02") < 0.2f) { obj.Tile.Room.ChangeGas("02", 0.01f * deltaTime); } else { } }
// this is used by our object factory static public InstalledObject CreatePrototype(string objectType, float movmentCost = 1f, int width = 1, int height = 1) { InstalledObject obj = new InstalledObject(); obj.objectType = objectType; obj.movmentCost = movmentCost; obj.width = 1; obj.height = 1; return(obj); }
// Internal Functions // TODO: Set this up to read from an XML file (or some other data file) void CreateInstalledObjectPrototypes() { installedObjectPrototypes = new Dictionary <string, InstalledObject> (); //Temporary code for figuring out how to structure everything InstalledObject treesPrototype = InstalledObject.CreatePrototype("Trees", 0, 1, 1); installedObjectPrototypes.Add("Trees", treesPrototype); Debug.Log("InstalledObjectPrototype dictionary populated"); }
public bool IsObjectDraggable() { if (!_buildModeIsObject) { return(true); } InstalledObject prototype = _world.InstalledObjectPrototypes[_buildModeObjectType]; return(prototype.LinksToNeighbor); }
void CreateinstalledOnjectPrototypes() { InstalledObjectPrototypes = new Dictionary <string, InstalledObject>(); InstalledObjectPrototypes.Add("Wall", InstalledObject.CreatePrototype( "Wall", 0, // Impassable 1, //width 1 //Height )); }
/// <summary> /// Returns if the passer can go through this tile. /// More in <see cref="TileObjectBase"/> /// </summary> /// <param name="passer"></param> /// <param name="entryDirection"></param> /// <returns></returns> public bool IsPassableFor(Living passer, Direction entryDirection) { if (InstalledObject == null) { return(true); } else { return(InstalledObject.IsPassableFor(passer, entryDirection)); } }
protected InstalledObject(InstalledObject proto, Tile tile) { this.objectType = proto.objectType; this.movementCost = proto.movementCost; this.width = proto.width; this.height = proto.height; this.tile = tile; //tile.installedObject = this; }
public void Populate(XElement element) { element.SetAttributeValue("x", Position.X); element.SetAttributeValue("y", Position.Y); if (InstalledObject != null) { XElement installedObjectElement = new XElement("InstalledObject"); element.Add(installedObjectElement); InstalledObject.Populate(installedObjectElement); } }
public bool PlaceObject(InstalledObject objInstance) { if (objInstance == null) { //We are uninstalling whatever was here beofre. installedObject = null; return true; } //objInstance isn't null if (installedObject != null) { Debug.LogError("Trying to assign an installed object to a tile that already has one!"); return false; } // At this point, everything fine! installedObject = objInstance; return true; }