Skip to content

Commit

Permalink
Disconnecting connection to lighthouses when lighthouse ownership change
Browse files Browse the repository at this point in the history
  • Loading branch information
McLargo committed Nov 14, 2024
1 parent 89873db commit 8480062
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 4 deletions.
18 changes: 14 additions & 4 deletions internal/engine/game/actions.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,14 @@ func (e *Game) moveToPosition(p *player.Player, action *player.Action) error {
p.Position = action.Destination

if e.gameMap.IsLighthouse(action.Destination) {
l, err := e.gameMap.GetLightHouse(action.Destination)
if err != nil {
return err
// add keys only if user doesn't have any key
if len(p.LighthouseKeys) == 0 {
l, err := e.gameMap.GetLightHouse(action.Destination)
if err != nil {
return err
}
p.AddLighthouseKey(*l)
}
p.AddLighthouseKey(*l)
}

return nil
Expand All @@ -60,6 +63,7 @@ func (e *Game) attackPosition(p *player.Player, action *player.Action) error {

for _, l := range e.gameMap.GetLightHouses() {
if l.Position.Equal(geom.XY, action.Destination) {
change_owner := false
if l.Owner == p.ID {
l.Energy += action.Energy
}
Expand All @@ -69,15 +73,21 @@ func (e *Game) attackPosition(p *player.Player, action *player.Action) error {
if lighthouseEnergy == 0 {
l.Energy = 0
l.Owner = -1
change_owner = true
}

if lighthouseEnergy < 0 {
l.Energy = int(math.Abs(float64(lighthouseEnergy)))
l.Owner = p.ID
change_owner = true
}

p.Energy -= action.Energy
fmt.Printf("Player %d attacked lighthouse %d. Current energy and owner are: %d, %d \n", p.ID, l.ID, l.Energy, l.Owner)

if change_owner {
l.Disconnect()
}
break
}
}
Expand Down
16 changes: 16 additions & 0 deletions internal/engine/lighthouse/lighthouse.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,22 @@ func (l *Lighthouse) Connect(lighthouse *Lighthouse) error {
return nil
}

func (l *Lighthouse) Disconnect() error {
for _, conn := range l.Connections {
for i, c := range conn.Connections {
if c.Position.Equal(geom.XY, l.Position) {
conn.Connections = append(conn.Connections[:i], conn.Connections[i+1:]...)
break
}
}
}

l.Connections = make([]*Lighthouse, 0)

return nil

}

func (l *Lighthouse) GenerateConnectionsId() {
l.ConnectionsId = make([]int, 0)
for _, c := range l.Connections {
Expand Down

0 comments on commit 8480062

Please sign in to comment.