void AttemptSpawn() { EVisitorPersuasion persuasion = ChooseVisitorPersuasion(); bool isGround = persuasion == EVisitorPersuasion.EVP_Pest; FeederLandingSpot landingSpot = null; Feeder spawningFeeder = null; foreach (Feeder feeder in PossibleFeeders) { if (feeder != null && feeder.GetIsUnlocked()) { if (isGround && feeder.CanSpawnGround()) { landingSpot = feeder.GetNextFreeGroundSlot(); spawningFeeder = feeder; break; } if (!isGround && feeder.CanSpawnAir()) { landingSpot = feeder.GetNextFreeAirSpot(); spawningFeeder = feeder; break; } } } if (spawningFeeder != null && landingSpot != null) { Spawner spawner = ChooseSpawner(isGround); Vector3 spawnPosition = spawner.transform.position; spawnPosition.z = 0; GardenVisitor attemptedVisitor = ChooseVisitorType(spawningFeeder.GetCurrentFeedRarity(), persuasion); if (attemptedVisitor == null) { return; } GardenVisitor newVisitor = Instantiate(attemptedVisitor, spawnPosition, Quaternion.identity, gameObject.transform); if (newVisitor.transform.position.x < 0.0f) { newVisitor.transform.Rotate(Vector3.up, 180); } newVisitor.InitializeGardenVisitor(); newVisitor.SetFeederId(spawningFeeder.FeederId); newVisitor.SetFeederLandingSpotId(landingSpot.LandingSpotId); newVisitor.SetSpawnerId(spawner.SpawnerId); spawningFeeder.AssignSlot(isGround); CurrentGardenVisitors.Add(newVisitor); } }
public FeederLandingSpot GetNextFreeGroundSlot() { FeederLandingSpot landingSpot = null; //Loop through LandingSpots for an empty FeederLandingSpot for (int i = 0; i < GroundLandingSpots.Count; i++) { //If FeederLandingSpot is empty, update returnValue to return the position in the array. if (!GroundLandingSpots[i].GetIsFilled()) { landingSpot = GroundLandingSpots[i]; break; } } return(landingSpot); }
public void ReleaseSlot(bool isGround, int slotId) { FeederLandingSpot spot = null; if (isGround) { spot = GetGroundLandingSpotById(slotId); } else { spot = GetAirLandingSpotById(slotId); } if (spot != null) { spot.EmptySlot(); } }
public int AssignSlot(bool isGround) { int spotId = -1; FeederLandingSpot spot = null; if (isGround) { spot = GetNextFreeGroundSlot(); } else { spot = GetNextFreeAirSpot(); } if (spot != null) { spot.FillSlot(); spotId = spot.LandingSpotId; } return(spotId); }
// Update is called once per frame void Update() { float frameDeltaTime = Time.deltaTime; TimeSinceLastSpawn += frameDeltaTime; if (TimeSinceLastSpawn >= TimeBetweenSpawns) { AttemptSpawn(); TimeSinceLastSpawn = 0.0f; TimeBetweenSpawns = Random.Range(MinTimeBetweenSpawns, MaxTimeBetweenSpawns); } List <GardenVisitor> pendingDestroy = new List <GardenVisitor>(); foreach (GardenVisitor visitor in CurrentGardenVisitors) { Feeder feeder = GetFeederById(visitor.GetFeederId()); bool eating = visitor.GetIsEating(); bool leaving = visitor.GetPendingLeave(); visitor.UpdateGardenVisitor(frameDeltaTime); int amountToConsume = visitor.GetPendingEatAmount(); if (!eating) { // t = 0 is a (feeder) // t = 1 is b (spawner) // Vector2.Lerp(Vector2 a, Vector2 b, float t) FeederLandingSpot landingSpot = null; if (visitor.IsGround) { landingSpot = feeder.GetGroundLandingSpotById(visitor.GetFeederLandingSpotId()); } else { landingSpot = feeder.GetAirLandingSpotById(visitor.GetFeederLandingSpotId()); } Vector2 landingSpotPosition2d = landingSpot.transform.position; Spawner spawner = GetSpawnerById(visitor.GetSpawnerId()); Vector2 spawnerPosition2d = spawner.transform.position; float visitorLerpPosition = visitor.GetLerpPosition(); visitor.transform.position = Vector2.Lerp(landingSpotPosition2d, spawnerPosition2d, visitorLerpPosition); if (visitor.GetPendingLeave()) { if (visitorLerpPosition >= 1.0f) { pendingDestroy.Add(visitor); } } else { if (visitorLerpPosition <= 0.0f) { visitor.BeginEating(); if (feeder.GetIsEmpty()) { visitor.SetAnimState(EVisitorAnimState.EVAS_Idle); } else { float direction = spawnerPosition2d.x * landingSpotPosition2d.x; if (direction < 0.0f) { visitor.transform.Rotate(Vector3.up, 180); visitor.SetFlippedToEat(true); } visitor.SetAnimState(EVisitorAnimState.EVAS_Eat); } } } } switch (visitor.EatType) { case EResourceType.ERT_Seed: feeder.Consume(amountToConsume); if (feeder.GetIsEmpty()) { foreach (GardenVisitor gv in CurrentGardenVisitors.Where(gardenVisitor => gardenVisitor.GetFeederId() == feeder.FeederId)) { if (gv.MinRarity >= 0) { gv.Leave(); } } } break; case EResourceType.ERT_Money: PlayerInventory.UpdateMoney(-1 * amountToConsume); break; } visitor.ConsumePending(); } foreach (GardenVisitor visitor in pendingDestroy) { CurrentGardenVisitors.Remove(visitor); Feeder feeder = GetFeederById(visitor.GetFeederId()); feeder.ReleaseSlot(visitor.IsGround, visitor.GetFeederLandingSpotId()); Destroy(visitor.gameObject); } pendingDestroy.Clear(); foreach (Feeder feeder in PossibleFeeders) { feeder.UpdateFeeder(frameDeltaTime); } //Update Money in real time TextField.text = "$" + GetMoney().ToString(); }