Skip to content

Commit f081c4c

Browse files
committed
Add game config
1 parent 0964b50 commit f081c4c

File tree

5 files changed

+51
-20
lines changed

5 files changed

+51
-20
lines changed

events.cge

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,19 @@
11
name hoverrace
22
version 0.4
33

4+
config {
5+
// The speed at which the throttle reacts to user input. default = 1
6+
throttle_speed: float,
7+
// The speed at which hovercrafts turn. default = 220
8+
turn_speed: float,
9+
// The maximum acceleration of hovercrafts. default = 5
10+
max_acceleration: float,
11+
// The maximum velocity of hovercrafts. default = 20
12+
max_velocity: float,
13+
// The amount of checkpoints per game. default = 10
14+
checkpoint_count: int
15+
}
16+
417
// Send the `ready` command to the server when you think the game should begin.
518
command ready {}
619

hoverrace/event_definitions.go

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,19 @@ package hoverrace
22

33
import "github.com/code-game-project/go-server/cg"
44

5+
type GameConfig struct {
6+
// The speed at which the throttle reacts to user input. default = 1
7+
ThrottleSpeed float64 `json:"throttle_speed"`
8+
// The speed at which hovercrafts turn. default = 220
9+
TurnSpeed float64 `json:"turn_speed"`
10+
// The maximum acceleration of hovercrafts. default = 5
11+
MaxAcceleration float64 `json:"max_acceleration"`
12+
// The maximum velocity of hovercrafts. default = 20
13+
MaxVelocity float64 `json:"max_velocity"`
14+
// The amount of checkpoints per game. default = 10
15+
CheckpointCount int `json:"checkpoint_count"`
16+
}
17+
518
// Send the `ready` command to the server when you think the game should begin.
619
const ReadyCmd cg.CommandName = "ready"
720

@@ -106,5 +119,3 @@ type Vec struct {
106119
// bottom to top
107120
Y float64 `json:"y"`
108121
}
109-
110-
type GameConfig struct{}

hoverrace/game.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ func NewGame(cgGame *cg.Game, config GameConfig) *Game {
2929
players: make(map[string]*Player),
3030
hovercrafts: make(map[string]Hovercraft),
3131
finishedPlayers: make([]FinishedPlayer, 0),
32-
checkpoints: make([]Vec, 0, 10),
32+
checkpoints: make([]Vec, 0, config.CheckpointCount),
3333
}
3434
game.cg.OnPlayerJoined = game.onPlayerJoined
3535
game.cg.OnPlayerLeft = game.onPlayerLeft

hoverrace/player.go

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,6 @@ import (
77
"github.com/code-game-project/go-server/cg"
88
)
99

10-
const (
11-
throttleSpeed = 1
12-
turnSpeed = 220
13-
maxAcceleration = 5
14-
maxVelocity = 20
15-
)
16-
1710
type Player struct {
1811
id string
1912
username string
@@ -59,30 +52,30 @@ func (p *Player) update(delta time.Duration) {
5952
func (p *Player) move(delta time.Duration) {
6053
if !p.finished && p.game.running {
6154
if p.targetThrottle > p.throttle {
62-
p.throttle += throttleSpeed * delta.Seconds()
55+
p.throttle += p.game.config.ThrottleSpeed * delta.Seconds()
6356
if p.throttle > p.targetThrottle {
6457
p.throttle = p.targetThrottle
6558
}
6659
} else if p.targetThrottle < p.throttle {
67-
p.throttle -= throttleSpeed * delta.Seconds()
60+
p.throttle -= p.game.config.ThrottleSpeed * delta.Seconds()
6861
if p.throttle < p.targetThrottle {
6962
p.throttle = p.targetThrottle
7063
}
7164
}
7265

7366
if p.targetAngle > p.angle {
74-
p.angle += turnSpeed * delta.Seconds()
67+
p.angle += p.game.config.TurnSpeed * delta.Seconds()
7568
if p.angle > p.targetAngle {
7669
p.angle = p.targetAngle
7770
}
7871
} else if p.targetAngle < p.angle {
79-
p.angle -= turnSpeed * delta.Seconds()
72+
p.angle -= p.game.config.TurnSpeed * delta.Seconds()
8073
if p.angle < p.targetAngle {
8174
p.angle = p.targetAngle
8275
}
8376
}
8477

85-
p.acc = VecFromAngle(p.angle).Mul(maxAcceleration * p.throttle)
78+
p.acc = VecFromAngle(p.angle).Mul(p.game.config.MaxAcceleration * p.throttle)
8679
} else {
8780
p.throttle = 0
8881

@@ -92,7 +85,7 @@ func (p *Player) move(delta time.Duration) {
9285
p.acc = Vec{
9386
X: -p.vel.X / velMag,
9487
Y: -p.vel.Y / velMag,
95-
}.Mul(math.Min(maxAcceleration, math.Abs(velMag)))
88+
}.Mul(math.Min(p.game.config.MaxAcceleration, math.Abs(velMag)))
9689
}
9790

9891
if p.vel.MagnitudeSquared() < 0.01 {
@@ -136,7 +129,7 @@ outer:
136129
p.game.finishedPlayers = append(p.game.finishedPlayers, FinishedPlayer{
137130
Id: p.id,
138131
Place: len(p.game.finishedPlayers) + 1,
139-
Duration: time.Now().Sub(p.game.startTime).Milliseconds(),
132+
Duration: time.Since(p.game.startTime).Milliseconds(),
140133
})
141134
p.game.cg.Send(FinishedPlayersEvent, FinishedPlayersEventData{
142135
Players: p.game.finishedPlayers,

main.go

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,25 @@ func main() {
4444
server.Run(func(cgGame *cg.Game, config json.RawMessage) {
4545
var gameConfig hoverrace.GameConfig
4646
err := json.Unmarshal(config, &gameConfig)
47-
if err == nil {
48-
cgGame.SetConfig(gameConfig)
49-
} else {
47+
if err != nil {
5048
cgGame.Log.Error("Failed to unmarshal game config: %s", err)
5149
}
50+
if gameConfig.ThrottleSpeed <= 0 {
51+
gameConfig.ThrottleSpeed = 1
52+
}
53+
if gameConfig.TurnSpeed <= 0 {
54+
gameConfig.TurnSpeed = 220
55+
}
56+
if gameConfig.MaxAcceleration <= 0 {
57+
gameConfig.MaxAcceleration = 5
58+
}
59+
if gameConfig.MaxVelocity <= 0 {
60+
gameConfig.MaxVelocity = 20
61+
}
62+
if gameConfig.CheckpointCount <= 0 {
63+
gameConfig.CheckpointCount = 10
64+
}
65+
cgGame.SetConfig(gameConfig)
5266

5367
hoverrace.NewGame(cgGame, gameConfig).Run()
5468
})

0 commit comments

Comments
 (0)