From 29e4a7572b36a3071611f85553762f964b3c1ef0 Mon Sep 17 00:00:00 2001 From: Sasha Koshka Date: Tue, 21 Feb 2023 18:53:19 -0500 Subject: [PATCH] Added health and stamina --- examples/raycaster/game.go | 59 ++++++++++++++++++++++++++++++--- examples/raycaster/main.go | 24 ++++++++++++-- examples/raycaster/raycaster.go | 3 ++ 3 files changed, 80 insertions(+), 6 deletions(-) diff --git a/examples/raycaster/game.go b/examples/raycaster/game.go index a219fc2..25a05c4 100644 --- a/examples/raycaster/game.go +++ b/examples/raycaster/game.go @@ -10,7 +10,11 @@ type Game struct { tickChan <- chan time.Time stopChan chan bool + stamina float64 + health float64 + controlState ControlState + onStatUpdate func () } func NewGame (world World, textures Textures) (game *Game) { @@ -21,6 +25,8 @@ func NewGame (world World, textures Textures) (game *Game) { game.Raycaster.OnControlStateChange (func (state ControlState) { game.controlState = state }) + game.stamina = 0.5 + game.health = 1 return } @@ -34,18 +40,45 @@ func (game *Game) DrawTo (canvas canvas.Canvas) { game.Raycaster.DrawTo(canvas) } +func (game *Game) Stamina () float64 { + return game.stamina +} + +func (game *Game) Health () float64 { + return game.health +} + +func (game *Game) OnStatUpdate (callback func ()) { + game.onStatUpdate = callback +} + func (game *Game) tick () { + moved := false + statUpdate := false + + speed := 0.07 + if game.controlState.Sprint { + speed = 0.16 + } + if game.stamina <= 0 { + speed = 0 + } + if game.controlState.WalkForward { - game.Walk(0.1) + game.Walk(speed) + moved = true } if game.controlState.WalkBackward { - game.Walk(-0.1) + game.Walk(-speed) + moved = true } if game.controlState.StrafeLeft { - game.Strafe(-0.1) + game.Strafe(-speed) + moved = true } if game.controlState.StrafeRight { - game.Strafe(0.1) + game.Strafe(speed) + moved = true } if game.controlState.LookLeft { game.Rotate(-0.1) @@ -54,7 +87,25 @@ func (game *Game) tick () { game.Rotate(0.1) } + if moved { + game.stamina -= speed / 50 + statUpdate = true + } else if game.stamina < 1 { + game.stamina += 0.005 + statUpdate = true + } + + if game.stamina > 1 { + game.stamina = 1 + } + if game.stamina < 0 { + game.stamina = 0 + } + tomo.Do(game.Draw) + if statUpdate && game.onStatUpdate != nil { + tomo.Do(game.onStatUpdate) + } } func (game *Game) run () { diff --git a/examples/raycaster/main.go b/examples/raycaster/main.go index e05118c..f5783bf 100644 --- a/examples/raycaster/main.go +++ b/examples/raycaster/main.go @@ -4,6 +4,7 @@ import "bytes" import _ "embed" import _ "image/png" import "git.tebibyte.media/sashakoshka/tomo" +import "git.tebibyte.media/sashakoshka/tomo/popups" import "git.tebibyte.media/sashakoshka/tomo/layouts/basic" import "git.tebibyte.media/sashakoshka/tomo/elements/basic" import _ "git.tebibyte.media/sashakoshka/tomo/backends/x" @@ -19,7 +20,7 @@ func run () { window, _ := tomo.NewWindow(640, 480) window.SetTitle("Raycaster") - container := basicElements.NewContainer(basicLayouts.Vertical { true, true }) + container := basicElements.NewContainer(basicLayouts.Vertical { false, false }) window.Adopt(container) wallTexture, _ := TextureFrom(bytes.NewReader(wallTextureBytes)) @@ -46,10 +47,29 @@ func run () { wallTexture, }) - container.Adopt(basicElements.NewLabel("Explore a 3D world!", false), false) + topBar := basicElements.NewContainer(basicLayouts.Horizontal { true, true }) + staminaBar := basicElements.NewProgressBar(game.Stamina()) + healthBar := basicElements.NewProgressBar(game.Health()) + + topBar.Adopt(basicElements.NewLabel("Stamina:", false), false) + topBar.Adopt(staminaBar, true) + topBar.Adopt(basicElements.NewLabel("Health:", false), false) + topBar.Adopt(healthBar, true) + container.Adopt(topBar, false) container.Adopt(game, true) game.Focus() + + game.OnStatUpdate (func () { + staminaBar.SetProgress(game.Stamina()) + }) window.OnClose(tomo.Stop) window.Show() + + popups.NewDialog ( + popups.DialogKindInfo, + "Welcome to the backrooms", + "You've no-clipped into the backrooms!\n" + + "Move with WASD, and look with the arrow keys.\n" + + "Keep an eye on your health and stamina.") } diff --git a/examples/raycaster/raycaster.go b/examples/raycaster/raycaster.go index 4cffc50..f4bfa7b 100644 --- a/examples/raycaster/raycaster.go +++ b/examples/raycaster/raycaster.go @@ -16,6 +16,7 @@ type ControlState struct { StrafeRight bool LookLeft bool LookRight bool + Sprint bool } type Raycaster struct { @@ -81,6 +82,7 @@ func (element *Raycaster) HandleKeyDown (key input.Key, modifiers input.Modifier case 'd', 'D': element.controlState.StrafeRight = true case 'w', 'W': element.controlState.WalkForward = true case 's', 'S': element.controlState.WalkBackward = true + case input.KeyLeftControl: element.controlState.Sprint = true default: return } @@ -97,6 +99,7 @@ func (element *Raycaster) HandleKeyUp(key input.Key, modifiers input.Modifiers) case 'd', 'D': element.controlState.StrafeRight = false case 'w', 'W': element.controlState.WalkForward = false case 's', 'S': element.controlState.WalkBackward = false + case input.KeyLeftControl: element.controlState.Sprint = false default: return }