Skip to content

Commit d7a6939

Browse files
committed
code org
1 parent 3f817d4 commit d7a6939

File tree

3 files changed

+60
-74
lines changed

3 files changed

+60
-74
lines changed

entity.go

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,28 +5,13 @@ import (
55
"encoding/binary"
66
"encoding/json"
77
"fmt"
8-
"sync"
98

109
"github.com/BurntSushi/toml"
1110
"github.com/google/uuid"
1211
)
1312

1413
type Entity uint64
1514

16-
var encoderMutex sync.Mutex
17-
var encoderWorld *World
18-
19-
var decoderMutex sync.Mutex
20-
var decoderWorld *World
21-
22-
func setEncoderWorld(w *World) {
23-
encoderWorld = w
24-
}
25-
26-
func setDecoderWorld(w *World) {
27-
decoderWorld = w
28-
}
29-
3015
func (e Entity) MarshalBinary() ([]byte, error) {
3116
id := encoderWorld.EntityUUID(e)
3217
if id.Version() < 1 {
@@ -113,8 +98,8 @@ func (e Entities) MarshalBinary() ([]byte, error) {
11398
}
11499

115100
func (e *Entities) UnmarshalBinary(data []byte) error {
116-
//TODO: this
117-
return nil
101+
//TODO: implement (e *Entities) UnmarshalBinary(data []byte) error
102+
return fmt.Errorf("not implemented")
118103
}
119104

120105
func (e Entities) MarshalText() (text []byte, err error) {

serialization.go

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package ecs
2+
3+
import (
4+
"sync"
5+
6+
"github.com/BurntSushi/toml"
7+
"github.com/google/uuid"
8+
)
9+
10+
type SerializedWorld struct {
11+
Entities []SerializedEntity `toml:"entities"`
12+
ComponentIndex ComponentIndex `toml:"component_index"`
13+
Enabled bool `toml:"enabled"`
14+
}
15+
16+
type DeserializedWorld struct {
17+
Entities []DeserializedEntity `toml:"entities"`
18+
ComponentIndex ComponentIndex `toml:"component_index"`
19+
Enabled bool `toml:"enabled"`
20+
}
21+
22+
type DeserializedEntity struct {
23+
UUID uuid.UUID `toml:"uuid"`
24+
Components []DeserializedComponentData `toml:"components"`
25+
}
26+
27+
type SerializedEntity struct {
28+
UUID uuid.UUID `toml:"uuid"`
29+
Components []interface{} `toml:"components"`
30+
}
31+
32+
type DeserializedComponentData struct {
33+
CI int `toml:"ci"` // component index
34+
Data toml.Primitive `toml:"data"`
35+
}
36+
37+
type SerializedComponentData struct {
38+
CI int `toml:"ci"` // component index
39+
Data interface{} `toml:"data"`
40+
}
41+
42+
type Encoder interface {
43+
Encode(interface{}) error
44+
}
45+
46+
var encoderMutex sync.Mutex
47+
var encoderWorld *World
48+
49+
var decoderMutex sync.Mutex
50+
var decoderWorld *World
51+
52+
func setEncoderWorld(w *World) {
53+
encoderWorld = w
54+
}
55+
56+
func setDecoderWorld(w *World) {
57+
decoderWorld = w
58+
}

world.go

Lines changed: 0 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"fmt"
55
"io"
66
"sort"
7-
"sync"
87

98
"github.com/BurntSushi/toml"
109
"github.com/google/uuid"
@@ -20,7 +19,6 @@ var (
2019

2120
type World struct {
2221
lastEntity Entity
23-
entityMutex sync.Mutex
2422
entities []Entity
2523
entityIDs map[Entity]uuid.UUID // this is used when serializing/deserializing data
2624
entityUUIDs map[uuid.UUID]Entity
@@ -96,28 +94,11 @@ func (w *World) EntityByUUID(id uuid.UUID) (Entity, bool) {
9694
}
9795

9896
func (w *World) NewEntity() Entity {
99-
w.entityMutex.Lock()
100-
defer w.entityMutex.Unlock()
10197
w.lastEntity++
10298
w.entities = append(w.entities, w.lastEntity)
10399
return w.lastEntity
104100
}
105101

106-
func (w *World) NewEntities(count int) []Entity {
107-
if count <= 0 {
108-
return nil
109-
}
110-
w.entityMutex.Lock()
111-
defer w.entityMutex.Unlock()
112-
entts := make([]Entity, count)
113-
for i := 0; i < count; i++ {
114-
entts[i] = w.lastEntity + Entity(i+1)
115-
}
116-
w.lastEntity += Entity(count)
117-
w.entities = append(w.entities, entts...)
118-
return entts
119-
}
120-
121102
var DefaultWorld = NewWorld()
122103

123104
func (w *World) addSystem(sys ISystem) int {
@@ -174,45 +155,11 @@ func (w *World) SetEnabled(v bool) {
174155

175156
// AllEntities returns all entities in the world
176157
func (w *World) AllEntities() []Entity {
177-
w.entityMutex.Lock()
178-
defer w.entityMutex.Unlock()
179158
ecopy := make([]Entity, len(w.entities))
180159
copy(ecopy, w.entities)
181160
return ecopy
182161
}
183162

184-
type SerializedWorld struct {
185-
Entities []SerializedEntity `toml:"entities"`
186-
ComponentIndex ComponentIndex `toml:"component_index"`
187-
Enabled bool `toml:"enabled"`
188-
}
189-
190-
type DeserializedWorld struct {
191-
Entities []DeserializedEntity `toml:"entities"`
192-
ComponentIndex ComponentIndex `toml:"component_index"`
193-
Enabled bool `toml:"enabled"`
194-
}
195-
196-
type DeserializedEntity struct {
197-
UUID uuid.UUID `toml:"uuid"`
198-
Components []DeserializedComponentData `toml:"components"`
199-
}
200-
201-
type SerializedEntity struct {
202-
UUID uuid.UUID `toml:"uuid"`
203-
Components []interface{} `toml:"components"`
204-
}
205-
206-
type DeserializedComponentData struct {
207-
CI int `toml:"ci"` // component index
208-
Data toml.Primitive `toml:"data"`
209-
}
210-
211-
type SerializedComponentData struct {
212-
CI int `toml:"ci"` // component index
213-
Data interface{} `toml:"data"`
214-
}
215-
216163
// MarshalTo marshals the world data to a writer
217164
func (w *World) MarshalTo(dw io.Writer) error {
218165
return w.serializeData(toml.NewEncoder(dw))
@@ -332,7 +279,3 @@ func (w *World) deserializeData(md toml.MetaData, dw *DeserializedWorld) error {
332279
w.isloading = false
333280
return nil
334281
}
335-
336-
type Encoder interface {
337-
Encode(interface{}) error
338-
}

0 commit comments

Comments
 (0)