Skip to content

Commit

Permalink
Added farming capacity limit (#12)
Browse files Browse the repository at this point in the history
* Added farming capacity limit

* Linter fixes
  • Loading branch information
k-karuna authored Jul 3, 2024
1 parent 60ee5a9 commit 56c4cc3
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 7 deletions.
2 changes: 1 addition & 1 deletion cardinal/component/farming.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package component

type Farming struct {
Type ResourceType `json:"type"`
Speed float32 `json:"speed"`
Speed float64 `json:"speed"`
Capacity int `json:"capacity,omitempty"`
}

Expand Down
2 changes: 1 addition & 1 deletion cardinal/component/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func GetAllResourceTypes() []ResourceType {

type Resource struct {
Type ResourceType `json:"type"`
Amount float32 `json:"amount"`
Amount float64 `json:"amount"`
Capacity int `json:"capacity,omitempty"`
}

Expand Down
2 changes: 1 addition & 1 deletion cardinal/query/player_resources.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func PlayerResources(world cardinal.WorldContext, req *PlayerResourcesRequest) (
return nil, fmt.Errorf("error querying player %s resources", req.Nickname)
}

aggregatedFarmingMap := make(map[comp.ResourceType]float32)
aggregatedFarmingMap := make(map[comp.ResourceType]float64)
for _, farming := range farmingComponents {
aggregatedFarmingMap[farming.Type] += farming.Speed
}
Expand Down
23 changes: 19 additions & 4 deletions cardinal/system/farming.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package system

import (
"math"
"time"

"pkg.world.dev/world-engine/cardinal"
Expand All @@ -18,22 +19,36 @@ func FarmingSystem(world cardinal.WorldContext) error {
playerComponent, _ := cardinal.GetComponent[comp.Player](world, id)
farmingComponent, _ := cardinal.GetComponent[comp.Farming](world, id)

playerEntityID, playerResources, err := QueryComponent[comp.PlayerResources](
playerEntityID, playerResources, _ := QueryComponent[comp.PlayerResources](
world,
playerComponent.Nickname,
filter.Component[comp.Player](),
filter.Component[comp.PlayerResources](),
)

_, playerBuildings, err := QueryAllComponents[comp.Building](
world,
playerComponent.Nickname,
filter.Component[comp.Player](),
filter.Component[comp.Building](),
)

totalCapacity := 0
for _, building := range playerBuildings {
totalCapacity += building.StorageCapacity
}

if err != nil {
return true
}

for i := range playerResources.Resources {
if playerResources.Resources[i].Type == farmingComponent.Type {
playerResources.Resources[i].Amount +=
farmingComponent.Speed * float32(constants.TickRate.Seconds()) / float32(time.Minute.Seconds())

tickFarmedAmount := farmingComponent.Speed * constants.TickRate.Seconds() / time.Minute.Seconds()
playerResources.Resources[i].Amount = math.Min(
playerResources.Resources[i].Amount+tickFarmedAmount,
float64(totalCapacity),
)
if err := cardinal.SetComponent[comp.PlayerResources](world, playerEntityID, playerResources); err != nil {
return true
}
Expand Down

0 comments on commit 56c4cc3

Please sign in to comment.