Alertbox added.
This commit is contained in:
49
elements/alert.go
Normal file
49
elements/alert.go
Normal file
@@ -0,0 +1,49 @@
|
||||
package elements
|
||||
|
||||
import (
|
||||
"fluids/fonts"
|
||||
|
||||
"github.com/hajimehoshi/ebiten/v2"
|
||||
"github.com/hajimehoshi/ebiten/v2/text/v2"
|
||||
)
|
||||
|
||||
const (
|
||||
AlertWidth = 50
|
||||
AlertHeight = 150
|
||||
)
|
||||
|
||||
type Alert struct {
|
||||
Sprite *ebiten.Image
|
||||
text string
|
||||
textfacesource *text.GoTextFaceSource
|
||||
}
|
||||
|
||||
func NewAlert() *Alert {
|
||||
a := &Alert{
|
||||
Sprite: ebiten.NewImage(AlertWidth, AlertHeight),
|
||||
textfacesource: fonts.PixelFont,
|
||||
}
|
||||
return a
|
||||
}
|
||||
|
||||
func (a *Alert) Draw() {
|
||||
a.Sprite.Clear()
|
||||
|
||||
fnt := &text.GoTextFace{
|
||||
Source: a.textfacesource,
|
||||
Size: 20,
|
||||
}
|
||||
|
||||
_, h := text.Measure(a.text, fnt, 0)
|
||||
top := &text.DrawOptions{}
|
||||
top.GeoM.Translate(0, h)
|
||||
text.Draw(a.Sprite, a.text, fnt, top)
|
||||
}
|
||||
|
||||
func (a *Alert) Update() {
|
||||
|
||||
}
|
||||
|
||||
func (a *Alert) SetText(t string) {
|
||||
a.text = t
|
||||
}
|
||||
BIN
fonts/Rockboxcond12.ttf
Normal file
BIN
fonts/Rockboxcond12.ttf
Normal file
Binary file not shown.
25
fonts/fonts.go
Normal file
25
fonts/fonts.go
Normal file
@@ -0,0 +1,25 @@
|
||||
package fonts
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
_ "embed"
|
||||
"log"
|
||||
|
||||
"github.com/hajimehoshi/ebiten/v2/text/v2"
|
||||
)
|
||||
|
||||
const ()
|
||||
|
||||
var (
|
||||
PixelFont *text.GoTextFaceSource
|
||||
//go:embed Rockboxcond12.ttf
|
||||
pixel_fnt []byte
|
||||
)
|
||||
|
||||
func init() {
|
||||
s, err := text.NewGoTextFaceSource(bytes.NewReader(pixel_fnt))
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
PixelFont = s
|
||||
}
|
||||
32
game/game.go
32
game/game.go
@@ -15,14 +15,13 @@ import (
|
||||
)
|
||||
|
||||
const (
|
||||
GameWidth = 640
|
||||
GameHeight = 360
|
||||
GameParticleCount = 2000
|
||||
GameGravity = 2
|
||||
GameParticleRadius = 5
|
||||
GameDamping = .7
|
||||
GameDeltaTimeStep = 0.5
|
||||
|
||||
GameWidth = 640
|
||||
GameHeight = 360
|
||||
GameParticleCount = 2000
|
||||
GameGravity = 2
|
||||
GameParticleRadius = 5
|
||||
GameDamping = .7
|
||||
GameDeltaTimeStep = 0.5
|
||||
GameInfluenceRadius = 30
|
||||
)
|
||||
|
||||
@@ -38,6 +37,7 @@ type Game struct {
|
||||
resolvecollisions bool
|
||||
resolvers []func(particle *elements.Particle)
|
||||
resolveridx int
|
||||
alertbox *elements.Alert
|
||||
}
|
||||
|
||||
func NewGame() *Game {
|
||||
@@ -47,6 +47,7 @@ func NewGame() *Game {
|
||||
renderquads: false,
|
||||
resolvecollisions: false,
|
||||
resolveridx: 0,
|
||||
alertbox: elements.NewAlert(),
|
||||
}
|
||||
|
||||
g.particlebox = &gamedata.Vector{
|
||||
@@ -104,6 +105,12 @@ func (g *Game) Draw(screen *ebiten.Image) {
|
||||
if g.renderquads {
|
||||
g.RenderQuadrants(screen)
|
||||
}
|
||||
|
||||
if g.paused {
|
||||
op := &ebiten.DrawImageOptions{}
|
||||
op.GeoM.Translate(50, 50)
|
||||
screen.DrawImage(g.alertbox.Sprite, op)
|
||||
}
|
||||
}
|
||||
|
||||
func (g *Game) Layout(x, y int) (int, int) {
|
||||
@@ -279,22 +286,30 @@ func (g *Game) RenderQuadrants(img *ebiten.Image) {
|
||||
}
|
||||
|
||||
func (g *Game) ParseInputs() {
|
||||
|
||||
//refresh particles
|
||||
if inpututil.IsKeyJustPressed(ebiten.KeyR) {
|
||||
g.InitializeParticles()
|
||||
}
|
||||
|
||||
//pause simulation
|
||||
if inpututil.IsKeyJustPressed(ebiten.KeyP) {
|
||||
g.paused = !g.paused
|
||||
g.alertbox.SetText("PAUSED")
|
||||
g.alertbox.Draw()
|
||||
}
|
||||
|
||||
//show quadtree quadrants
|
||||
if inpututil.IsKeyJustPressed(ebiten.KeyQ) {
|
||||
g.renderquads = !g.renderquads
|
||||
}
|
||||
|
||||
//enable collision resolution
|
||||
if inpututil.IsKeyJustPressed(ebiten.KeyC) {
|
||||
g.resolvecollisions = !g.resolvecollisions
|
||||
}
|
||||
|
||||
//switch between collision resolvers
|
||||
if inpututil.IsKeyJustPressed(ebiten.KeyLeft) {
|
||||
g.resolveridx = g.resolveridx - 1
|
||||
if g.resolveridx < 0 {
|
||||
@@ -305,7 +320,6 @@ func (g *Game) ParseInputs() {
|
||||
if inpututil.IsKeyJustPressed(ebiten.KeyRight) {
|
||||
g.resolveridx = (g.resolveridx + 1) % len(g.resolvers)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func (g *Game) RebuildQuadtree() {
|
||||
|
||||
4
go.mod
4
go.mod
@@ -8,7 +8,11 @@ require (
|
||||
github.com/ebitengine/gomobile v0.0.0-20250923094054-ea854a63cce1 // indirect
|
||||
github.com/ebitengine/hideconsole v1.0.0 // indirect
|
||||
github.com/ebitengine/purego v0.9.0 // indirect
|
||||
github.com/go-text/typesetting v0.3.0 // indirect
|
||||
github.com/jezek/xgb v1.1.1 // indirect
|
||||
github.com/rivo/uniseg v0.4.7 // indirect
|
||||
golang.org/x/image v0.31.0 // indirect
|
||||
golang.org/x/sync v0.17.0 // indirect
|
||||
golang.org/x/sys v0.36.0 // indirect
|
||||
golang.org/x/text v0.29.0 // indirect
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user