refactor: group launched/in_space state
This commit is contained in:
@@ -46,12 +46,10 @@ func (sgs ShipGroupState) String() string {
|
||||
}
|
||||
|
||||
type InSpace struct {
|
||||
Origin uint `json:"origin"`
|
||||
X Float `json:"x"`
|
||||
Y Float `json:"y"`
|
||||
// zero is for Launched status
|
||||
// TODO: calculate range dynamically -BUT- if affects ShipGroup.State()
|
||||
Range Float `json:"range"`
|
||||
// X, Y are nil for Launched state
|
||||
Origin uint `json:"origin"`
|
||||
X *Float `json:"x,omitempty"`
|
||||
Y *Float `json:"y,omitempty"`
|
||||
}
|
||||
|
||||
func (is InSpace) Equal(other InSpace) bool {
|
||||
@@ -59,7 +57,10 @@ func (is InSpace) Equal(other InSpace) bool {
|
||||
}
|
||||
|
||||
func (is InSpace) Launched() bool {
|
||||
return is.Range == 0
|
||||
if (is.X == nil && is.Y != nil) || (is.X != nil && is.Y == nil) {
|
||||
panic("group in space state invalid: one of coordinate is not set")
|
||||
}
|
||||
return is.X == nil && is.Y == is.X
|
||||
}
|
||||
|
||||
type InUpgrade struct {
|
||||
@@ -132,7 +133,7 @@ func (sg ShipGroup) State() ShipGroupState {
|
||||
case sg.StateInSpace == nil && sg.StateUpgrade == nil:
|
||||
return StateInOrbit
|
||||
case sg.StateInSpace != nil && sg.StateUpgrade == nil:
|
||||
if sg.StateInSpace.Range > 0 {
|
||||
if !sg.StateInSpace.Launched() {
|
||||
return StateInSpace
|
||||
}
|
||||
if sg.StateTransfer {
|
||||
|
||||
@@ -198,9 +198,11 @@ func TestShipGroupEqual(t *testing.T) {
|
||||
assert.False(t, left.Equal(right))
|
||||
|
||||
right = *left
|
||||
coord := game.Float(1)
|
||||
left.StateInSpace = &game.InSpace{
|
||||
Origin: 1,
|
||||
Range: 1,
|
||||
X: &coord,
|
||||
Y: &coord,
|
||||
}
|
||||
assert.False(t, left.Equal(right))
|
||||
|
||||
|
||||
Reference in New Issue
Block a user