Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Delete building query #11

Merged
merged 1 commit into from
Jul 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions cardinal/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ func MustInitWorld(w *cardinal.World) {
cardinal.RegisterMessage[msg.CreatePlayerMsg, msg.CreatePlayerResult](w, "create-player"),
cardinal.RegisterMessage[msg.AttackPlayerMsg, msg.AttackPlayerMsgReply](w, "attack-player"),
cardinal.RegisterMessage[msg.CreateBuildingMsg, msg.CreateBuildingResult](w, "create-building"),
cardinal.RegisterMessage[msg.DeleteBuildingMsg, msg.DeleteBuildingResult](w, "delete-building"),
)

// Register queries
Expand Down Expand Up @@ -80,6 +81,7 @@ func MustInitWorld(w *cardinal.World) {
system.FarmingSystem,
system.PlayerSpawnerSystem,
system.CreateBuildingSystem,
system.DeleteBuildingSystem,
))
}

Expand Down
9 changes: 9 additions & 0 deletions cardinal/msg/delete_building.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package msg

type DeleteBuildingMsg struct {
TileIndex int `json:"tileID"`
}

type DeleteBuildingResult struct {
Success bool `json:"success"`
}
68 changes: 68 additions & 0 deletions cardinal/system/delete_building.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package system

import (
"fmt"

"pkg.world.dev/world-engine/cardinal"
"pkg.world.dev/world-engine/cardinal/search/filter"

comp "oceanus-shard/component"
"oceanus-shard/msg"
)

// DeleteBuildingSystem removes buildings.
func DeleteBuildingSystem(world cardinal.WorldContext) error {
return cardinal.EachMessage[msg.DeleteBuildingMsg, msg.DeleteBuildingResult](
world,
func(request cardinal.TxData[msg.DeleteBuildingMsg]) (msg.DeleteBuildingResult, error) {
mapEntityID, playerMap, _ := QueryComponent[comp.TileMap](
world,
request.Tx.PersonaTag,
filter.Component[comp.Player](),
filter.Component[comp.TileMap](),
)

buildingsEntityIDs, buildingComponents, _ := QueryAllComponents[comp.Building](
world,
request.Tx.PersonaTag,
filter.Component[comp.Player](),
filter.Component[comp.Building](),
)

if playerMap == nil {
return msg.DeleteBuildingResult{Success: false},
fmt.Errorf("failed to delete building, this player did not have tilemap")
}

tileMapPlayer, _ := cardinal.GetComponent[comp.Player](world, mapEntityID)
if tileMapPlayer.Nickname != request.Tx.PersonaTag {
return msg.DeleteBuildingResult{Success: false}, fmt.Errorf("can't delete another player building")
}

tiles := *playerMap.Tiles

if request.Msg.TileIndex < 0 || request.Msg.TileIndex >= len(tiles) {
return msg.DeleteBuildingResult{Success: false}, fmt.Errorf("index of tiles out of range")
}

tile := &(*playerMap.Tiles)[request.Msg.TileIndex]
if tile.Building == nil {
return msg.DeleteBuildingResult{Success: false},
fmt.Errorf("failed to delete building, this tile didn't have buildings")
}
for i, building := range buildingComponents {
if building.Type == tile.Building.Type {
if err := cardinal.Remove(world, buildingsEntityIDs[i]); err != nil {
return msg.DeleteBuildingResult{Success: false}, fmt.Errorf("failed to delete building entity: %w", err)
}
}
}
tile.Building = nil

if err := cardinal.SetComponent(world, mapEntityID, playerMap); err != nil {
return msg.DeleteBuildingResult{Success: false}, fmt.Errorf("failed to delete building from tile: %w", err)
}

return msg.DeleteBuildingResult{Success: true}, nil
})
}
12 changes: 6 additions & 6 deletions cardinal/system/query_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@ func QueryAllComponents[T types.Component](
world cardinal.WorldContext,
targetNickname string,
components ...filter.ComponentWrapper,
) (types.EntityID, []*T, error) {
var entityID types.EntityID
) ([]types.EntityID, []*T, error) {
var entityIDs []types.EntityID
var targetComponents []*T
var err error

Expand All @@ -72,18 +72,18 @@ func QueryAllComponents[T types.Component](
}

if player.Nickname == targetNickname {
entityID = id
entityIDs = append(entityIDs, id)
targetComponent, _ := cardinal.GetComponent[T](world, id)
targetComponents = append(targetComponents, targetComponent)
}

return true
})
if searchErr != nil {
return 0, nil, err
return nil, nil, err
}
if err != nil {
return 0, nil, err
return nil, nil, err
}
return entityID, targetComponents, err
return entityIDs, targetComponents, err
}
Loading