From 373e249c6ae147847c3675e16dc4d59b940fa6fd Mon Sep 17 00:00:00 2001 From: Konst Date: Wed, 3 Jul 2024 17:49:35 +0300 Subject: [PATCH] Added Delete building query --- cardinal/main.go | 2 + cardinal/msg/delete_building.go | 9 ++++ cardinal/system/delete_building.go | 68 ++++++++++++++++++++++++++++++ cardinal/system/query_utils.go | 12 +++--- 4 files changed, 85 insertions(+), 6 deletions(-) create mode 100644 cardinal/msg/delete_building.go create mode 100644 cardinal/system/delete_building.go diff --git a/cardinal/main.go b/cardinal/main.go index ba47941..c821acd 100644 --- a/cardinal/main.go +++ b/cardinal/main.go @@ -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 @@ -80,6 +81,7 @@ func MustInitWorld(w *cardinal.World) { system.FarmingSystem, system.PlayerSpawnerSystem, system.CreateBuildingSystem, + system.DeleteBuildingSystem, )) } diff --git a/cardinal/msg/delete_building.go b/cardinal/msg/delete_building.go new file mode 100644 index 0000000..facf32c --- /dev/null +++ b/cardinal/msg/delete_building.go @@ -0,0 +1,9 @@ +package msg + +type DeleteBuildingMsg struct { + TileIndex int `json:"tileID"` +} + +type DeleteBuildingResult struct { + Success bool `json:"success"` +} diff --git a/cardinal/system/delete_building.go b/cardinal/system/delete_building.go new file mode 100644 index 0000000..a4e85b2 --- /dev/null +++ b/cardinal/system/delete_building.go @@ -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 + }) +} diff --git a/cardinal/system/query_utils.go b/cardinal/system/query_utils.go index 265e7c8..17ed6fc 100644 --- a/cardinal/system/query_utils.go +++ b/cardinal/system/query_utils.go @@ -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 @@ -72,7 +72,7 @@ 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) } @@ -80,10 +80,10 @@ func QueryAllComponents[T types.Component]( 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 }