/// <summary> /// Controleert of we de onderkant raken van de static sprite /// </summary> /// <param name="staticObject">static collision object</param> /// <param name="movingCollision">bewegend collision object</param> /// <returns>returned true als het bewegend object de onderkant van het static object aanraakt</returns> private bool IsTouchingBottom(ICollide staticObject, IMovingCollide movingCollision) { return(movingCollision.CollisionRectangle.Top + movingCollision.Velocity.Y < staticObject.CollisionRectangle.Bottom && movingCollision.CollisionRectangle.Bottom > staticObject.CollisionRectangle.Bottom && movingCollision.CollisionRectangle.Right > staticObject.CollisionRectangle.Left && movingCollision.CollisionRectangle.Left < staticObject.CollisionRectangle.Right); }
/// <summary> /// Controleert of we de rechterkant van de static sprite aanraken /// </summary> /// <param name="staticObject">static collision object</param> /// <param name="movingCollision">bewegend collision object</param> /// <returns>returned true als het bewegend object de rechterkant van het static object aanraakt</returns> private bool IsTouchingRight(ICollide staticObject, IMovingCollide movingCollision) { return(movingCollision.CollisionRectangle.Left + movingCollision.Velocity.X < staticObject.CollisionRectangle.Right && movingCollision.CollisionRectangle.Right > staticObject.CollisionRectangle.Right && movingCollision.CollisionRectangle.Bottom > staticObject.CollisionRectangle.Top && movingCollision.CollisionRectangle.Top < staticObject.CollisionRectangle.Bottom); }
/// <summary> /// Reset alle booleans van het bewegend collision object /// </summary> /// <param name="movingCollide">collision object dat beweegt</param> private static void ResetState(IMovingCollide movingCollide) { movingCollide.TouchingGround = false; movingCollide.TouchingTop = false; movingCollide.TouchingLeft = false; movingCollide.TouchingRight = false; }
/// <summary> /// Kijkt of een enemy iets heeft aangeraakt /// </summary> /// <param name="collideObjects">Lijst met alle objecten waar we mee kunnen colliden</param> /// <param name="movingCollide">object dat kan colliden en beweegt</param> private static void DetectEnemy(List <ICollide> collideObjects, IMovingCollide movingCollide) { foreach (Enemy currentEnemy in collideObjects.OfType <Enemy>().ToList()) { if (movingCollide is Hero && movingCollide.CollisionRectangle.Intersects(currentEnemy.CollisionRectangle)) { currentEnemy.TouchedHero = true; } if (movingCollide is Projectile && movingCollide.CollisionRectangle.Intersects(currentEnemy.CollisionRectangle)) { movingCollide.TouchingLeft = true; currentEnemy.IsHit = true; } } }
/// <summary> /// Update de movement voor een bewegend collision object /// </summary> /// <param name="movingCollision">bewegend object dat kan colliden</param> public override void Update(IMovingCollide movingCollision) { if (movingCollision.TouchingGround) { movingCollision.ChangeVelocity(null, 0); } if (movingCollision.TouchingLeft) { if (movingCollision.CurrentCollisionBlock is IMoveBlock) { movingCollision.ChangePosition(movingCollision.Position.X + movingCollision.MovingSpeed + movingCollision.CurrentCollisionBlock.MovingSpeed, null); } else { movingCollision.ChangePosition(movingCollision.Position.X + movingCollision.MovingSpeed, null); } movingCollision.ChangeVelocity(0, null); } if (movingCollision.TouchingRight) { if (movingCollision.CurrentCollisionBlock is IMoveBlock) { movingCollision.ChangePosition(movingCollision.Position.X - movingCollision.MovingSpeed - movingCollision.CurrentCollisionBlock.MovingSpeed, null); } else { movingCollision.ChangePosition(movingCollision.Position.X - movingCollision.MovingSpeed, null); } movingCollision.ChangeVelocity(0, null); } if (movingCollision.TouchingTop) { movingCollision.ChangeVelocity(null, 0.1f); } ReadButtonInput(movingCollision); if (movingCollision.Velocity.Y != 0) { movingCollision.ChangeVelocity(null, movingCollision.Velocity.Y + 0.2f); } movingCollision.Position += movingCollision.Velocity; if (!movingCollision.TouchingGround && movingCollision.Velocity.Y == 0) { movingCollision.ChangeVelocity(null, 0.2f); } }
/// <summary> /// Leest de input van de ingedrukte knoppen /// </summary> /// <param name="movingCollide">het bewegend collision object</param> private void ReadButtonInput(IMovingCollide movingCollide) { KeyboardState stateKey = Keyboard.GetState(); if (stateKey.IsKeyUp(Keys.Right)) { Right = false; movingCollide.ChangeVelocity(0, null); } if (stateKey.IsKeyUp(Keys.Left)) { Left = false; movingCollide.ChangeVelocity(0, null); } if (stateKey.IsKeyDown(Keys.Left) && Right != true) { Left = true; movingCollide.ChangeVelocity(-movementSpeed, null); } if (stateKey.IsKeyDown(Keys.Up) && movingCollide.Velocity.Y == 0) { movingCollide.ChangeVelocity(null, -8); Jump = true; } if (stateKey.IsKeyUp(Keys.Up)) { Jump = false; } if (stateKey.IsKeyDown(Keys.Space)) { Attack = true; } if (stateKey.IsKeyUp(Keys.Space)) { Attack = false; } if (stateKey.IsKeyDown(Keys.Right) && Left != true) { Right = true; movingCollide.ChangeVelocity(movementSpeed, null); } }
/// <summary> /// Controleert of we een static block hebben aangeraakt /// </summary> /// <param name="collideObjects">Lijst met alle objecten waar we mee kunnen colliden</param> /// <param name="movingCollide">object dat kan colliden en beweegt</param> private void DetectStaticBlocks(List <ICollide> collideObjects, IMovingCollide movingCollide) { foreach (ICollide currentBlock in collideObjects.OfType <StaticBlock>()) { if (currentBlock is ICollectable || currentBlock is IDeathBlock || movingCollide is IDeathBlock) { continue; } if (movingCollide.Velocity.X > 0 && IsTouchingLeft(currentBlock, movingCollide)) { if (currentBlock is IMoveBlock) { movingCollide.CurrentCollisionBlock = (IMoveBlock)currentBlock; } else { movingCollide.CurrentCollisionBlock = null; } movingCollide.TouchingRight = true; } if (movingCollide.Velocity.X < 0 && IsTouchingRight(currentBlock, movingCollide)) { if (currentBlock is IMoveBlock) { movingCollide.CurrentCollisionBlock = (IMoveBlock)currentBlock; } else { movingCollide.CurrentCollisionBlock = null; } movingCollide.TouchingLeft = true; } if (movingCollide.Velocity.Y > 0 && IsTouchingTop(currentBlock, movingCollide)) { movingCollide.TouchingGround = true; } if (movingCollide.Velocity.Y < 0 && IsTouchingBottom(currentBlock, movingCollide)) { movingCollide.TouchingTop = true; } } }
/// <summary> /// Kijkt of er een collision gebeurt /// </summary> /// <param name="collideObjects">Lijst met elk object dat kan colliden</param> /// <param name="movingCollision">bewegend object dat kan colliden</param> public void CollisionDetect(List <ICollide> collideObjects, IMovingCollide movingCollision) { ResetState(movingCollision); if (movingCollision is Hero) { DetectHeroDeath(collideObjects, (IDie)movingCollision); } DetectStaticBlocks(collideObjects, movingCollision); DetectEnemy(collideObjects, movingCollision); if (movingCollision is Hero) { DetectCollectable(collideObjects, (ICanCollect)movingCollision); } if (movingCollision is Enemy) { DetectInvisibleBlock(collideObjects, (Enemy)movingCollision); } if (movingCollision is IDeathBlock) { DetectInvisibleBlockForProjectiles(collideObjects, movingCollision); } }
abstract public void Update(IMovingCollide hero);
/// <summary> /// Detecteert onzichtbare blokken voor de projectielen /// </summary> /// <param name="collideObjects">Lijst met elk object dat kan colliden</param> /// <param name="movingCollide">bewegend object dat kan colliden</param> private static void DetectInvisibleBlockForProjectiles(List <ICollide> collideObjects, IMovingCollide movingCollide) { foreach (IInvisible invisibleBlock in collideObjects.OfType <IInvisible>()) { if (movingCollide.CollisionRectangle.Intersects(invisibleBlock.CollisionRectangle)) { movingCollide.TouchingGround = true; } } }