/// <summary> /// Instantiates newSubjectId's prefab at the spawnPoint position. /// </summary> /// <param name="newSubjectId">The SubjectID of the NPC to spawn.</param> /// <param name="spawnPoint">The position to spawn the NPC at.</param> public GameObject SpawnObject(int newSubjectId, Vector3 spawnPoint) { //Use the id to pull the Subject card Subject newSubject = masterSubjectList.GetSubject(newSubjectId); if (newSubject != null) { GameObject newObject = Instantiate(newSubject.Prefab, spawnPoint, Quaternion.identity); if (newObject != null) { SubjectObjectScript script = newObject.GetComponent <SubjectObjectScript>() as SubjectObjectScript; script.InitializeFromSubject(masterSubjectList, newSubject); return(newObject); } } return(null); }
// Update is called once per frame void Update() { // ------------------------------------------------------------------------ // ------------------------ START CAMERA MOVEMENT ------------------------- // ------------------------------------------------------------------------ if (lastSelector != null) { cameraDestination = new Vector3(lastSelector.transform.position.x, cameraDestination.y, lastSelector.transform.position.z); } else if (Input.GetMouseButton(2)) { // panning camera float mouseY = Input.GetAxis("Mouse Y"); if (mouseY != 0) { cameraDestination = new Vector3(cameraDestination.x, cameraDestination.y, Camera.main.transform.position.z + mouseY * (cameraDestination.y / 25)); } float mouseX = Input.GetAxis("Mouse X"); if (mouseX != 0) { cameraDestination = new Vector3(Camera.main.transform.position.x + mouseX * (cameraDestination.y / 25), cameraDestination.y, cameraDestination.z); } } float mouseWheelDelta = Input.GetAxis("Mouse ScrollWheel"); if (mouseWheelDelta != 0) { Vector3 camPosition = Camera.main.transform.position; cameraDestination = new Vector3(camPosition.x, camPosition.y - mouseWheelDelta * (camPosition.y), Camera.main.transform.position.z); } if (Vector3.Distance(Camera.main.transform.position, cameraDestination) > 0.1f) { Camera.main.transform.position = Vector3.Slerp(Camera.main.transform.position, cameraDestination, Time.deltaTime * 20.0f); } // ------------------------------------------------------------------------ // ------------------------ END CAMERA MOVEMENT --------------------------- // ------------------------------------------------------------------------ if ((lastSelector != null) && (lastSelector.transform.parent != null)) { SubjectObjectScript script = lastSelector.transform.parent.GetComponent <SubjectObjectScript>() as SubjectObjectScript; _ID.text = "ID: <b>" + script.Subject.SubjectID.ToString() + "</b>"; _Name.text = "Name: <b>" + script.Subject.Name.ToString() + "</b>"; _Description.text = "Desc: <b>" + script.Subject.Description.ToString() + "</b>"; testPanel.SetActive(true); PlantSubject plantSub = script.Subject as PlantSubject; if (plantSub != null) { PlantObjectScript plantScript = script as PlantObjectScript; Subject produceSubject = masterSubjectList.GetSubject(plantSub.ProduceID); float maturePercent = Mathf.Min(plantScript.CurrentGrowth / plantSub.MatureGrowth, 1.0f); plantPanel.SetActive(true); animalPanel.SetActive(false); locationPanel.SetActive(false); _Produce.text = "Produce: <b>" + plantSub.ProduceID.ToString() + " - " + produceSubject.Name.ToString() + "</b>"; _Growth.text = "Growth: <b>" + plantScript.CurrentGrowth.ToString() + " / " + plantSub.MaxGrowth.ToString() + "</b>"; _Maturity.text = "Maturity: <b>" + maturePercent.ToString() + "</b>"; _Inventory.text = "Inventory: <b>" + plantScript.InventoryPercent().ToString() + "</b>"; } else { AnimalSubject animalSub = script.Subject as AnimalSubject; if (animalSub != null) { animalPanel.SetActive(true); plantPanel.SetActive(false); locationPanel.SetActive(false); AnimalObjectScript animalScript = script as AnimalObjectScript; _Health.text = "Health: <b>" + animalScript.GetHealth().ToString() + "</b>"; _Safety.text = "Safety: <b>" + animalScript.GetSafety().ToString() + "</b>"; _Food.text = "Food: <b>" + animalScript.GetFood().ToString() + "</b>"; } else { LocationSubject locationSub = script.Subject as LocationSubject; if (locationSub != null) { locationPanel.SetActive(true); plantPanel.SetActive(false); animalPanel.SetActive(false); _Center.text = "Center: <b>" + locationSub.Coordinates.ToString() + "</b>"; _Radius.text = "Radius: <b>" + locationSub.Radius.ToString() + "</b>"; } else { locationPanel.SetActive(false); plantPanel.SetActive(false); animalPanel.SetActive(false); } } } } else { //testPanel.SetActive(false); } //Place or use tool if (placeID < 1) { if (placeID < -1) //placeID == -2 { if (!IsOverMenu() && (Input.GetMouseButtonDown(0))) { centerPosition = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, Camera.main.transform.position.y)); SelectAtPosition(centerPosition, 0.4f); } } else if (placeID < 0) //placeID == -1 { //setting to Damage if (!IsOverMenu() && (Input.GetMouseButtonDown(0))) { centerPosition = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, Camera.main.transform.position.y)); if (HarmAtPosition(centerPosition, 1.0f)) { PopMessage("Kill!", centerPosition, 0); } } } else //placeID == 0 { //setting to delete objects if (!IsOverMenu() && (Input.GetMouseButtonDown(0))) { centerPosition = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, Camera.main.transform.position.y)); if (DeleteAtPosition(centerPosition, 1.0f)) { PopMessage("DELETED", centerPosition, 1); } } } } else // is placeable { if (!placementStarted) { //Since we have not started, we can look for our mouse for placement if (!IsOverMenu() && (Input.GetMouseButtonDown(0))) { //Set the center based on mouse position centerPosition = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, Camera.main.transform.position.y)); //We will let everything start with a radius of 0.5 if (CheckPlacementPosition(centerPosition, 0.5f, null)) { if (placeID == 2) //this is a location, which requires 2 steps { placedObject = Instantiate(locationStart, centerPosition, Quaternion.identity); //calculate our edge and manipulate the scale until finalized edgePosition = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, Camera.main.transform.position.y)); float distance = Vector3.Distance(centerPosition, edgePosition); if (CheckPlacementPosition(centerPosition, distance, placedObject)) { placedObject.transform.localScale = new Vector3(distance * 2, 0.1f, distance * 2); } placementStarted = true; } else { //Use the id to pull the Subject card Subject newSubject = masterSubjectList.GetSubject(placeID); if (newSubject != null) { placedObject = Instantiate(newSubject.Prefab, centerPosition, Quaternion.identity); if (placedObject != null) { SubjectObjectScript script = placedObject.GetComponent <SubjectObjectScript>() as SubjectObjectScript; script.InitializeFromSubject(masterSubjectList, newSubject); placementStarted = true; } } } } } } else { //We have started to place - is it a location? if (placeID == 2) { //calculate our edge and manipulate the scale until finalized edgePosition = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, Camera.main.transform.position.y)); float distance = Vector3.Distance(centerPosition, edgePosition); if (CheckPlacementPosition(centerPosition, distance, placedObject)) { placedObject.transform.localScale = new Vector3(distance * 2, 0.1f, distance * 2); lastDistance = distance; } //Look for a second mouse click to finalize the location //No more changes to distance if (!IsOverMenu() && (Input.GetMouseButtonDown(0))) { placementStarted = false; Destroy(placedObject); //get rid of our temp area //create a new location using the above values CreateLocation(centerPosition, lastDistance); } } else { //We can finalize everything else automatically placementStarted = false; } } } }