Entity Component System framework for Ebitengine
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

78 lines
1.5 KiB

9 months ago
//go:build example
// +build example
package system
import (
"code.rocketnine.space/tslocum/gohan"
"code.rocketnine.space/tslocum/gohan/examples/twinstick/component"
9 months ago
"github.com/hajimehoshi/ebiten/v2"
)
type MovementSystem struct {
ScreenW, ScreenH float64
}
func (_ *MovementSystem) Matches(entity gohan.Entity) bool {
position := component.Position(entity)
velocity := component.Velocity(entity)
9 months ago
return position != nil && velocity != nil
}
func (s *MovementSystem) Update(entity gohan.Entity) error {
position := component.Position(entity)
velocity := component.Velocity(entity)
9 months ago
bullet := entity.Component(component.BulletComponentID)
// Check for collision.
if position.X+velocity.X < 16 {
if bullet != nil {
entity.Remove()
return nil
}
position.X = 16
velocity.X = 0
} else if position.X+velocity.X > s.ScreenW-16 {
if bullet != nil {
entity.Remove()
return nil
}
position.X = s.ScreenW - 16
velocity.X = 0
}
if position.Y+velocity.Y < 16 {
if bullet != nil {
entity.Remove()
return nil
9 months ago
}
position.Y = 16
velocity.Y = 0
} else if position.Y+velocity.Y > s.ScreenH-16 {
if bullet != nil {
entity.Remove()
return nil
9 months ago
}
position.Y = s.ScreenH - 16
velocity.Y = 0
9 months ago
}
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
}
func (_ *MovementSystem) Draw(_ gohan.Entity, _ *ebiten.Image) error {
9 months ago
return gohan.ErrSystemWithoutDraw
}