void AddCreature(int cId, CharacterManager.BirthCause cause) { //if we're running in the editor, we can instantiate as prefabs. //This lets us change variables in the prefab and see the effects #if UNITY_EDITOR GameObject cObj = (GameObject)UnityEditor.PrefabUtility.InstantiatePrefab(prefabs[cId]); #else GameObject cObj = GameObject.Instantiate(prefabs[cId]); #endif SwimmingCreature c = cObj.GetComponent <SwimmingCreature>(); c.creatureFlock = creatures; c.id = cId; //move it out of the way so that it doesn't flicker before spawning. c.transform.position = c.transform.position + new Vector3(99999, 9999, 0); switch (cause) { case CharacterManager.BirthCause.Bought: c.StartBuying(); break; case CharacterManager.BirthCause.Reproduction: c.StartReproducing(player.reproducePart, findRandomCreatureOfID(cId).transform.position); break; } creatures.Add(c); }
public bool amITheTarget(SwimmingCreature c) { if (fishes.Count > 0) { return(c == fishes[0]); } return(false); }
public void addTarget(SwimmingCreature c) { fishes.Add(c); if (fishes.Count == 1) { target = c.transform.position; } }
public FishHunt(SwimmingCreature predator, SwimmingCreature prey, float time) { this.predator = predator; this.prey = prey; startTime = time; currentTime = time; predatorStart = predator.transform.position; started = false; }
void RemoveCreature(int cId, CharacterManager.DeathCause cause) { bool foundOne = false; int index = 0; int found = -1; while (index < creatures.Count && !foundOne) { if (creatures[index].id == cId && !creatures[index].isDying) { foundOne = true; found = index; } else { index++; } } if (foundOne) { SwimmingCreature c = creatures[found]; //creatures.Remove(c); if (cause == null) { cause = CharacterManager.DeathCause.Starve; } switch (cause) { case CharacterManager.DeathCause.Sold: c.startFishing(lures[Random.Range(0, lures.Count)]); break; case CharacterManager.DeathCause.Hot: c.startDying(player.tooHotPart); break; case CharacterManager.DeathCause.Cold: c.startDying(player.tooCoolPart); break; case CharacterManager.DeathCause.Eaten: SwimmingCreature predator = findRandomCreatureOfTier(c.level + 1); float fasterHunting = Mathf.Max(1, predator.huntingFish.Count); FishHunt hunt = new FishHunt(predator, c, predatorTime / fasterHunting); predator.huntingFish.Add(hunt); c.getEaten(hunt); predator.startEating(); break; case CharacterManager.DeathCause.Starve: c.startDying(player.starvedPart); break; } } }
// Update is called once per frame void Update() { if (fishes.Count > 0) { SwimmingCreature t = fishes[0]; if (t == null || !t.gameObject.activeSelf) { fishes.Remove(t); Reset(); if (fishes.Count > 0) { target = fishes[0].transform.position; //fish faster if we've got a lot of fish we need to get through if (fishes.Count > 1) { float timeSquish = 1 / Mathf.Sqrt(fishes.Count); timeSquish = Mathf.Max(0.1f, timeSquish); fishes[0].multDeathTime(timeSquish); } } } else if (t.getDeathRatio() <= .5f) { //pull up the fish float amount1 = 1 - t.getDeathRatio() * 2; transform.position = Vector3.Lerp(target, origin, amount1); Vector3 lookDir = origin - target; //transform.rotation = Quaternion.LookRotation(new Vector3(lookDir.x, lookDir.y, 0).normalized); line.SetPosition(1, Vector3.Lerp( new Vector3(target.x, origin.y, origin.z), new Vector3(origin.x * 2, origin.y, origin.z), amount1) + lineOffset); } else { target = fishes[0].transform.position; //go down to the fish float amount2 = 1 - (t.getDeathRatio() - .5f) * 2; transform.position = Vector3.Lerp( new Vector3(target.x, origin.y, origin.z), target, amount2); //transform.rotation = Quaternion.Euler(Vector3.zero); line.SetPosition(1, new Vector3(target.x, origin.y, origin.z) + lineOffset); } line.SetPosition(0, transform.position + lineOffset + new Vector3(0, 0, 1)); } }
private Vector2 Hunt(SwimmingCreature creature, float distSq) { if (distSq < huntSq) { Vector3 ourPos = transform.position; Vector3 theirPos = creature.transform.position; //go towards the other creature Vector2 attractVector = new Vector2(theirPos.x - ourPos.x, theirPos.y - ourPos.y); attractVector.Normalize(); //TODO: weight by distance return(attractVector); } else { return(Vector2.zero); } }
private Vector2 Flee(SwimmingCreature creature, float distSq) { if (distSq < fleeSq) { Vector3 ourPos = transform.position; Vector3 theirPos = creature.transform.position; //go away from the other creature Vector2 avoidVector = new Vector2(ourPos.x - theirPos.x, ourPos.y - theirPos.y); avoidVector.Normalize(); //TODO: weight by distance return(avoidVector); } else { return(Vector2.zero); } }
private Vector2 Align(SwimmingCreature creature, float distSq) { if (distSq < alignSq) { //align with the other creature //Debug.Log("V- " + creature.velocity); Vector2 alignVector = creature.velocity; alignVector.Normalize(); //Debug.Log("VV- " + alignVector); //TODO: weight by distance return(alignVector); } else { return(Vector2.zero); } }