2021-11-19 04:13:28 +00:00
|
|
|
//go:build example
|
|
|
|
// +build example
|
|
|
|
|
|
|
|
package system
|
|
|
|
|
|
|
|
import (
|
|
|
|
"code.rocketnine.space/tslocum/gohan"
|
2021-11-22 02:48:54 +00:00
|
|
|
"code.rocketnine.space/tslocum/gohan/examples/twinstick/component"
|
2021-11-19 04:13:28 +00:00
|
|
|
"github.com/hajimehoshi/ebiten/v2"
|
|
|
|
)
|
|
|
|
|
|
|
|
type MovementSystem struct {
|
|
|
|
ScreenW, ScreenH float64
|
|
|
|
}
|
|
|
|
|
|
|
|
func (_ *MovementSystem) Matches(entity gohan.EntityID) bool {
|
|
|
|
position := entity.Component(component.PositionComponentID)
|
|
|
|
velocity := entity.Component(component.VelocityComponentID)
|
|
|
|
|
|
|
|
return position != nil && velocity != nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *MovementSystem) Update(entity gohan.EntityID) error {
|
|
|
|
position := entity.Component(component.PositionComponentID).(*component.PositionComponent)
|
|
|
|
velocity := entity.Component(component.VelocityComponentID).(*component.VelocityComponent)
|
|
|
|
|
|
|
|
bullet := entity.Component(component.BulletComponentID)
|
|
|
|
|
|
|
|
// Check for collision.
|
2021-11-22 02:48:54 +00:00
|
|
|
if position.X+velocity.X < 16 {
|
|
|
|
if bullet != nil {
|
|
|
|
gohan.RemoveEntity(entity)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
position.X = 16
|
|
|
|
velocity.X = 0
|
|
|
|
} else if position.X+velocity.X > s.ScreenW-16 {
|
|
|
|
if bullet != nil {
|
|
|
|
gohan.RemoveEntity(entity)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
position.X = s.ScreenW - 16
|
|
|
|
velocity.X = 0
|
|
|
|
}
|
|
|
|
if position.Y+velocity.Y < 16 {
|
|
|
|
if bullet != nil {
|
|
|
|
gohan.RemoveEntity(entity)
|
|
|
|
return nil
|
2021-11-19 04:13:28 +00:00
|
|
|
}
|
2021-11-22 02:48:54 +00:00
|
|
|
|
|
|
|
position.Y = 16
|
|
|
|
velocity.Y = 0
|
|
|
|
} else if position.Y+velocity.Y > s.ScreenH-16 {
|
|
|
|
if bullet != nil {
|
|
|
|
gohan.RemoveEntity(entity)
|
|
|
|
return nil
|
2021-11-19 04:13:28 +00:00
|
|
|
}
|
2021-11-22 02:48:54 +00:00
|
|
|
|
|
|
|
position.Y = s.ScreenH - 16
|
|
|
|
velocity.Y = 0
|
2021-11-19 04:13:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
position.X, position.Y = position.X+velocity.X, position.Y+velocity.Y
|
|
|
|
|
|
|
|
if bullet == nil {
|
|
|
|
velocity.X *= 0.95
|
|
|
|
velocity.Y *= 0.95
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-11-22 02:48:54 +00:00
|
|
|
func (_ *MovementSystem) Draw(_ gohan.EntityID, _ *ebiten.Image) error {
|
2021-11-19 04:13:28 +00:00
|
|
|
return gohan.ErrSystemWithoutDraw
|
|
|
|
}
|