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 #18

Merged
merged 6 commits into from
Jul 16, 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
10 changes: 10 additions & 0 deletions cardinal/component/position.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package component

type Position struct {
Island [2]float64 `json:"island"`
Shipwreck [2]float64 `json:"shipwreck"`
}

func (Position) Name() string {
return "Position"
}
46 changes: 46 additions & 0 deletions cardinal/component/shipwreck_resources.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package component

import (
"fmt"
"math/rand/v2"
"oceanus-shard/constants"
)

type ShipwreckResources struct {
Resources []Resource `json:"resources"`
}

func GetShipwreckResourceTypes() []ResourceType {
return []ResourceType{
Wood,
Stone,
Fish,
}
}

func randomIntInRange(min, max int) float64 {
if min >= max {
panic("min should be lower than max")
}
return float64(min) + rand.Float64()*float64(max-min)
}

func GetShipwreckDefaultResourceAmount(resourceType ResourceType) float64 {
switch resourceType {
case Wood:
return randomIntInRange(constants.ShipwreckMinWoodAmount, constants.ShipwreckMaxWoodAmount)
case Stone:
return randomIntInRange(constants.ShipwreckMinStoneAmount, constants.ShipwreckMaxStoneAmount)
case Fish:
return randomIntInRange(constants.ShipwreckMinFishAmount, constants.ShipwreckMaxFishAmount)
case Cotton:
case Iron:
default:
panic(fmt.Sprintf("unexpected component.ResourceType: %#v", resourceType))
}
return 0
}

func (ShipwreckResources) Name() string {
return "ShipwreckResources"
}
2 changes: 2 additions & 0 deletions cardinal/constants/buildings.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,5 @@ const (
UnitLimitHouseResourcesStoneAmount = 100
UnitLimitHouseUnitLimit = 15
)

const ShipwreckDistanceFromIsland = 0.8
6 changes: 5 additions & 1 deletion cardinal/constants/constants.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
package constants

import "time"
import (
"math"
"time"
)

const TickRate = time.Second
const TwoPi = 2 * math.Pi
16 changes: 16 additions & 0 deletions cardinal/constants/shipwreck.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package constants

const (
ShipwreckMinWoodAmount = 5
ShipwreckMaxWoodAmount = 10
)

const (
ShipwreckMinStoneAmount = 5
ShipwreckMaxStoneAmount = 10
)

const (
ShipwreckMinFishAmount = 5
ShipwreckMaxFishAmount = 10
)
2 changes: 1 addition & 1 deletion cardinal/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ go 1.22.1

