public World() { this.InputManager = new InputManager(); this.player = new Player(PlayerStartX, PlayerStartY, this.InputManager); const double ratioX = 0.0390625; const double ratioY = 0.0390625; var obstacles = new RectCollider[] { new RectCollider(0 * ratioX, 512 * ratioY, 768 * ratioX, 256 * ratioY), new RectCollider(16 * ratioX, 256 * ratioY, 16 * ratioX, 256 * ratioY), new RectCollider(384 * ratioX, 448 * ratioY, 96 * ratioX, 16 * ratioY), new RectCollider(128 * ratioX, 416 * ratioY, 96 * ratioX, 16 * ratioY), new RectCollider(256 * ratioX, 368 * ratioY, 96 * ratioX, 16 * ratioY), new RectCollider(384 * ratioX, 320 * ratioY, 96 * ratioX, 16 * ratioY), new RectCollider(512 * ratioX, 320 * ratioY, 96 * ratioX, 16 * ratioY), new RectCollider(512 * ratioX, 240 * ratioY, 96 * ratioX, 16 * ratioY), new RectCollider(256 * ratioX, 112 * ratioY, 96 * ratioX, 16 * ratioY), new RectCollider(160 * ratioX, 112 * ratioY, 96 * ratioX, 16 * ratioY) }; this.updatables = new List<IUpdatable>(); this.updatables.Add(this.player); this.drawables = new List<IDrawable>(); this.drawables.Add(this.player); foreach (var obstacle in obstacles) { this.drawables.Add(obstacle); } this.collidables = new List<ICollidable>(); this.collidables.Add(this.player); foreach (var obstacle in obstacles) { this.collidables.Add(obstacle); } this.player.AddCollidables(this.collidables); }
public void Update(double elapsedSeconds) { double initialVelocityY = this.velocityY; double velocityX = 0; bool movingLeft = false; bool movingRight = false; GravitationalAcceleration = this.inputManager.SliderValue; JumpVelocityY = -Math.Sqrt(2 * GravitationalAcceleration * 3 * Height); Debug.WriteLine("Gravitational Acceleration: " + GravitationalAcceleration + ", JumpVelocityY: " + JumpVelocityY); if (this.inputManager.LeftKeyPressed && this.inputManager.RightKeyPressed) { // Left and Right keys cancel each other out } else if (this.inputManager.LeftKeyPressed) { velocityX = -SpeedX; movingLeft = true; } else if (this.inputManager.RightKeyPressed) { velocityX = SpeedX; movingRight = true; } if (!this.grounded) { this.velocityY += GravitationalAcceleration * elapsedSeconds; this.velocityY = Math.Min(this.velocityY, MaxVelocityY); } else { if (this.inputManager.SpaceKeyPressed) { if (!this.spaceKeyProcessed) { initialVelocityY = JumpVelocityY; this.velocityY = initialVelocityY + GravitationalAcceleration * elapsedSeconds; this.grounded = false; this.groundCache = null; this.spaceKeyProcessed = true; } } else { this.spaceKeyProcessed = false; } } if (movingLeft || movingRight) { double newX = this.collider.X + velocityX * elapsedSeconds; var pathCollider = new RectCollider( movingLeft ? newX : this.collider.X + this.collider.Width, this.collider.Y, Math.Abs(this.collider.X - newX), this.collider.Height); foreach (var collidable in this.collidables) { if (collidable != this && pathCollider.IsColliding(collidable)) { Debug.Assert(!this.IsColliding(collidable)); Rect collided = collidable.GetCollider(); newX = movingLeft ? collided.X + collided.Width : collided.X - this.collider.Width; pathCollider = new RectCollider( movingLeft ? newX : this.collider.X + this.collider.Width, this.collider.Y, Math.Abs(this.collider.X - newX), this.collider.Height); } } this.collider.X = newX; } if (!this.grounded) { // TODO Epsilon bool movingUp = this.velocityY < 0; double newY = this.collider.Y + initialVelocityY * elapsedSeconds + (GravitationalAcceleration * elapsedSeconds * elapsedSeconds) / 2; var pathCollider = new RectCollider( this.collider.X, movingUp ? newY : this.collider.Y + this.collider.Height, this.collider.Width, Math.Abs(this.collider.Y - newY)); foreach (var collidable in this.collidables) { if (collidable != this && pathCollider.IsColliding(collidable)) { Debug.Assert(!this.IsColliding(collidable)); if (movingUp) { this.velocityY = 0; } else { this.velocityY = 0; this.grounded = true; this.groundCache = collidable; } Rect collided = collidable.GetCollider(); newY = movingUp ? collided.Y + collided.Height : collided.Y - this.collider.Height; pathCollider = new RectCollider( this.collider.X, movingUp ? newY : this.collider.Y + this.collider.Height, this.collider.Width, Math.Abs(this.collider.Y - newY)); } } this.collider.Y = newY; } else { var groundCheck = new RectCollider( this.collider.X, this.collider.Y - 1, this.collider.Width, this.collider.Height); if (!groundCheck.IsColliding(this.groundCache)) { this.grounded = false; this.groundCache = null; } } }