load dimension example

This commit is contained in:
Tnze
2022-05-27 00:59:18 +08:00
parent 474d6a229b
commit 691e507fcf
3 changed files with 18 additions and 3 deletions

View File

@ -4,6 +4,7 @@ import (
"context" "context"
_ "embed" _ "embed"
"flag" "flag"
"github.com/Tnze/go-mc/server/ecs"
"github.com/Tnze/go-mc/server/world" "github.com/Tnze/go-mc/server/world"
"image" "image"
_ "image/png" _ "image/png"
@ -57,7 +58,11 @@ func main() {
keepAlive, keepAlive,
commands, commands,
) )
world.NewDimensionManager(game) ecs.Register[world.Dimension, *ecs.HashMapStorage[world.Dimension]](game.World)
dimList := world.NewDimensionManager(game)
dimList.Add(game.CreateEntity(world.NewDimension(
"minecraft:overworld", *regionPath,
)), "minecraft:overworld")
player.SpawnSystem(game, "./save/testdata/playerdata") player.SpawnSystem(game, "./save/testdata/playerdata")
player.PosAndRotSystem(game) player.PosAndRotSystem(game)
go game.Run(context.Background()) go game.Run(context.Background())

View File

@ -7,7 +7,6 @@ import (
"github.com/Tnze/go-mc/server" "github.com/Tnze/go-mc/server"
"github.com/Tnze/go-mc/server/ecs" "github.com/Tnze/go-mc/server/ecs"
"github.com/Tnze/go-mc/server/world" "github.com/Tnze/go-mc/server/world"
"log"
) )
type PlayerProfile struct { type PlayerProfile struct {
@ -31,7 +30,6 @@ func (p playerSpawnSystem) Update(w *ecs.World) {
client.PutErr(fmt.Errorf("read player data fail: %w", err)) client.PutErr(fmt.Errorf("read player data fail: %w", err))
return return
} }
log.Println("load player info successes", profile)
dim, ok := dimensionRes.Find(profile.Dimension) dim, ok := dimensionRes.Find(profile.Dimension)
if !ok { if !ok {
panic("dimension " + profile.Dimension + " not found") panic("dimension " + profile.Dimension + " not found")

View File

@ -22,6 +22,13 @@ type Dimension struct {
Name string Name string
} }
func NewDimension(name, path string) Dimension {
return Dimension{
Name: name,
storage: storage{regionDir: path},
}
}
type DimensionList struct { type DimensionList struct {
Dims []ecs.Index Dims []ecs.Index
DimNames []string DimNames []string
@ -41,6 +48,11 @@ func (d *DimensionList) Find(dim string) (ecs.Index, bool) {
return 0, false return 0, false
} }
func (d *DimensionList) Add(id ecs.Index, name string) {
d.Dims = append(d.Dims, id)
d.DimNames = append(d.DimNames, name)
}
func NewDimensionManager(g *server.Game) *DimensionList { func NewDimensionManager(g *server.Game) *DimensionList {
return ecs.SetResource(g.World, DimensionList{ return ecs.SetResource(g.World, DimensionList{
Dims: nil, Dims: nil,