// Update is called once per frame void Update() { if (RatingSystem.IsGenerating) { //Debug.Log(System.DateTime.Now.ToString() + "\tStability: " + GetLevelStability()); // Wait until level is stable, then take screenshot if (stabilityCounter <= 0f && (IsLevelStable() || timeToStable >= STABILITY_TIMEOUT)) { if (!RatingSystem.IsEvolution) { Camera camera = GameplayCam.GetCamera(); int resWidth = camera.pixelWidth; int resHeight = camera.pixelHeight; RenderTexture rt = new RenderTexture(resWidth, resHeight, 24); camera.targetTexture = rt; Texture2D screenShot = new Texture2D(resWidth, resHeight, TextureFormat.RGB24, false); camera.Render(); RenderTexture.active = rt; screenShot.ReadPixels(new Rect(0, 0, resWidth, resHeight), 0, 0); screenShot.Apply(); camera.targetTexture = null; RenderTexture.active = null; // JC: added to avoid errors Destroy(rt); // for saving screenshot as png //byte[] bytes = screenShot.EncodeToPNG(); //string filename = string.Format("{0}/screenshots/screen_{1}x{2}_{3}.png", // Application.dataPath, // resWidth, resHeight, // System.DateTime.Now.ToString("yyyy-MM-dd_HH-mm-ss")); //System.IO.File.WriteAllBytes(filename, bytes); //Debug.Log(string.Format("Took screenshot to: {0}", filename)); // save sprite Sprite levelSprite = Sprite.Create(screenShot, new Rect(0, 0, resWidth, resHeight), new Vector2(0, 0)); RatingSystem.AddLevel(LevelList.Instance.CurrentIndex, levelSprite, HUD.Instance.GetScore(), timeToStable, _pigs.Count > 0); //Debug.Log(System.DateTime.Now.ToString() + "\tLevel Count: " + RatingSystem.levelData.Count); } else { RatingSystem.keptForEvolution[RatingSystem.CurrentLSystemIndex].fitness += RatingSystem.GetLevelFitness(_pigs.Count > 0, HUD.Instance.GetScore(), timeToStable) / RatingSystem.MAX_LEVELS; } // go to next level NextLevel(); } else { stabilityCounter -= Time.deltaTime; timeToStable += Time.deltaTime; } } else { // Check if birds was trown, if it died and swap them when needed ManageBirds(); } }
private void AdaptCameraWidthToLevel() { Collider2D [] bodies = _blocksTransform.GetComponentsInChildren <Collider2D>(); if (bodies.Length == 0) { return; } // Adapt the camera to show all the blocks float levelLeftBound = -LevelWidth / 2f; float groundSurfacePos = LevelHeight / 2f; float minPosX = Mathf.Infinity; float maxPosX = -Mathf.Infinity; float maxPosY = -Mathf.Infinity; // Get position of first non-empty stack for (int i = 0; i < bodies.Length; i++) { float minPosXCandidate = bodies[i].transform.position.x - bodies[i].bounds.size.x / 2f; if (minPosXCandidate < minPosX) { minPosX = minPosXCandidate; } float maxPosXCandidate = bodies[i].transform.position.x + bodies[i].bounds.size.x / 2f; if (maxPosXCandidate > maxPosX) { maxPosX = maxPosXCandidate; } float maxPosYCandidate = bodies[i].transform.position.y + bodies[i].bounds.size.y / 2f; if (maxPosYCandidate > maxPosY) { maxPosY = maxPosYCandidate; } } float cameraWidth = Mathf.Abs(minPosX - levelLeftBound) + Mathf.Max(Mathf.Abs(maxPosX - minPosX), Mathf.Abs(maxPosY - groundSurfacePos)) + 0.5f; GameplayCam.SetCameraWidth(cameraWidth); }
public static Matrix4x4 Matrix(GameplayCam cam) => Read <Matrix4x4>(Address(cam), 0x1F0);
// Use this for initialization void Start() { //_isSimulation = RatingSystem.IsGenerating; if (RatingSystem.IsGenerating) { //Debug.Log("In level " + LevelList.Instance.CurrentIndex); HideViewCam.GetCamera().enabled = true; hudObject.SetActive(false); stabilityCounter = 0.3f; timeToStable = 0f; GameplayCam.GetCamera().backgroundColor = new Color(0, 0, 0); } else { //Debug.Log("Not generating screenshots for level " + LevelList.Instance.CurrentIndex); //HideViewCam.GetCamera().enabled = false; HideViewCam.gameObject.SetActive(false); hudObject.SetActive(true); } _pigs = new List <ABPig>(); _birds = new List <ABBird>(); _birdTrajectory = new List <ABParticle>(); _levelCleared = false; if (!_isSimulation) { GetComponent <AudioSource>().PlayOneShot(_clips[0]); GetComponent <AudioSource>().PlayOneShot(_clips[1]); } GameObject slingshot = GameObject.Find("Slingshot"); // If there are objects in the scene, use them to play if (_blocksTransform.childCount > 0 || _birdsTransform.childCount > 0 || _plaftformsTransform.childCount > 0 || slingshot != null) { foreach (Transform bird in _birdsTransform) { AddBird(bird.GetComponent <ABBird>()); } foreach (Transform block in _blocksTransform) { ABPig pig = block.GetComponent <ABPig>(); if (pig != null) { _pigs.Add(pig); } } LevelHeight = ABConstants.LEVEL_ORIGINAL_SIZE.y; LevelWidth = ABConstants.LEVEL_ORIGINAL_SIZE.x; _slingshot = GameObject.Find("Slingshot"); } else { ABLevel currentLevel = LevelList.Instance.GetCurrentLevel(); if (currentLevel != null) { DecodeLevel(currentLevel); AdaptCameraWidthToLevel(); _levelTimesTried = 0; } } _slingshotBaseTransform = GameObject.Find("slingshot_base").transform; }
public static IntPtr Address(GameplayCam cam) => GetGameplayCameraAddress();