Skip to content

Commit

Permalink
bunker destruction!
Browse files Browse the repository at this point in the history
  • Loading branch information
ChrisPritchard committed May 2, 2019
1 parent f5dfc14 commit b83b1e1
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 11 deletions.
1 change: 1 addition & 0 deletions samples/space-invaders-clone/Config.fs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ let invaderShuffleIncrease = 25L
let shuffleDecrease = 20L
let minShuffle = 50L

let projectileWidth = 2
let projectileHeight = 10
let invaderProjectileSpeed = 2
let playerProjectileSpeed = 4
Expand Down
30 changes: 19 additions & 11 deletions samples/space-invaders-clone/Program.fs
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,8 @@ type Message =
| Victory
| Restart

let playerRect model = rect model.playerX playerY playerWidth playerHeight

let invaderImpact x y w h model =
let testRect = rect x y w h
model.invaders
Expand Down Expand Up @@ -175,21 +177,27 @@ let moveProjectiles model =
| Some invaderIndex -> None, Cmd.ofMsg (InvaderHit invaderIndex)
| None -> Some next, Cmd.none

let nextInvaderProjectiles, cmdResult =
(([], cmdResult), model.invaderProjectiles)
||> List.fold (fun (acc, cmdResult) p ->
let playerRect = playerRect model

let nextInvaderProjectiles, cmdResult, newBunkers =
(([], cmdResult, model.bunkers), model.invaderProjectiles)
||> List.fold (fun (acc, cmdResult, bunkers) p ->
let next = { p with y = p.y + invaderProjectileSpeed }
if next.y > resHeight then acc, cmdResult
if next.y > resHeight then acc, cmdResult, bunkers
else
if
p.x >= model.playerX && p.x < model.playerX + playerWidth
&& p.y >= playerY && p.y < playerY + playerHeight then
acc, Cmd.batch [cmdResult; Cmd.ofMsg PlayerHit]
let shotRect = rect p.x p.y projectileWidth projectileHeight
if shotRect.Intersects playerRect then
acc, Cmd.batch [cmdResult; Cmd.ofMsg PlayerHit], bunkers
else
next::acc, cmdResult)
let destroyed, newBunkers = bunkers |> List.partition (fun b -> b.Intersects shotRect)
if List.isEmpty destroyed then
next::acc, cmdResult, bunkers
else
acc, cmdResult, newBunkers)

{ model with
playerProjectile = nextPlayerProjectile
bunkers = newBunkers
invaderProjectiles = nextInvaderProjectiles }, cmdResult

let destroyInvader targetRow index model =
Expand Down Expand Up @@ -265,7 +273,7 @@ let view model dispatch =

yield! model.invaderProjectiles
|> List.map (fun projectile ->
colour Colour.White (1, projectileHeight) (projectile.x, projectile.y))
colour Colour.White (projectileWidth, projectileHeight) (projectile.x, projectile.y))

if not model.freeze then
yield onupdate (fun inputs ->
Expand All @@ -274,7 +282,7 @@ let view model dispatch =

match model.playerProjectile with
| Some p ->
yield colour Colour.White (1, projectileHeight) (p.x, p.y)
yield colour Colour.White (projectileWidth, projectileHeight) (p.x, p.y)
| _ ->
yield onkeydown Keys.Space (fun () -> dispatch PlayerShoot)

Expand Down

0 comments on commit b83b1e1

Please sign in to comment.