feat: made spawning and deleting faster.

This commit is contained in:
Andrey Parhomenko 2024-06-02 23:29:11 +05:00
parent 8a57cfe9e6
commit 0abd485f55
4 changed files with 83 additions and 28 deletions

View file

@ -94,7 +94,7 @@ func (t *Tri) OnUpdate(c Context) {
tt := *t tt := *t
tt.Spawned = true tt.Spawned = true
tt.Disconnect() tt.Disconnect()
if e.Spawn(&tt) == nil { if e.Spawn(&tt) {
counter++ counter++
} }
} }

View file

@ -37,10 +37,6 @@ type WindowConfig struct {
VSync bool VSync bool
} }
type Objects struct {
store []Object
}
// The main structure that represents // The main structure that represents
// current state of [game] engine. // current state of [game] engine.
type Engine struct { type Engine struct {
@ -49,7 +45,7 @@ type Engine struct {
// The main holder for objects. // The main holder for objects.
// Uses the map structure to quickly // Uses the map structure to quickly
// delete and create new objects. // delete and create new objects.
Objects *Objects objects *Objects
// The main camera to display in window. // The main camera to display in window.
// If is set to nil then the engine will panic. // If is set to nil then the engine will panic.
@ -114,7 +110,7 @@ func NewEngine(
ret.wcfg = cfg ret.wcfg = cfg
ret.outerEvents = make(EventChan) ret.outerEvents = make(EventChan)
ret.handleEvents = make(EventChan) ret.handleEvents = make(EventChan)
ret.Objects = &Objects{} ret.objects = NewObjects()
ret.buttons = MouseButtonMap{} ret.buttons = MouseButtonMap{}
return ret return ret
} }
@ -133,30 +129,32 @@ func (e *Engine) EventInput() EventChan {
return e.outerEvents return e.outerEvents
} }
// Add new object to the Engine's view. func (e *Engine) Exist(object Object) bool {
func (e *Engine) Spawn(o Object) error { return e.objects.has(object)
/*if e.Objects.Has(b) { }
return ObjectExistErr
}*/
o.OnStart(Context{ // Add new objects to the Engine's view.
func (e *Engine) Spawn(object Object) bool {
ctx := Context{
engine: e, engine: e,
}) }
e.Objects.store = append(e.Objects.store, o) ok := e.objects.add(object)
object.OnStart(ctx)
return nil return ok
} }
// Delete object from Engine. // Delete object from Engine.
func (e *Engine) Delete(b Object) error { func (e *Engine) Delete(object Object) bool {
/*if !e.Objects.Has(b) {
return ObjectNotExistErr ctx := Context{
engine: e,
} }
ok := e.objects.remove(object)
object.OnDelete(ctx)
b.Delete(&Context{Engine: e}) return ok
e.Objects.Del(b)*/
return nil
} }
var ( var (
@ -329,7 +327,7 @@ func (e *engine) Update() error {
engine: eng, engine: eng,
events: events, events: events,
} }
for _, object := range e.Objects.store { for _, object := range e.objects.store {
object.OnUpdate(c) object.OnUpdate(c)
} }
@ -358,7 +356,7 @@ func (e *engine) Draw(img *ebiten.Image) {
e.drawdt = time.Since(e.drawLastTime) e.drawdt = time.Since(e.drawLastTime)
eng := (*Engine)(e) eng := (*Engine)(e)
m := map[Layer][]Drawer{} m := map[Layer][]Drawer{}
for _, object := range eng.Objects.store { for _, object := range eng.objects.store {
// Skipping the ones we do not need to draw. // Skipping the ones we do not need to draw.
if !object.IsVisible() { if !object.IsVisible() {
continue continue

View file

@ -5,8 +5,7 @@ import (
) )
var ( var (
ObjectExistErr = errors.New("the object already exists") ErrObjectExist = errors.New("the object already exists")
ObjectNotExistErr = errors.New("the object does not exist") ErrObjectNotExist = errors.New("the object does not exist")
ObjectNotImplementedErr = errors.New("none of object methods are implemented")
) )

58
objects.go Normal file
View file

@ -0,0 +1,58 @@
package gg
// The structure to store objects
// with O(1) add, remove and search operations.
type Objects struct {
// The place to store all the objects.
store []Object
// Find index by Object.
searchMap map[Object] int
// Available nil values in the store.
freeIndexes []int
}
func NewObjects() *Objects {
ret := &Objects{}
ret.searchMap = map[Object] int {}
return ret
}
func (objects *Objects) has(object Object) bool {
_, ok := objects.searchMap[object]
return ok
}
func (objects *Objects) add(object Object) bool {
if objects.has(object) {
return false
}
if len(objects.freeIndexes) > 0 {
freeIndex := objects.freeIndexes[0]
objects.freeIndexes = objects.freeIndexes[1:]
objects.store[freeIndex] = object
objects.searchMap[object] = freeIndex
} else {
objects.store = append(objects.store, object)
objects.searchMap[object] = len(objects.store)-1
}
return true
}
func (objects *Objects) remove(object Object) bool {
objectIndex, ok := objects.searchMap[object]
if !ok {
return false
}
objects.store[objectIndex] = nil
objects.freeIndexes = append(
objects.freeIndexes,
objectIndex,
)
return true
}