2019-01-23 15:22:47 +03:00
|
|
|
package mapobject
|
|
|
|
|
|
|
|
import (
|
|
|
|
"mapserver/app"
|
2019-01-28 15:31:48 +03:00
|
|
|
"mapserver/eventbus"
|
2019-01-23 18:06:56 +03:00
|
|
|
"mapserver/mapblockparser"
|
2019-01-28 16:19:11 +03:00
|
|
|
"mapserver/mapobjectdb"
|
2019-01-23 15:22:47 +03:00
|
|
|
)
|
|
|
|
|
2019-01-28 16:19:11 +03:00
|
|
|
type MapObjectListener interface {
|
2019-01-28 16:33:32 +03:00
|
|
|
onMapObject(x, y, z int, block *mapblockparser.MapBlock) *mapobjectdb.MapObject
|
2019-01-28 16:19:11 +03:00
|
|
|
}
|
|
|
|
|
2019-01-23 15:41:26 +03:00
|
|
|
type Listener struct {
|
2019-01-28 16:33:32 +03:00
|
|
|
ctx *app.App
|
2019-01-28 16:19:11 +03:00
|
|
|
objectlisteners map[string]MapObjectListener
|
|
|
|
}
|
|
|
|
|
2019-01-28 16:33:32 +03:00
|
|
|
func (this *Listener) AddMapObject(blockname string, ol MapObjectListener) {
|
2019-01-28 16:19:11 +03:00
|
|
|
this.objectlisteners[blockname] = ol
|
2019-01-23 15:41:26 +03:00
|
|
|
}
|
|
|
|
|
2019-01-28 15:31:48 +03:00
|
|
|
func (this *Listener) OnEvent(eventtype string, o interface{}) {
|
|
|
|
if eventtype != eventbus.MAPBLOCK_RENDERED {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
block := o.(*mapblockparser.MapBlock)
|
|
|
|
|
2019-01-24 10:26:28 +03:00
|
|
|
err := this.ctx.Objectdb.RemoveMapData(&block.Pos)
|
2019-01-23 18:06:56 +03:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2019-01-23 15:41:26 +03:00
|
|
|
|
2019-01-23 18:06:56 +03:00
|
|
|
for id, name := range block.BlockMapping {
|
2019-01-28 16:19:11 +03:00
|
|
|
for k, v := range this.objectlisteners {
|
|
|
|
if k == name {
|
|
|
|
//block matches
|
|
|
|
|
|
|
|
for x := 0; x < 16; x++ {
|
|
|
|
for y := 0; y < 16; y++ {
|
|
|
|
for z := 0; z < 16; z++ {
|
|
|
|
nodeid := block.GetNodeId(x, y, z)
|
|
|
|
if nodeid == id {
|
2019-01-28 16:33:32 +03:00
|
|
|
obj := v.onMapObject(x, y, z, block)
|
|
|
|
|
|
|
|
if obj != nil {
|
|
|
|
this.ctx.Objectdb.AddMapData(obj)
|
|
|
|
this.ctx.WebEventbus.Emit("mapobject", obj)
|
|
|
|
}
|
2019-01-28 16:19:11 +03:00
|
|
|
}
|
2019-01-28 16:33:32 +03:00
|
|
|
} //z
|
|
|
|
} //y
|
|
|
|
} //x
|
2019-01-28 16:19:11 +03:00
|
|
|
|
|
|
|
}
|
2019-01-28 16:33:32 +03:00
|
|
|
} //for k,v
|
|
|
|
} //for id, name
|
2019-01-23 15:41:26 +03:00
|
|
|
}
|
|
|
|
|
2019-01-23 15:22:47 +03:00
|
|
|
func Setup(ctx *app.App) {
|
2019-01-28 16:19:11 +03:00
|
|
|
l := Listener{
|
2019-01-28 16:33:32 +03:00
|
|
|
ctx: ctx,
|
2019-01-28 16:19:11 +03:00
|
|
|
objectlisteners: make(map[string]MapObjectListener),
|
|
|
|
}
|
|
|
|
|
|
|
|
l.AddMapObject("mapserver:poi", &PoiBlock{})
|
|
|
|
l.AddMapObject("mapserver:train", &TrainBlock{})
|
|
|
|
l.AddMapObject("travelnet:travelnet", &TravelnetBlock{})
|
|
|
|
l.AddMapObject("protector:protect", &ProtectorBlock{})
|
|
|
|
l.AddMapObject("protector:protect2", &ProtectorBlock{})
|
|
|
|
|
|
|
|
ctx.BlockAccessor.Eventbus.AddListener(&l)
|
2019-01-23 15:22:47 +03:00
|
|
|
}
|