From 7c6f8f8f45b283a497726de807c0407f2aa67b11 Mon Sep 17 00:00:00 2001 From: surdeus Date: Mon, 3 Jun 2024 01:53:43 +0500 Subject: [PATCH] feat: keep working on Tags. --- cmd/test/debug.go | 5 ++++ cmd/test/main.go | 6 ++++ cmd/test/trianlge.go | 11 ++++++++ engine.go | 15 ++++++---- object.go | 2 +- objects.go | 66 ++++++++++++++++++++++++++++++++++++++++++++ ox/object.go | 6 +++- tag.go | 11 ++++++++ 8 files changed, 114 insertions(+), 8 deletions(-) diff --git a/cmd/test/debug.go b/cmd/test/debug.go index 3b3c2f3..fbf9dc1 100644 --- a/cmd/test/debug.go +++ b/cmd/test/debug.go @@ -67,6 +67,11 @@ func (d *Debug) Draw(c Context) *gg.Drawing { "GetAbsWinSize(...) = ", c.Engine().GetAbsWinSize(), "\n", + + "ConnectedTriangleAmount = ", + len(c.Engine().Objects().FindByTags( + TagTriangle, TagConnectedToPlayer, + )), "\n", ) return nil diff --git a/cmd/test/main.go b/cmd/test/main.go index f0d83f2..4299d18 100644 --- a/cmd/test/main.go +++ b/cmd/test/main.go @@ -26,6 +26,12 @@ const ( LayerLowest ) +const ( + TagPlayer gg.Tag = gg.TagLast + 1 + iota + TagConnectedToPlayer + TagTriangle +) + const ( Stand ax.AnimationID = iota Walk diff --git a/cmd/test/trianlge.go b/cmd/test/trianlge.go index 8a6fcd0..b2788c4 100644 --- a/cmd/test/trianlge.go +++ b/cmd/test/trianlge.go @@ -10,6 +10,7 @@ var ( counter int ) + type Tri struct { ox.DrawablePolygon Spawned bool @@ -32,6 +33,16 @@ func NewTri(t *ox.Transform) *Tri { return ret } +func (t *Tri) GetTags(c Context) gg.TagMap { + tags := gg.TagMap{ + TagTriangle: struct{}{}, + } + if t.IsConnected() { + tags[TagConnectedToPlayer] = struct{}{} + } + return tags +} + func (t *Tri) OnStart(c Context) { log.Println("added a tri") } diff --git a/engine.go b/engine.go index 3962a70..89b5478 100644 --- a/engine.go +++ b/engine.go @@ -323,22 +323,25 @@ func (e *engine) Update() error { // but for it is simple enough. events := e.updateEvents() + c := Context{ engine: eng, events: events, } - for _, object := range e.objects.store { - if object == nil { - continue - } - object.OnUpdate(c) - } + + // Should think of the order? + e.objects.updateTags(c) + e.objects.updateObjects(c) e.lastTime = time.Now() e.frame++ return nil } +func (e *Engine) Objects() *Objects { + return e.objects +} + var ( fullPageIndexes = func() [MaxVertices]uint16 { diff --git a/object.go b/object.go index 9b2c160..51a6be5 100644 --- a/object.go +++ b/object.go @@ -8,7 +8,7 @@ type Object interface { OnStart(Context) OnUpdate(Context) OnDelete(Context) - GetTags() TagMap + GetTags(Context) TagMap } diff --git a/objects.go b/objects.go index a1d830f..24bdfba 100644 --- a/objects.go +++ b/objects.go @@ -1,5 +1,7 @@ package gg +type ObjectMap map[Object] struct{} + // The structure to store objects // with O(1) add, remove and search operations. type Objects struct { @@ -9,6 +11,8 @@ type Objects struct { // Find index by Object. searchMap map[Object] int + tagMap map[Tag] ObjectMap + // Available nil values in the store. freeIndexes []int } @@ -16,6 +20,7 @@ type Objects struct { func NewObjects() *Objects { ret := &Objects{} ret.searchMap = map[Object] int {} + ret.tagMap = map[Tag]ObjectMap{} return ret } @@ -56,3 +61,64 @@ func (objects *Objects) remove(object Object) bool { return true } +func (objects *Objects) FindByTags( + t Tag, rest ...Tag, +) []Object { + tagMap := objects.tagMap + objectMap := tagMap[t] + retMap := map[Object]struct{}{} + for object := range objectMap { + retMap[object] = struct{}{} + } + + for _, tag := range rest { + m, ok := tagMap[tag] + if !ok { + continue + } + for object := range m { + _, ok := m[object] + if !ok { + delete(retMap, object) + } + } + } + + ret := []Object{} + for object := range retMap { + ret = append(ret, object) + } + + return ret +} + +func (objects *Objects) updateObjects(c Context){ + for _, object := range objects.store { + if object == nil { + continue + } + object.OnUpdate(c) + } +} + +func (objects *Objects) updateTags(c Context) { + tagMap := objects.tagMap + updateMap := make(map[Tag]ObjectMap, len(tagMap)) + + for _, object := range objects.store { + tags := object.GetTags(c) + for tag := range tags { + tm, ok := updateMap[tag] + if ok { + tm[object] = struct{}{} + } else { + updateMap[tag] = ObjectMap{ + object: struct{}{}, + } + } + + } + } + objects.tagMap = updateMap +} + diff --git a/ox/object.go b/ox/object.go index eb2015f..ea341de 100644 --- a/ox/object.go +++ b/ox/object.go @@ -13,7 +13,11 @@ type BeherImpl struct{} func (o BeherImpl) OnStart(c gg.Context) {} func (o BeherImpl) OnUpdate(c gg.Context) {} func (o BeherImpl) OnDelete(c gg.Context) {} -func (o BeherImpl) GetTags() gg.TagMap {return nil} +func (o BeherImpl) GetTags( + c gg.Context, +) gg.TagMap { + return nil +} // The standard empty implementation // of the Object interface to embed diff --git a/tag.go b/tag.go index a49d737..53d191f 100644 --- a/tag.go +++ b/tag.go @@ -3,3 +3,14 @@ package gg type Tag int64 type TagMap map[Tag] struct{} +const ( + _ Tag = iota + TagUI + + // The constant is used + // to make after it custom tags + // so they do not cross with the builtins + // like "TagUI". + TagLast +) +