Add brake

This commit is contained in:
Trevor Slocum 2023-07-08 17:30:31 -07:00
parent 4f2bf69ccb
commit b82216c2f9
6 changed files with 75 additions and 23 deletions

13
flags.go Normal file
View file

@ -0,0 +1,13 @@
//go:build !js || !wasm
// +build !js !wasm
package main
import (
"flag"
)
func parseFlags(game *Game) {
flag.IntVar(&game.debug, "debug", 0, "print debug information")
flag.Parse()
}

14
flags_web.go Normal file
View file

@ -0,0 +1,14 @@
//go:build js && wasm
// +build js,wasm
package main
import (
"github.com/hajimehoshi/ebiten/v2"
)
func parseFlags(game *Game) {
game.wasm = true
ebiten.SetFullscreen(true)
}

2
go.mod
View file

@ -8,7 +8,7 @@ require (
)
require (
github.com/ebitengine/purego v0.4.0-alpha.4 // indirect
github.com/ebitengine/purego v0.4.0-alpha.5 // indirect
github.com/go-gl/glfw/v3.3/glfw v0.0.0-20221017161538-93cebf72946b // indirect
github.com/jezek/xgb v1.1.0 // indirect
github.com/qmuntal/gltf v0.24.0 // indirect

4
go.sum
View file

@ -1,5 +1,5 @@
github.com/ebitengine/purego v0.4.0-alpha.4 h1:Y7yIV06Yo5M2BAdD7EVPhfp6LZ0tEcQo5770OhYUVes=
github.com/ebitengine/purego v0.4.0-alpha.4/go.mod h1:ah1In8AOtksoNK6yk5z1HTJeUkC1Ez4Wk2idgGslMwQ=
github.com/ebitengine/purego v0.4.0-alpha.5 h1:1jd+ClW6gD2x2F/+j4lCGc3TQMWL3hNYgLu/xF9Jz0g=
github.com/ebitengine/purego v0.4.0-alpha.5/go.mod h1:ah1In8AOtksoNK6yk5z1HTJeUkC1Ez4Wk2idgGslMwQ=
github.com/go-gl/glfw/v3.3/glfw v0.0.0-20221017161538-93cebf72946b h1:GgabKamyOYguHqHjSkDACcgoPIz3w0Dis/zJ1wyHHHU=
github.com/go-gl/glfw/v3.3/glfw v0.0.0-20221017161538-93cebf72946b/go.mod h1:tQ2UAYgL5IevRw8kRxooKSPJfGvJ9fJQFa0TUsXzTg8=
github.com/go-test/deep v1.0.1 h1:UQhStjbkDClarlmv0am7OXXO4/GaPdCGiUiMTvi28sg=

49
main.go
View file

@ -1,13 +1,14 @@
package main
import (
"flag"
"fmt"
"image/color"
"math"
"math/rand"
"os"
"github.com/hajimehoshi/ebiten/v2/inpututil"
"github.com/hajimehoshi/ebiten/v2/ebitenutil"
_ "embed"
@ -24,6 +25,8 @@ type Game struct {
truck *garbageTruck
wasm bool
debug int
}
@ -245,18 +248,21 @@ func (g *Game) Init() {
// Garbage truck.
{
mesh := tetra3d.NewCubeMesh()
model := tetra3d.NewModel(mesh, "")
model.Color = tetra3d.NewColor(0.12, 0.44, 0.24, 1)
model.Grow(-0.75, -0.25, -0.3)
{
mesh := tetra3d.NewCubeMesh()
model := tetra3d.NewModel(mesh, "")
model.Color = tetra3d.NewColor(0.12, 0.44, 0.24, 1)
model.Grow(-0.75, -0.25, -0.3)
g.truck.Model = model
g.Scene.Root.AddChildren(g.truck.Model)
g.truck.Model = model
g.Scene.Root.AddChildren(g.truck.Model)
}
lightX := 0.55
headlightDistance := 0.85
headlightShade := float32(0.75)
const (
lightX = 0.55
headlightDistance = 0.85
headlightShade = float32(0.75)
)
// Headlights.
{
@ -305,6 +311,18 @@ func (g *Game) Update() error {
return nil
}
if !g.wasm && ebiten.IsKeyPressed(ebiten.KeyEscape) {
os.Exit(0)
return nil
}
if inpututil.IsKeyJustPressed(ebiten.KeyV) && ebiten.IsKeyPressed(ebiten.KeyControl) && ebiten.IsKeyPressed(ebiten.KeyShift) {
g.debug++
if g.debug > 1 {
g.debug = 0
}
}
const moveSize = 1
moveX, moveY := 0, 0
if ebiten.IsKeyPressed(ebiten.KeyLeft) || ebiten.IsKeyPressed(ebiten.KeyA) {
@ -319,10 +337,11 @@ func (g *Game) Update() error {
if ebiten.IsKeyPressed(ebiten.KeyDown) || ebiten.IsKeyPressed(ebiten.KeyS) {
moveY += moveSize
}
braking := ebiten.IsKeyPressed(ebiten.KeySpace)
// Update truck.
t := g.truck
t.MoveX, t.MoveY = moveX, moveY
t.MoveX, t.MoveY, t.Braking = moveX, moveY, braking
t.Update()
// Move truck model and camera.
@ -334,7 +353,6 @@ func (g *Game) Update() error {
}
func (g *Game) Draw(screen *ebiten.Image) {
// Clear the screen with a color.
screen.Fill(color.RGBA{60, 70, 80, 255})
@ -371,10 +389,7 @@ func main() {
ebiten.SetTPS(100)
game := NewGame()
flag.IntVar(&game.debug, "debug", 0, "print debug information")
flag.Parse()
parseFlags(game)
if err := ebiten.RunGame(game); err != nil {
panic(err)
}

View file

@ -16,8 +16,9 @@ type garbageTruck struct {
angle float64
angularVelocity float64
MoveX int // -1 left, 0 center, 1 right
MoveY int // -1 backward, 0 center, 1 forward
MoveX int // -1 left, 0 center, 1 right
MoveY int // -1 backward, 0 center, 1 forward
Braking bool
Model *tetra3d.Model
}
@ -38,6 +39,10 @@ func (t *garbageTruck) Update() {
isDriving := true
if isDriving { // TODO
if t.Braking {
t.MoveY = 0
}
if t.MoveY < 0 {
t.power += powerFactor
} else {
@ -49,6 +54,12 @@ func (t *garbageTruck) Update() {
t.reverse -= reverseFactor
}
if t.Braking {
const adjustment = 0.9975
t.power = t.power * adjustment
t.reverse = t.reverse * adjustment
}
t.power = math.Max(0, math.Min(maxPower, t.power))
t.reverse = math.Max(0, math.Min(maxReverse, t.reverse))
@ -68,7 +79,6 @@ func (t *garbageTruck) Update() {
t.xVelocity += math.Sin(t.angle) * (t.power - t.reverse)
t.yVelocity += math.Cos(t.angle) * (t.power - t.reverse)
t.x += t.xVelocity
t.y -= t.yVelocity
t.xVelocity *= drag