From e3187518eeea36e4aa42a0c8934e1f4b0a1ac931 Mon Sep 17 00:00:00 2001 From: Konst Date: Tue, 16 Jul 2024 18:52:49 +0300 Subject: [PATCH 1/6] Reduced ShipwreckDistanceFromIsland to 0.4 --- cardinal/constants/buildings.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cardinal/constants/buildings.go b/cardinal/constants/buildings.go index 1aea86d..d8e5d29 100644 --- a/cardinal/constants/buildings.go +++ b/cardinal/constants/buildings.go @@ -47,4 +47,4 @@ const ( UnitLimitHouseUnitLimit = 15 ) -const ShipwreckDistanceFromIsland = 0.8 +const ShipwreckDistanceFromIsland = 0.4 From 12f8e7732100b3cdc2569e358bcc2166c36f6b17 Mon Sep 17 00:00:00 2001 From: Konst Date: Thu, 18 Jul 2024 16:18:10 +0300 Subject: [PATCH 2/6] Added position to effect --- cardinal/component/effect.go | 1 + cardinal/system/effects_spawner.go | 3 +++ 2 files changed, 4 insertions(+) diff --git a/cardinal/component/effect.go b/cardinal/component/effect.go index 6350ecc..1196922 100644 --- a/cardinal/component/effect.go +++ b/cardinal/component/effect.go @@ -28,6 +28,7 @@ type Effect struct { Amount int `json:"amount"` Capacity int `json:"capacity"` Resources []Resource `json:"resources,omitempty"` + Position [2]float64 `json:"position,omitempty"` BuildingTimeSeconds int `json:"buildingTimeSeconds,omitempty"` BuildingTimeStartedAt uint64 `json:"buildingTimeStartedAt,omitempty"` } diff --git a/cardinal/system/effects_spawner.go b/cardinal/system/effects_spawner.go index 26168f2..360ad62 100644 --- a/cardinal/system/effects_spawner.go +++ b/cardinal/system/effects_spawner.go @@ -47,11 +47,14 @@ func EffectsSpawnerSystem(world cardinal.WorldContext) error { return true } + mapPosition, _ := cardinal.GetComponent[comp.Position](world, playerMapEntityID) + effectComponent.Amount = min( constants.ShipyardEffectRaftCapacity, effectComponent.Amount+1, ) effectComponent.BuildingTimeStartedAt = 0 + effectComponent.Position = mapPosition.Island buildingComponent.Effect = effectComponent tile := &(*playerMap.Tiles)[buildingComponent.TileID] tile.Building = buildingComponent From e07ddde4e048fd59ed40d9343e7e91d915a07567 Mon Sep 17 00:00:00 2001 From: Konst Date: Fri, 19 Jul 2024 16:36:20 +0300 Subject: [PATCH 3/6] Added sail shipwreck and all ships queries --- cardinal/component/effect.go | 17 ++-- cardinal/constants/buildings.go | 24 ++--- cardinal/main.go | 6 ++ cardinal/msg/sail_shipwreck.go | 9 ++ cardinal/query/all_ships.go | 32 +++++++ cardinal/query/global_map.go | 7 +- cardinal/query/map_state.go | 19 ++-- cardinal/system/create_building.go | 4 + cardinal/system/sail_ship.go | 68 ++++++++++++++ .../system/start_sail_shipwreck_system.go | 89 +++++++++++++++++++ 10 files changed, 246 insertions(+), 29 deletions(-) create mode 100644 cardinal/msg/sail_shipwreck.go create mode 100644 cardinal/query/all_ships.go create mode 100644 cardinal/system/sail_ship.go create mode 100644 cardinal/system/start_sail_shipwreck_system.go diff --git a/cardinal/component/effect.go b/cardinal/component/effect.go index 1196922..1948121 100644 --- a/cardinal/component/effect.go +++ b/cardinal/component/effect.go @@ -24,13 +24,16 @@ func GetCapacityByEffectType(effectType EffectType) int { } type Effect struct { - Type EffectType `json:"type"` - Amount int `json:"amount"` - Capacity int `json:"capacity"` - Resources []Resource `json:"resources,omitempty"` - Position [2]float64 `json:"position,omitempty"` - BuildingTimeSeconds int `json:"buildingTimeSeconds,omitempty"` - BuildingTimeStartedAt uint64 `json:"buildingTimeStartedAt,omitempty"` + ID string `json:"id"` + Player string `json:"player"` + Type EffectType `json:"type"` + Amount int `json:"amount"` + Capacity int `json:"capacity"` + Resources []Resource `json:"resources,omitempty"` + Position [2]float64 `json:"position,omitempty"` + TargetPosition *[2]float64 `json:"targetPosition,omitempty"` + BuildingTimeSeconds int `json:"buildingTimeSeconds,omitempty"` + BuildingTimeStartedAt uint64 `json:"buildingTimeStartedAt,omitempty"` } func (Effect) Name() string { diff --git a/cardinal/constants/buildings.go b/cardinal/constants/buildings.go index d8e5d29..ec6e15c 100644 --- a/cardinal/constants/buildings.go +++ b/cardinal/constants/buildings.go @@ -1,28 +1,28 @@ package constants const ( - MainTileID = 22 - MainUnitLimit = 5 - MainStorageCapacity = 10 - // MainStorageCapacity = 100 + MainTileID = 22 + MainUnitLimit = 5 + // MainStorageCapacity = 10 + MainStorageCapacity = 100 ) const ( - WoodcutterFarmingSpeed = 5 - // WoodcutterFarmingSpeed = 50 + // WoodcutterFarmingSpeed = 5 + WoodcutterFarmingSpeed = 50 ) const ( QuarryResourcesWoodAmount = 5 - QuarryFarmingStoneSpeed = 5 - // QuarryFarmingStoneSpeed = 50 + // QuarryFarmingStoneSpeed = 5 + QuarryFarmingStoneSpeed = 50 ) const ( FishermanHutResourcesWoodAmount = 10 FishermanHutResourcesStoneAmount = 10 - FishermanHutFarmingFishSpeed = 5 - // FishermanHutFarmingFishSpeed = 50 + // FishermanHutFarmingFishSpeed = 5 + FishermanHutFarmingFishSpeed = 50 ) const ( @@ -35,6 +35,10 @@ const ( ShipyardEffectRaftBuildSeconds = 60 ) +const ( + RaftTravelSpeedPerMinute = 0.4 +) + const ( WarehouseResourcesWoodAmount = 10 WarehouseResourcesFishAmount = 10 diff --git a/cardinal/main.go b/cardinal/main.go index 6c7580e..d643ccf 100644 --- a/cardinal/main.go +++ b/cardinal/main.go @@ -53,6 +53,7 @@ func MustInitWorld(w *cardinal.World) { cardinal.RegisterMessage[msg.DeleteBuildingMsg, msg.DeleteBuildingResult](w, "delete-building"), cardinal.RegisterMessage[msg.CreateEffectMsg, msg.CreateEffectResult](w, "create-effect"), cardinal.RegisterMessage[msg.RelocateBuildingMsg, msg.RelocateBuildingResult](w, "relocate-building"), + cardinal.RegisterMessage[msg.SailShipwreckMsg, msg.SailShipWreckResult](w, "sail-shipwreck"), ) // Register queries @@ -74,6 +75,10 @@ func MustInitWorld(w *cardinal.World) { query.GlobalMapRequest, []query.GlobalMapResponse, ](w, "global-map", query.GlobalMap), + cardinal.RegisterQuery[ + query.AllShipsRequest, + map[string]component.Effect, + ](w, "all-ships", query.AllShips), ) // Each system executes deterministically in the order they are added. @@ -88,6 +93,7 @@ func MustInitWorld(w *cardinal.World) { system.CreateEffectSystem, system.EffectsSpawnerSystem, system.RelocateBuildingSystem, + system.StartSailShipwreckSystem, )) } diff --git a/cardinal/msg/sail_shipwreck.go b/cardinal/msg/sail_shipwreck.go new file mode 100644 index 0000000..293c796 --- /dev/null +++ b/cardinal/msg/sail_shipwreck.go @@ -0,0 +1,9 @@ +package msg + +type SailShipwreckMsg struct { + Player string `json:"player"` +} + +type SailShipWreckResult struct { + Success bool `json:"success"` +} diff --git a/cardinal/query/all_ships.go b/cardinal/query/all_ships.go new file mode 100644 index 0000000..862acb7 --- /dev/null +++ b/cardinal/query/all_ships.go @@ -0,0 +1,32 @@ +package query + +import ( + comp "oceanus-shard/component" + "oceanus-shard/system" + + "pkg.world.dev/world-engine/cardinal" + "pkg.world.dev/world-engine/cardinal/search/filter" +) + +type AllShipsRequest struct { + Nickname string +} + +// AllShips - +func AllShips(world cardinal.WorldContext, req *AllShipsRequest) (*map[string]comp.Effect, error) { + _, effects, err := system.QueryAllComponents[comp.Effect]( + world, + filter.Component[comp.Player](), + filter.Component[comp.Building](), + filter.Component[comp.Effect](), + ) + + shipsMap := make(map[string]comp.Effect) + for _, effect := range effects { + if effect.Amount > 0 { + shipsMap[effect.ID] = *effect + } + } + + return &shipsMap, err +} diff --git a/cardinal/query/global_map.go b/cardinal/query/global_map.go index d86c3e6..4179ad4 100644 --- a/cardinal/query/global_map.go +++ b/cardinal/query/global_map.go @@ -11,9 +11,10 @@ import ( type GlobalMapRequest struct{} type GlobalMapResponse struct { - Player string `json:"player"` - Island ResourcePoint `json:"island"` - Shipwreck ResourcePoint `json:"shipwreck"` + Player string `json:"player"` + Island ResourcePoint `json:"island"` + Shipwreck ResourcePoint `json:"shipwreck"` + Ships *[]comp.Effect `json:"ships"` } type ResourcePoint struct { diff --git a/cardinal/query/map_state.go b/cardinal/query/map_state.go index 38a790b..adb33cc 100644 --- a/cardinal/query/map_state.go +++ b/cardinal/query/map_state.go @@ -14,26 +14,27 @@ type MapStateRequest struct { } type MapStateResponse struct { - Tiles *[]comp.Tile `json:"tiles"` - Width int `json:"width"` - Height int `json:"height"` + Tiles *[]comp.Tile `json:"tiles"` + Width int `json:"width"` + Height int `json:"height"` + Position [2]float64 `json:"position"` } func PlayerMap(world cardinal.WorldContext, req *MapStateRequest) (*MapStateResponse, error) { - _, playerMap, err := system.QueryPlayerComponent[comp.TileMap]( + playerMapEntityID, playerMap, err := system.QueryPlayerComponent[comp.TileMap]( world, req.Nickname, filter.Component[comp.Player](), filter.Component[comp.TileMap](), ) - if playerMap == nil { return nil, fmt.Errorf("error querying players %s map", req.Nickname) } - + position, _ := cardinal.GetComponent[comp.Position](world, playerMapEntityID) return &MapStateResponse{ - Tiles: playerMap.Tiles, - Width: playerMap.Width, - Height: playerMap.Height, + Tiles: playerMap.Tiles, + Width: playerMap.Width, + Height: playerMap.Height, + Position: position.Island, }, err } diff --git a/cardinal/system/create_building.go b/cardinal/system/create_building.go index e2477c0..46a7007 100644 --- a/cardinal/system/create_building.go +++ b/cardinal/system/create_building.go @@ -3,6 +3,7 @@ package system import ( "fmt" + "github.com/google/uuid" "pkg.world.dev/world-engine/cardinal" "pkg.world.dev/world-engine/cardinal/search/filter" @@ -69,6 +70,9 @@ func CreateBuildingSystem(world cardinal.WorldContext) error { } if building.Effect != nil { + building.Effect.Player = request.Tx.PersonaTag + building.Effect.ID = uuid.New().String() + _ = cardinal.AddComponentTo[comp.Effect](world, buildingEntityID) _ = cardinal.SetComponent(world, buildingEntityID, building.Effect) } diff --git a/cardinal/system/sail_ship.go b/cardinal/system/sail_ship.go new file mode 100644 index 0000000..d9d9c69 --- /dev/null +++ b/cardinal/system/sail_ship.go @@ -0,0 +1,68 @@ +package system + +import ( + "math" + "time" + + "pkg.world.dev/world-engine/cardinal" + "pkg.world.dev/world-engine/cardinal/search/filter" + "pkg.world.dev/world-engine/cardinal/types" + + comp "oceanus-shard/component" + "oceanus-shard/constants" +) + +// SailShip - +func SailShip(world cardinal.WorldContext) error { + return cardinal.NewSearch().Entity( + filter.Contains(filter.Component[comp.Building](), filter.Component[comp.Effect]())). + Each(world, func(id types.EntityID) bool { + effectComponent, _ := cardinal.GetComponent[comp.Effect](world, id) + + if effectComponent.Amount == 0 || effectComponent.TargetPosition == nil { + return true + } + + // playerComponent, _ := cardinal.GetComponent[comp.Player](world, id) + buildingComponent, _ := cardinal.GetComponent[comp.Building](world, id) + RaftTravelDistancePerTick := constants.RaftTravelSpeedPerMinute / time.Minute.Seconds() * constants.TickRate.Seconds() + + buildingComponent.Effect.Position = findPointAtDistance( + buildingComponent.Effect.Position, + *buildingComponent.Effect.TargetPosition, + RaftTravelDistancePerTick, + ) + + if buildingComponent.Effect.Position == *buildingComponent.Effect.TargetPosition { + buildingComponent.Effect.TargetPosition = nil + } + + if err := cardinal.SetComponent(world, id, buildingComponent); err != nil { + return true + } + + return true + }) +} + +func findPointAtDistance(start, end [2]float64, distance float64) [2]float64 { + x1, y1 := start[0], start[1] + x2, y2 := end[0], end[1] + + dx := x2 - x1 + dy := y2 - y1 + + length := math.Sqrt(dx*dx + dy*dy) + + if distance > length { + return end + } + + unitDx := dx / length + unitDy := dy / length + + newX := x1 + unitDx*distance + newY := y1 + unitDy*distance + + return [2]float64{newX, newY} +} diff --git a/cardinal/system/start_sail_shipwreck_system.go b/cardinal/system/start_sail_shipwreck_system.go new file mode 100644 index 0000000..2dedf2c --- /dev/null +++ b/cardinal/system/start_sail_shipwreck_system.go @@ -0,0 +1,89 @@ +package system + +import ( + "fmt" + + "pkg.world.dev/world-engine/cardinal" + "pkg.world.dev/world-engine/cardinal/search/filter" + "pkg.world.dev/world-engine/cardinal/types" + + comp "oceanus-shard/component" + "oceanus-shard/msg" +) + +// StartSailShipwreckSystem - +func StartSailShipwreckSystem(world cardinal.WorldContext) error { + return cardinal.EachMessage[msg.SailShipwreckMsg, msg.SailShipWreckResult]( + world, + func(request cardinal.TxData[msg.SailShipwreckMsg]) (msg.SailShipWreckResult, error) { + mapEntityID, playerMap, _ := QueryPlayerComponent[comp.TileMap]( + world, + request.Tx.PersonaTag, + filter.Component[comp.Player](), + filter.Component[comp.TileMap](), + ) + + _, targetPlayerPosition, err := QueryPlayerComponent[comp.Position]( + world, + request.Msg.Player, + filter.Component[comp.Player](), + filter.Component[comp.TileMap](), + ) + + if err != nil { + return msg.SailShipWreckResult{Success: false}, err + } + + playerBuildingsEntityIDs, playerBuildings, _ := QueryAllPlayerComponents[comp.Building]( + world, + request.Tx.PersonaTag, + filter.Component[comp.Player](), + filter.Component[comp.Building](), + ) + + if playerMap == nil { + return msg.SailShipWreckResult{Success: false}, + fmt.Errorf("failed to sail shipwreck, this player did not have tilemap") + } + + player, _ := cardinal.GetComponent[comp.Player](world, mapEntityID) + playerPosition, _ := cardinal.GetComponent[comp.Position](world, mapEntityID) + if player.Nickname != request.Tx.PersonaTag { + return msg.SailShipWreckResult{Success: false}, fmt.Errorf("can't sail another player building") + } + + var shipyard *comp.Building + var shipyardEntityID types.EntityID + for playerBuildingID, playerBuilding := range playerBuildings { + if playerBuilding.Type != comp.Shipyard { + continue + } + if playerBuilding.Effect.Amount == 0 { + continue + } + if playerBuilding.Effect.Position != playerPosition.Island { + continue + } + shipyard = playerBuilding + shipyardEntityID = playerBuildingsEntityIDs[playerBuildingID] + } + + if shipyard == nil { + return msg.SailShipWreckResult{Success: false}, fmt.Errorf("player didn't have ships on base") + } + + shipyard.Effect.TargetPosition = &targetPlayerPosition.Shipwreck + tile := &(*playerMap.Tiles)[shipyard.TileID] + tile.Building = shipyard + + if err := cardinal.SetComponent(world, shipyardEntityID, shipyard); err != nil { + return msg.SailShipWreckResult{Success: false}, err + } + + if err := cardinal.SetComponent(world, mapEntityID, playerMap); err != nil { + return msg.SailShipWreckResult{Success: false}, err + } + + return msg.SailShipWreckResult{Success: true}, nil + }) +} From 0b45b7a9cb6d3bd04dbf5dc27edd62462724a06e Mon Sep 17 00:00:00 2001 From: Konst Date: Fri, 19 Jul 2024 19:29:43 +0300 Subject: [PATCH 4/6] Added SailShip system --- cardinal/main.go | 1 + cardinal/system/sail_ship.go | 10 +------- .../system/start_sail_shipwreck_system.go | 19 +++++---------- cardinal/system/utils.go | 24 +++++++++++++++++++ 4 files changed, 32 insertions(+), 22 deletions(-) diff --git a/cardinal/main.go b/cardinal/main.go index d643ccf..2e21b18 100644 --- a/cardinal/main.go +++ b/cardinal/main.go @@ -94,6 +94,7 @@ func MustInitWorld(w *cardinal.World) { system.EffectsSpawnerSystem, system.RelocateBuildingSystem, system.StartSailShipwreckSystem, + system.SailShip, )) } diff --git a/cardinal/system/sail_ship.go b/cardinal/system/sail_ship.go index d9d9c69..d12ef9b 100644 --- a/cardinal/system/sail_ship.go +++ b/cardinal/system/sail_ship.go @@ -23,7 +23,6 @@ func SailShip(world cardinal.WorldContext) error { return true } - // playerComponent, _ := cardinal.GetComponent[comp.Player](world, id) buildingComponent, _ := cardinal.GetComponent[comp.Building](world, id) RaftTravelDistancePerTick := constants.RaftTravelSpeedPerMinute / time.Minute.Seconds() * constants.TickRate.Seconds() @@ -33,14 +32,7 @@ func SailShip(world cardinal.WorldContext) error { RaftTravelDistancePerTick, ) - if buildingComponent.Effect.Position == *buildingComponent.Effect.TargetPosition { - buildingComponent.Effect.TargetPosition = nil - } - - if err := cardinal.SetComponent(world, id, buildingComponent); err != nil { - return true - } - + updateEffect(world, id, buildingComponent.Effect) return true }) } diff --git a/cardinal/system/start_sail_shipwreck_system.go b/cardinal/system/start_sail_shipwreck_system.go index 2dedf2c..a7def10 100644 --- a/cardinal/system/start_sail_shipwreck_system.go +++ b/cardinal/system/start_sail_shipwreck_system.go @@ -16,13 +16,15 @@ func StartSailShipwreckSystem(world cardinal.WorldContext) error { return cardinal.EachMessage[msg.SailShipwreckMsg, msg.SailShipWreckResult]( world, func(request cardinal.TxData[msg.SailShipwreckMsg]) (msg.SailShipWreckResult, error) { - mapEntityID, playerMap, _ := QueryPlayerComponent[comp.TileMap]( + mapEntityID, playerPosition, _ := QueryPlayerComponent[comp.Position]( world, request.Tx.PersonaTag, filter.Component[comp.Player](), filter.Component[comp.TileMap](), ) + player, _ := cardinal.GetComponent[comp.Player](world, mapEntityID) + _, targetPlayerPosition, err := QueryPlayerComponent[comp.Position]( world, request.Msg.Player, @@ -41,13 +43,11 @@ func StartSailShipwreckSystem(world cardinal.WorldContext) error { filter.Component[comp.Building](), ) - if playerMap == nil { + if playerPosition == nil { return msg.SailShipWreckResult{Success: false}, fmt.Errorf("failed to sail shipwreck, this player did not have tilemap") } - player, _ := cardinal.GetComponent[comp.Player](world, mapEntityID) - playerPosition, _ := cardinal.GetComponent[comp.Position](world, mapEntityID) if player.Nickname != request.Tx.PersonaTag { return msg.SailShipWreckResult{Success: false}, fmt.Errorf("can't sail another player building") } @@ -73,17 +73,10 @@ func StartSailShipwreckSystem(world cardinal.WorldContext) error { } shipyard.Effect.TargetPosition = &targetPlayerPosition.Shipwreck - tile := &(*playerMap.Tiles)[shipyard.TileID] - tile.Building = shipyard - - if err := cardinal.SetComponent(world, shipyardEntityID, shipyard); err != nil { - return msg.SailShipWreckResult{Success: false}, err - } - - if err := cardinal.SetComponent(world, mapEntityID, playerMap); err != nil { + err = updateEffect(world, shipyardEntityID, shipyard.Effect) + if err != nil { return msg.SailShipWreckResult{Success: false}, err } - return msg.SailShipWreckResult{Success: true}, nil }) } diff --git a/cardinal/system/utils.go b/cardinal/system/utils.go index 1ddb609..b10aa55 100644 --- a/cardinal/system/utils.go +++ b/cardinal/system/utils.go @@ -186,3 +186,27 @@ func GetTotalPlayersAmount(world cardinal.WorldContext) (int, error) { } return totalPlayers, searchErr } + +func updateEffect(world cardinal.WorldContext, buildingEntityID types.EntityID, effect *comp.Effect) error { + building, _ := cardinal.GetComponent[comp.Building](world, buildingEntityID) + player, _ := cardinal.GetComponent[comp.Player](world, buildingEntityID) + mapEntityID, playerMap, _ := QueryPlayerComponent[comp.TileMap]( + world, + player.Nickname, + filter.Component[comp.Player](), + filter.Component[comp.TileMap](), + ) + + building.Effect = effect + tile := &(*playerMap.Tiles)[building.TileID] + tile.Building = building + + if err := cardinal.SetComponent(world, buildingEntityID, building); err != nil { + return err + } + + if err := cardinal.SetComponent(world, mapEntityID, playerMap); err != nil { + return err + } + return nil +} From 65eed13f1c516b7c57fb9f36c476e3b96ae79bf8 Mon Sep 17 00:00:00 2001 From: Konst Date: Mon, 22 Jul 2024 19:20:31 +0300 Subject: [PATCH 5/6] Fixed bug with no moving ship --- cardinal/go.mod | 2 +- cardinal/system/start_sail_shipwreck_system.go | 4 ++-- cardinal/system/utils.go | 6 ++++-- 3 files changed, 7 insertions(+), 5 deletions(-) diff --git a/cardinal/go.mod b/cardinal/go.mod index f0bab8e..081a931 100644 --- a/cardinal/go.mod +++ b/cardinal/go.mod @@ -3,6 +3,7 @@ module oceanus-shard go 1.22.1 require ( + github.com/google/uuid v1.6.0 github.com/rs/zerolog v1.32.0 golang.org/x/exp v0.0.0-20231110203233-9a3e6036ecaa pkg.world.dev/world-engine/cardinal v1.5.1 @@ -46,7 +47,6 @@ require ( github.com/golang/protobuf v1.5.4 // indirect github.com/google/go-cmp v0.6.0 // indirect github.com/google/pprof v0.0.0-20230901174712-0191c66da455 // indirect - github.com/google/uuid v1.6.0 // indirect github.com/hashicorp/errwrap v1.1.0 // indirect github.com/hashicorp/go-multierror v1.1.1 // indirect github.com/hashicorp/hcl v1.0.1-vault-5 // indirect diff --git a/cardinal/system/start_sail_shipwreck_system.go b/cardinal/system/start_sail_shipwreck_system.go index a7def10..303c9d9 100644 --- a/cardinal/system/start_sail_shipwreck_system.go +++ b/cardinal/system/start_sail_shipwreck_system.go @@ -71,8 +71,8 @@ func StartSailShipwreckSystem(world cardinal.WorldContext) error { if shipyard == nil { return msg.SailShipWreckResult{Success: false}, fmt.Errorf("player didn't have ships on base") } - - shipyard.Effect.TargetPosition = &targetPlayerPosition.Shipwreck + shipyardEffect := shipyard.Effect + shipyardEffect.TargetPosition = &targetPlayerPosition.Shipwreck err = updateEffect(world, shipyardEntityID, shipyard.Effect) if err != nil { return msg.SailShipWreckResult{Success: false}, err diff --git a/cardinal/system/utils.go b/cardinal/system/utils.go index b10aa55..1297b26 100644 --- a/cardinal/system/utils.go +++ b/cardinal/system/utils.go @@ -188,6 +188,7 @@ func GetTotalPlayersAmount(world cardinal.WorldContext) (int, error) { } func updateEffect(world cardinal.WorldContext, buildingEntityID types.EntityID, effect *comp.Effect) error { + building, _ := cardinal.GetComponent[comp.Building](world, buildingEntityID) player, _ := cardinal.GetComponent[comp.Player](world, buildingEntityID) mapEntityID, playerMap, _ := QueryPlayerComponent[comp.TileMap]( @@ -200,11 +201,12 @@ func updateEffect(world cardinal.WorldContext, buildingEntityID types.EntityID, building.Effect = effect tile := &(*playerMap.Tiles)[building.TileID] tile.Building = building - + if err := cardinal.SetComponent(world, buildingEntityID, effect); err != nil { + return err + } if err := cardinal.SetComponent(world, buildingEntityID, building); err != nil { return err } - if err := cardinal.SetComponent(world, mapEntityID, playerMap); err != nil { return err } From 262798c926d18bc6f14620bdaa15aaeb5e6f9bc6 Mon Sep 17 00:00:00 2001 From: Konst Date: Mon, 22 Jul 2024 19:35:50 +0300 Subject: [PATCH 6/6] Linter fixes --- cardinal/query/all_ships.go | 6 +- cardinal/system/sail_ship.go | 10 ++- .../system/start_sail_shipwreck_system.go | 73 +++++++++++-------- cardinal/system/utils.go | 1 - 4 files changed, 51 insertions(+), 39 deletions(-) diff --git a/cardinal/query/all_ships.go b/cardinal/query/all_ships.go index 862acb7..f14f160 100644 --- a/cardinal/query/all_ships.go +++ b/cardinal/query/all_ships.go @@ -8,12 +8,10 @@ import ( "pkg.world.dev/world-engine/cardinal/search/filter" ) -type AllShipsRequest struct { - Nickname string -} +type AllShipsRequest struct{} // AllShips - -func AllShips(world cardinal.WorldContext, req *AllShipsRequest) (*map[string]comp.Effect, error) { +func AllShips(world cardinal.WorldContext, _ *AllShipsRequest) (*map[string]comp.Effect, error) { _, effects, err := system.QueryAllComponents[comp.Effect]( world, filter.Component[comp.Player](), diff --git a/cardinal/system/sail_ship.go b/cardinal/system/sail_ship.go index d12ef9b..3d5f2c7 100644 --- a/cardinal/system/sail_ship.go +++ b/cardinal/system/sail_ship.go @@ -24,15 +24,19 @@ func SailShip(world cardinal.WorldContext) error { } buildingComponent, _ := cardinal.GetComponent[comp.Building](world, id) - RaftTravelDistancePerTick := constants.RaftTravelSpeedPerMinute / time.Minute.Seconds() * constants.TickRate.Seconds() + raftTravelDistancePerTick := + constants.RaftTravelSpeedPerMinute / time.Minute.Seconds() * constants.TickRate.Seconds() buildingComponent.Effect.Position = findPointAtDistance( buildingComponent.Effect.Position, *buildingComponent.Effect.TargetPosition, - RaftTravelDistancePerTick, + raftTravelDistancePerTick, ) - updateEffect(world, id, buildingComponent.Effect) + err := updateEffect(world, id, buildingComponent.Effect) + if err != nil { + return true + } return true }) } diff --git a/cardinal/system/start_sail_shipwreck_system.go b/cardinal/system/start_sail_shipwreck_system.go index 303c9d9..61c7d7b 100644 --- a/cardinal/system/start_sail_shipwreck_system.go +++ b/cardinal/system/start_sail_shipwreck_system.go @@ -36,13 +36,6 @@ func StartSailShipwreckSystem(world cardinal.WorldContext) error { return msg.SailShipWreckResult{Success: false}, err } - playerBuildingsEntityIDs, playerBuildings, _ := QueryAllPlayerComponents[comp.Building]( - world, - request.Tx.PersonaTag, - filter.Component[comp.Player](), - filter.Component[comp.Building](), - ) - if playerPosition == nil { return msg.SailShipWreckResult{Success: false}, fmt.Errorf("failed to sail shipwreck, this player did not have tilemap") @@ -51,32 +44,50 @@ func StartSailShipwreckSystem(world cardinal.WorldContext) error { if player.Nickname != request.Tx.PersonaTag { return msg.SailShipWreckResult{Success: false}, fmt.Errorf("can't sail another player building") } - - var shipyard *comp.Building - var shipyardEntityID types.EntityID - for playerBuildingID, playerBuilding := range playerBuildings { - if playerBuilding.Type != comp.Shipyard { - continue - } - if playerBuilding.Effect.Amount == 0 { - continue - } - if playerBuilding.Effect.Position != playerPosition.Island { - continue - } - shipyard = playerBuilding - shipyardEntityID = playerBuildingsEntityIDs[playerBuildingID] - } - - if shipyard == nil { - return msg.SailShipWreckResult{Success: false}, fmt.Errorf("player didn't have ships on base") - } - shipyardEffect := shipyard.Effect - shipyardEffect.TargetPosition = &targetPlayerPosition.Shipwreck - err = updateEffect(world, shipyardEntityID, shipyard.Effect) + err = createSailData(world, request.Tx.PersonaTag, playerPosition, targetPlayerPosition) if err != nil { return msg.SailShipWreckResult{Success: false}, err } - return msg.SailShipWreckResult{Success: true}, nil + return msg.SailShipWreckResult{Success: true}, err }) } + +func createSailData( + world cardinal.WorldContext, + personaTag string, + playerPosition *comp.Position, + targetPlayerPosition *comp.Position) error { + playerBuildingsEntityIDs, playerBuildings, _ := QueryAllPlayerComponents[comp.Building]( + world, + personaTag, + filter.Component[comp.Player](), + filter.Component[comp.Building](), + ) + + var shipyard *comp.Building + var shipyardEntityID types.EntityID + for playerBuildingID, playerBuilding := range playerBuildings { + if playerBuilding.Type != comp.Shipyard { + continue + } + if playerBuilding.Effect.Amount == 0 { + continue + } + if playerBuilding.Effect.Position != playerPosition.Island { + continue + } + shipyard = playerBuilding + shipyardEntityID = playerBuildingsEntityIDs[playerBuildingID] + } + + if shipyard == nil { + return fmt.Errorf("player didn't have ships on base") + } + shipyardEffect := shipyard.Effect + shipyardEffect.TargetPosition = &targetPlayerPosition.Shipwreck + err := updateEffect(world, shipyardEntityID, shipyard.Effect) + if err != nil { + return err + } + return err +} diff --git a/cardinal/system/utils.go b/cardinal/system/utils.go index 1297b26..8be5eb3 100644 --- a/cardinal/system/utils.go +++ b/cardinal/system/utils.go @@ -188,7 +188,6 @@ func GetTotalPlayersAmount(world cardinal.WorldContext) (int, error) { } func updateEffect(world cardinal.WorldContext, buildingEntityID types.EntityID, effect *comp.Effect) error { - building, _ := cardinal.GetComponent[comp.Building](world, buildingEntityID) player, _ := cardinal.GetComponent[comp.Player](world, buildingEntityID) mapEntityID, playerMap, _ := QueryPlayerComponent[comp.TileMap](