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/raft #14

Merged
merged 5 commits into from
Jul 8, 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: 8 additions & 8 deletions cardinal/component/building.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ func GetAllBuildingTypes() []BuildingType {
}

type Building struct {
TileID int `json:"tileID"`
Level int `json:"level"`
Type BuildingType `json:"type"`
Farming *Farming `json:"farming,omitempty"`
Expand All @@ -43,6 +44,7 @@ func (Building) Name() string {
}

type BuildingConstants struct {
TileID int
UnitLimit int
StorageCapacity int
Resources []Resource
Expand All @@ -53,6 +55,7 @@ type BuildingConstants struct {

var BuildingConfigs = map[BuildingType]BuildingConstants{
Main: {
TileID: constants.MainTileID,
Resources: []Resource{},
UnitLimit: constants.MainUnitLimit,
StorageCapacity: constants.MainStorageCapacity,
Expand Down Expand Up @@ -100,6 +103,7 @@ var BuildingConfigs = map[BuildingType]BuildingConstants{
{Type: Wood, Amount: constants.ShipyardEffectRaftResourceWood},
{Type: Fish, Amount: constants.ShipyardEffectRaftResourceFish},
},
BuildingTimeSeconds: constants.ShipyardEffectRaftBuildSeconds,
},
TileType: CoastlineTile,
},
Expand Down Expand Up @@ -128,6 +132,7 @@ func GetBuilding(buildingType BuildingType) (Building, error) {
switch buildingType {
case Main:
return Building{
TileID: config.TileID,
Level: 1,
Type: buildingType,
UnitLimit: config.UnitLimit,
Expand All @@ -153,14 +158,9 @@ func GetBuilding(buildingType BuildingType) (Building, error) {
}, nil
case Shipyard:
return Building{
Level: 1,
Type: buildingType,
Effect: &Effect{
Type: config.Effect.Type,
Amount: 0,
Capacity: config.Effect.Capacity,
Resources: config.Effect.Resources,
},
Level: 1,
Type: buildingType,
Effect: config.Effect,
}, nil
case Warehouse:
return Building{
Expand Down
21 changes: 17 additions & 4 deletions cardinal/component/effect.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package component

import "oceanus-shard/constants"

type EffectType string

const (
Expand All @@ -12,11 +14,22 @@ func GetAllEffectTypes() []EffectType {
}
}

func GetCapacityByEffectType(effectType EffectType) int {
switch effectType {
case Raft:
return constants.ShipyardEffectRaftCapacity
default:
return 0
}
}

type Effect struct {
Type EffectType `json:"type"`
Amount int `json:"amount"`
Capacity int `json:"capacity"`
Resources []Resource `json:"resources,omitempty"`
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"`
}

func (Effect) Name() string {
Expand Down
2 changes: 2 additions & 0 deletions cardinal/constants/buildings.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package constants

const (
MainTileID = 22
MainUnitLimit = 5
// MainStorageCapacity = 10
MainStorageCapacity = 100
Expand Down Expand Up @@ -31,6 +32,7 @@ const (
ShipyardEffectRaftCapacity = 1
ShipyardEffectRaftResourceWood = 10
ShipyardEffectRaftResourceFish = 20
ShipyardEffectRaftBuildSeconds = 60
)

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

require (
github.com/rs/zerolog v1.32.0
gotest.tools/v3 v3.5.1
pkg.world.dev/world-engine/cardinal v1.5.1
)

Expand Down Expand Up @@ -113,6 +112,7 @@ require (
gopkg.in/DataDog/dd-trace-go.v1 v1.63.1 // indirect
gopkg.in/ini.v1 v1.67.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
gotest.tools/v3 v3.5.1 // indirect
pkg.world.dev/world-engine/rift v1.1.0-beta.0.20240402214846-de1fc179818a // indirect
pkg.world.dev/world-engine/sign v1.0.1-beta // indirect
)
9 changes: 3 additions & 6 deletions cardinal/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,18 +47,14 @@ func MustInitWorld(w *cardinal.World) {
// NOTE: You must register your transactions here for it to be executed.
Must(
cardinal.RegisterMessage[msg.CreatePlayerMsg, msg.CreatePlayerResult](w, "create-player"),
cardinal.RegisterMessage[msg.AttackPlayerMsg, msg.AttackPlayerMsgReply](w, "attack-player"),
cardinal.RegisterMessage[msg.CreateBuildingMsg, msg.CreateBuildingResult](w, "create-building"),
cardinal.RegisterMessage[msg.DeleteBuildingMsg, msg.DeleteBuildingResult](w, "delete-building"),
cardinal.RegisterMessage[msg.CreateEffectMsg, msg.CreateEffectResult](w, "create-effect"),
)

// Register queries
// NOTE: You must register your queries here for it to be accessible.
Must(
cardinal.RegisterQuery[
query.PlayerHealthRequest,
query.PlayerHealthResponse,
](w, "player-health", query.PlayerHealth),
cardinal.RegisterQuery[
query.MapStateRequest,
query.MapStateResponse,
Expand All @@ -78,11 +74,12 @@ func MustInitWorld(w *cardinal.World) {
// For example, you may want to run the attack system before the regen system
// so that the player's HP is subtracted (and player killed if it reaches 0) before HP is regenerated.
Must(cardinal.RegisterSystems(w,
system.AttackSystem,
system.FarmingSystem,
system.PlayerSpawnerSystem,
system.CreateBuildingSystem,
system.DeleteBuildingSystem,
system.CreateEffectSystem,
system.EffectsSpawnerSystem,
))
}

Expand Down
9 changes: 0 additions & 9 deletions cardinal/msg/attack_player.go

This file was deleted.

10 changes: 10 additions & 0 deletions cardinal/msg/create_effect.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package msg

type CreateEffectMsg struct {
EffectType string `json:"effectType"`
TileIndex int `json:"tileIndex"`
}

type CreateEffectResult struct {
Success bool `json:"success"`
}
58 changes: 0 additions & 58 deletions cardinal/query/player_health.go

This file was deleted.

38 changes: 0 additions & 38 deletions cardinal/system/attack.go

This file was deleted.

24 changes: 9 additions & 15 deletions cardinal/system/create_building.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,18 +35,20 @@ func CreateBuildingSystem(world cardinal.WorldContext) error {
fmt.Errorf("failed to create building, this building doesn't fit this tiletype")
}

if err := SubtractResourcesToBuild(world, building, request.Tx.PersonaTag); err != nil {
return msg.CreateBuildingResult{Success: false}, err
}

tile := &(*playerMap.Tiles)[request.Msg.TileIndex]
if tile.Building == nil {
building.TileID = request.Msg.TileIndex
tile.Building = &building
} else {
return msg.CreateBuildingResult{Success: false},
fmt.Errorf("failed to create building, this tile already have another building")
}

resourcesForBuild := comp.BuildingConfigs[building.Type].Resources
if err := SubtractResources(world, resourcesForBuild, request.Tx.PersonaTag); err != nil {
return msg.CreateBuildingResult{Success: false}, err
}

player, _ := cardinal.GetComponent[comp.Player](world, mapEntityID)

if err := cardinal.SetComponent(world, mapEntityID, playerMap); err != nil {
Expand All @@ -55,14 +57,7 @@ func CreateBuildingSystem(world cardinal.WorldContext) error {

buildingEntityID, _ := cardinal.Create(world,
player,
comp.Building{
Level: building.Level,
Type: building.Type,
Farming: building.Farming,
Effect: building.Effect,
UnitLimit: building.UnitLimit,
StorageCapacity: building.StorageCapacity,
},
building,
)

if building.Farming != nil {
Expand All @@ -83,16 +78,15 @@ func CreateBuildingSystem(world cardinal.WorldContext) error {
})
}

func SubtractResourcesToBuild(world cardinal.WorldContext, building comp.Building, personaTag string) error {
func SubtractResources(world cardinal.WorldContext, resources []comp.Resource, personaTag string) error {
playerResourcesEntityID, playerResources, _ := QueryComponent[comp.PlayerResources](
world,
personaTag,
filter.Component[comp.Player](),
filter.Component[comp.PlayerResources](),
)

resourcesToBuild := comp.BuildingConfigs[building.Type].Resources
for _, resource := range resourcesToBuild {
for _, resource := range resources {
var playerResource *comp.Resource
var err error
if playerResource, err = GetResourceByType(playerResources, resource.Type); err != nil {
Expand Down
Loading
Loading