public MainMenu(ScreenManager manager) { cam.Zoom = 5; cam.Pos = new Vector2(GameConstants.MAP_SIZE_HORIZONTAL * GameConstants.TILE_SIZE / 2, GameConstants.MAP_SIZE_VERTICAL * GameConstants.TILE_SIZE / 2); Microsoft.Xna.Framework.Media.MediaPlayer.Stop(); for (int i = 0; i < GameConstants.MAP_SIZE_HORIZONTAL; i++) { for (int j = 0; j < GameConstants.MAP_SIZE_VERTICAL; j++) { tiles[i, j] = new Tile(ContentChest.Instance.floor[0], i * GameConstants.TILE_SIZE, j * GameConstants.TILE_SIZE); if (j == GameConstants.MAP_SIZE_VERTICAL - 1 || j == 0) { if (i == GameConstants.MAP_SIZE_HORIZONTAL - 1 || i == 0) { BoxSpawner mover = new BoxSpawner(i * GameConstants.TILE_SIZE, j * GameConstants.TILE_SIZE); entities.Add(mover); } else { BoxMover mover = null; if (j == 0) { mover = new BoxMover(i * GameConstants.TILE_SIZE, j * GameConstants.TILE_SIZE, BoxMover.Type.item); } else if (j == GameConstants.MAP_SIZE_VERTICAL - 1) { mover = new BoxMover(i * GameConstants.TILE_SIZE, j * GameConstants.TILE_SIZE, BoxMover.Type.box); } mover.breakable = false; entities.Add(mover); } } } } // Font for the footer. font = ContentChest.Instance.defaultFont; // Saves big parameters in the button initialising. int middleScreen = GameConstants.GAME_WIDTH / 2 - ContentChest.Instance.buttons[0].Width / 2; int buttonYStart = GameConstants.GAME_HEIGHT / 2; // Initialising all of the buttons. for (int i = 0; i < buttons.Length; i++) { buttons[i] = new Button(ContentChest.Instance.buttons[i], new Vector2(middleScreen, buttonYStart), (ButtonTag)i); buttonYStart += buttonPadding + ContentChest.Instance.buttons[i].Height; } // Positioning of the footer string. ludumStringX = GameConstants.GAME_WIDTH / 2 - (int)font.MeasureString(ludumString).X / 2; ludumStringY = GameConstants.GAME_HEIGHT - (int)font.MeasureString(ludumString).Y - 10; ludumStringPos = new Vector2(ludumStringX, ludumStringY); this.manager = manager; // For access to screen changing. }
private void Update() { updateTouch(); if (grounded) { velocity.y = 0; if (jumpInput != 0) { velocity.y = Mathf.Sqrt(2 * jumpHeight * Mathf.Abs(Physics2D.gravity.y)); } } float acceleration = grounded ? walkAcceleration : airAcceleration; float deceleration = grounded ? groundDeceleration : 0; if (moveInput != 0) { velocity.x = Mathf.MoveTowards(velocity.x, speed * moveInput, acceleration * Time.deltaTime); } else { if (!onPlatform) { velocity.x = Mathf.MoveTowards(velocity.x, 0, deceleration * Time.deltaTime); } } velocity.y += Physics2D.gravity.y * Time.deltaTime; grounded = false; // Retrieve all colliders we have intersected after velocity has been applied. Collider2D[] hits = Physics2D.OverlapBoxAll(transform.position, boxCollider.size, 0); onPlatform = false; bool noBox = true; bool noLever = true; foreach (Collider2D hit in hits) { // Ignore our own collider. if (hit == boxCollider) { continue; } if (hit.gameObject.tag == "Deadzone") { transform.position = startPos; transform.rotation = startRot; velocity.x = 0; velocity.y = 0; break; } if (hit.gameObject.tag == "Lever") { Lever lever = hit.gameObject.GetComponent <Lever>(); lever.collisionWithCharacterOccurred(); continue; } if (hit == doorCollider) { if (Random.Range(0, 12) < 2) { Advertisement.Show(); } PlayerPrefs.SetInt("nextLevel", (++level)); string name = "Level" + level; Debug.Log("Lade nächstes Level: " + name); if (scenesInBuild.Contains(name)) { SceneManager.LoadScene(name, LoadSceneMode.Single); } //FIXME Später eine "Spielbeendet" Szene erstellen und hier verwenden else { SceneManager.LoadScene("Menu", LoadSceneMode.Single); } continue; } if (hit.gameObject.tag == "Box") { BoxMover box = hit.gameObject.GetComponent <BoxMover>(); noBox = false; if (Mathf.Abs(box.gameObject.transform.position.y - gameObject.transform.position.y) < 0.9f) { box.overrideVelocityWithPlayerVelocity(velocity.x); } } ColliderDistance2D colliderDistance = hit.Distance(boxCollider); // Ensure that we are still overlapping this collider. // The overlap may no longer exist due to another intersected collider // pushing us out of this one. if (colliderDistance.isOverlapped) { transform.Translate(colliderDistance.pointA - colliderDistance.pointB); // If we intersect an object beneath us, set grounded to true. if (Vector2.Angle(colliderDistance.normal, Vector2.up) < 90 && velocity.y < 0) { grounded = true; } } } if (noBox && lastBox != null) { lastBox.overrideVelocityWithPlayerVelocity(0f); lastBox = null; } if (noLever && lastLever != null) { lastLever.unpressLever(); lastLever = null; } transform.Translate(velocity * Time.deltaTime); Camera.main.transform.position = transform.position; }