Browse Source

Moved movable global variables to a proper closed space

master
parent
commit
ff2972ad30
  1. 1
      src/go.mod
  2. 2
      src/go.sum
  3. 93
      src/main.go
  4. 15
      src/resources.go
  5. BIN
      src/resources/boop.wav
  6. BIN
      src/resources/leather.wav
  7. BIN
      src/resources/menu_switch.wav
  8. BIN
      src/resources/woop.wav

1
src/go.mod

@ -8,6 +8,7 @@ require (
) )
require ( require (
github.com/ebitengine/oto/v3 v3.1.0 // indirect
github.com/ebitengine/purego v0.5.0 // indirect github.com/ebitengine/purego v0.5.0 // indirect
github.com/go-gl/glfw/v3.3/glfw v0.0.0-20200707082815-5321531c36a2 // indirect github.com/go-gl/glfw/v3.3/glfw v0.0.0-20200707082815-5321531c36a2 // indirect
github.com/hajimehoshi/ebiten v1.12.12 // indirect github.com/hajimehoshi/ebiten v1.12.12 // indirect

2
src/go.sum

@ -1,4 +1,6 @@
github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym/WlBOVXweHU+Q+/VP0lqqI8lqeDx9IjBqo= github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym/WlBOVXweHU+Q+/VP0lqqI8lqeDx9IjBqo=
github.com/ebitengine/oto/v3 v3.1.0 h1:9tChG6rizyeR2w3vsygTTTVVJ9QMMyu00m2yBOCch6U=
github.com/ebitengine/oto/v3 v3.1.0/go.mod h1:IK1QTnlfZK2GIB6ziyECm433hAdTaPpOsGMLhEyEGTg=
github.com/ebitengine/purego v0.5.0 h1:JrMGKfRIAM4/QVKaesIIT7m/UVjTj5GYhRSQYwfVdpo= github.com/ebitengine/purego v0.5.0 h1:JrMGKfRIAM4/QVKaesIIT7m/UVjTj5GYhRSQYwfVdpo=
github.com/ebitengine/purego v0.5.0/go.mod h1:ah1In8AOtksoNK6yk5z1HTJeUkC1Ez4Wk2idgGslMwQ= github.com/ebitengine/purego v0.5.0/go.mod h1:ah1In8AOtksoNK6yk5z1HTJeUkC1Ez4Wk2idgGslMwQ=
github.com/go-gl/glfw/v3.3/glfw v0.0.0-20200707082815-5321531c36a2 h1:Ac1OEHHkbAZ6EUnJahF0GKcU0FjPc/V8F1DvjhKngFE= github.com/go-gl/glfw/v3.3/glfw v0.0.0-20200707082815-5321531c36a2 h1:Ac1OEHHkbAZ6EUnJahF0GKcU0FjPc/V8F1DvjhKngFE=

93
src/main.go

@ -14,10 +14,12 @@ import (
"time" "time"
"github.com/hajimehoshi/ebiten/v2" "github.com/hajimehoshi/ebiten/v2"
"github.com/hajimehoshi/ebiten/v2/audio"
"github.com/hajimehoshi/ebiten/v2/inpututil" "github.com/hajimehoshi/ebiten/v2/inpututil"
"github.com/hajimehoshi/ebiten/v2/text" "github.com/hajimehoshi/ebiten/v2/text"
"golang.org/x/image/font" "golang.org/x/image/font"
"golang.org/x/image/font/opentype" "golang.org/x/image/font/opentype"
"golang.org/x/image/font/sfnt"
) )
const Version string = "v0.1" const Version string = "v0.1"
@ -33,36 +35,40 @@ const (
SaveFileName string = "capyclickSave.json" SaveFileName string = "capyclickSave.json"
) )
var WorkingDir string = "."
type Game struct { type Game struct {
WorkingDir string
Config conf.Configuration Config conf.Configuration
Save save.Save Save save.Save
AudioContext *audio.Context
ImageResources map[string]*ebiten.Image
Font *sfnt.Font
} }
// Resource images func NewGame() *Game {
var ( return &Game{
ImgCapybara1 = ebiten.NewImageFromImage(ImageFromFile("capybara_1.png")) WorkingDir: ".",
ImgCapybara2 = ebiten.NewImageFromImage(ImageFromFile("capybara_2.png")) Config: conf.Default(),
ImgCapybara3 = ebiten.NewImageFromImage(ImageFromFile("capybara_3.png")) Save: save.Default(),
AudioContext: audio.NewContext(48000),
GameFont = util.NewFont(ResourceGetFont("PixeloidSans-Bold.otf"), &opentype.FaceOptions{ ImageResources: map[string]*ebiten.Image{
Size: 10, "capybara1": ebiten.NewImageFromImage(ImageFromFile("capybara_1.png")),
DPI: 72, "capybara2": ebiten.NewImageFromImage(ImageFromFile("capybara_2.png")),
Hinting: font.HintingVertical, "capybara3": ebiten.NewImageFromImage(ImageFromFile("capybara_3.png")),
}) },
) Font: ResourceGetFont("PixeloidSans-Bold.otf"),
}
}
// Saves configuration information and game data // Saves configuration information and game data
func SaveData(game *Game) error { func SaveData(game *Game) error {
// Save configuration information and game data // Save configuration information and game data
err := save.Create(filepath.Join(WorkingDir, SaveFileName), game.Save) err := save.Create(filepath.Join(game.WorkingDir, SaveFileName), game.Save)
if err != nil { if err != nil {
logger.Error("[SaveData] Failed to save game data before closing: %s!", err) logger.Error("[SaveData] Failed to save game data before closing: %s!", err)
return err return err
} }
err = conf.Create(filepath.Join(WorkingDir, ConfigurationFileName), game.Config) err = conf.Create(filepath.Join(game.WorkingDir, ConfigurationFileName), game.Config)
if err != nil { if err != nil {
logger.Error("[SaveData] Failed to save game configuration before closing: %s!", err) logger.Error("[SaveData] Failed to save game configuration before closing: %s!", err)
return err return err
@ -112,19 +118,29 @@ func (g *Game) Draw(screen *ebiten.Image) {
screen.Fill(color.Black) screen.Fill(color.Black)
// Capybara // Capybara
scale := 15.0
op := &ebiten.DrawImageOptions{} op := &ebiten.DrawImageOptions{}
op.GeoM.Scale(15, 15) op.GeoM.Scale(scale, scale)
op.GeoM.Translate(float64(screen.Bounds().Dx()/3), float64(screen.Bounds().Dy()/3)) width := g.ImageResources["capybara1"].Bounds().Dx() * int(scale)
screen.DrawImage(ImgCapybara1, op) height := g.ImageResources["capybara1"].Bounds().Dy() * int(scale)
op.GeoM.Translate(
float64(screen.Bounds().Dx()/2)-float64(width/2),
float64(screen.Bounds().Dy()/2)-float64(height/2),
)
screen.DrawImage(g.ImageResources["capybara1"], op)
// Points // Points
msg := fmt.Sprintf("Points: %d", g.Save.Points) msg := fmt.Sprintf("Points: %d", g.Save.Points)
text.Draw( text.Draw(
screen, screen,
msg, msg,
GameFont, util.NewFont(g.Font, &opentype.FaceOptions{
Size: 24,
DPI: 72,
Hinting: font.HintingVertical,
}),
10, 10,
20, 30,
color.White, color.White,
) )
@ -133,9 +149,13 @@ func (g *Game) Draw(screen *ebiten.Image) {
text.Draw( text.Draw(
screen, screen,
msg, msg,
GameFont, util.NewFont(g.Font, &opentype.FaceOptions{
screen.Bounds().Dx()-len(msg)*10, Size: 24,
20, DPI: 72,
Hinting: font.HintingVertical,
}),
screen.Bounds().Dx()-len(msg)*24,
30,
color.White, color.White,
) )
@ -144,9 +164,13 @@ func (g *Game) Draw(screen *ebiten.Image) {
text.Draw( text.Draw(
screen, screen,
msg, msg,
GameFont, util.NewFont(g.Font, &opentype.FaceOptions{
Size: 24,
DPI: 72,
Hinting: font.HintingVertical,
}),
10, 10,
screen.Bounds().Dy()-10, screen.Bounds().Dy()-30,
color.White, color.White,
) )
} }
@ -174,10 +198,7 @@ func main() {
} }
// Create a game instance // Create a game instance
var game *Game = &Game{ var game *Game = NewGame()
Config: conf.Default(),
Save: save.Default(),
}
if !*noFiles { if !*noFiles {
// Work out working directory // Work out working directory
@ -186,17 +207,17 @@ func main() {
logger.Error("[Init] Failed to get executable's path: %s", err) logger.Error("[Init] Failed to get executable's path: %s", err)
os.Exit(1) os.Exit(1)
} }
WorkingDir = filepath.Dir(exeDir) game.WorkingDir = filepath.Dir(exeDir)
} else { } else {
WorkingDir = "" game.WorkingDir = ""
} }
if !*noFiles { if !*noFiles {
// Open/Create configuration file // Open/Create configuration file
var config *conf.Configuration var config *conf.Configuration
config, err := conf.FromFile(filepath.Join(WorkingDir, ConfigurationFileName)) config, err := conf.FromFile(filepath.Join(game.WorkingDir, ConfigurationFileName))
if err != nil { if err != nil {
err = conf.Create(filepath.Join(WorkingDir, ConfigurationFileName), game.Config) err = conf.Create(filepath.Join(game.WorkingDir, ConfigurationFileName), game.Config)
if err != nil { if err != nil {
logger.Error("[Init] Failed to create a new configuration file: %s", err) logger.Error("[Init] Failed to create a new configuration file: %s", err)
os.Exit(1) os.Exit(1)
@ -226,9 +247,9 @@ func main() {
if !*noFiles { if !*noFiles {
// Open/Create save file // Open/Create save file
gameSave, err := save.FromFile(filepath.Join(WorkingDir, SaveFileName)) gameSave, err := save.FromFile(filepath.Join(game.WorkingDir, SaveFileName))
if err != nil { if err != nil {
err = save.Create(filepath.Join(WorkingDir, SaveFileName), game.Save) err = save.Create(filepath.Join(game.WorkingDir, SaveFileName), game.Save)
if err != nil { if err != nil {
logger.Error("[Init] Failed to create a new save file \"%s\": %s", SaveFileName, err) logger.Error("[Init] Failed to create a new save file \"%s\": %s", SaveFileName, err)
os.Exit(1) os.Exit(1)

15
src/resources.go

@ -5,6 +5,7 @@ import (
"embed" "embed"
"image" "image"
"github.com/hajimehoshi/ebiten/v2/audio/wav"
"golang.org/x/image/font/opentype" "golang.org/x/image/font/opentype"
"golang.org/x/image/font/sfnt" "golang.org/x/image/font/sfnt"
) )
@ -46,3 +47,17 @@ func ResourceGetFont(fontFile string) *sfnt.Font {
return tt return tt
} }
func ResourceGetWav(wavFile string) *wav.Stream {
data := ResourceGet(wavFile)
if data == nil {
return nil
}
stream, err := wav.DecodeWithoutResampling(bytes.NewReader(data))
if err != nil {
return nil
}
return stream
}

BIN
src/resources/boop.wav

Binary file not shown.

BIN
src/resources/leather.wav

Binary file not shown.

BIN
src/resources/menu_switch.wav

Binary file not shown.

BIN
src/resources/woop.wav

Binary file not shown.
Loading…
Cancel
Save