public void SpawnStartingProducers(List <Item> buildableItems) { if (buildableItems.Count <= 0) { return; } Vector2 startingPos = new Vector2(-3, -4); foreach (Item item in buildableItems) { Tile_Data baseTile = TileManager.instance.GetTile(startingPos); if (baseTile == null) { continue; } Producer producer = CreateProducerInstance(GetProducerPrototype(item.name)); GameObject prodGObj = SpawnProducer(producer, startingPos); if (prodGObj == null) { continue; } prodGObj.GetComponent <Producer_Controller>().Init(item, producer, baseTile); startingPos.x += producer.tileWidth; } }
public void createBoard() { tiles = new Tile_Data[height * width]; /*for(int i=0; i < tiles.Length; i++){ * tiles[i] = new Tile_Data(); * }*/ foreach (Tile_Behaviour child in transform.GetComponentsInChildren <Tile_Behaviour>()) { DestroyImmediate(child.gameObject); } int index; for (int i = 0; i < height; i++) { for (int j = 0; j < width; j++) { if (mirror) { tile_pos = new Vector3((float)j * -tile_size, 0, (float)i * tile_size); } else { tile_pos = new Vector3((float)j * tile_size, 0, (float)i * tile_size); } GameObject tile_go = Instantiate(TilePrefab, tile_pos, Quaternion.identity, transform); index = i * width + j; //----Data-Structure--------------- tiles[index] = new Tile_Data(); tiles[index].index = index; tiles[index].myGO = tile_go; tiles[index].myBehaviour = tile_go.GetComponent <Tile_Behaviour>(); if (i != 0) { tiles[index].neighbours[0] = (i - 1) * width + j; } if (i != height - 1) { tiles[index].neighbours[1] = (i + 1) * width + j; } if (j != 0) { tiles[index].neighbours[2] = i * width + j - 1; } if (j != width - 1) { tiles[index].neighbours[3] = i * width + j + 1; } //-----GO-Setup-------------- tile_go.name = "Tile [" + index + "] " + i + " : " + j; tile_go.GetComponent <Tile_Behaviour>().myTile = tiles[index]; tile_go.GetComponent <Tile_Behaviour>().index = index; } } }
public void SaveTileData() { if (grid_data == null) { return; } SavedTiles tiles = new SavedTiles(); List <STile> saved = new List <STile>(); for (int x = 0; x < map_width; x++) { for (int y = 0; y < map_height; y++) { Tile_Data tile = grid_data[x, y]; if (tile == null) { continue; } STile savedTile = new STile(); savedTile.grid_x = tile.X; savedTile.grid_y = tile.Y; savedTile.world_x = tile.worldPos.x; savedTile.world_y = tile.worldPos.y; savedTile.tileType = tile.tileType; if (tile.machine_controller != null) { if (tile.machine_controller.baseTile == tile && tile.machine_controller.machine.buildableType != BuildableType.Producer) { Machine machine = tile.machine_controller.machine; savedTile.hasMachine = true; savedTile.machineName = machine.name; savedTile.machineCondition = machine.machineCondition; Debug.Log("Saving " + savedTile.machineName); } } if (tile.producer != null) { if (tile.machine_controller.baseTile != tile) { continue; } savedTile.hasProducer = true; savedTile.producerName = tile.producer.name; savedTile.productionStage = tile.producer.productionStage; savedTile.itemProduced = tile.producer.current_Blueprint.itemProduced.itemName; Debug.Log("Saving " + savedTile.producerName); } saved.Add(savedTile); } } tiles.areaID = currentArea.id; tiles.savedTiles = saved.ToArray(); JsonWriter.WriteTilesToJson(tiles, currentArea.id); }
public void MoveUnitToTile(Tile_Data targetTile) { GameBoard.singleton.tiles[tile_index].status = statuses.clear; transform.position = targetTile.myGO.transform.position; tile_index = targetTile.index; GameBoard.singleton.tiles[tile_index].status = unitOwner; targetTile.status = unitOwner; exhausted = true; }
public void ReInit(Item _machineItem, Machine _machine, ShipManager ship, Tile_Data tile, List <Tile_Data> neighbors) { // Store the machine as Item that can be placed into an inventory machineItem = _machineItem; machine = _machine; gameObject.name = machine.name; baseTile = tile; worldPosition = tile.worldPos; shipManager = ship; neighborTiles = neighbors; InitVisuals(); }
void StopClean() { Debug.Log("Stopping clean"); characterMovement.LockMovement(false); isUsing = false; DoToolHolder(false); if (tileBeingCleaned == null) { return; } tileBeingCleaned.DecreaseDirtness(); tileBeingCleaned = null; }
public void OnTileDirty(Tile_Data tile) { if (currentArea.tilemap == null) { return; } if (currentArea.id != AreaID.Player_Ship) { return; } if (dirtyTilesGObjMap.ContainsKey(tile)) { if (tile.Dirtiness <= 0) { // Pool the dirty tile GameObject gobj = dirtyTilesGObjMap[tile]; gobj.transform.SetParent(null); ObjectPool.instance.PoolObject(gobj); dirtyTilesGObjMap.Remove(tile); return; } // Increase or Decrease dirt by changing sprite Sprite newDirt = Sprite_Manager.instance.GetDirt(Mathf.RoundToInt(tile.Dirtiness * 10)); if (newDirt == null) { return; } dirtyTilesGObjMap[tile].GetComponentInChildren <SpriteRenderer>().sprite = newDirt; return; } if (tile.Dirtiness <= 0) { Debug.Log("TileManager-- not changing dirty tile because its dirtiness is less than 0"); return; } // Create new one: GameObject dirt = ObjectPool.instance.GetObjectForType("Dirty Tile", true, tile.worldPos); Sprite dirtSprite = Sprite_Manager.instance.GetDirt(Mathf.RoundToInt(tile.Dirtiness * 10)); if (dirtSprite == null) { dirtSprite = Sprite_Manager.instance.GetDirt(1); } dirt.GetComponentInChildren <SpriteRenderer>().sprite = dirtSprite; dirt.transform.SetParent(currentArea.tilemap.transform); dirtyTilesGObjMap.Add(tile, dirt); }
public void Initialize(Item _item) { item = _item; spriteRenderer.sprite = item.sprite; // Check the tile under this item and make it DIRTY! Tile_Data tile = TileManager.instance.GetTile(transform.position); if (tile == null) { Debug.Log("Item found no tile on position " + transform.position); return; } tile.IncreaseDirtness(); }
void TryCleanTile(Tile_Data tile) { if (isUsing == true) { return; } if (tile == null) { return; } DoToolHolder(true); animator.SetTrigger("clean"); characterMovement.LockMovement(true); tileBeingCleaned = tile; Invoke("StopClean", 1); }
public void Init(Item producerAsItem, Producer _producer, Tile_Data _baseTile) { producer = _producer; if (_baseTile.AddProducer(producer) == false) { return; } base.InitMachine(producerAsItem, producer, _baseTile, ShipManager.instance); // The machine controller sets the list of Neighbor tiles, // then we Add the producers on neighbor tiles as well foreach (Tile_Data tile in neighborTiles) { tile.AddProducer(producer); } item_Manager = Item_Manager.instance; timer = new CountdownHelper(0); //storage_inventory = new Inventory(1); inventory_Controller = GetComponentInChildren <Inventory_Controller>(); inventory_Controller.Initialize(ID_Generator.instance.GetMachineID(this), 1); // Position the growth visuals X correctly according to the machine's tile width if (growth_visuals != null) { growth_visuals.transform.localPosition = new Vector2(producer.tileWidth > 1 ? 0.5f : 0, 0.5f); } Debug.Log("Initialized a producer with a blueprint for " + producer.current_Blueprint.itemProduced.itemName); // This runs when a producer has already started producing before if (producer.current_Blueprint.itemProduced.count > 0 && producer.productionStage >= 0) { if (producer.productionStage >= 3) { itemInProduction = item_Manager.CreateInstance(item_Manager.GetPrototype(producer.current_Blueprint.itemProduced.itemName)); SetGrowthVisuals(); CompleteProduction(); return; } StartProduction(true); return; } SetProductionStage(0); // Debug.Log("Producer INITIALIZED: " + " key ingredient = " + producer.productionBlueprints[0].keyIngredient.count + " " + producer.productionBlueprints[0].keyIngredient.itemName + // " secondary ingredient " + producer.productionBlueprints[0].secondaryIngredients[0].count + " " + producer.productionBlueprints[0].secondaryIngredients[0].itemName); }
void RightClickInteract() { if (isUsing == true && machineUsed != null) { machineUsed.CancelRepair(); return; } if (iteminHand != null) { // Check for a machine under the mouse to see if we are dropping this in a cargo hold Vector3 mousePosition = Camera.main.ScreenToWorldPoint(Input.mousePosition); mousePosition.z = 0; Tile_Data tile = TileManager.instance.GetTile(mousePosition); if (tile != null) { if (tile.machine_controller != null) { /* if (tile.machine.Interact(this.gameObject) == true){ * if (tile.machine.machine.systemControlled == ShipSystemType.CargoHold) * DepositItem(); * } */ tile.machine_controller.Interact(this.gameObject); return; //if (tile.machine.Interact(this.gameObject) == true) // return; } } } RaycastHit2D hit = Physics2D.Raycast(Camera.main.ScreenToWorldPoint(Input.mousePosition), Vector2.zero, 0, interactableMask); if (hit.collider != null) { Debug.Log("Hit interactable!"); if (hit.collider.gameObject.GetComponentInParent <Interactable>() != null) { hit.collider.gameObject.GetComponentInParent <Interactable>().TryInteract(this.gameObject); } } else { // NO hit, we are probably trying to drop the currently held item DropItem(); } }
void Use() { Vector3 mousePosition = Camera.main.ScreenToWorldPoint(Input.mousePosition); mousePosition.z = 0; Tile_Data tile = TileManager.instance.GetTile(mousePosition); if (tile != null) { if (tile.machine_controller != null) { // Check distance to mouse, if too far, return if (Vector2.Distance(transform.position, new Vector2(tile.worldPos.x, tile.worldPos.y)) > 4f) { return; } if (iteminHand != null && iteminHand.itemUseType == ItemUseType.Repair) { TryRepairMachine(tile.machine_controller); isUsing = true; return; } // If player is holding nothing or not holding a tool: tile.machine_controller.UserUseMachine(); } else { // Check distance to mouse, if too far, return if (Vector2.Distance(transform.position, mousePosition) > 1.5f) { return; } if (iteminHand != null && iteminHand.itemUseType == ItemUseType.Clean) { TryCleanTile(tile); isUsing = true; return; } } } }
void SetGridFromTileMap() { // Verify that we don't alreay have the tile data /* if (AreaToGridDataMap.ContainsKey(currentArea.id) == true){ * // Load it if we got it * grid_data = AreaToGridDataMap[currentArea.id]; * SetWalkableClamps(); * return; * } */ grid_data = new Tile_Data[map_width, map_height]; for (int x = 0; x < map_width; x++) { for (int y = 0; y < map_height; y++) { Vector3Int nextTileWorldPos = new Vector3Int(startingX + x, startingY + y, 0); if (currentArea.tilemap.HasTile(nextTileWorldPos) == false) { continue; } if (currentArea.tilemap.GetSprite(nextTileWorldPos).name == "Floor") { // Tile is Floor grid_data[x, y] = new Tile_Data(x, y, nextTileWorldPos, TileType.Floor); // If this is a ship, process dirty tiles if (currentArea.id == AreaID.Player_Ship) { grid_data[x, y].RegisterOnDirtCB(OnTileDirty); } continue; } grid_data[x, y] = new Tile_Data(x, y, nextTileWorldPos, TileType.Wall); } } //AreaToGridDataMap.Add(currentArea.id, grid_data); SetWalkableClamps(); }
public void InitMachine(Item _machineItem, Machine _machine, Tile_Data tile, ShipManager ship) { if (tile.AddMachine(this) == false) { return; } // Store the machine as Item that can be placed into an inventory machineItem = _machineItem; machine = _machine; gameObject.name = machine.name; baseTile = tile; worldPosition = tile.worldPos; shipManager = ship; neighborTiles = new List <Tile_Data>(); if (machine.tileWidth > 1 || machine.tileHeight > 1) { if (baseTile == null) { return; } for (int x = 0; x < machine.tileWidth; x++) { for (int y = 0; y < machine.tileHeight; y++) { Tile_Data neighbor = TileManager.instance.GetTile(baseTile.X + x, baseTile.Y + y); if (neighbor != null && neighbor != baseTile) { if (neighbor.AddMachine(this) == true) { neighborTiles.Add(neighbor); //Debug.Log("machine added to neighbor at " + neighbor.worldPos); } } } } InitVisuals(); } }
public Tile_Data[] GetNeighbors(bool getDiags = true) { Tile_Data[] neighbors = new Tile_Data[8]; if (getDiags == true) { neighbors[0] = TileManager.instance.GetTile(X, Y + 1); // N neighbors[1] = TileManager.instance.GetTile(X + 1, Y + 1); // NE neighbors[2] = TileManager.instance.GetTile(X + 1, Y); // E neighbors[3] = TileManager.instance.GetTile(X + 1, Y - 1); // SE neighbors[4] = TileManager.instance.GetTile(X, Y - 1); // S neighbors[5] = TileManager.instance.GetTile(X - 1, Y - 1); // SW neighbors[6] = TileManager.instance.GetTile(X - 1, Y); // W neighbors[7] = TileManager.instance.GetTile(X - 1, Y + 1); // NW } else { neighbors = new Tile_Data[4]; neighbors[0] = TileManager.instance.GetTile(X, Y + 1); // N neighbors[1] = TileManager.instance.GetTile(X + 1, Y); // E neighbors[2] = TileManager.instance.GetTile(X, Y - 1); // S neighbors[3] = TileManager.instance.GetTile(X - 1, Y); // W } return(neighbors.Where(tile => tile != null).ToArray()); }
public IEnumerator MoveOverTime(Tile_Data.Node prev_tile, Tile_Data.Node temp_tile) { float elapsedTime = 0; float duration = 2; while (elapsedTime < duration) { transform.position = Vector3.Lerp(new Vector3(controller.curr_scenario.tile_grid.getTiles()[prev_tile.id[0], prev_tile.id[1]].position.x, (float)(controller.curr_scenario.tile_grid.getTiles()[prev_tile.id[0], prev_tile.id[1]].position.y + (controller.curr_scenario.tile_grid.getTiles()[prev_tile.id[0], prev_tile.id[1]].GetComponent<SpriteRenderer>().sprite.rect.height) / 100) + 0.15f, curr_tile.position.z), new Vector3(controller.curr_scenario.tile_grid.getTiles()[temp_tile.id[0], temp_tile.id[1]].position.x, (float)(controller.curr_scenario.tile_grid.getTiles()[temp_tile.id[0], temp_tile.id[1]].position.y + (controller.curr_scenario.tile_grid.getTiles()[temp_tile.id[0], temp_tile.id[1]].GetComponent<SpriteRenderer>().sprite.rect.height) / 100) + 0.15f, curr_tile.position.z), elapsedTime/duration); elapsedTime += Time.deltaTime; yield return new WaitForEndOfFrame(); } }
void HighLightWalkableTiles(Tile_Data tile, int walkLength) { //Debug.Log("--------------------"); int steps = 0; int thisStepSize = 0; indexesToCheck = new List <int>(); indexesChecked = new List <int>(); indexesOfHighlighted = new List <int>(); indexesToCheck.Add(tile.index); while (steps < walkLength) { thisStepSize = indexesToCheck.Count; for (int j = 0; j < thisStepSize; j++) { Tile_Data tileToCheck = GameBoard.singleton.tiles[indexesToCheck[0]]; //Debug.Log(tileToCheck.myGO.name); //checking one tile for neighbours for (int i = 0; i < tileToCheck.neighbours.Length; i++) { if (tileToCheck.neighbours[i] >= 0) { if (!indexesChecked.Contains(tileToCheck.neighbours[i])) { if (!indexesToCheck.Contains(tileToCheck.neighbours[i])) { if (GameBoard.singleton.tiles[tileToCheck.neighbours[i]].status == statuses.clear) { //Debug.Log(GameBoard.singleton.tiles[tileToCheck.neighbours[i]].myGO.name + " is clear"); GameBoard.singleton.tiles[tileToCheck.neighbours[i]].myBehaviour.highlighted = true; GameBoard.singleton.tiles[tileToCheck.neighbours[i]].myBehaviour.CheckForHighlight(); indexesOfHighlighted.Add(tileToCheck.neighbours[i]); indexesToCheck.Add(tileToCheck.neighbours[i]); } else if (GameBoard.singleton.tiles[tileToCheck.neighbours[i]].status == selectedUnit.unitOwner) { //Debug.Log(GameBoard.singleton.tiles[tileToCheck.neighbours[i]].myGO.name + " is owned by my unit"); indexesToCheck.Add(tileToCheck.neighbours[i]); } else { //Debug.Log(GameBoard.singleton.tiles[tileToCheck.neighbours[i]].myGO.name + " is blocked"); } } else { //Debug.Log(tileToCheck.neighbours[i]+" is already in list to be checked"); } } else { //Debug.Log(tileToCheck.neighbours[i]+" is already checked"); } } } indexesToCheck.Remove(tileToCheck.index); indexesChecked.Add(tileToCheck.index); } steps++; } }