Skip to content

Commit

Permalink
Added restart game (F2 Key), game over scene show (lives <= 0), expor…
Browse files Browse the repository at this point in the history
…t explosion anim textures
  • Loading branch information
evgTSV committed Sep 8, 2024
1 parent 3ff68d2 commit db4a96f
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 11 deletions.
18 changes: 13 additions & 5 deletions O21.Game/Game.fs
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ type Game(window: WindowParameters, content: LocalContent, data: U95Data) =
let context = new QueueSynchronizationContext(eventQueue)
let prevContext = SynchronizationContext.Current
do SynchronizationContext.SetSynchronizationContext(context)

let mutable state = {
let initialState = {
Scene = MainMenuScene.Init(window, content, data)
Settings = { SoundVolume = 0.1f }
U95Data = data
Expand All @@ -34,6 +34,8 @@ type Game(window: WindowParameters, content: LocalContent, data: U95Data) =
MusicPlayer = None
}

let mutable state = initialState

let pumpQueue() =
while not eventQueue.IsEmpty do
match eventQueue.TryDequeue() with
Expand All @@ -50,8 +52,11 @@ type Game(window: WindowParameters, content: LocalContent, data: U95Data) =

member _.Initialize(lifetime: Lifetime): unit =
launchMusicPlayer lifetime

member _.Restart(): unit =
state <- { initialState with Game = GameEngine.Create({ Total = GetTime(); Delta = GetFrameTime() }, data.Levels[GameRules.StartingLevel]) }

member _.Update() =
member this.Update() =
let input = Input.Handle(state.Scene.Camera)
let time = { Total = GetTime(); Delta = GetFrameTime() }

Expand All @@ -64,8 +69,11 @@ type Game(window: WindowParameters, content: LocalContent, data: U95Data) =
let scene: IScene =
match event with
| Some (NavigateTo Scene.MainMenu) -> MainMenuScene.Init(window, content, data)
| Some (NavigateTo Scene.Play) -> PlayScene.Init content
| Some (NavigateTo Scene.GameOver) -> GameOverScene.Init(window, content, state.Language)
| Some (NavigateTo Scene.Play) ->
this.Restart()
PlayScene.Init content
| Some (NavigateTo Scene.GameOver) ->
GameOverScene.Init(window, content, state.Language)
| Some (NavigateTo Scene.Help) ->
let loadedHelp = (state.Language |> state.U95Data.Help)
HelpScene.Init(window, content, loadedHelp, state.Language)
Expand Down
3 changes: 2 additions & 1 deletion O21.Game/Input.fs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ open System.Numerics
open Raylib_CsLo
open type Raylib_CsLo.Raylib

type Key = Left | Right | Up | Down | Fire | Pause
type Key = Left | Right | Up | Down | Fire | Pause | Restart

type Input = {
Pressed: Set<Key>
Expand All @@ -26,6 +26,7 @@ module Input =
KeyboardKey.KEY_LEFT, Left
KeyboardKey.KEY_RIGHT, Right
KeyboardKey.KEY_SPACE, Fire
KeyboardKey.KEY_F2, Restart
KeyboardKey.KEY_F3, Pause
]

Expand Down
13 changes: 9 additions & 4 deletions O21.Game/Scenes/PlayScene.fs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,9 @@ module private InputProcessor =
game <- game'
effects <- Array.append effects effects'
game, effects

let RestartKeyPressed (input: Input) = Set.contains Key.Restart input.Pressed


type PlayScene = {
HUD: HUD
Expand Down Expand Up @@ -104,12 +107,14 @@ type PlayScene = {
let sounds =
state.SoundsToStartPlaying +
(allEffects |> Seq.map(fun (PlaySound s) -> s) |> Set.ofSeq)
let navigationEvent =
if this.HUD.Lives < 0 then
// TODO[#32]: Should be handled by the game engine
let state, navigationEvent =
if this.HUD.Lives <= 0 then
{ state with Game = fst <| state.Game.ApplyCommand(PlayerCommand.Suspend) },
Some (NavigateTo Scene.GameOver)
else if InputProcessor.RestartKeyPressed input then
state, Some (NavigateTo Scene.Play)
else
None
state, None

{ state with SoundsToStartPlaying = sounds }, navigationEvent

Expand Down
4 changes: 3 additions & 1 deletion O21.Game/U95/Sprites.fs
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,16 @@ type PlayerSprites =
{
Left: Texture[]
Right: Texture[]
Explosion: Texture[]
}

static member Load(lifetime: Lifetime) (images: Dib[]): PlayerSprites =
let loadImageByIndex i =
CreateTransparentSprite lifetime images[i] images[i + 24]
let right = seq { 6; yield! [| 17..23 |] } |> Seq.map loadImageByIndex |> Seq.toArray
let left = seq { yield! [| 7..13 |]; 24 } |> Seq.map loadImageByIndex |> Seq.toArray
{ Left = left; Right = right }
let explosion = seq { yield! [| 16..18 |] } |> Seq.map loadImageByIndex |> Seq.toArray
{ Left = left; Right = right; Explosion = explosion }

type Sprites = {
Bricks: Map<int, Texture>
Expand Down

0 comments on commit db4a96f

Please sign in to comment.