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 farming capacity limit #12

Merged
merged 2 commits 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: 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
Loading