public void BuildBuilding(Building building) { float baseHeight = GetTileAtPosition(building.coveredTiles [0]).transform.position.y; if (!GameManager.isLoadingFromSave) { List <ResourcePickup> resourcesToConsume = new List <ResourcePickup> (); foreach (Vector2 pos in building.coveredTiles) { resourcesToConsume.Add(ResourcePickup.GetAtPosition(pos)); tiles [pos].MoveTile(baseHeight); } ConsumeResources(resourcesToConsume); } else { foreach (Vector2 pos in building.coveredTiles) { tiles [pos].MoveTile(baseHeight); } } building.Build(); buildings.Add(building); if (building is ProductionBuilding) { Transform pad = (building as ProductionBuilding).pad; if (pad != null) { Vector2 pos2D = PosToV2(pad.position); pads.Add(pos2D, pad.position.y); } } }
public SavedPickup(Pickup pickup) { tilePos = new SavedTilePos(pickup.position); if (pickup is ResourcePickup) { ResourcePickup resourcePickup = pickup as ResourcePickup; pickupType = 0; index = resourcePickup.info.ToIndex(); stackHeight = pickup.gameObjects.Count; } else if (pickup is WeaponPickup) { WeaponPickup weaponPickup = pickup as WeaponPickup; pickupType = 1; index = weaponPickup.info.ToIndex(); stackHeight = 1; } else if (pickup is AugmentPickup) { AugmentPickup augmentPickup = pickup as AugmentPickup; pickupType = 2; index = augmentPickup.info.ToIndex(); stackHeight = 1; } else { Debug.LogError("Unrecognized pickup type"); pickupType = -1; index = -1; stackHeight = -1; } }
IEnumerator BuildBuilding(Building building) { isBuilding = true; buildBar.transform.position = building.gameObject.transform.position - new Vector3(building.info.anchorOffset.x, 0, building.info.anchorOffset.y); buildBar.gameObject.SetActive(true); barUI.transform.localScale = new Vector3(0, 1, 1); float endTime = Time.time + buildTime; while (isBuilding && (Time.time < endTime)) { float timeLeft = endTime - Time.time; barUI.transform.localScale = new Vector3(1 - (timeLeft / buildTime), 1, 1); yield return(null); } if (isBuilding) { if (building.state == Building.BuildingState.Blueprint) { blueprints.Remove(ResourcePickup.GetAtPosition(building.coveredTiles [0])); tm.BuildBuilding(building); } else if (building.state == Building.BuildingState.Inactive) { tm.BreakDownBuilding(building); } } isBuilding = false; buildBar.gameObject.SetActive(false); SavedGame.UpdateBuildings(); }
public void TestForCrafting() { foreach (Vector2 key in tm.pickups.Keys.ToList()) { if (ResourcePickup.IsAtPosition(key)) { ResourcePickup resourceToTest = tm.pickups [key] as ResourcePickup; Vector3 spawnPos; Craftable craftableInfo = GetCraftableForResource(resourceToTest, out spawnPos); if (craftableInfo is BuildingInfo) { BuildingInfo buildingInfo = craftableInfo as BuildingInfo; if (!blueprints.ContainsKey(resourceToTest)) { Building newBuilding = tm.SpawnBuilding(spawnPos, buildingInfo.prefab, buildingInfo, resourceToTest.island); blueprints.Add(resourceToTest, newBuilding); } } else if (craftableInfo is WeaponInfo) { WeaponInfo weaponInfo = craftableInfo as WeaponInfo; tm.SpawnWeapon(spawnPos, weaponInfo, resourceToTest.island); } else if (craftableInfo is AugmentInfo) { AugmentInfo augmentInfo = craftableInfo as AugmentInfo; tm.SpawnAugment(spawnPos, augmentInfo, resourceToTest.island); } } } }
public void TestForBlueprints() { List <ResourcePickup> blueprintKeysToRemove = blueprints.Keys.ToList(); foreach (Vector2 key in tm.pickups.Keys.ToList()) { if (ResourcePickup.IsAtPosition(key)) { ResourcePickup resourceToTest = tm.pickups [key] as ResourcePickup; Vector3 spawnPos; Craftable craftableInfo = GetCraftableForResource(resourceToTest, out spawnPos); if (craftableInfo is BuildingInfo) { blueprintKeysToRemove.Remove(resourceToTest); } } } foreach (ResourcePickup key in blueprintKeysToRemove) { Destroy(blueprints[key].gameObject); blueprints.Remove(key); } }
private void PickupResource(ResourcePickup pickup) { CarriedResourceAmount += pickup.resourceQuantity; CarriedResourceType = pickup.resourceType; // Just in case Destroy(pickup.gameObject); onResourceCollected?.Invoke(this); }
private void GatherOrder(ResourcePickup pickup) { if (CarriedResourceAmount == 0 || CarriedResourceType == pickup.resourceType) { currentOrder = Orders.GATHER; gatherTarget = pickup; CarriedResourceType = pickup.resourceType; agent.SetDestination(pickup.gameObject.transform.position); } }
public void MoveTile(float newHeight) { tile.transform.position = new Vector3(tile.transform.position.x, newHeight, tile.transform.position.z); Vector2 posV2 = TerrainManager.PosToV2(tile.transform.position); if (ResourcePickup.IsAtPosition(posV2)) { ResourcePickup resource = ResourcePickup.GetAtPosition(posV2); float resourceHeight = TerrainManager.instance.PadAtPosition(posV2).GetValueOrDefault(tile.transform.position.y); for (int i = 0; i < resource.gameObjects.Count; i++) { resource.gameObjects[i].transform.position = new Vector3(resource.gameObjects[i].transform.position.x, resourceHeight + (TerrainManager.instance.stackHeight * i), resource.gameObjects[i].transform.transform.position.z); } resource.UpdatePosition(); } }
/* Name: OnTriggerEnter2D() * Description: Processes reactions to entering trigger regions on the map. * Including: Doors/exits * Parameters: Collider2D trigger - The trigger region entered. */ protected void OnTriggerEnter2D(Collider2D trigger) { //base.OnTriggerEnter2D(triggerZone); // Check for any resource dropped on the WarpPad. if (trigger.tag == "Resource") { ResourcePickup resourceToWarp = trigger.GetComponent <ResourcePickup>(); Player.AcquireMineral(resourceToWarp.Value()); Destroy(trigger.gameObject); //this.GetComponent<Animator>(). } }
public void PickUpPickup() { Vector2 playerPos = TerrainManager.PosToV2(playerTransform.position); if (ResourcePickup.IsAtPosition(playerPos)) { CollectResource(ResourcePickup.GetAtPosition(playerPos)); } else if (WeaponPickup.IsAtPosition(playerPos)) { PickupWeapon(WeaponPickup.GetAtPosition(playerPos)); } else if (AugmentPickup.IsAtPosition(playerPos)) { PickupAugment(AugmentPickup.GetAtPosition(playerPos)); } }
public static void GetResourcesInfos() { string infos = ""; foreach (ChunkTypes type in Enum.GetValues(typeof(ChunkTypes))) { ResourcePickup prefab = Singleton.Manager <ResourceManager> .inst.SpawnResource(type, Vector3.zero, Quaternion.identity); if (prefab) { infos += prefab.name + ";" + type.ToString() + ";" + prefab.ChunkRarity.ToString() + ";" + RecipeManager.inst.GetChunkPrice(type) + ";" + prefab.rbody.mass + "\n"; } } infos += "ID;Object Name;Rarity;Value;Mass"; File.WriteAllText(Path.Combine(FolderStructure.DataFolder, "ResourcesInfos.csv"), infos); }
public virtual void ProduceResources() { turnsUntilNextResource = turnWaitPerResource; for (int i = 0; i < resourcesProducedPerCycle; i++) { if (hasLimitedSupply && supply <= 0) { break; } if (exceedsPadCapacity) { break; } TerrainManager.instance.SpawnResource(position: spawnPos, info: ResourceInfo.GetInfoFromType(resourceType), island: island); if (hasLimitedSupply) { supply -= 1; } } if (movesResources) { ResourcePickup.GetAtPosition(TerrainManager.PosToV2(spawnPos)).AnimateMove(productionCenter.position, false, resourcesProducedPerCycle); } SavedGame.UpdateBuildingSupply(this); if (!autoCycles) { state = BuildingState.Waiting; } if (hasLimitedSupply && supply <= 0) { Deactivate(); } }
public void CollectResource(ResourcePickup resource) { int stackCount = resource.gameObjects.Count; int countToPickUp = stackCount; float carriedResourceCount = GetCarriedResourceCount(); if (carriedResourceCount + stackCount > curInventorySize) { int remainingSpace = Mathf.FloorToInt(curInventorySize - carriedResourceCount); if (remainingSpace <= 0) { return; } else { countToPickUp = remainingSpace; } } if (countToPickUp >= stackCount) { resource.island.pickups.Remove(resource); tm.pickups.Remove(resource.position); } inventory [resource.info.ToIndex()] += (float)countToPickUp; List <ResourcePickup> affectedResources = new List <ResourcePickup> { resource }; List <int> countsToPickUp = new List <int> { countToPickUp }; tm.ConsumeResources(affectedResources, countsToPickUp); GameManager.instance.SaveThisTurn(); UpdateInventoryUI(); }
void UpdateBehavior() { switch (currentOrder) { case Orders.MOVE: if (Vector3.Distance(gameObject.transform.position, moveTarget) <= moveThreshold) { currentOrder = Orders.IDLE; } break; case Orders.GATHER: if (gatherTarget == null) { //If old target lost, look around for new resource of that same type. Collider[] potentialTargets = Physics.OverlapSphere(gameObject.transform.position, gatherAquisitionRadius); for (int i = 0; i < potentialTargets.Length; ++i) { ResourcePickup newTarget = null; Transform parent = potentialTargets[i].gameObject.transform.parent; if (parent) { newTarget = potentialTargets[i].gameObject.transform.parent.GetComponent <ResourcePickup>(); } if (newTarget && newTarget.resourceType == CarriedResourceType) { gatherTarget = newTarget; agent.SetDestination(gatherTarget.transform.position); return; } } //No new targets found if (CarriedResourceAmount == 0) { CarriedResourceType = ResourceTypes.NONE; } currentOrder = Orders.IDLE; } else { Debug.Log("Have target"); //Check if we have arrived at our target. if (Vector3.Distance(gameObject.transform.position, gatherTarget.transform.position) < interactRadius) { Debug.Log("In range"); PickupResource(gatherTarget); } } break; case Orders.MINE: if (miningTarget == null) { currentOrder = Orders.IDLE; agent.SetDestination(transform.position); break; } if (Vector3.Distance(transform.position, miningTarget.transform.position) < miningRadius) { agent.SetDestination(transform.position); if (miningTimer <= 0) { miningTimer = miningCooldown; miningTarget.GetComponent <ResourceRock>().Damage(miningDamage); } } else { agent.SetDestination(miningTarget.transform.position); } break; case Orders.DELIVER: if (deliverTarget) { //Check if we have arrived at target. if (Vector3.Distance(gameObject.transform.position, deliverTarget.targetObject.transform.position) < interactRadius) { Debug.Log("In range of building"); CarriedResourceAmount -= deliverTarget.targetObject.GetComponent <Building>().Deliver(CarriedResourceType, CarriedResourceAmount); currentOrder = Orders.IDLE; } } else { currentOrder = Orders.IDLE; } break; } }
public ResourcePickup SpawnResource(Vector3 position, ResourceInfo info, Island island, bool initialSpawn = false) { Vector2 posV2 = PosToV2(position); if (island == null || WeaponPickup.IsAtPosition(posV2)) { return(null); } float startingHeight; GameObject buildingObj = GetBuildingAtPosition(posV2); if (buildingObj != null) { Building building = buildingObj.GetComponent <Building> (); startingHeight = building.height + GetTileAtPosition(posV2).transform.position.y; if (building is ProductionBuilding) { (building as ProductionBuilding).SpawnTop(); } } else { startingHeight = PadAtPosition(posV2).GetValueOrDefault(position.y); } List <Vector2> allDirections = new List <Vector2> () { Vector2.zero, Vector2.right, Vector2.up, Vector2.left, Vector2.down }; foreach (Vector2 direction in allDirections) { Vector2 posToCheck = posV2 + direction; // island = tiles [posToCheck].island; if (GetTileAtPosition(posToCheck) != null) { float posHeightOffset = GetTileAtPosition(posToCheck).transform.position.y - GetTileAtPosition(posV2).transform.position.y; if (ResourcePickup.IsAtPosition(posToCheck)) { ResourcePickup curResource = ResourcePickup.GetAtPosition(posToCheck); // print ("Dir: " + direction + " Resource At Pos, Type: " + curResource.info.type); if (curResource.info.type == info.type) { GameObject resourceGO = CreateResource(info, island.transform); resourceGO.transform.position = new Vector3(posToCheck.x, 0f, posToCheck.y); curResource.gameObjects.Add(resourceGO); resourceGO.transform.Translate(Vector3.up * ((stackHeight * (curResource.gameObjects.Count - 1)) + startingHeight + posHeightOffset)); if (!initialSpawn && !GameManager.isLoadingFromSave) { UpdateResources(); SavedGame.UpdatePickups(); } return(curResource); } } else { // print ("Dir: " + direction + " creating new resource"); GameObject resourceGO = CreateResource(info, island.transform); float height = 0f; if (GetTileAtPosition(posToCheck)) { height = GetTileAtPosition(posToCheck).transform.position.y; } resourceGO.transform.position = new Vector3(posToCheck.x, 0f, posToCheck.y); ResourcePickup resource = new ResourcePickup(info, resourceGO, island); pickups.Add(posToCheck, resource); island.pickups.Add(resource); resourceGO.transform.Translate(Vector3.up * (startingHeight + posHeightOffset)); if (!initialSpawn && !GameManager.isLoadingFromSave) { SavedGame.UpdatePickups(); UpdateResources(); } return(resource); } } else { // print ("Dir: " + direction + " no tile at pos:" + posToCheck); } } return(null); }
public ResourcePickupData(ResourcePickup resourcePickup) { id = resourcePickup.id; nrOfPickups = resourcePickup.nrOfPickups; persistentObject = resourcePickup.persistentObject; }
Craftable GetCraftableForResource(ResourcePickup anchorResource, out Vector3 spawnPos) { ResourceInfo.ResourceType tileType = tm.tiles[anchorResource.position].resourceType; List <Crafting.Recipe> possibleRecipes = GetPossibleRecipes(new Stack(anchorResource.info.type, tileType, anchorResource.gameObjects.Count)); bool hasRecipe = false; Crafting.Recipe confirmedRecipe = new Crafting.Recipe(); List <ResourcePickup> affectedResources = new List <ResourcePickup> (); foreach (var recipe in possibleRecipes) { int correct = 0; for (int y = 0; y < recipe.resources.GetLength(1); y++) { for (int x = 0; x < recipe.resources.GetLength(0); x++) { Vector2 posToCheck = new Vector2(anchorResource.position.x + x, anchorResource.position.y + y); if (ResourcePickup.IsAtPosition(posToCheck) && tm.PadAtPosition(posToCheck) == null) { ResourcePickup resourceAtPos = ResourcePickup.GetAtPosition(posToCheck); if (resourceAtPos.island.buildable) { if (resourceAtPos.info.type == recipe.resources [x, y].resourceType && resourceAtPos.gameObjects.Count == recipe.resources [x, y].count) { // check tile ResourceInfo.ResourceType curTileType = tm.tiles [posToCheck].resourceType; if (curTileType == recipe.resources [x, y].tileType) { affectedResources.Add(resourceAtPos); correct++; } } } } } } if (correct >= recipe.resources.Length) { hasRecipe = true; confirmedRecipe = recipe; break; } } if (hasRecipe) { Craftable craftableInfo = craftableInfos [confirmedRecipe.name]; spawnPos = anchorResource.gameObjects[0].transform.position + new Vector3(craftableInfo.anchorOffset.x, 0, craftableInfo.anchorOffset.y); if (confirmedRecipe.type != Recipe.RecipeType.Building) { tm.ConsumeResources(affectedResources); } return(craftableInfo); } else { spawnPos = Vector3.zero; return(null); } }