feat: keep working on Tags.

This commit is contained in:
Andrey Parhomenko 2024-06-03 01:53:43 +05:00
parent c1a2d6b741
commit 7c6f8f8f45
8 changed files with 114 additions and 8 deletions

View file

@ -67,6 +67,11 @@ func (d *Debug) Draw(c Context) *gg.Drawing {
"GetAbsWinSize(...) = ", "GetAbsWinSize(...) = ",
c.Engine().GetAbsWinSize(), "\n", c.Engine().GetAbsWinSize(), "\n",
"ConnectedTriangleAmount = ",
len(c.Engine().Objects().FindByTags(
TagTriangle, TagConnectedToPlayer,
)), "\n",
) )
return nil return nil

View file

@ -26,6 +26,12 @@ const (
LayerLowest LayerLowest
) )
const (
TagPlayer gg.Tag = gg.TagLast + 1 + iota
TagConnectedToPlayer
TagTriangle
)
const ( const (
Stand ax.AnimationID = iota Stand ax.AnimationID = iota
Walk Walk

View file

@ -10,6 +10,7 @@ var (
counter int counter int
) )
type Tri struct { type Tri struct {
ox.DrawablePolygon ox.DrawablePolygon
Spawned bool Spawned bool
@ -32,6 +33,16 @@ func NewTri(t *ox.Transform) *Tri {
return ret 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) { func (t *Tri) OnStart(c Context) {
log.Println("added a tri") log.Println("added a tri")
} }

View file

@ -323,22 +323,25 @@ func (e *engine) Update() error {
// but for it is simple enough. // but for it is simple enough.
events := e.updateEvents() events := e.updateEvents()
c := Context{ c := Context{
engine: eng, engine: eng,
events: events, events: events,
} }
for _, object := range e.objects.store {
if object == nil { // Should think of the order?
continue e.objects.updateTags(c)
} e.objects.updateObjects(c)
object.OnUpdate(c)
}
e.lastTime = time.Now() e.lastTime = time.Now()
e.frame++ e.frame++
return nil return nil
} }
func (e *Engine) Objects() *Objects {
return e.objects
}
var ( var (
fullPageIndexes = func() [MaxVertices]uint16 { fullPageIndexes = func() [MaxVertices]uint16 {

View file

@ -8,7 +8,7 @@ type Object interface {
OnStart(Context) OnStart(Context)
OnUpdate(Context) OnUpdate(Context)
OnDelete(Context) OnDelete(Context)
GetTags() TagMap GetTags(Context) TagMap
} }

View file

@ -1,5 +1,7 @@
package gg package gg
type ObjectMap map[Object] struct{}
// The structure to store objects // The structure to store objects
// with O(1) add, remove and search operations. // with O(1) add, remove and search operations.
type Objects struct { type Objects struct {
@ -9,6 +11,8 @@ type Objects struct {
// Find index by Object. // Find index by Object.
searchMap map[Object] int searchMap map[Object] int
tagMap map[Tag] ObjectMap
// Available nil values in the store. // Available nil values in the store.
freeIndexes []int freeIndexes []int
} }
@ -16,6 +20,7 @@ type Objects struct {
func NewObjects() *Objects { func NewObjects() *Objects {
ret := &Objects{} ret := &Objects{}
ret.searchMap = map[Object] int {} ret.searchMap = map[Object] int {}
ret.tagMap = map[Tag]ObjectMap{}
return ret return ret
} }
@ -56,3 +61,64 @@ func (objects *Objects) remove(object Object) bool {
return true 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
}

View file

@ -13,7 +13,11 @@ type BeherImpl struct{}
func (o BeherImpl) OnStart(c gg.Context) {} func (o BeherImpl) OnStart(c gg.Context) {}
func (o BeherImpl) OnUpdate(c gg.Context) {} func (o BeherImpl) OnUpdate(c gg.Context) {}
func (o BeherImpl) OnDelete(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 // The standard empty implementation
// of the Object interface to embed // of the Object interface to embed

11
tag.go
View file

@ -3,3 +3,14 @@ package gg
type Tag int64 type Tag int64
type TagMap map[Tag] struct{} 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
)