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