1
1
mirror of https://github.com/OpenDiablo2/OpenDiablo2 synced 2025-02-18 14:36:44 -05:00
OpenDiablo2/d2core/d2systems/movement.go

72 lines
1.6 KiB
Go
Raw Normal View History

package d2systems
import (
"time"
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2util"
2020-10-12 17:35:11 -04:00
"github.com/gravestench/akara"
"github.com/OpenDiablo2/OpenDiablo2/d2core/d2components"
)
const (
logPrefixMovementSystem = "Movement System"
)
// static check that MovementSystem implements the System interface
2020-10-12 17:35:11 -04:00
var _ akara.System = &MovementSystem{}
// MovementSystem handles entity movement based on velocity and position components
type MovementSystem struct {
akara.BaseSubscriberSystem
*d2util.Logger
d2components.TransformFactory
d2components.VelocityFactory
movableEntities *akara.Subscription
}
// Init initializes the system with the given world
func (m *MovementSystem) Init(world *akara.World) {
m.World = world
m.Logger = d2util.NewLogger()
m.SetPrefix(logPrefixMovementSystem)
m.Info("initializing ...")
m.InjectComponent(&d2components.Transform{}, &m.Transform)
m.InjectComponent(&d2components.Velocity{}, &m.Velocity)
movable := m.NewComponentFilter().Require(
&d2components.Transform{},
&d2components.Velocity{},
).Build()
m.movableEntities = m.AddSubscription(movable)
}
// Update positions of all entities with their velocities
func (m *MovementSystem) Update() {
entities := m.movableEntities.GetEntities()
for entIdx := range entities {
m.move(entities[entIdx])
}
}
func (m *MovementSystem) move(id akara.EID) {
transform, found := m.GetTransform(id)
if !found {
return
}
velocity, found := m.GetVelocity(id)
if !found {
return
}
s := float64(m.World.TimeDelta) / float64(time.Second)
transform.Translation.Add(velocity.Clone().Scale(s))
}