This repository has been archived on 2023-08-08. You can view files and clone it, but cannot push or open issues or pull requests.
tomo-old/examples/raycaster/game.go

127 lines
2.2 KiB
Go
Raw Normal View History

package main
import "time"
2023-03-11 23:33:05 -07:00
import "image"
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
controlState ControlState
2023-02-21 16:53:19 -07:00
onStatUpdate func ()
}
2023-02-21 14:48:56 -07:00
func NewGame (world World, textures Textures) (game *Game) {
game = &Game {
2023-02-21 14:48:56 -07:00
Raycaster: NewRaycaster(world, textures),
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
return
}
2023-03-11 23:33:05 -07:00
func (game *Game) DrawTo (canvas canvas.Canvas, bounds image.Rectangle) {
if canvas == nil {
2023-03-11 23:33:05 -07:00
select {
case game.stopChan <- true:
default:
}
} else if !game.running {
game.running = true
go game.run()
}
2023-03-11 23:33:05 -07:00
game.Raycaster.DrawTo(canvas, bounds)
}
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
}
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
}
if game.controlState.WalkForward {
2023-02-21 16:53:19 -07:00
game.Walk(speed)
moved = true
}
if game.controlState.WalkBackward {
2023-02-21 16:53:19 -07:00
game.Walk(-speed)
moved = true
}
if game.controlState.StrafeLeft {
2023-02-21 16:53:19 -07:00
game.Strafe(-speed)
moved = true
}
if game.controlState.StrafeRight {
2023-02-21 16:53:19 -07:00
game.Strafe(speed)
moved = true
}
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
}
tomo.Do(game.Draw)
2023-02-21 16:53:19 -07:00
if statUpdate && game.onStatUpdate != nil {
tomo.Do(game.onStatUpdate)
}
}
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()
}
}
}