require (
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 @@ -98,7 +99,6 @@ require (
go.uber.org/atomic v1.11.0 // indirect
go.uber.org/multierr v1.9.0 // indirect
golang.org/x/crypto v0.21.0 // indirect
golang.org/x/exp v0.0.0-20231110203233-9a3e6036ecaa // indirect
golang.org/x/mod v0.14.0 // indirect
golang.org/x/net v0.23.0 // indirect
golang.org/x/sys v0.19.0 // indirect
Expand Down
6 changes: 6 additions & 0 deletions cardinal/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,9 @@ func MustInitWorld(w *cardinal.World) {
cardinal.RegisterComponent[component.Farming](w),
cardinal.RegisterComponent[component.Building](w),
cardinal.RegisterComponent[component.PlayerResources](w),
cardinal.RegisterComponent[component.ShipwreckResources](w),
cardinal.RegisterComponent[component.TileMap](w),
cardinal.RegisterComponent[component.Position](w),
cardinal.RegisterComponent[component.Effect](w),
)

Expand Down Expand Up @@ -68,6 +70,10 @@ func MustInitWorld(w *cardinal.World) {
query.PlayerResourcesRequest,
query.PlayerResourcesResponse,
](w, "player-resources", query.PlayerResources),
cardinal.RegisterQuery[
query.GlobalMapRequest,
[]query.GlobalMapResponse,
](w, "global-map", query.GlobalMap),
)

// Each system executes deterministically in the order they are added.
Expand Down
52 changes: 52 additions & 0 deletions cardinal/query/global_map.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
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 GlobalMapRequest struct{}

type GlobalMapResponse struct {
Player string `json:"player"`
Island ResourcePoint `json:"island"`
Shipwreck ResourcePoint `json:"shipwreck"`
}

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

func GlobalMap(world cardinal.WorldContext, _ *GlobalMapRequest) (*[]GlobalMapResponse, error) {
playerEntityIDs, players, _ := system.QueryAllComponents[comp.Player](
world,
filter.Component[comp.Player](),
filter.Component[comp.TileMap](),
filter.Component[comp.Position](),
)

response := make([]GlobalMapResponse, len(playerEntityIDs))

for i, islandEntityID := range playerEntityIDs {
playerResources, _ := cardinal.GetComponent[comp.PlayerResources](world, islandEntityID)
shipwreckResources, _ := cardinal.GetComponent[comp.ShipwreckResources](world, islandEntityID)
position, _ := cardinal.GetComponent[comp.Position](world, islandEntityID)

response[i] = GlobalMapResponse{
Player: players[i].Nickname,
Island: ResourcePoint{
Position: position.Island,
Resources: playerResources.Resources,
},
Shipwreck: ResourcePoint{
Position: position.Shipwreck,
Resources: shipwreckResources.Resources,
},
}
}
return &response, nil
}
2 changes: 1 addition & 1 deletion cardinal/query/map_state.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ type MapStateResponse struct {
}

func PlayerMap(world cardinal.WorldContext, req *MapStateRequest) (*MapStateResponse, error) {
_, playerMap, err := system.QueryComponent[comp.TileMap](
_, playerMap, err := system.QueryPlayerComponent[comp.TileMap](
world,
req.Nickname,
filter.Component[comp.Player](),
Expand Down
6 changes: 3 additions & 3 deletions cardinal/query/player_resources.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,21 +20,21 @@ type PlayerResourcesResponse struct {
}

func PlayerResources(world cardinal.WorldContext, req *PlayerResourcesRequest) (*PlayerResourcesResponse, error) {
_, playerResources, _ := system.QueryComponent[comp.PlayerResources](
_, playerResources, _ := system.QueryPlayerComponent[comp.PlayerResources](
world,
req.Nickname,
filter.Component[comp.Player](),
filter.Component[comp.PlayerResources](),
)

_, farmingComponents, _ := system.QueryAllComponents[comp.Farming](
_, farmingComponents, _ := system.QueryAllPlayerComponents[comp.Farming](
world,
req.Nickname,
filter.Component[comp.Building](),
filter.Component[comp.Farming](),
)

_, allBuildings, err := system.QueryAllComponents[comp.Building](
_, allBuildings, err := system.QueryAllPlayerComponents[comp.Building](
world,
req.Nickname,
filter.Component[comp.Building](),
Expand Down
2 changes: 1 addition & 1 deletion cardinal/system/create_building.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ func CreateBuildingSystem(world cardinal.WorldContext) error {
return cardinal.EachMessage[msg.CreateBuildingMsg, msg.CreateBuildingResult](
world,
func(request cardinal.TxData[msg.CreateBuildingMsg]) (msg.CreateBuildingResult, error) {
mapEntityID, playerMap, _ := QueryComponent[comp.TileMap](
mapEntityID, playerMap, _ := QueryPlayerComponent[comp.TileMap](
world,
request.Tx.PersonaTag,
filter.Component[comp.Player](),
Expand Down
4 changes: 2 additions & 2 deletions cardinal/system/create_effect.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@ import (
func CreateEffectSystem(world cardinal.WorldContext) error {
return cardinal.EachMessage[msg.CreateEffectMsg, msg.CreateEffectResult](world,
func(request cardinal.TxData[msg.CreateEffectMsg]) (msg.CreateEffectResult, error) {
playerMapEntityID, playerMap, _ := QueryComponent[comp.TileMap](
playerMapEntityID, playerMap, _ := QueryPlayerComponent[comp.TileMap](
world,
request.Tx.PersonaTag,
filter.Component[comp.Player](),
filter.Component[comp.TileMap](),
)

playerBuildingsEntityIDs, playerBuildingsWithEffect, _ := QueryAllComponents[comp.Building](
playerBuildingsEntityIDs, playerBuildingsWithEffect, _ := QueryAllPlayerComponents[comp.Building](
world,
request.Tx.PersonaTag,
filter.Component[comp.Player](),
Expand Down
4 changes: 2 additions & 2 deletions cardinal/system/delete_building.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@ func DeleteBuildingSystem(world cardinal.WorldContext) error {
return cardinal.EachMessage[msg.DeleteBuildingMsg, msg.DeleteBuildingResult](
world,
func(request cardinal.TxData[msg.DeleteBuildingMsg]) (msg.DeleteBuildingResult, error) {
mapEntityID, playerMap, _ := QueryComponent[comp.TileMap](
mapEntityID, playerMap, _ := QueryPlayerComponent[comp.TileMap](
world,
request.Tx.PersonaTag,
filter.Component[comp.Player](),
filter.Component[comp.TileMap](),
)

buildingsEntityIDs, playerBuildings, _ := QueryAllComponents[comp.Building](
buildingsEntityIDs, playerBuildings, _ := QueryAllPlayerComponents[comp.Building](
world,
request.Tx.PersonaTag,
filter.Component[comp.Player](),
Expand Down
4 changes: 2 additions & 2 deletions cardinal/system/effects_spawner.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,14 @@ func EffectsSpawnerSystem(world cardinal.WorldContext) error {
return true
}

playerMapEntityID, playerMap, _ := QueryComponent[comp.TileMap](
playerMapEntityID, playerMap, _ := QueryPlayerComponent[comp.TileMap](
world,
playerComponent.Nickname,
filter.Component[comp.Player](),
filter.Component[comp.PlayerResources](),
)

playerResourcesEntityID, playerResources, _ := QueryComponent[comp.PlayerResources](
playerResourcesEntityID, playerResources, _ := QueryPlayerComponent[comp.PlayerResources](
world,
playerComponent.Nickname,
filter.Component[comp.Player](),
Expand Down
4 changes: 2 additions & 2 deletions cardinal/system/farming.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,14 @@ func FarmingSystem(world cardinal.WorldContext) error {
playerComponent, _ := cardinal.GetComponent[comp.Player](world, id)
farmingComponent, _ := cardinal.GetComponent[comp.Farming](world, id)

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

_, playerBuildings, err := QueryAllComponents[comp.Building](
_, playerBuildings, err := QueryAllPlayerComponents[comp.Building](
world,
playerComponent.Nickname,
filter.Component[comp.Player](),
Expand Down
69 changes: 69 additions & 0 deletions cardinal/system/global_map_utils.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package system

import (
"errors"
)

type Direction int

const (
Left Direction = 0
Top Direction = 1
Right Direction = 2
Bottom Direction = 3
)

const InitialRowLength = 2
const RowLengthIncrement = 2

func getNextDirection(current Direction) (next Direction) {
switch current {
case Left:
return Top
case Top:
return Right
case Right:
return Bottom
case Bottom:
return Left
default:
err := errors.New("unexpected Direction value")
panic(err)
}
}

func getNextPointPosition(currentDirection Direction, currentPoint [2]float64) (nextPoint [2]float64) {
switch currentDirection {
case Left:
return [2]float64{currentPoint[0] - 1, currentPoint[1]}
case Top:
return [2]float64{currentPoint[0], currentPoint[1] + 1}
case Right:
return [2]float64{currentPoint[0] + 1, currentPoint[1]}
case Bottom:
return [2]float64{currentPoint[0], currentPoint[1] - 1}
default:
err := errors.New("unexpected Direction value")
panic(err)
}
}

func getIslandCoordinates(n int) [2]float64 {
objectsInRow := InitialRowLength
direction := Left
currentIslandsInRow := 0
points := make([][2]float64, 0, n)
points = append(points, [2]float64{0, 0})
for range n {
if currentIslandsInRow == objectsInRow {
direction = getNextDirection(direction)
if direction == Left || direction == Right {
objectsInRow += RowLengthIncrement
}
currentIslandsInRow = 0
}
points = append(points, getNextPointPosition(direction, points[len(points)-1]))
currentIslandsInRow++
}
return points[n]
}
16 changes: 16 additions & 0 deletions cardinal/system/math_utils.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package system

import (
"math"
"oceanus-shard/constants"

"golang.org/x/exp/rand"
)

func getRandomPointOnCircle(x, y, r float64) [2]float64 {
angle := rand.Float64() * constants.TwoPi
var points [2]float64
points[0] = x + r*math.Cos(angle)
points[1] = y + r*math.Sin(angle)
return points
}
Loading
Loading