2023-02-19 23:52:50 -07:00
|
|
|
package main
|
|
|
|
|
|
|
|
import "time"
|
|
|
|
import "git.tebibyte.media/sashakoshka/tomo"
|
|
|
|
import "git.tebibyte.media/sashakoshka/tomo/canvas"
|
|
|
|
|
|
|
|
type Game struct {
|
|
|
|
*Raycaster
|
|
|
|
running bool
|
|
|
|
tickChan <- chan time.Time
|
|
|
|
stopChan chan bool
|
|
|
|
|
2023-02-21 16:53:19 -07:00
|
|
|
stamina float64
|
|
|
|
health float64
|
|
|
|
|
2023-02-19 23:52:50 -07:00
|
|
|
controlState ControlState
|
2023-02-21 16:53:19 -07:00
|
|
|
onStatUpdate func ()
|
2023-02-19 23:52:50 -07:00
|
|
|
}
|
|
|
|
|
2023-02-21 14:48:56 -07:00
|
|
|
func NewGame (world World, textures Textures) (game *Game) {
|
2023-02-19 23:52:50 -07:00
|
|
|
game = &Game {
|
2023-02-21 14:48:56 -07:00
|
|
|
Raycaster: NewRaycaster(world, textures),
|
2023-02-19 23:52:50 -07:00
|
|
|
stopChan: make(chan bool),
|
|
|
|
}
|
|
|
|
game.Raycaster.OnControlStateChange (func (state ControlState) {
|
|
|
|
game.controlState = state
|
|
|
|
})
|
2023-02-21 16:53:19 -07:00
|
|
|
game.stamina = 0.5
|
|
|
|
game.health = 1
|
2023-02-19 23:52:50 -07:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (game *Game) DrawTo (canvas canvas.Canvas) {
|
|
|
|
if canvas == nil {
|
|
|
|
game.stopChan <- true
|
|
|
|
} else if !game.running {
|
|
|
|
game.running = true
|
|
|
|
go game.run()
|
|
|
|
}
|
|
|
|
game.Raycaster.DrawTo(canvas)
|
|
|
|
}
|
|
|
|
|
2023-02-21 16:53:19 -07:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2023-02-19 23:52:50 -07:00
|
|
|
func (game *Game) tick () {
|
2023-02-21 16:53:19 -07:00
|
|
|
moved := false
|
|
|
|
statUpdate := false
|
|
|
|
|
|
|
|
speed := 0.07
|
|
|
|
if game.controlState.Sprint {
|
|
|
|
speed = 0.16
|
|
|
|
}
|
|
|
|
if game.stamina <= 0 {
|
|
|
|
speed = 0
|
|
|
|
}
|
|
|
|
|
2023-02-19 23:52:50 -07:00
|
|
|
if game.controlState.WalkForward {
|
2023-02-21 16:53:19 -07:00
|
|
|
game.Walk(speed)
|
|
|
|
moved = true
|
2023-02-19 23:52:50 -07:00
|
|
|
}
|
|
|
|
if game.controlState.WalkBackward {
|
2023-02-21 16:53:19 -07:00
|
|
|
game.Walk(-speed)
|
|
|
|
moved = true
|
2023-02-19 23:52:50 -07:00
|
|
|
}
|
|
|
|
if game.controlState.StrafeLeft {
|
2023-02-21 16:53:19 -07:00
|
|
|
game.Strafe(-speed)
|
|
|
|
moved = true
|
2023-02-19 23:52:50 -07:00
|
|
|
}
|
|
|
|
if game.controlState.StrafeRight {
|
2023-02-21 16:53:19 -07:00
|
|
|
game.Strafe(speed)
|
|
|
|
moved = true
|
2023-02-19 23:52:50 -07:00
|
|
|
}
|
|
|
|
if game.controlState.LookLeft {
|
|
|
|
game.Rotate(-0.1)
|
|
|
|
}
|
|
|
|
if game.controlState.LookRight {
|
|
|
|
game.Rotate(0.1)
|
|
|
|
}
|
|
|
|
|
2023-02-21 16:53:19 -07:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2023-02-19 23:52:50 -07:00
|
|
|
tomo.Do(game.Draw)
|
2023-02-21 16:53:19 -07:00
|
|
|
if statUpdate && game.onStatUpdate != nil {
|
|
|
|
tomo.Do(game.onStatUpdate)
|
|
|
|
}
|
2023-02-19 23:52:50 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func (game *Game) run () {
|
|
|
|
ticker := time.NewTicker(time.Second / 30)
|
|
|
|
game.tickChan = ticker.C
|
|
|
|
for game.running {
|
|
|
|
select {
|
|
|
|
case <- game.tickChan:
|
|
|
|
game.tick()
|
|
|
|
case <- game.stopChan:
|
|
|
|
ticker.Stop()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|