Browse Source

PORT: Ported capyclick to web

master
parent
commit
7a14f4eedb
  1. 25
      Makefile
  2. 16
      build/capyclick.html
  3. 10
      src/main.go

25
Makefile

@ -1,14 +1,25 @@
game:=capyclick
bin:=bin
desktopBin:=bin/desktop
webBin:=bin/web
wasmHtml:=build/capyclick.html
wasmExec:=$(shell go env GOROOT)
all: clean
mkdir -p bin
cd src && go build && mv $(game)* ../bin
current: clean environment
cd src && go build && mv $(game)* ../$(desktopBin)
cross: clean
mkdir -p bin
cd src && GOOS=windows GOARCH=amd64 go build && mv $(game)* ../bin
web: environment
cd src && env GOOS=js GOARCH=wasm go build -o $(game).wasm . && mv $(game).wasm ../$(webBin)
cp $(wasmHtml) $(webBin)
cp $(wasmExec)/misc/wasm/wasm_exec.js $(webBin)
desktop: clean environment
cd src && GOOS=windows GOARCH=amd64 go build && mv $(game)* ../$(desktopBin)
cd src && GOOS=linux GOARCH=amd64 go build && mv $(game)* ../$(desktopBin)
cross: clean environment web desktop
environment:
mkdir -p $(desktopBin) $(webBin)
clean:
rm -rf bin

16
build/capyclick.html

@ -0,0 +1,16 @@
<!DOCTYPE html>
<script src="wasm_exec.js"></script>
<script>
if (!WebAssembly.instantiateStreaming) {
WebAssembly.instantiateStreaming = async (resp, importObject) => {
const source = await (await resp).arrayBuffer();
return await WebAssembly.instantiate(source, importObject);
};
}
const go = new Go();
WebAssembly.instantiateStreaming(fetch("capyclick.wasm"), go.importObject).then(result => {
go.run(result.instance);
});
</script>

10
src/main.go

@ -27,7 +27,7 @@ const Version string = "v0.1"
var (
silent *bool = flag.Bool("silent", false, "Set to true in order to discard all logging")
version *bool = flag.Bool("version", false, "Prints version information")
noFiles *bool = flag.Bool("no-files", false, "Run the game without outputting/reading configuration or save files")
saveFiles *bool = flag.Bool("saveFiles", false, "Run the game with configuration and save files")
)
const (
@ -316,7 +316,7 @@ func main() {
// Create a game instance
var game *Game = NewGame()
if !*noFiles {
if *saveFiles {
// Work out working directory
exeDir, err := os.Executable()
if err != nil {
@ -328,7 +328,7 @@ func main() {
game.WorkingDir = ""
}
if !*noFiles {
if *saveFiles {
// Open/Create configuration file
var config *conf.Configuration
config, err := conf.FromFile(filepath.Join(game.WorkingDir, ConfigurationFileName))
@ -361,7 +361,7 @@ func main() {
ebiten.SetWindowPosition(game.Config.LastWindowPosition[0], game.Config.LastWindowPosition[1])
ebiten.SetWindowTitle(fmt.Sprintf("Capyclick %s", Version))
if !*noFiles {
if *saveFiles {
// Open/Create save file
gameSave, err := save.FromFile(filepath.Join(game.WorkingDir, SaveFileName))
if err != nil {
@ -390,7 +390,9 @@ func main() {
err := ebiten.RunGame(game)
if err == ebiten.Termination || err == nil {
logger.Info("[Main] Shutting down!")
if *saveFiles {
game.SaveData()
}
os.Exit(0)
} else {
logger.Error("[Main] Fatal game error: %s", err)

Loading…
Cancel
Save