Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 7d7f7c8

Browse files
committed
parse maps
1 parent b21af33 commit 7d7f7c8

File tree

3 files changed

+72
-19
lines changed

3 files changed

+72
-19
lines changed

‎main.go‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88

99
"github.com/blobs-io/blobsgame/database"
1010
"github.com/blobs-io/blobsgame/http/web"
11+
"github.com/blobs-io/blobsgame/models/gamemap"
1112
"github.com/blobs-io/blobsgame/models/room"
1213
"github.com/blobs-io/blobsgame/utils/config"
1314
)
@@ -28,6 +29,15 @@ func main() {
2829
panic(err)
2930
}
3031
err = config.ParseDatabaseConfig("configs/database.json")
32+
if err != nil {
33+
panic(err)
34+
}
35+
err = gamemap.LoadMaps()
36+
if err != nil {
37+
panic(err)
38+
} else if len(gamemap.GameMaps) < 1 {
39+
panic("no maps found")
40+
}
3141

3242
// Init database connection
3343
err = database.Init(config.DatabaseConfig)

‎models/gamemap/gamemap.go‎

Lines changed: 60 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,63 @@
11
package gamemap
22

3+
import (
4+
"encoding/json"
5+
"io/ioutil"
6+
"strings"
7+
)
8+
9+
var GameMaps = make(map[string]GameMap)
10+
11+
type GameMapSize struct {
12+
Width int `json:"width"`
13+
Height int `json:"height"`
14+
}
15+
16+
type GameMapObject struct {
17+
NoNomAreas []GameMapNoNomArea `json:"noNomAreas"`
18+
Walls []GameMapWall `json:"walls"`
19+
}
20+
21+
type GameMapNoNomArea struct {
22+
StartsAt int `json:"startsAt"`
23+
EndsAt int `json:"endsAt"`
24+
}
25+
26+
type GameMapWall struct {
27+
X int `json:"x"`
28+
Y int `json:"y"`
29+
Width int `json:"width"`
30+
Height int `json:"height"`
31+
Type uint8 `json:"type"`
32+
}
33+
334
type GameMap struct {
4-
Name string `json:"name"`
5-
MapSize struct {
6-
Width int `json:"width"`
7-
Height int `json:"height"`
8-
} `json:"mapSize"`
9-
Objects []struct {
10-
NoNomAreas []struct {
11-
StartsAt int `json:"startsAt"`
12-
EndsAt int `json:"endsAt"`
13-
} `json:"noNomAreas"`
14-
Walls []struct {
15-
X int `json:"x"`
16-
Y int `json:"y"`
17-
Width int `json:"width"`
18-
Height int `json:"height"`
19-
Type uint8 `json:"type"`
20-
} `json:"walls"`
21-
} `json:"objects"`
22-
}
35+
Name string `json:"name"`
36+
MapSize GameMapSize `json:"mapSize"`
37+
Objects GameMapObject `json:"objects"`
38+
}
39+
40+
func LoadMaps() error {
41+
files, err := ioutil.ReadDir("maps")
42+
if err != nil {
43+
return err
44+
}
45+
46+
for _, file := range files {
47+
if strings.HasSuffix(file.Name(), ".json") { // only allow maps in JSON format
48+
contents, err := ioutil.ReadFile("maps/" + file.Name())
49+
if err != nil {
50+
return err
51+
}
52+
53+
var gameMap GameMap
54+
err = json.Unmarshal(contents, &gameMap)
55+
if err != nil {
56+
return err
57+
}
58+
GameMaps[gameMap.Name] = gameMap
59+
}
60+
}
61+
62+
return nil
63+
}

‎models/room/room.go‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@ func New(mode uint8) *Room {
5757
r.ID = strings.ToLower(r.ModeToString()) + strconv.Itoa(len(Rooms))
5858
r.CreatedAt = time.Now().UnixNano() / int64(time.Millisecond)
5959

60+
// TODO: custom maps?
61+
r.Map = gamemap.GameMaps["default"]
6062
r.Players = make([]player.Player, 0)
6163
r.Items = make([]item.Item, 0)
6264

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /