feat: keep working on Tags.
This commit is contained in:
parent
c1a2d6b741
commit
7c6f8f8f45
8 changed files with 114 additions and 8 deletions
|
@ -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
|
||||
|
|
|
@ -26,6 +26,12 @@ const (
|
|||
LayerLowest
|
||||
)
|
||||
|
||||
const (
|
||||
TagPlayer gg.Tag = gg.TagLast + 1 + iota
|
||||
TagConnectedToPlayer
|
||||
TagTriangle
|
||||
)
|
||||
|
||||
const (
|
||||
Stand ax.AnimationID = iota
|
||||
Walk
|
||||
|
|
|
@ -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")
|
||||
}
|
||||
|
|
15
engine.go
15
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 {
|
||||
|
|
|
@ -8,7 +8,7 @@ type Object interface {
|
|||
OnStart(Context)
|
||||
OnUpdate(Context)
|
||||
OnDelete(Context)
|
||||
GetTags() TagMap
|
||||
GetTags(Context) TagMap
|
||||
}
|
||||
|
||||
|
||||
|
|
66
objects.go
66
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
|
||||
}
|
||||
|
||||
|
|
|
@ -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
|
||||
|
|
11
tag.go
11
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
|
||||
)
|
||||
|
||||
|
|
Loading…
Reference in a new issue