/** * Returns closest nutrient within range of TargetColor's type * that isn't already targeted and is in direct line of sight * or null if there are no valid targets */ private Nutrient AcquireTarget() { // get all the nutrients of the target color from the nutrient manager IList <Nutrient> nutrients = m_NutrientManager.GetNutrients(m_TargetColor); if (nutrients == null) // if there are no nutrients of that color then there are no targets { return(null); // return null to indicate there are no targets } // now we need to find the closest, untargeted nutrient as that is what we will shoot at Nutrient closestNutrient = null; // initially set the closest nutrient to null float closestDistance = float.MaxValue; // set the closest distance to infinity // now iterate through the nutrients to find the closest one foreach (Nutrient e in nutrients) { if (!e.IsTargetted) // first check that the nutrient being examined is not already targetted { // now find the distance to that target float distance = MDPUtility.DistanceSquared(gameObject, e.gameObject); // check if this distance is less than the next known closest distance, and that the target is in LOS if (distance < closestDistance && distance < FiringRange * FiringRange && IsInLineOfSight(e.transform)) { // if it passed the check closestNutrient = e; // assign this nutrient to be the closest one closestDistance = distance; // assign this nutrient's distance to be the closest distance } } } // after we are done iterating through the nutrients then return what we found was the closest one as the target return(closestNutrient); }
public GameObject nutrientLostSound; //!< for holding a reference to the nutrient lost sound /** * the function to generate what nutrients are on the food blob */ public void GenerateEnzymes(int minNutrients, int maxNutrients, Color[] availableColors) { NumNutrients = Random.Range(minNutrients, maxNutrients + 1); // randomly choose the number of nutrients on the blob // find a reference to the current nutrient manager and game manager m_NutrientManager = FindObjectOfType(typeof(NutrientManager)) as NutrientManager; m_GameManager = FindObjectOfType(typeof(IntestineGameManager)) as IntestineGameManager; // populate the number of nutrients decided earlier for (int i = 0; i < NumNutrients; i++) { float radius = .4f; // choose .4f as a radius to start with float angle = ((2 * Mathf.PI) / NumNutrients) * i; // divide the circle into the right number of angle chunks in rads float xPos = radius * Mathf.Cos(angle); // find the x position as radius*cos(theta) float zPos = radius * Mathf.Sin(angle); // find the y position as radius*sin(theta) Vector3 position = transform.position; // 3 dimensional vector for position position.x += xPos; // set the x position of the vector position.z += zPos; // set the z position of the vector position.y = .5f; // set the y position of the vector // randomly choose a color for the nutrient int randomIndex = MDPUtility.RandomInt(availableColors.Length); Nutrient nutrient = m_NutrientManager.InstantiateNutrient(availableColors[randomIndex], position); nutrient.intestineGameManager = m_GameManager; // assign the game manager reference on the nutrient to be // the same as the one referenced in this class // Attach new enzyme as a child object nutrient.transform.parent = gameObject.transform; ((Behaviour)nutrient.GetComponent("Halo")).enabled = false; // halo should be false unless explicitly enabled } }
/*public static bool Collides(CircleCollider o1, CircleCollider o2) * { * float radiusSum = o1.Properties.Radius + o2.Properties.Radius; * * float distanceSquared = MDPUtility.DistanceSquared(o1.gameObject, o2.gameObject); * * // Square root is a slow operation so square the radii instead * return distanceSquared < radiusSum * radiusSum; * }*/ public static bool CircleToCircle(MDPEntity o1, MDPEntity o2) { float radiusSum = o1.Collider.Properties.Radius + o2.Collider.Properties.Radius; float distanceSquared = MDPUtility.DistanceSquared(o1.gameObject, o2.gameObject); // Square root is a slow operation so square the radii instead return(distanceSquared < radiusSum * radiusSum); }
/** * asynchronous function that checks mouse clicks to see if it brings up the tower menu or closes a tower menu */ private IEnumerator CheckMouseClick() { Vector3 mousePos = MDPUtility.MouseToWorldPosition(); // get the mouse click position mousePos.y = 5; // change the y position to 5 for proper hit detection RaycastHit hitInfo; // storing raycast hits // check if we click on a tower by doing a raycast down to see if a tower was below it if (Physics.Raycast(mousePos, Vector3.down, out hitInfo, mousePos.y)) { // if we click on tower, toggle whether menu is showed if (hitInfo.transform.position == transform.position) { if (!m_OneClick) { m_OneClick = true; clickTimer = Time.time; } else { m_OneClick = false; //double click is true; Debug.Log("Double Clicked"); IsEnabled = !IsEnabled; // change the value in this class m_GameManager.setTowerMenuUp(IsEnabled); // also set the flag in the game manager } } else // if we didn't click directly on a tower disable a menu if it's up { if (IsEnabled) { IsEnabled = false; // change the value in this class m_GameManager.setTowerMenuUp(false); // also set the flag in the game manager } } } else { // otherwise if we clicked in a random place cancel the menu yield return(new WaitForSeconds(.1f)); // wait for .1 seconds if (IsEnabled) { IsEnabled = false; //change the value in this class m_GameManager.setTowerMenuUp(false); // change the value in game manager } } yield return(new WaitForSeconds(.0f)); }
/** * function that spawns towers */ public void SpawnTower(Color color) { GameObject towerType = Towers [MDPUtility.RandomInt(Towers.Length)]; // randomly choose a type of tower m_SpawnedTower = Instantiate(towerType) as GameObject; // instantiate the chosen tower Tower tower = m_SpawnedTower.GetComponent <Tower> (); // get the script on the new tower tower.SetColor(color); // set the color of the tower tower.SetActiveModel("Base"); // set the model name of the tower // Set up spawn indicator float range = tower.FiringRange; // calculate the firing range for the tower m_Indicator = Instantiate(SpawnIndicator) as GameObject; // instantiate a spawn indicator m_Indicator.transform.localScale = new Vector3(range * 2, 1, range * 2); // adjust the size of the indicator based on the firing range m_IsSpawnActive = true; // set the isspawnactive to true since there is now an active tower spawned }
/** * Called ever frame * Helps with spawning tower and showing spawn indicator * Checks for any updates to tower cost from debugger */ void Update() { // check if the game is paused we destroy any spawned tower that isn't currently placed if (Time.timeScale <= 0.001f) { // this handles the cleanup of variables that need to be reassigned based on destroying unplaced tower if (m_IsSpawnActive) { m_IsSpawnActive = false; // set that isspawnactive flag to false DestroyImmediate(m_Indicator.GetComponent <Renderer>().material); // destroy the indicator material Destroy(m_Indicator); // destroy the indicator Destroy(m_SpawnedTower); // destroy the spawned tower m_SpawnedTower = null; // set the spawnedtower reference to null } } // if we are not in the tutorial and we are using the debugger, check for updates to the tower base cost if (Application.loadedLevelName != "SmallIntestineTutorial") { if (debugConfig.debugActive) { TOWER_BASE_COST = debugConfig.TOWER_BASE_COST; } } // Handle valid spawn locations if player is spawning a tower if (m_IsSpawnActive) { // check that the mouse button is not currently pressed if (Input.GetMouseButtonUp(0)) { Time.timeScale = 1; m_IsSpawnActive = false; // set the spawn active flag to false DestroyImmediate(m_Indicator.GetComponent <Renderer>().material); // destroy the spawn indicator material Destroy(m_Indicator); // destroy the spawn indicator // check if the user has enough nutrients to spawn a tower if (m_IsMouseOverWallLastFrame && m_GameManager.nutrients - TOWER_BASE_COST >= 0) { // if they do have enough nutrients m_SpawnedTower.GetComponent <Tower> ().enabled = true; // enable the tower m_SpawnedTower.transform.position = wall.transform.position + new Vector3(0, 0.5f, 0); // set it's location on the wall m_SpawnedTower.GetComponent <Tower>().wall = wall; // set the reference to the wall the tower is on m_SpawnedTower.GetComponent <TowerMenu> ().Initialize(); // initialize the tower menu m_GameManager.nutrients = m_GameManager.nutrients - TOWER_BASE_COST; // deduct cost // play placement sound Instantiate(placementSound); // track stats PlayerPrefs.SetInt("SIStats_towersPlaced", PlayerPrefs.GetInt("SIStats_towersPlaced") + 1); PlayerPrefs.SetInt("SIStats_nutrientsSpent", PlayerPrefs.GetInt("SIStats_nutrientsSpent") + TOWER_BASE_COST); PlayerPrefs.Save(); } else { // if they didn't have enough nutrients to place the tower then destroy it Destroy(m_SpawnedTower); m_SpawnedTower = null; } } else { // if they haven't tried to place the tower by clicking/tapping keep letting them drag the tower m_SpawnedTower.transform.position = MDPUtility.MouseToWorldPosition() + Vector3.up; // check if the tower is over a wall or not and color the indicator red or green accordingly // also adjust the indicator position so it is still centered over the tower m_Indicator.transform.position = MDPUtility.MouseToWorldPosition() + Vector3.up; Color color = m_IsMouseOverWallLastFrame ? Color.green : Color.red; color.a = 0.5f; m_Indicator.GetComponent <Renderer>().material.color = color; } } m_IsMouseOverWallLastFrame = IsMouseOverWall; // set the "last frame" variable to the value from this frame }
/** * function that helps tower menu initialization * this is called by the tower spawner when a tower is spawned and helps the tower menu show up * in the correct location when brought up for a tower */ public void Initialize() { m_ScreenPosition = MDPUtility.WorldToScreenPosition(transform.position); m_ScreenPosition.y -= Screen.height * (105f / 768f); }