Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merge hierarchy package into transform package #156

Merged
merged 3 commits into from
Nov 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading