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

Feat/shipwreck rafting #19

Merged
merged 6 commits into from
Jul 22, 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
16 changes: 10 additions & 6 deletions cardinal/component/effect.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +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"`
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 {
Expand Down
26 changes: 15 additions & 11 deletions cardinal/constants/buildings.go
Original file line number Diff line number Diff line change
@@ -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 (
Expand All @@ -35,6 +35,10 @@ const (
ShipyardEffectRaftBuildSeconds = 60
)

const (
RaftTravelSpeedPerMinute = 0.4
)

const (
WarehouseResourcesWoodAmount = 10
WarehouseResourcesFishAmount = 10
Expand All @@ -47,4 +51,4 @@ const (
UnitLimitHouseUnitLimit = 15
)

const ShipwreckDistanceFromIsland = 0.8
const ShipwreckDistanceFromIsland = 0.4
2 changes: 1 addition & 1 deletion cardinal/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
7 changes: 7 additions & 0 deletions cardinal/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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.
Expand All @@ -88,6 +93,8 @@ func MustInitWorld(w *cardinal.World) {
system.CreateEffectSystem,
system.EffectsSpawnerSystem,
system.RelocateBuildingSystem,
system.StartSailShipwreckSystem,
system.SailShip,
))
}

Expand Down
9 changes: 9 additions & 0 deletions cardinal/msg/sail_shipwreck.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package msg

type SailShipwreckMsg struct {
Player string `json:"player"`
}

type SailShipWreckResult struct {
Success bool `json:"success"`
}
30 changes: 30 additions & 0 deletions cardinal/query/all_ships.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
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{}

// AllShips -
func AllShips(world cardinal.WorldContext, _ *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
}
7 changes: 4 additions & 3 deletions cardinal/query/global_map.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
19 changes: 10 additions & 9 deletions cardinal/query/map_state.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
4 changes: 4 additions & 0 deletions cardinal/system/create_building.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down Expand Up @@ -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)
}
Expand Down
3 changes: 3 additions & 0 deletions cardinal/system/effects_spawner.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
64 changes: 64 additions & 0 deletions cardinal/system/sail_ship.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
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
}

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,
)

err := updateEffect(world, id, buildingComponent.Effect)
if 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}
}
Loading
Loading