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.6 KiB

//go:build example
// +build example
package system
import (
"code.rocketnine.space/tslocum/gohan"
"code.rocketnine.space/tslocum/gohan/examples/twinstick/component"
"code.rocketnine.space/tslocum/gohan/examples/twinstick/world"
"github.com/hajimehoshi/ebiten/v2"
)
type MovementSystem struct {
Position *component.Position
Velocity *component.Velocity
}
func NewMovementSystem() *MovementSystem {
return &MovementSystem{}
}
func (s *MovementSystem) Update(entity gohan.Entity) error {
bullet := entity != world.Player
// Position the player at the center of the screen when the game starts.
if !bullet && s.Position.X == -1 && s.Position.Y == -1 {
s.Position.X, s.Position.Y = float64(world.ScreenW)/2-16, float64(world.ScreenH)/2-16
}
// Check for collision.
if s.Position.X+s.Velocity.X < 16 {
if bullet {
entity.Remove()
return nil
}
s.Position.X = 16
s.Velocity.X = 0
} else if s.Position.X+s.Velocity.X > world.ScreenW-16 {
if bullet {
entity.Remove()
return nil
}
s.Position.X = world.ScreenW - 16
s.Velocity.X = 0
}
if s.Position.Y+s.Velocity.Y < 16 {
if bullet {
entity.Remove()
return nil
}
s.Position.Y = 16
s.Velocity.Y = 0
} else if s.Position.Y+s.Velocity.Y > world.ScreenH-16 {
if bullet {
entity.Remove()
return nil
}
s.Position.Y = world.ScreenH - 16
s.Velocity.Y = 0
}
s.Position.X, s.Position.Y = s.Position.X+s.Velocity.X, s.Position.Y+s.Velocity.Y
if !bullet {
s.Velocity.X *= 0.95
s.Velocity.Y *= 0.95
}
return nil
}
func (_ *MovementSystem) Draw(_ gohan.Entity, _ *ebiten.Image) error {
return gohan.ErrUnregister
}