Skip to content

Commit

Permalink
Add unit tests for check box collision and adding scores for kill ene…
Browse files Browse the repository at this point in the history
…my (bomb)
  • Loading branch information
evgTSV committed Jan 9, 2025
1 parent e24c04f commit 08f35e3
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 5 deletions.
62 changes: 57 additions & 5 deletions O21.Tests/GameEngineTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ open O21.Game.U95.Parser
open Xunit

open O21.Game.Engine
open O21.Tests.Helpers

let private frameUp time =
let mutable currentTime = time
Expand Down Expand Up @@ -135,15 +136,15 @@ module Player =
|]
}
let player = { Player.Default with TopLeft = Point(GameRules.BrickSize.X - GameRules.PlayerSize.X, 0) }
let player' = player.Update(Helpers.getEmptyPlayerEnvWithLevel level, 1)
let player' = player.Update(getEmptyPlayerEnvWithLevel level, 1)
Assert.True(match player' with | PlayerEffect.Update _ -> true | _ -> false)

let player = {
player with
Velocity = Vector(1, 0)
}
let player' = player.Update(Helpers.getEmptyPlayerEnvWithLevel level, 1)
Assert.True(match player' with | PlayerEffect.Die -> true | _ -> false)
let player' = player.Update(getEmptyPlayerEnvWithLevel level, 1)
Assert.True(match player' with | PlayerEffect.Die -> true | _ -> false)

module OxygenSystem =

Expand All @@ -164,11 +165,11 @@ module OxygenSystem =
let player = Player.Default

let player = { player with Player.Oxygen.Amount = 1 }
let player' = player.Update(Helpers.getEmptyPlayerEnvWithLevel level, 0);
let player' = player.Update(getEmptyPlayerEnvWithLevel level, 0);
Assert.True(match player' with | PlayerEffect.Update _ -> true | _ -> false)

let player = { player with Player.Oxygen.Amount = -1 }
let player' = player.Update(Helpers.getEmptyPlayerEnvWithLevel level, 0)
let player' = player.Update(getEmptyPlayerEnvWithLevel level, 0)
Assert.True(match player' with | PlayerEffect.Die -> true | _ -> false)

module ParticleSystem =
Expand Down Expand Up @@ -285,6 +286,31 @@ module Bullets =
let actualVelocity = gameEngine.Bullets[0].Velocity
Assert.Equal(expectedVelocity, actualVelocity)

module ScoreSystem =
[<Fact>]
let ``Adding scores for hit enemy``(): unit =
let level = {
LevelMap = [|
[| Empty; Bomb |]
|]
}

let engine = newEngine.ChangeLevel(level)
let engine =
{ engine with
Player = { newEngine.Player with
TopLeft = Point(engine.Bombs[0].TopLeft.X
- GameRules.PlayerSize.X - GameRules.BulletSize.X,
- GameRules.PlayerSize.Y / 2) }}

let initialScores = engine.Player.Scores

let engine, _ = engine.ApplyCommand PlayerCommand.Shoot
let engine = engine |> frameUpN timeZero
((engine.Bombs[0].TopLeft.X - engine.Bullets[0].Box.TopRight.X) / GameRules.BulletVelocity + 1)
let actualScores = engine.Player.Scores
Assert.Equal(initialScores + GameRules.GiveScoresForBomb, actualScores)

module Geometry =
open O21.Game.Engine.Geometry

Expand All @@ -309,3 +335,29 @@ module Geometry =

let box2 = { TopLeft = Point(GameRules.BrickSize.X, 0); Size = Vector(1, 1) }
Assert.Equal(Collision.CollidesBrick, CheckCollision level box2 [||])

[<Fact>]
let ``Box collision check``(): unit =
let level = createEmptyLevel 50 50
let box1 = { TopLeft = Point(20, 20); Size = Vector(10, 10) }
let box2 = { TopLeft = box1.TopRight + Vector(1, 1); Size = Vector(10, 10) }

Assert.Equal(Collision.None, CheckCollision level box1 [| box2 |])

let size = Vector(2, 2)

let topLeft = [|
Point(box1.TopLeft.X - size.X / 2, box1.TopLeft.Y - size.Y / 2) // TopLeft collides
Point(box1.TopRight.X - size.X / 2, box1.TopLeft.Y - size.Y / 2) // TopRight collides
Point(box1.BottomLeft.X - size.X / 2, box1.BottomLeft.Y - size.Y / 2) // BottomLeft collides
Point(box1.BottomRight.X - size.X / 2, box1.BottomRight.Y - size.Y / 2) // BottomRight collides
Point(box1.TopLeft.X + size.X / 2, box1.TopLeft.Y + size.Y / 2) // Inner collides
|]

let isAllCollides =
(topLeft, true)
||> Array.foldBack (fun p acc ->
let box = { TopLeft = p; Size = size }
acc && (CheckCollision level box1 [| box |]).IsCollidesBox)

Assert.True(isAllCollides)
6 changes: 6 additions & 0 deletions O21.Tests/Helpers.fs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ module O21.Tests.Helpers

open O21.Game.U95
open O21.Game.Engine.Environments
open O21.Game.U95.Parser

let getEmptyPlayerEnvWithLevel (level: Level) =
{
Expand All @@ -10,3 +11,8 @@ let getEmptyPlayerEnvWithLevel (level: Level) =
EnemyColliders = [||]
BonusColliders = [||]
}

let createEmptyLevel width height = {
LevelMap =
Array.create height (Array.create width MapOfLevel.Empty)
}

0 comments on commit 08f35e3

Please sign in to comment.