/// <summary> /// Grows the plant components each tic /// </summary> /// <param name="frontEnd"></param> /// <returns></returns> public PlantComponent GrowPlant(PlantComponent comp) { PlantComponent newComp; switch (comp.componentType) { case PlantComponentType.Roots: newComp = new PlantComponent(PlantComponentType.Roots, roots[0].color); roots.Add(newComp); break; case PlantComponentType.Stem: newComp = new PlantComponent(PlantComponentType.Stem, stems[0].color); stems.Add(newComp); break; default: newComp = new PlantComponent(PlantComponentType.Empty, roots[0].color); Debug.LogError("Trying to grow bad component. Empty comp created"); break; } newComp.parent = comp; Debug.Log("Growing " + newComp.GetID()); return(newComp); }
private GameObject InstantiateComponent(PlantComponent newComp, Transform parentCompTransform) { string prefabPath = ConstantValues.PathPrefabs + "/"; switch (newComp.componentType) { case PlantComponentType.Roots: prefabPath = prefabPath + ConstantValues.PrefabRoots; break; case PlantComponentType.Stem: prefabPath = prefabPath + ConstantValues.PrefabStem; break; case PlantComponentType.Leaves: prefabPath = prefabPath + ConstantValues.PrefabLeaves; break; default: Debug.LogError("PrefabPath bad"); break; } GameObject newCompGO = Instantiate(Resources.Load(prefabPath), parentCompTransform) as GameObject; newCompGO.GetComponent <PlantComponentFrontEnd>().PlantComponent = newComp; newCompGO.GetComponent <PlantComponentFrontEnd>().parent = parentCompTransform; newCompGO.name = newComp.GetID(); newCompGO.transform.localPosition = Vector3.up * 0.5f; // TODO: arbitrary amount up so it looks good now return(newCompGO); }
public Vector3 SnapToWorktable(PlantComponentFrontEnd plantComponent, Vector3 originalPosition) { Vector3 location; switch (plantComponent.GetPlantComponentType()) { case PlantComponentType.Roots: if (plant.roots.Count == 0) { location = rootLocation.position; addedComponent = plantComponent.PlantComponent; } else { location = originalPosition; Debug.Log("Your plant already has roots"); } break; case PlantComponentType.Stem: if (plant.roots.Count == 0) { location = originalPosition; Debug.Log("Your plant needs roots before you can graft on a stem"); } else if (plant.stems.Count != 0) { location = originalPosition; Debug.Log("Your plant already has a stem"); } else { location = stemLocation.position; addedComponent = plantComponent.PlantComponent; } break; case PlantComponentType.Leaves: if (plant == null || plant.leaves.componentType != PlantComponentType.Empty) { location = originalPosition; } else { location = leafLocation.position; addedComponent = plantComponent.PlantComponent; } break; default: location = originalPosition; Debug.LogError("could not snap to place"); break; } return(location); }
static void InstantiateComponent(PlantComponent comp, Transform plant) { string prefabType; switch (comp.componentType) { case PlantComponentType.Roots: prefabType = ConstantValues.PrefabRoots; break; case PlantComponentType.Stem: prefabType = ConstantValues.PrefabStem; break; case PlantComponentType.Leaves: prefabType = ConstantValues.PrefabLeaves; break; default: prefabType = ConstantValues.PrefabRoots; Debug.LogError("Could not instantiate componenet. Prefab type bad"); break; } GameObject compGO = Instantiate(Resources.Load(ConstantValues.PathPrefabs + "/" + prefabType)) as GameObject; compGO.GetComponent <PlantComponentFrontEnd>().PlantComponent = comp; if (comp.componentType != PlantComponentType.Empty) { if (comp.parent != null) { Debug.LogError("parent 1: " + comp.parent.GetID()); } if (comp.parent == null) { compGO.GetComponent <PlantComponentFrontEnd>().parent = plant; Debug.Log("parent = plant"); } else { Debug.LogError("newComp: " + compGO); Debug.LogError("parent: " + comp.parent.GetID()); compGO.GetComponent <PlantComponentFrontEnd>().parent = GameObject.Find(comp.parent.GetID()).transform; Debug.Log("parent = " + compGO.GetComponent <PlantComponentFrontEnd>().parent.name); } compGO.transform.SetParent(compGO.GetComponent <PlantComponentFrontEnd>().parent); compGO.name = comp.GetID(); Debug.Log("instantiated comp name: " + compGO.name); compGO.transform.localPosition = Vector3.up * 0.5f; // TODO: arbitrary amount up so it looks good now } }
public void SetForPlantComponent(PlantComponent Plant) { ToggleForSlot(false); PlantProperty PP = Plant.PlantProperty; OutputText.text = "Output: " + PP.ProductionValue.ToString(); FuelColorText.text = "Color: " + PP.Fuel.Color.ToString(); FuelShapeText.text = "Shape: " + PP.Fuel.Shape.ToString(); // FuelSizeText.text = "Size: " + PP.Fuel.Size.ToString (); FootprintColorText.text = "Color: " + PP.Footprint.Color.ToString(); FootprintShapeText.text = "Shape: " + PP.Footprint.Shape.ToString(); // FootprintSizeText.text = "Size: " + PP.Footprint.Size.ToString (); // FuelText.text = Plant.PlantProperty.ProductionValue; }
// Graft? public void AddComponent(PlantComponent newComponent) { switch (newComponent.componentType) { case PlantComponentType.Roots: Debug.LogError("Shouldn't add roots this way"); break; case PlantComponentType.Stem: stems.Add(newComponent); break; case PlantComponentType.Leaves: leaves = newComponent; break; } }
void CalculateResources() { GameObject[] PlantObjects = GameObject.FindGameObjectsWithTag("Plant"); ActivePlantComponents = new List <PlantComponent> (); foreach (GameObject plant in PlantObjects) { PlantComponent component = plant.GetComponent <PlantComponent> (); if (component.IsSlotted()) { ActivePlantComponents.Add(component); } } // Have the resource value set on ActivePlant component, this is set based on the footprint of what was there before int GeneratedResources = 0; foreach (PlantComponent Plant in ActivePlantComponents) { GeneratedResources += Plant.CurrentProductionValue(); } StartCoroutine(FillResources(GeneratedResources)); // perhaps produce first, drain second..... }
public void Execute(ArchetypeChunk chunk, int chunkIndex, int firstEntityIndex) { var translations = chunk.GetNativeArray(TranslationTypeHandle); var entityIntents = chunk.GetNativeArray(EntityInfoHandle); var entities = chunk.GetNativeArray(EntityType); for (var i = 0; i < chunk.Count; i++) { Tiles state = (Tiles)entityIntents[i].type; switch (state) { case Tiles.Rock: //Debug.Log("destroying rock"); // destroy rock addRemoveTags.Enqueue(new TagInfo { shouldRemove = 0, entity = entityIntents[i].specificEntity, type = Tags.Disable }); // remove perform task and add needs task tags addRemoveTags.Enqueue(new TagInfo { shouldRemove = 0, entity = entities[i], type = Tags.NeedsTask }); addRemoveTags.Enqueue(new TagInfo { shouldRemove = 1, entity = entities[i], type = Tags.PerformTask }); break; case Tiles.Till: float tillBlockHeight = 0.25f; EntityInfo tillInfo = new EntityInfo { type = (int)Tiles.Till }; if ( grid.TryAdd(GridData.ConvertToHash((int)translations[i].Value.x, (int)translations[i].Value.z), tillInfo)) { float3 pos = new float3((int)translations[i].Value.x, tillBlockHeight, (int)translations[i].Value.z); changes.Enqueue(new float2((int)pos.x, (int)pos.z)); } // add needs task and remove perform task tags addRemoveTags.Enqueue(new TagInfo { shouldRemove = 0, entity = entities[i], type = Tags.NeedsTask }); addRemoveTags.Enqueue(new TagInfo { shouldRemove = 1, entity = entities[i], type = Tags.PerformTask }); break; case Tiles.Plant: // since the plant needs to be instantiated and then cached // into the hash table it's done in the main thread // add needs task and remove perform task tags addRemoveTags.Enqueue(new TagInfo { shouldRemove = 0, entity = entities[i], type = Tags.NeedsTask }); addRemoveTags.Enqueue(new TagInfo { shouldRemove = 1, entity = entities[i], type = Tags.PerformTask }); break; case Tiles.Harvest: EntityInfo harvestInfo = new EntityInfo { type = (int)Tiles.Till }; if (plantInfo.HasComponent(entityIntents[i].specificEntity)) { PlantComponent plant = plantInfo[entityIntents[i].specificEntity]; if (plant.reserveIndex == entities[i].Index && grid.TryAdd( GridData.ConvertToHash((int)translations[i].Value.x, (int)translations[i].Value.z), harvestInfo)) { //UnityEngine.Debug.Log("harvesting : " + entityIntents[i].specificEntity.Index); // plant needs to follow the farmer PlantComponent plantData2 = new PlantComponent { timeGrown = plantGrowthMax, state = (int)PlantState.Following, farmerToFollow = entities[i], reserveIndex = plant.reserveIndex }; setInfo.Enqueue(new ComponentSetInfo { entity = entityIntents[i].specificEntity, plantComponent = plantData2 }); } else if (plant.reserveIndex != entities[i].Index) { entityIntents[i] = new EntityInfo { specificEntity = entityIntents[i].specificEntity, type = (short)Tiles.None }; } } else { entityIntents[i] = new EntityInfo { specificEntity = entityIntents[i].specificEntity, type = (short)Tiles.None }; } // remove perform task and add needs task addRemoveTags.Enqueue(new TagInfo { shouldRemove = 0, entity = entities[i], type = Tags.NeedsTask }); addRemoveTags.Enqueue(new TagInfo { shouldRemove = 1, entity = entities[i], type = Tags.PerformTask }); break; case Tiles.Store: // multiple entities can try to delete this plant at the store // the single threaded job at the end takes care of this // we need to remove the plant from the farmer PlantComponent plantData = new PlantComponent { timeGrown = plantGrowthMax, state = (int)PlantState.Deleted }; setInfo.Enqueue(new ComponentSetInfo { entity = entityIntents[i].specificEntity, plantComponent = plantData }); // remove perform task and add needs task tags addRemoveTags.Enqueue(new TagInfo { shouldRemove = 0, entity = entities[i], type = Tags.NeedsTask }); addRemoveTags.Enqueue(new TagInfo { shouldRemove = 1, entity = entities[i], type = Tags.PerformTask }); // and actually sell stuff here unsafe { Interlocked.Increment(ref ((int *)plantsSold.GetUnsafePtr())[0]); } break; default: break; } } }
// GET: api/Dashboard public List <Plant> GetPlants() { PlantComponent plantComp = new PlantComponent(); return(plantComp.GetPlants()); }
// randomly determines a task and then finds the right tiles that // will help the task occur public void Execute(ArchetypeChunk chunk, int chunkIndex, int firstEntityIndex) { var translations = chunk.GetNativeArray(TranslationTypeHandle); var movements = chunk.GetNativeArray(MovementTypeHandle); var entityIntents = chunk.GetNativeArray(EntityInfoHandle); var entities = chunk.GetNativeArray(EntityType); for (var i = 0; i < chunk.Count; i++) { // // determine what the task for this entity is // Tiles taskValue = (Tiles)(randArray[(nextIndex + entities[i].Index) % randArray.Length] % 4) + 1; nextIndex++; if (entityIntents[i].type == (int)Tiles.Harvest) { // we just harvested and now need to get the plant // to the store taskValue = Tiles.Store; } // // look for the best tile for performing that task // float2 pos = new float2(translations[i].Value.x, translations[i].Value.z); float2 foundLocation; switch (taskValue) { case Tiles.Rock: foundLocation = GridData.Search(randArray, nextIndex, gridHashMap, pos, radiusForSearch, (int)taskValue, gridSize, gridSize); nextIndex++; break; case Tiles.Till: // default is currently Till foundLocation = GridData.Search(randArray, nextIndex, gridHashMap, pos, radiusForSearch, 0, gridSize, gridSize); nextIndex++; if (foundLocation.x == -1) { foundLocation = GridData.Search(randArray, nextIndex, gridHashMap, pos, radiusForSearch * 3, 0, gridSize, gridSize); nextIndex++; } break; case Tiles.Plant: // need to search for tilled soil foundLocation = GridData.Search(randArray, nextIndex, gridHashMap, pos, radiusForSearch, (int)Tiles.Till, gridSize, gridSize); nextIndex++; if (foundLocation.x == -1) { foundLocation = GridData.Search(randArray, nextIndex, gridHashMap, pos, radiusForSearch * 3, (int)Tiles.Till, gridSize, gridSize); nextIndex++; } break; case Tiles.Harvest: // searches for the plants to go harvest them foundLocation = GridData.FindMaturePlant(randArray, nextIndex, gridHashMap, pos, radiusForSearch, (int)Tiles.Plant, gridSize, gridSize, ref IsPlantType, plantGrowthMax); nextIndex++; if (foundLocation.x == -1) { foundLocation = GridData.FindMaturePlant(randArray, nextIndex, gridHashMap, pos, radiusForSearch * 3, (int)Tiles.Plant, gridSize, gridSize, ref IsPlantType, plantGrowthMax); nextIndex++; } break; default: // searches for the stores to go and sell a plant foundLocation = GridData.Search(randArray, nextIndex, gridHashMap, pos, radiusForSearch, (int)Tiles.Store, gridSize, gridSize); nextIndex++; // no store close by if (foundLocation.x == -1) { // need to find somewhere to get rid of the plant foundLocation = GridData.Search(randArray, nextIndex, gridHashMap, pos, gridSize, (int)Tiles.Store, gridSize, gridSize); nextIndex++; } break; } //UnityEngine.Debug.Log("finding new task : " + taskValue + " for entity " + entities[i].Index + // " found " + foundLocation.x); // // If a tile was found, set up the data for the task // if there's a rock in the way, then just go break the rock // if (foundLocation.x != -1 && foundLocation.y != -1) { float2 findMiddle = MovementSystem.FindMiddlePos(pos, foundLocation); var rockPos = GridData.FindTheRock(gridHashMap, pos, findMiddle, foundLocation, gridSize, gridSize); //Debug.Log(index + " Start: " + pos.x + " " + pos.y + " middle : " + findMiddle.x + " " + findMiddle.y + " target pos : " + // foundLocation.x + " " + foundLocation.y + " " + rockPos + " intention: " + taskValue); if ((int)rockPos.x != -1 && (int)rockPos.y != -1) { // we found a rock so go mine it on the path // if rock position on an x or y direction then don't change the middle // otherwise re-find the middle if ((int)rockPos.x == (int)pos.x || (int)rockPos.y == (int)pos.y) { findMiddle = MovementSystem.FindMiddlePos(pos, rockPos); } //Debug.Log("Updated rock position to: " + rockPos + "Actor is now chasing a rock"); rockPos = new float2(rockPos.x + 0.5f, rockPos.y + 0.5f); var data = new MovementComponent { startPos = pos, speed = 2, targetPos = rockPos, middlePos = findMiddle, }; movements[i] = data; // if we are on the way to the store then destroy the plant and // mine the rock if (taskValue == Tiles.Store) { // destroy the plant as there's a rock in the way or no place to take it //UnityEngine.Debug.Log("plant should be destroyed on farmer"); PlantComponent plantInfo = new PlantComponent { timeGrown = plantGrowthMax, state = (int)PlantState.Deleted, }; setInfo.Enqueue(new ComponentSetInfo { entity = entityIntents[i].specificEntity, plantComponent = plantInfo }); } // get the index into the array of rocks so that we can find it // to destroy it EntityInfo fullRockData = GridData.getFullHashValue(gridHashMap, (int)rockPos.x, (int)rockPos.y); entityIntents[i] = fullRockData; //Debug.Log("rock task happening : " + rockEntityIndex + " " + tmp.Index); // remove needs task and add moving tag addRemoveTags.Enqueue(new TagInfo { shouldRemove = 1, entity = entities[i], type = Tags.NeedsTask }); addRemoveTags.Enqueue(new TagInfo { shouldRemove = 0, entity = entities[i], type = Tags.Moving }); int key = GridData.ConvertToHash((int)rockPos.x, (int)rockPos.y); removals.Enqueue(new RemovalInfo { key = key, requestingEntity = entities[i] }); } else { foundLocation = new float2(foundLocation.x + 0.5f, foundLocation.y + 0.5f); var data = new MovementComponent { startPos = pos, speed = 2, targetPos = foundLocation, middlePos = findMiddle, }; movements[i] = data; //Debug.Log("doing a task and about to move: " + pos.x + " " + pos.y + // " target is : " + data.targetPos.x + " " + data.targetPos.y); //Debug.Log("rock value: " + rockPos); // remove needs task and add moving tag addRemoveTags.Enqueue(new TagInfo { shouldRemove = 1, entity = entities[i], type = Tags.NeedsTask }); addRemoveTags.Enqueue(new TagInfo { shouldRemove = 0, entity = entities[i], type = Tags.Moving }); int key; switch (taskValue) { case Tiles.Rock: key = GridData.ConvertToHash((int)foundLocation.x, (int)foundLocation.y); // get the index into the array of rocks so that we can find it // to destroy it EntityInfo fullRockData = GridData.getFullHashValue(gridHashMap, (int)rockPos.x, (int)rockPos.y); //Debug.Log("rock task happening : " + rockEntityIndex + " " + tmp.Index); entityIntents[i] = fullRockData; // remove the rock from the hash so that nobody else tries to get it removals.Enqueue(new RemovalInfo { key = key, requestingEntity = entities[i] }); break; case Tiles.Till: EntityInfo tillData = new EntityInfo { type = (int)Tiles.Till }; entityIntents[i] = tillData; break; case Tiles.Plant: key = GridData.ConvertToHash((int)foundLocation.x, (int)foundLocation.y); removals.Enqueue(new RemovalInfo { key = key, requestingEntity = entities[i] }); EntityInfo plantData = new EntityInfo { type = (int)Tiles.Plant }; entityIntents[i] = plantData; break; case Tiles.Harvest: key = GridData.ConvertToHash((int)foundLocation.x, (int)foundLocation.y); EntityInfo fullData = GridData.getFullHashValue(gridHashMap, (int)foundLocation.x, (int)foundLocation.y); // check to make sure plant is grown before harvesting // if it's not then find something else to do PlantComponent plantInfo = IsPlantType[fullData.specificEntity]; if (plantInfo.timeGrown >= plantGrowthMax) { removals.Enqueue(new RemovalInfo { key = key, requestingEntity = entities[i] }); EntityInfo harvestData = new EntityInfo { type = (int)Tiles.Harvest, specificEntity = fullData.specificEntity }; entityIntents[i] = harvestData; //Debug.Log("plant ready to harvest at : " + fullData.specificEntity.Index + " " + index + " " + // foundLocation.x + " " + foundLocation.y); } else { // not ready to harvest, try something else // add needs task and remove moving tag addRemoveTags.Enqueue(new TagInfo { shouldRemove = 0, entity = entities[i], type = Tags.NeedsTask }); addRemoveTags.Enqueue(new TagInfo { shouldRemove = 1, entity = entities[i], type = Tags.Moving }); } break; case Tiles.Store: EntityInfo storeInfo = new EntityInfo { type = (int)Tiles.Store, specificEntity = entityIntents[i].specificEntity }; entityIntents[i] = storeInfo; //Debug.Log("plant going to the store " + foundLocation.x + " " + foundLocation.y + " " + entityIntent.specificEntity.Index); break; default: break; } } } else { // no tile was found for the task, so this method // will run again and hopefully pick another task next time // Debug.Log("location wasn't found - find another task"); } } }
public void Execute(ArchetypeChunk batchInChunk, int batchIndex) { var plantComponents = batchInChunk.GetNativeArray(PlantComponentTypeHandle); var scales = batchInChunk.GetNativeArray(NonUniformScaleTypeHandle); var entities = batchInChunk.GetNativeArray(EntityType); for (var i = 0; i < batchInChunk.Count; i++) { PlantState state = (PlantState)plantComponents[i].state; switch (state) { case PlantState.None: break; case PlantState.Growing: float currentTotalTime = deltaTime + plantComponents[i].timeGrown; if (currentTotalTime < maxGrowth) { float currentScale = currentTotalTime / 5.0f; scales[i] = new NonUniformScale { Value = new float3(currentScale, 1.0f, currentScale) }; var data = new PlantComponent { timeGrown = currentTotalTime, state = (int)PlantState.Growing, }; plantComponents[i] = data; } else { var data = new PlantComponent { timeGrown = maxGrowth, state = (int)PlantState.None, }; plantComponents[i] = data; } break; case PlantState.Following: float3 pos = translations[plantComponents[i].farmerToFollow].Value; float3 trans = new float3(pos.x, pos.y + 2, pos.z); setInfo.Enqueue(new ComponentTransInfo { entity = entities[i], trans = trans }); break; case PlantState.Deleted: // multiple entities can try to delete the plant // taken care of in the single threaded end of the jobs //UnityEngine.Debug.Log("deleting a plant " + entity.Index); plantChanges.Enqueue(entities[i]); break; default: break; } } }
protected override void OnUpdate() { // // chunk vars // var plantType = GetComponentTypeHandle<PlantComponent>(); // var scaleType = GetComponentTypeHandle<NonUniformScale>(); // var entities = GetEntityTypeHandle(); // // // job // var job = new PlantSystemJob(); // job.deltaTime = UnityEngine.Time.deltaTime; // job.maxGrowth = MAX_GROWTH; // job.plantChanges = plantCreationDeletionInfo.AsParallelWriter(); // job.translations = GetComponentDataFromEntity<Translation>(true); // job.PlantComponentTypeHandle = plantType; // job.NonUniformScaleTypeHandle = scaleType; // job.setInfo = componentSetInfo.AsParallelWriter(); // job.EntityType = entities; // // int batchesPerChunk = 4; // this.Dependency = job.ScheduleParallel(m_Group, batchesPerChunk, this.Dependency); float deltaTime = UnityEngine.Time.deltaTime; float maxGrowth = MAX_GROWTH; var translations = GetComponentDataFromEntity <Translation>(true); var setInfo = componentSetInfo.AsParallelWriter(); var plantChanges = plantCreationDeletionInfo.AsParallelWriter(); JobHandle job = Entities.ForEach((Entity currentEntity, ref PlantComponent plant, ref NonUniformScale scale) => { PlantState state = (PlantState)plant.state; switch (state) { case PlantState.None: break; case PlantState.Growing: float currentTotalTime = deltaTime + plant.timeGrown; if (currentTotalTime < maxGrowth) { float currentScale = currentTotalTime / 5.0f; scale = new NonUniformScale { Value = new float3(currentScale, 1.0f, currentScale) }; var data = new PlantComponent { timeGrown = currentTotalTime, state = (int)PlantState.Growing, }; plant = data; } else { var data = new PlantComponent { timeGrown = maxGrowth, state = (int)PlantState.None, }; plant = data; } break; case PlantState.Following: float3 pos = translations[plant.farmerToFollow].Value; float3 trans = new float3(pos.x, pos.y + 2, pos.z); setInfo.Enqueue(new ComponentTransInfo { entity = currentEntity, trans = trans }); break; case PlantState.Deleted: // multiple entities can try to delete the plant // taken care of in the single threaded end of the jobs //UnityEngine.Debug.Log("deleting a plant " + entity.Index); plantChanges.Enqueue(currentEntity); break; default: break; } }).WithReadOnly(translations).ScheduleParallel(this.Dependency); this.Dependency = job; }