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