openquell/components/velocity.go

71 lines
1.2 KiB
Go
Raw Permalink Normal View History

2024-02-06 15:26:20 +01:00
package components
import (
. "openquell/config"
)
// movement in relation to position
type Velocity struct {
Data Position
Direction int
PointingAt int
Speed int
}
func (velocity *Velocity) Set(new *Velocity) {
velocity.Direction = new.Direction
velocity.Data.Set(&new.Data)
}
func (velocity *Velocity) ResetDirectionAndStop() {
velocity.Data.X = 0
velocity.Data.Y = 0
velocity.Direction = velocity.PointingAt
2024-02-06 15:26:20 +01:00
}
func (velocity *Velocity) Change(direction int) {
switch direction {
case East:
2024-02-11 14:24:30 +01:00
velocity.Data.X = 1
2024-02-06 15:26:20 +01:00
velocity.Data.Y = 0
case West:
2024-02-11 14:24:30 +01:00
velocity.Data.X = -1
2024-02-06 15:26:20 +01:00
velocity.Data.Y = 0
case South:
velocity.Data.X = 0
2024-02-11 14:24:30 +01:00
velocity.Data.Y = 1
2024-02-06 15:26:20 +01:00
case North:
velocity.Data.X = 0
2024-02-11 14:24:30 +01:00
velocity.Data.Y = -1
2024-02-06 15:26:20 +01:00
case Stop:
velocity.Data.X = 0
velocity.Data.Y = 0
}
velocity.Direction = direction
}
func (velocity *Velocity) Moving() bool {
return velocity.Data.X != 0 || velocity.Data.Y != 0
}
func (velocity *Velocity) InvertDirection() int {
switch velocity.Direction {
case East:
return West
case West:
return East
case South:
return North
case North:
return South
case All:
return All
case Stop:
return Stop
}
// should not happen
return All
}