Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
aerth committed Jul 9, 2022
1 parent 8203296 commit f2178fe
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 27 deletions.
27 changes: 26 additions & 1 deletion cmd/mapgen/main.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,35 @@
package main

import (
"encoding/json"
"io/ioutil"
"log"
"os"

"github.com/aerth/rpc/librpg/common"
"github.com/aerth/rpc/librpg/maps"
)

func SaveMap(olist []common.Object) {
os.MkdirAll("maps", 0755)
b, err := json.Marshal(olist)
if err != nil {
log.Println(err)
return
}
f, err := ioutil.TempFile("maps", "map")
if err != nil {
log.Println(err)
return
}
_, err = f.Write(b)
if err != nil {
log.Println(err)
return
}
log.Println("saved file:", f.Name())
}
func main() {
olist := maps.GenerateMap("")
maps.SaveMap(olist)
SaveMap(olist)
}
22 changes: 0 additions & 22 deletions librpg/maps/map.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ package maps

import (
"crypto/md5"
"encoding/json"
"io/ioutil"
"log"
"math"
"math/big"
Expand Down Expand Up @@ -115,26 +113,6 @@ func GenerateMap(seed string) []common.Object {
return olist
}

func SaveMap(olist []common.Object) {

b, err := json.Marshal(olist)
if err != nil {
log.Println(err)
return
}
f, err := ioutil.TempFile("maps", "map")
if err != nil {
log.Println(err)
return
}
_, err = f.Write(b)
if err != nil {
log.Println(err)
return
}
log.Println("saved file:", f.Name())
}

func randfloat() float64 {
step := 32.00
f := float64(rand.Intn(int(BOUNDS)))
Expand Down
3 changes: 3 additions & 0 deletions librpg/newworld.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,16 @@ func NewGame(win *pixelgl.Window, difficulty int, leveltest string, worldseed st
os.Exit(111)
}

// LoadMapFile from disk
func (w *World) LoadMapFile(path string) error {
b, err := ioutil.ReadFile(path)
if err != nil {
return err
}
return w.loadmap(b)
}

// LoadMap from embedded assets
func (w *World) LoadMap(path string) error {
b, err := assets.Asset(path)
if err != nil {
Expand Down
8 changes: 4 additions & 4 deletions librpg/world.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func NewWorld(name string, difficulty int, seed string) *World {
seed = fmt.Sprintf("%d", rand.Int63())
}
if name == "" {
w.Name = fmt.Sprintf("world seed %d", seed)
w.Name = fmt.Sprintf("world seed %q", seed)
}
w.Color = RandomColor()
w.Sheets = make(map[EntityType]pixel.Picture)
Expand All @@ -76,9 +76,9 @@ func NewWorld(name string, difficulty int, seed string) *World {
if name != "" {
log.Println("Loading map", name)
if e := w.LoadMap("maps/" + name + ".map"); e != nil {
log.Println(e)
if e = w.LoadMapFile(name); e != nil {
log.Println(e)
// log.Println(e)
if e2 := w.LoadMapFile(name); e2 != nil {
log.Println("fatal:", e2)
return nil
}
}
Expand Down

0 comments on commit f2178fe

Please sign in to comment.