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
}

func (velocity *Velocity) Change(direction int) {
	switch direction {
	case East:
		velocity.Data.X = 1
		velocity.Data.Y = 0
	case West:
		velocity.Data.X = -1
		velocity.Data.Y = 0
	case South:
		velocity.Data.X = 0
		velocity.Data.Y = 1
	case North:
		velocity.Data.X = 0
		velocity.Data.Y = -1
	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
}
