添加一些代码说明
添加一个简单的examples
This commit is contained in:
parent
3b10586590
commit
719def7aa6
13
README.md
Normal file
13
README.md
Normal file
@ -0,0 +1,13 @@
|
||||
# ECS(Entity Component System)框架
|
||||
|
||||
框架暂时基于[donburi](https://pkg.go.dev/github.com/yohamta/donburi)实现
|
||||
|
||||
# 核心组件概念
|
||||
|
||||
- 组件数据:保存状态的数据结构
|
||||
- 组件(Component):组件数据的包装,调用 ecs.NewComponentType 生成的组件对象
|
||||
- 实体(Entity):一个或多个组件构成一个实体,World.Create 或 World.CreateMany 创建
|
||||
- 入口(Entry):对实体进行操作的入口,获取或修改实体组件数据
|
||||
- 查询(Query): 用于组件查询,可以使用多种过滤器,同一个 Query 不能多线程使用
|
||||
- 过滤器(filter):组件查询使用
|
||||
- 系统(ISystem):对于功能逻辑的抽象,用于更新组件数据
|
45
examples/rtss/main.go
Normal file
45
examples/rtss/main.go
Normal file
@ -0,0 +1,45 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"joylink.club/ecs"
|
||||
system "joylink.club/ecs/examples/rtss/sys"
|
||||
)
|
||||
|
||||
func main() {
|
||||
start := time.Now()
|
||||
for i := 0; i < 10; i++ {
|
||||
|
||||
w := ecs.NewWorld(20)
|
||||
turnoutSys := system.NewTurnoutSys()
|
||||
w.AddSystem(turnoutSys)
|
||||
idcomp := ecs.IdComp
|
||||
entities := w.CreateMany(10, idcomp, system.TurnoutStateComp)
|
||||
for i, entity := range entities {
|
||||
entry := w.Entry(entity)
|
||||
idcomp.SetValue(entry, ecs.Id(fmt.Sprintf("%d", i)))
|
||||
var db bool
|
||||
var fb bool
|
||||
if i%2 == 0 {
|
||||
db = true
|
||||
} else {
|
||||
fb = true
|
||||
}
|
||||
system.TurnoutStateComp.SetValue(entry, system.TurnoutState{Db: db, Fb: fb})
|
||||
}
|
||||
// tet := ecs.NewEventType[TestEvent](w)
|
||||
// tet.Subscribe()
|
||||
w.StartUp()
|
||||
go func() {
|
||||
for i := 0; i < 10; i++ {
|
||||
system.DingCao(w, "3")
|
||||
time.Sleep(200 * time.Millisecond)
|
||||
}
|
||||
}()
|
||||
}
|
||||
dt := time.Duration(time.Now().Nanosecond() - start.Nanosecond())
|
||||
fmt.Println("执行耗时", dt)
|
||||
time.Sleep(5 * time.Second)
|
||||
}
|
53
examples/rtss/sys/trunout.sys.go
Normal file
53
examples/rtss/sys/trunout.sys.go
Normal file
@ -0,0 +1,53 @@
|
||||
package system
|
||||
|
||||
import (
|
||||
"log"
|
||||
|
||||
"github.com/yohamta/donburi/filter"
|
||||
"joylink.club/ecs"
|
||||
)
|
||||
|
||||
type TurnoutState struct {
|
||||
Db, Fb, Dc, Fc bool
|
||||
}
|
||||
|
||||
var TurnoutStateComp = ecs.NewComponentType[TurnoutState]()
|
||||
|
||||
type TurnoutSys struct {
|
||||
stateQuery *ecs.Query
|
||||
}
|
||||
|
||||
func NewTurnoutSys() *TurnoutSys {
|
||||
sys := &TurnoutSys{
|
||||
stateQuery: ecs.NewQuery(filter.Contains(TurnoutStateComp)),
|
||||
}
|
||||
return sys
|
||||
}
|
||||
|
||||
func (sys *TurnoutSys) Update(w ecs.World) {
|
||||
sys.stateQuery.Each(w, func(entry *ecs.Entry) {
|
||||
state := TurnoutStateComp.Get(entry)
|
||||
if state.Dc {
|
||||
if state.Db {
|
||||
state.Dc = false
|
||||
} else {
|
||||
state.Fb = false
|
||||
}
|
||||
}
|
||||
})
|
||||
// log.Println("道岔系统更新")
|
||||
}
|
||||
|
||||
// 道岔定操
|
||||
func DingCao(w ecs.World, id string) {
|
||||
query := ecs.NewQuery(filter.Contains(ecs.IdComp, TurnoutStateComp))
|
||||
query.Each(w, func(entry *ecs.Entry) {
|
||||
// log.Println("存在id组件")
|
||||
idcomp := ecs.IdComp.Get(entry)
|
||||
if *idcomp == ecs.Id(id) {
|
||||
ts := TurnoutStateComp.Get(entry)
|
||||
ts.Dc = true
|
||||
log.Println("id=", w.Id(), "的仿真", "道岔定操: id=", id)
|
||||
}
|
||||
})
|
||||
}
|
Loading…
Reference in New Issue
Block a user