Compare commits

...

2 Commits

Author SHA1 Message Date
f6fead2ddd Boundary fix, at small performance cost. 2025-11-29 09:58:26 -05:00
bc66aa740e Alertbox added. 2025-11-28 18:10:37 -05:00
5 changed files with 104 additions and 10 deletions

49
elements/alert.go Normal file
View 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

Binary file not shown.

25
fonts/fonts.go Normal file
View 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
}

View File

@@ -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) {
@@ -230,8 +237,10 @@ func (g *Game) UpdateParticles() {
if g.resolvecollisions {
g.resolvers[g.resolveridx](particle)
}
}
g.BoundParticle(particle)
for _, p := range g.particles {
g.BoundParticle(p)
}
}
@@ -279,22 +288,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 +322,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
View File

@@ -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
)