Added health and stamina

This commit is contained in:
Sasha Koshka 2023-02-21 18:53:19 -05:00
parent ddb960571f
commit 29e4a7572b
3 changed files with 80 additions and 6 deletions

View File

@ -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 () {

View File

@ -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.")
}

View File

@ -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
}