package ecs
import "unsafe"
type Unsafe struct {
world *World
cachedRelations []relationID
}
func (u Unsafe ) NewEntity (ids ...ID ) Entity {
entity , mask , _ := u .world .newEntity (ids , nil , 0 )
u .world .storage .observers .FireCreateEntityIfHas (entity , mask )
return entity
}
func (u Unsafe ) NewEntityRel (ids []ID , relations ...Relation ) Entity {
u .cachedRelations = relationSlice (relations ).ToRelationIDsForUnsafe (u .world , u .cachedRelations [:0 ])
entity , mask , _ := u .world .newEntity (ids , u .cachedRelations , 0 )
u .world .storage .observers .FireCreateEntityIfHas (entity , mask )
if len (relations ) > 0 {
u .world .storage .observers .FireCreateEntityRelIfHas (entity , mask )
}
return entity
}
func (u Unsafe ) Get (entity Entity , comp ID ) unsafe .Pointer {
return u .world .storage .get (entity , comp )
}
func (u Unsafe ) GetUnchecked (entity Entity , comp ID ) unsafe .Pointer {
return u .world .storage .getUnchecked (entity , comp )
}
func (u Unsafe ) Has (entity Entity , comp ID ) bool {
return u .world .storage .has (entity , comp )
}
func (u Unsafe ) HasUnchecked (entity Entity , comp ID ) bool {
return u .world .storage .hasUnchecked (entity , comp )
}
func (u Unsafe ) GetRelation (entity Entity , comp ID ) Entity {
return u .world .storage .getRelation (entity , comp )
}
func (u Unsafe ) GetRelationUnchecked (entity Entity , comp ID ) Entity {
return u .world .storage .getRelationUnchecked (entity , comp )
}
func (u Unsafe ) SetRelations (entity Entity , relations ...Relation ) {
u .cachedRelations = relationSlice (relations ).ToRelationIDsForUnsafe (u .world , u .cachedRelations [:0 ])
u .world .setRelations (entity , u .cachedRelations )
}
func (u Unsafe ) Add (entity Entity , comp ...ID ) {
if !u .world .Alive (entity ) {
panic ("can't add components to a dead entity" )
}
oldMask , newMask := u .world .add (entity , comp , nil )
u .world .storage .observers .FireAddIfHas (OnAddComponents , entity , oldMask , newMask )
}
func (u Unsafe ) AddRel (entity Entity , comps []ID , relations ...Relation ) {
if !u .world .Alive (entity ) {
panic ("can't add components to a dead entity" )
}
u .cachedRelations = relationSlice (relations ).ToRelationIDsForUnsafe (u .world , u .cachedRelations [:0 ])
oldMask , newMask := u .world .add (entity , comps , u .cachedRelations )
u .world .storage .observers .FireAddIfHas (OnAddComponents , entity , oldMask , newMask )
if len (relations ) > 0 {
u .world .storage .observers .FireAddIfHas (OnAddRelations , entity , oldMask , newMask )
}
}
func (u Unsafe ) Remove (entity Entity , comp ...ID ) {
if !u .world .Alive (entity ) {
panic ("can't remove components from a dead entity" )
}
u .world .remove (entity , comp )
}
func (u Unsafe ) Exchange (entity Entity , add []ID , remove []ID , relations ...Relation ) {
if !u .world .Alive (entity ) {
panic ("can't exchange components on a dead entity" )
}
u .cachedRelations = relationSlice (relations ).ToRelationIDsForUnsafe (u .world , u .cachedRelations [:0 ])
oldMask , newMask := u .world .exchange (entity , add , remove , u .cachedRelations )
if len (add ) > 0 {
u .world .storage .observers .FireAddIfHas (OnAddComponents , entity , oldMask , newMask )
if len (relations ) > 0 {
u .world .storage .observers .FireAddIfHas (OnAddRelations , entity , oldMask , newMask )
}
}
}
func (u Unsafe ) IDs (entity Entity ) IDs {
if !u .world .Alive (entity ) {
panic ("can't get component IDs of a dead entity" )
}
index := u .world .storage .entities [entity .id ]
return newIDs (u .world .storage .tables [index .table ].ids )
}
func (u Unsafe ) DumpEntities () EntityDump {
filter := NewFilter0 (u .world )
query := filter .Query ()
alive := make ([]uint32 , 0 , query .Count ())
for query .Next () {
alive = append (alive , uint32 (query .Entity ().id ))
}
data := EntityDump {
Entities : append ([]Entity {}, u .world .storage .entityPool .entities ...),
Alive : alive ,
Next : uint32 (u .world .storage .entityPool .next ),
Available : u .world .storage .entityPool .available ,
}
return data
}
func (u Unsafe ) LoadEntities (data *EntityDump ) {
u .world .checkLocked ()
if len (u .world .storage .entityPool .entities ) > 2 || u .world .storage .entityPool .available > 0 {
panic ("can set entity data only on a fresh or reset world" )
}
capacity := len (data .Entities )
entities := make ([]Entity , capacity )
copy (entities , data .Entities )
if capacity > 0 {
u .world .storage .entityPool = entityPool {
entities : entities ,
next : entityID (data .Next ),
available : data .Available ,
reserved : entityID (reservedEntities ),
}
u .world .storage .entityPool .pointer = unsafe .Pointer (&u .world .storage .entityPool .entities [0 ])
}
u .world .storage .entities = make ([]entityIndex , capacity )
u .world .storage .isTarget = make ([]bool , capacity )
table := &u .world .storage .tables [0 ]
table .Extend (uint32 (len (data .Alive )))
for _ , idx := range data .Alive {
entity := u .world .storage .entityPool .entities [idx ]
tableIdx := table .Add (entity )
u .world .storage .entities [entity .id ] = entityIndex {table : table .id , row : tableIdx }
}
}
The pages are generated with Golds v0.8.7 . (GOOS=linux GOARCH=amd64)
Golds is a Go 101 project developed by Tapir Liu .
PR and bug reports are welcome and can be submitted to the issue list .
Please follow @zigo_101 (reachable from the left QR code) to get the latest news of Golds .