Skip to content

Commit 5bfadf7

Browse files
committed
(ForNeVeR#27) Fish: randomize speed, adjust vertical speed
1 parent cc4090b commit 5bfadf7

File tree

5 files changed

+38
-28
lines changed

5 files changed

+38
-28
lines changed

O21.Game/Engine/Enemies.fs

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ type Fish = {
2323
TopLeft: Point
2424
Type: int
2525
/// The current velocity the fish will be moving in the designated direction.
26-
AbsoluteVelocity: int
26+
HorizontalVelocity: int
2727
HorizontalDirection: HorizontalDirection
2828
VerticalDirection: VerticalDirection
2929
} with
@@ -34,7 +34,7 @@ type Fish = {
3434
Array.append [|fishEnv.PlayerCollider|] fishEnv.BulletColliders
3535
match CheckCollision fishEnv.Level this.Box entities with
3636
| Collision.None ->
37-
let newFish = this.WithNextPosition fishEnv.Level
37+
let newFish = this.WithNextPosition fishEnv.Level fishEnv.Random
3838
EnemyEffect.Update newFish
3939
| Collision.OutOfBounds ->
4040
EnemyEffect.Despawn
@@ -44,7 +44,7 @@ type Fish = {
4444
| Collision.CollidesObject _ ->
4545
EnemyEffect.PlayerHit this.Id
4646

47-
member private this.WithNextPosition level: Fish =
47+
member private this.WithNextPosition level random: Fish =
4848
// TODO[#27]: Stick to the wall if there's any space (so that we should move as close to it as possible).
4949
let checkState(state: Fish) =
5050
match CheckCollision level state.Box Array.empty with
@@ -55,14 +55,14 @@ type Fish = {
5555
let newDirection = if keepDirection then this.HorizontalDirection else this.HorizontalDirection.Invert()
5656
let newState = {
5757
this with
58-
TopLeft = this.TopLeft.Move(newDirection, this.AbsoluteVelocity)
58+
TopLeft = this.TopLeft.Move(newDirection, this.HorizontalVelocity)
5959
HorizontalDirection = newDirection
6060
}
6161
checkState newState
6262

6363
let moveVertically (dir: VerticalDirection) =
6464
{ this with
65-
TopLeft = this.TopLeft.Move(dir, this.AbsoluteVelocity)
65+
TopLeft = this.TopLeft.Move(dir, GameRules.FishVerticalVelocity)
6666
VerticalDirection = dir } |> checkState
6767

6868
// Returns length of the wall ahead and whether there's an open path
@@ -73,14 +73,14 @@ type Fish = {
7373
match CheckCollision level { fish.Box with TopLeft = nextPoint } Array.empty with
7474
| Collision.CollidesBrick ->
7575
let beforeBrick =
76-
nextPoint.Move(fish.HorizontalDirection.Invert(), fish.AbsoluteVelocity)
76+
nextPoint.Move(fish.HorizontalDirection.Invert(), fish.HorizontalVelocity)
7777
match CheckCollision level { fish.Box with TopLeft = beforeBrick } Array.empty with
7878
| Collision.None -> count (n + 1) nextPoint
7979
| _ -> struct (n, false)
8080
| Collision.None ->
8181
struct (n, true)
8282
| _ -> struct (n, false)
83-
count 0 (fish.TopLeft.Move(fish.HorizontalDirection, fish.AbsoluteVelocity))
83+
count 0 (fish.TopLeft.Move(fish.HorizontalDirection, fish.HorizontalVelocity))
8484

8585
let chooseDirectionOnWallCollision() =
8686
let struct (lengthUp, isOpenPathUp) = wallAheadInfo this level VerticalDirection.Up
@@ -99,6 +99,10 @@ type Fish = {
9999
moveVertically VerticalDirection.Down
100100
| _ -> None // Short wall (barrier), the next functions handle it
101101

102+
let updateVelocityOnDirectionChange(fish: Fish) =
103+
if fish.HorizontalDirection = this.HorizontalDirection then fish
104+
else { fish with HorizontalVelocity = random.RandomChoice GameRules.FishHorizontalVelocity }
105+
102106
moveHorizontally true
103107
|> Option.orElseWith(fun () ->
104108
chooseDirectionOnWallCollision())
@@ -107,13 +111,14 @@ type Fish = {
107111
|> Option.orElseWith(fun () ->
108112
moveVertically (this.VerticalDirection.Invert()))
109113
|> Option.defaultValue this
114+
|> updateVelocityOnDirectionChange
110115

111116

112117
static member Default = {
113118
Id = Guid.Empty
114119
TopLeft = Point(0, 0)
115120
Type = 0
116-
AbsoluteVelocity = GameRules.FishBaseVelocity
121+
HorizontalVelocity = Array.head GameRules.FishHorizontalVelocity
117122
HorizontalDirection = HorizontalDirection.Right
118123
VerticalDirection = VerticalDirection.Up
119124
}
@@ -125,7 +130,7 @@ type Fish = {
125130
Id = Guid.NewGuid()
126131
TopLeft = position
127132
Type = random.NextExcluding GameRules.FishKinds
128-
AbsoluteVelocity = GameRules.FishBaseVelocity
133+
HorizontalVelocity = random.RandomChoice GameRules.FishHorizontalVelocity
129134
HorizontalDirection = direction
130135
}
131136

@@ -134,7 +139,7 @@ type Fish = {
134139
Id = Guid.NewGuid()
135140
TopLeft = topLeft
136141
Type = ``type``
137-
AbsoluteVelocity = velocity
142+
HorizontalVelocity = velocity
138143
HorizontalDirection = direction
139144
}
140145

O21.Game/Engine/Environments.fs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ type EnemyEnv = {
2929
Level: Level
3030
PlayerCollider: Box
3131
BulletColliders: Box[]
32+
Random: ReproducibleRandom
3233
}
3334

3435
type BonusEnv = EnemyEnv

O21.Game/Engine/GameEngine.fs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ type GameEngine = {
9393
Level = this.CurrentLevel
9494
PlayerCollider = this.Player.Box
9595
BulletColliders = Array.map (fun (x: Bullet) -> x.Box) this.Bullets
96+
Random = this.Random
9697
}
9798

9899
member this.GetBonusEnv(): BonusEnv = this.GetEnemyEnv()

O21.Game/Engine/GameRules.fs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,8 @@ let BonusSize = Vector(30, 30)
120120
// ------------------------------ Enemy Constants ------------------------------
121121
let FishKinds: int = FishSizes.Length
122122
let LevelEntryFishSpawnProbability: float = 0.00363 // 0.363%
123-
let FishBaseVelocity = 4
123+
let FishHorizontalVelocity = [| 3; 4; 5 |]
124+
let FishVerticalVelocity = 5
124125
let BombKinds: int = 5
125126

126127
// ----------------------- Functions -----------------------

O21.Tests/EnemyTests.fs

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -18,49 +18,51 @@ let ``Basic fish get spawned on level``(): unit =
1818
let fish =
1919
Fish.SpawnOnLevelEntry(random, level, player)
2020
|> Array.map (fun f -> { f with Id = Guid.Empty }) // Ignore IDs for comparison
21-
Assert.Equal<Fish>(
21+
Assert.Equivalent(
2222
[| { Id = Guid.Empty
23-
TopLeft = Point (60, 240)
24-
Type = 0
25-
AbsoluteVelocity = 4
23+
TopLeft = Point (60, 12)
24+
Type = 3
25+
HorizontalVelocity = 4
2626
HorizontalDirection = HorizontalDirection.Right
2727
VerticalDirection = VerticalDirection.Up }
2828
{ Id = Guid.Empty
29-
TopLeft = Point (72, 252)
30-
Type = 2
31-
AbsoluteVelocity = 4
29+
TopLeft = Point (204, 84)
30+
Type = 4
31+
HorizontalVelocity = 3
3232
HorizontalDirection = HorizontalDirection.Left
3333
VerticalDirection = VerticalDirection.Up }
3434
{ Id = Guid.Empty
35-
TopLeft = Point (240, 204)
35+
TopLeft = Point (228, 24)
3636
Type = 2
37-
AbsoluteVelocity = 4
38-
HorizontalDirection = HorizontalDirection.Left
37+
HorizontalVelocity = 4
38+
HorizontalDirection = HorizontalDirection.Right
3939
VerticalDirection = VerticalDirection.Up }
4040
{ Id = Guid.Empty
41-
TopLeft = Point (528, 192)
41+
TopLeft = Point (528, 108)
4242
Type = 3
43-
AbsoluteVelocity = 4
44-
HorizontalDirection = HorizontalDirection.Left
45-
VerticalDirection = VerticalDirection.Up } |],
46-
fish
43+
HorizontalVelocity = 3
44+
HorizontalDirection = HorizontalDirection.Right
45+
VerticalDirection = VerticalDirection.Up }|],
46+
fish,
47+
strict = true
4748
)
4849

4950
let private DefaultEnemyEnv = {
5051
Level = Helpers.EmptyLevel
5152
PlayerCollider = Player.Default.Box
5253
BulletColliders = Array.empty
54+
Random = ReproducibleRandom.FromSeed 123
5355
}
5456

5557
[<Fact>]
5658
let ``Fish should move forward``(): unit =
5759
let fish1 = Fish.SpawnNew(
5860
Point(60, 240),
5961
1,
60-
GameRules.FishBaseVelocity,
62+
Helpers.DefaultRandom().RandomChoice GameRules.FishHorizontalVelocity,
6163
HorizontalDirection.Right
6264
)
6365
match fish1.Tick DefaultEnemyEnv with
6466
| EnemyEffect.Update fish2 ->
65-
Assert.Equal(fish1.TopLeft.Move(fish1.HorizontalDirection, fish1.AbsoluteVelocity), fish2.TopLeft)
67+
Assert.Equal(fish1.TopLeft.Move(fish1.HorizontalDirection, fish1.HorizontalVelocity), fish2.TopLeft)
6668
| effect -> Assert.Fail $"Incorrect effect: {effect}."

0 commit comments

Comments
 (0)