Skip to content

Commit

Permalink
Merge hierarchy package into transform package (#156)
Browse files Browse the repository at this point in the history
  • Loading branch information
yohamta authored Nov 12, 2024
1 parent ad1b847 commit 199b75a
Show file tree
Hide file tree
Showing 7 changed files with 236 additions and 239 deletions.
38 changes: 0 additions & 38 deletions features/hierarchy/children.go

This file was deleted.

158 changes: 0 additions & 158 deletions features/hierarchy/parent.go

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package hierarchy
package transform

import (
"github.com/yohamta/donburi"
Expand All @@ -12,26 +12,26 @@ type hierarchySystem struct {

// HierarchySystem is a system that removes children of invalid parents.
var HierarchySystem = &hierarchySystem{
query: donburi.NewQuery(filter.Contains(parentComponent)),
query: donburi.NewQuery(filter.Contains(hierarchyParentComponent)),
}

// RemoveChildren removes children of invalid parents.
// RemoveHierarchyChildren removes children of invalid parents.
// This function is useful when you want to remove children of invalid parents.
// You don't need to use this system function when you use
// hierarchy.RemoveRecursive() or hierarchy.RemoveChildrenRecursive() instead.
func (hs *hierarchySystem) RemoveChildren(ecs *ecs.ECS) {
func (hs *hierarchySystem) RemoveHierarchyChildren(ecs *ecs.ECS) {
hs.query.Each(ecs.World, func(entry *donburi.Entry) {
if !entry.Valid() {
return
}
if pd, ok := getParentData(entry); ok {
if pd, ok := getHierarchyParentData(entry); ok {
if pd.Parent.Valid() {
return
}
c, ok := GetChildren(entry)
c, ok := getHierarchyChildren(entry)
if ok {
for _, e := range c {
RemoveRecursive(e)
removeHierarchyRecursive(e)
}
}
entry.Remove()
Expand Down
32 changes: 32 additions & 0 deletions features/transform/hierarchy_children.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package transform

import (
"github.com/yohamta/donburi"
)

type hierarchyChildrenData struct {
Children []*donburi.Entry
}

var hierarchyChildrenComponent = donburi.NewComponentType[hierarchyChildrenData]()

// getHierarchyChildren returns children of the entry.
func getHierarchyChildren(entry *donburi.Entry) ([]*donburi.Entry, bool) {
if cd, ok := getHierarchyChildrenData(entry); ok {
return cd.Children, true
}
return nil, false
}

// hasHierarchyChildren returns true if the entry has children.
func hasHierarchyChildren(entry *donburi.Entry) bool {
return entry.HasComponent(hierarchyChildrenComponent)
}

func getHierarchyChildrenData(entry *donburi.Entry) (*hierarchyChildrenData, bool) {
if hasHierarchyChildren(entry) {
c := donburi.Get[hierarchyChildrenData](entry, hierarchyChildrenComponent)
return c, true
}
return nil, false
}
Loading

0 comments on commit 199b75a

Please sign in to comment.