83 lines
1.5 KiB
Go
83 lines
1.5 KiB
Go
//go:build example
|
|
// +build example
|
|
|
|
package system
|
|
|
|
import (
|
|
"code.rocketnine.space/tslocum/gohan"
|
|
"code.rocketnine.space/tslocum/gohan/examples/twinstick/component"
|
|
"github.com/hajimehoshi/ebiten/v2"
|
|
)
|
|
|
|
type MovementSystem struct {
|
|
ScreenW, ScreenH float64
|
|
Player gohan.Entity
|
|
}
|
|
|
|
func (_ *MovementSystem) Needs() []gohan.ComponentID {
|
|
return []gohan.ComponentID{
|
|
component.PositionComponentID,
|
|
component.VelocityComponentID,
|
|
}
|
|
}
|
|
|
|
func (s *MovementSystem) Uses() []gohan.ComponentID {
|
|
return nil
|
|
}
|
|
|
|
func (s *MovementSystem) Update(ctx *gohan.Context) error {
|
|
position := component.Position(ctx)
|
|
velocity := component.Velocity(ctx)
|
|
|
|
bullet := ctx.Entity != s.Player
|
|
|
|
// Check for collision.
|
|
if position.X+velocity.X < 16 {
|
|
if bullet {
|
|
ctx.RemoveEntity()
|
|
return nil
|
|
}
|
|
|
|
position.X = 16
|
|
velocity.X = 0
|
|
} else if position.X+velocity.X > s.ScreenW-16 {
|
|
if bullet {
|
|
ctx.RemoveEntity()
|
|
return nil
|
|
}
|
|
|
|
position.X = s.ScreenW - 16
|
|
velocity.X = 0
|
|
}
|
|
if position.Y+velocity.Y < 16 {
|
|
if bullet {
|
|
ctx.RemoveEntity()
|
|
return nil
|
|
}
|
|
|
|
position.Y = 16
|
|
velocity.Y = 0
|
|
} else if position.Y+velocity.Y > s.ScreenH-16 {
|
|
if bullet {
|
|
ctx.RemoveEntity()
|
|
return nil
|
|
}
|
|
|
|
position.Y = s.ScreenH - 16
|
|
velocity.Y = 0
|
|
}
|
|
|
|
position.X, position.Y = position.X+velocity.X, position.Y+velocity.Y
|
|
|
|
if !bullet {
|
|
velocity.X *= 0.95
|
|
velocity.Y *= 0.95
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func (_ *MovementSystem) Draw(_ *gohan.Context, _ *ebiten.Image) error {
|
|
return gohan.ErrSystemWithoutDraw
|
|
}
|