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 game:=capyclick
bin:=bin desktopBin:=bin/desktop
webBin:=bin/web
wasmHtml:=build/capyclick.html
wasmExec:=$(shell go env GOROOT)
all: clean current: clean environment
mkdir -p bin cd src && go build && mv $(game)* ../$(desktopBin)
cd src && go build && mv $(game)* ../bin
cross: clean web: environment
mkdir -p bin cd src && env GOOS=js GOARCH=wasm go build -o $(game).wasm . && mv $(game).wasm ../$(webBin)
cd src && GOOS=windows GOARCH=amd64 go build && mv $(game)* ../bin 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: clean:
rm -rf bin 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 ( var (
silent *bool = flag.Bool("silent", false, "Set to true in order to discard all logging") silent *bool = flag.Bool("silent", false, "Set to true in order to discard all logging")
version *bool = flag.Bool("version", false, "Prints version information") 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 ( const (
@ -316,7 +316,7 @@ func main() {
// Create a game instance // Create a game instance
var game *Game = NewGame() var game *Game = NewGame()
if !*noFiles { if *saveFiles {
// Work out working directory // Work out working directory
exeDir, err := os.Executable() exeDir, err := os.Executable()
if err != nil { if err != nil {
@ -328,7 +328,7 @@ func main() {
game.WorkingDir = "" game.WorkingDir = ""
} }
if !*noFiles { if *saveFiles {
// Open/Create configuration file // Open/Create configuration file
var config *conf.Configuration var config *conf.Configuration
config, err := conf.FromFile(filepath.Join(game.WorkingDir, ConfigurationFileName)) 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.SetWindowPosition(game.Config.LastWindowPosition[0], game.Config.LastWindowPosition[1])
ebiten.SetWindowTitle(fmt.Sprintf("Capyclick %s", Version)) ebiten.SetWindowTitle(fmt.Sprintf("Capyclick %s", Version))
if !*noFiles { if *saveFiles {
// Open/Create save file // Open/Create save file
gameSave, err := save.FromFile(filepath.Join(game.WorkingDir, SaveFileName)) gameSave, err := save.FromFile(filepath.Join(game.WorkingDir, SaveFileName))
if err != nil { if err != nil {
@ -390,7 +390,9 @@ func main() {
err := ebiten.RunGame(game) err := ebiten.RunGame(game)
if err == ebiten.Termination || err == nil { if err == ebiten.Termination || err == nil {
logger.Info("[Main] Shutting down!") logger.Info("[Main] Shutting down!")
if *saveFiles {
game.SaveData() game.SaveData()
}
os.Exit(0) os.Exit(0)
} else { } else {
logger.Error("[Main] Fatal game error: %s", err) logger.Error("[Main] Fatal game error: %s", err)

Loading…
Cancel
Save