Skip to content

Commit

Permalink
Added Raft returning back to the island after looting; Added shipwrec…
Browse files Browse the repository at this point in the history
…k resources spawner once in a hour;
  • Loading branch information
k-karuna committed Jul 23, 2024
1 parent c66e196 commit fb34f77
Show file tree
Hide file tree
Showing 11 changed files with 129 additions and 17 deletions.
1 change: 1 addition & 0 deletions cardinal/component/building.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ var BuildingConfigs = map[BuildingType]BuildingConstants{
{Type: Fish, Amount: constants.ShipyardEffectRaftResourceFish},
},
BuildingTimeSeconds: constants.ShipyardEffectRaftBuildSeconds,
Speed: constants.RaftTravelSpeedPerMinute,
},
TileType: CoastlineTile,
},
Expand Down
2 changes: 2 additions & 0 deletions cardinal/component/effect.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ type Effect struct {
Amount int `json:"amount"`
Capacity int `json:"capacity"`
Resources []Resource `json:"resources,omitempty"`
LootResources *[]Resource `json:"lootResources,omitempty"`
Speed float64 `json:"speed"`
Position [2]float64 `json:"position,omitempty"`
TargetPosition *[2]float64 `json:"targetPosition,omitempty"`
BuildingTimeSeconds int `json:"buildingTimeSeconds,omitempty"`
Expand Down
3 changes: 2 additions & 1 deletion cardinal/component/shipwreck_resources.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ import (
)

type ShipwreckResources struct {
Resources []Resource `json:"resources"`
Resources *[]Resource `json:"resources"`
LastSpawnTime uint64 `json:"lastSpawnTime"`
}

func GetShipwreckResourceTypes() []ResourceType {
Expand Down
7 changes: 6 additions & 1 deletion cardinal/constants/buildings.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package constants

import "time"

const (
MainTileID = 22
MainUnitLimit = 5
Expand Down Expand Up @@ -51,4 +53,7 @@ const (
UnitLimitHouseUnitLimit = 15
)

const ShipwreckDistanceFromIsland = 0.4
const (
ShipwreckDistanceFromIsland = 0.4
ShipwreckResourcesRespawnInterval = time.Hour
)
12 changes: 6 additions & 6 deletions cardinal/constants/shipwreck.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
package constants

const (
ShipwreckMinWoodAmount = 5
ShipwreckMaxWoodAmount = 10
ShipwreckMinWoodAmount = 50
ShipwreckMaxWoodAmount = 100
)

const (
ShipwreckMinStoneAmount = 5
ShipwreckMaxStoneAmount = 10
ShipwreckMinStoneAmount = 50
ShipwreckMaxStoneAmount = 100
)

const (
ShipwreckMinFishAmount = 5
ShipwreckMaxFishAmount = 10
ShipwreckMinFishAmount = 50
ShipwreckMaxFishAmount = 100
)
1 change: 1 addition & 0 deletions cardinal/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ func MustInitWorld(w *cardinal.World) {
system.RelocateBuildingSystem,
system.StartSailShipwreckSystem,
system.SailShip,
system.ShipwreckResourcesSpawner,
))
}

Expand Down
6 changes: 3 additions & 3 deletions cardinal/query/global_map.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ type GlobalMapResponse struct {
}

type ResourcePoint struct {
Position [2]float64 `json:"position"`
Resources []comp.Resource `json:"resources"`
Position [2]float64 `json:"position"`
Resources *[]comp.Resource `json:"resources"`
}

func GlobalMap(world cardinal.WorldContext, _ *GlobalMapRequest) (*[]GlobalMapResponse, error) {
Expand All @@ -41,7 +41,7 @@ func GlobalMap(world cardinal.WorldContext, _ *GlobalMapRequest) (*[]GlobalMapRe
Player: players[i].Nickname,
Island: ResourcePoint{
Position: position.Island,
Resources: playerResources.Resources,
Resources: &playerResources.Resources,
},
Shipwreck: ResourcePoint{
Position: position.Shipwreck,
Expand Down
10 changes: 6 additions & 4 deletions cardinal/system/farming.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,12 @@ func FarmingSystem(world cardinal.WorldContext) error {
for i := range playerResources.Resources {
if playerResources.Resources[i].Type == farmingComponent.Type {
tickFarmedAmount := farmingComponent.Speed * constants.TickRate.Seconds() / time.Minute.Seconds()
playerResources.Resources[i].Amount = math.Min(
playerResources.Resources[i].Amount+tickFarmedAmount,
float64(totalCapacity),
)
if playerResources.Resources[i].Amount <= float64(totalCapacity) {
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
3 changes: 2 additions & 1 deletion cardinal/system/player_spawner.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,8 @@ func createPlayerData(world cardinal.WorldContext, nickname string) error {
Effects: effects,
},
comp.ShipwreckResources{
Resources: shipWreckResources,
Resources: &shipWreckResources,
LastSpawnTime: world.Timestamp(),
},
comp.Position{
Island: islandCoordinates,
Expand Down
64 changes: 63 additions & 1 deletion cardinal/system/sail_ship.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ func SailShip(world cardinal.WorldContext) error {
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
}
Expand All @@ -33,6 +32,22 @@ func SailShip(world cardinal.WorldContext) error {
raftTravelDistancePerTick,
)

_, playerPosition, _ := QueryPlayerComponent[comp.Position](
world,
buildingComponent.Effect.Player,
filter.Component[comp.Player](),
filter.Component[comp.TileMap](),
filter.Component[comp.Position](),
)

if buildingComponent.Effect.Position == playerPosition.Island {
buildingComponent.Effect.TargetPosition = nil
_ = unloadShip(world, buildingComponent.Effect)
} else if buildingComponent.Effect.Position == *buildingComponent.Effect.TargetPosition {
buildingComponent.Effect.TargetPosition = &playerPosition.Island
_ = lootShipwreck(world, buildingComponent.Effect)
}

err := updateEffect(world, id, buildingComponent.Effect)
if err != nil {
return true
Expand Down Expand Up @@ -62,3 +77,50 @@ func findPointAtDistance(start, end [2]float64, distance float64) [2]float64 {

return [2]float64{newX, newY}
}

func lootShipwreck(world cardinal.WorldContext, effect *comp.Effect) error {
playerPositionsIDs, playerPositions, _ := QueryAllComponents[comp.Position](
world,
filter.Component[comp.Player](),
filter.Component[comp.TileMap](),
filter.Component[comp.Position](),
)

var shipwreckResources *comp.ShipwreckResources
var playerMapEntityID types.EntityID
for i, position := range playerPositions {
if position.Shipwreck == effect.Position {
playerMapEntityID = playerPositionsIDs[i]
shipwreckResources, _ = cardinal.GetComponent[comp.ShipwreckResources](world, playerMapEntityID)
break
}
}

effect.LootResources = shipwreckResources.Resources
shipwreckResources.Resources = nil
if err := cardinal.SetComponent(world, playerMapEntityID, shipwreckResources); err != nil {
return err
}

return nil
}

func unloadShip(world cardinal.WorldContext, effect *comp.Effect) error {
playerResourcesID, playerResources, _ := QueryPlayerComponent[comp.PlayerResources](
world,
effect.Player,
filter.Component[comp.Player](),
filter.Component[comp.TileMap](),
filter.Component[comp.PlayerResources](),
)

var err error
for _, lootResource := range *effect.LootResources {
playerResource, _ := GetResourceByType(playerResources, lootResource.Type)
playerResource.Amount += lootResource.Amount
SetResourceByType(playerResources, *playerResource)
err = cardinal.SetComponent(world, playerResourcesID, playerResources)
}
effect.LootResources = nil
return err
}
37 changes: 37 additions & 0 deletions cardinal/system/shipwreck_resources_spawner.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package system

import (
"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"
)

// ShipwreckResourcesSpawner -
func ShipwreckResourcesSpawner(world cardinal.WorldContext) error {
return cardinal.NewSearch().Entity(
filter.Contains(filter.Component[comp.Player](), filter.Component[comp.ShipwreckResources]())).
Each(world, func(id types.EntityID) bool {
shipwreckResources, _ := cardinal.GetComponent[comp.ShipwreckResources](world, id)
if (shipwreckResources.LastSpawnTime +
uint64(constants.ShipwreckResourcesRespawnInterval.Milliseconds())) < world.Timestamp() {
return true
}
if shipwreckResources.Resources != nil {
return true
}

shipWreckResources := make([]comp.Resource, len(comp.GetShipwreckResourceTypes()))
for i, resourceType := range comp.GetShipwreckResourceTypes() {
shipWreckResources[i] = comp.Resource{
Type: resourceType,
Amount: comp.GetShipwreckDefaultResourceAmount(resourceType),
}
}
shipwreckResources.Resources = &shipWreckResources
_ = cardinal.SetComponent(world, id, shipwreckResources)
return true
})
}

0 comments on commit fb34f77

Please sign in to comment.