Skip to content

Commit 1664f9d

Browse files
committed
Rename throttle command to control
1 parent a60deb8 commit 1664f9d

File tree

4 files changed

+73
-45
lines changed

4 files changed

+73
-45
lines changed

events.cge

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ name hoverrace
22
version 0.4
33

44
config {
5-
// The speed at which the throttle reacts to user input. default = 1
5+
// The speed at which the thrust level reacts to user input. default = 1
66
throttle_speed: float,
77
// The speed at which hovercrafts turn. default = 220
88
turn_speed: float,
@@ -11,18 +11,20 @@ config {
1111
// The maximum velocity of hovercrafts. default = 20
1212
max_velocity: float,
1313
// The amount of checkpoints per game. default = 10
14-
checkpoint_count: int
14+
checkpoint_count: int,
15+
// The time in milliseconds that a game is allowed to last. default = infinite
16+
timeout: int
1517
}
1618

1719
// Send the `ready` command to the server when you think the game should begin.
1820
command ready {}
1921

20-
// The `throttle` command allows you to change your throttle level and direction.
22+
// The `control` command allows you to change your throttle level and direction.
2123
// **NOTE:** These values are targets. The hovercraft needs some time to reach the desired values.
22-
command throttle {
23-
// Throttle level between -1 - 1.
24-
level: float,
25-
// The angle in degrees the hovercraft should be facing (up = 0°).
24+
command control {
25+
// Thrust level between -1 and 1.
26+
thrust: float,
27+
// The angle in degrees the hovercraft should be facing (right = 0°).
2628
angle: float
2729
}
2830

@@ -69,14 +71,22 @@ event finished_players {
6971
players: list<finished_player>
7072
}
7173

74+
// The `game_over` event is sent when all players finished the game or the time runs out.
75+
event game_over {
76+
// The players that have finished the game before the time ran out.
77+
finished_players: list<finished_player>,
78+
// The IDs of the players that have not finished the game before the time ran out.
79+
unfinished_players: list<string>
80+
}
81+
7282
// A hovercraft is a circle with a diameter of 1 unit.
7383
type hovercraft {
7484
// The position of the center of the hovercraft.
7585
pos: vec,
7686
// The current velocity of the hovercraft.
7787
velocity: vec,
78-
// The current throttle of the hovercraft.
79-
throttle: float64,
88+
// The current thrust level of the hovercraft.
89+
thrust: float64,
8090
// The angle in degrees the hovercraft is facing (up = 0°).
8191
angle: float,
8292
// The amount of reached checkpoints.

hoverrace/event_definitions.go

Lines changed: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ package hoverrace
33
import "github.com/code-game-project/go-server/cg"
44

55
type GameConfig struct {
6-
// The speed at which the throttle reacts to user input. default = 1
6+
// The speed at which the thrust level reacts to user input. default = 1
77
ThrottleSpeed float64 `json:"throttle_speed"`
88
// The speed at which hovercrafts turn. default = 220
99
TurnSpeed float64 `json:"turn_speed"`
@@ -13,34 +13,39 @@ type GameConfig struct {
1313
MaxVelocity float64 `json:"max_velocity"`
1414
// The amount of checkpoints per game. default = 10
1515
CheckpointCount int `json:"checkpoint_count"`
16+
// The time in milliseconds that a game is allowed to last. default = infinite
17+
Timeout int `json:"timeout"`
1618
}
1719

1820
// Send the `ready` command to the server when you think the game should begin.
1921
const ReadyCmd cg.CommandName = "ready"
2022

21-
type ReadyCmdData struct{}
23+
type ReadyCmdData struct {
24+
}
2225

23-
// The `throttle` command allows you to change your throttle level and direction.
26+
// The `control` command allows you to change your throttle level and direction.
2427
// **NOTE:** These values are targets. The hovercraft needs some time to reach the desired values.
25-
const ThrottleCmd cg.CommandName = "throttle"
28+
const ControlCmd cg.CommandName = "control"
2629

27-
type ThrottleCmdData struct {
28-
// Throttle level between -1 - 1.
29-
Level float64 `json:"level"`
30-
// The angle in degrees the hovercraft should be facing (up = 0°).
30+
type ControlCmdData struct {
31+
// Thrust level between -1 and 1.
32+
Thrust float64 `json:"thrust"`
33+
// The angle in degrees the hovercraft should be facing (right = 0°).
3134
Angle float64 `json:"angle"`
3235
}
3336

3437
// The `start` event is sent when the race begins.
3538
// The game begins once at least 2 players have joined and all players have sent the `ready` event.
3639
const StartEvent cg.EventName = "start"
3740

38-
type StartEventData struct{}
41+
type StartEventData struct {
42+
}
3943

4044
// The `in_progress` event is sent to sockets which connect to the game while it's running.
4145
const InProgressEvent cg.EventName = "in_progress"
4246

43-
type InProgressEventData struct{}
47+
type InProgressEventData struct {
48+
}
4449

4550
// The `countdown` counts down from 5. When the value reaches 0 a `start` event will be sent instead of the `countdown` event.
4651
const CountdownEvent cg.EventName = "countdown"
@@ -88,14 +93,24 @@ type FinishedPlayersEventData struct {
8893
Players []FinishedPlayer `json:"players"`
8994
}
9095

96+
// The `game_over` event is sent when all players finished the game or the time runs out.
97+
const GameOverEvent cg.EventName = "game_over"
98+
99+
type GameOverEventData struct {
100+
// The players that have finished the game before the time ran out.
101+
FinishedPlayers []FinishedPlayer `json:"finished_players"`
102+
// The IDs of the players that have not finished the game before the time ran out.
103+
UnfinishedPlayers []string `json:"unfinished_players"`
104+
}
105+
91106
// A hovercraft is a circle with a diameter of 1 unit.
92107
type Hovercraft struct {
93108
// The position of the center of the hovercraft.
94109
Pos Vec `json:"pos"`
95110
// The current velocity of the hovercraft.
96111
Velocity Vec `json:"velocity"`
97-
// The current throttle of the hovercraft.
98-
Throttle float64 `json:"throttle"`
112+
// The current thrust level of the hovercraft.
113+
Thrust float64 `json:"thrust"`
99114
// The angle in degrees the hovercraft is facing (up = 0°).
100115
Angle float64 `json:"angle"`
101116
// The amount of reached checkpoints.

hoverrace/game.go

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,9 @@ func (g *Game) onPlayerLeft(player *cg.Player) {
9898
return
9999
}
100100

101+
delete(g.players, player.Id)
102+
delete(g.hovercrafts, player.Id)
103+
101104
if !g.running {
102105
g.positionHovercrafts()
103106
for _, p := range g.players {
@@ -207,8 +210,8 @@ func (g *Game) handleCommand(origin *cg.Player, cmd cg.Command) {
207210
switch cmd.Name {
208211
case ReadyCmd:
209212
g.handleReady(origin.Id)
210-
case ThrottleCmd:
211-
g.handleThrottle(origin.Id, cmd)
213+
case ControlCmd:
214+
g.handleControl(origin.Id, cmd)
212215
default:
213216
origin.Log.ErrorData(cmd, fmt.Sprintf("unexpected command: %s", cmd.Name))
214217
}
@@ -239,24 +242,24 @@ func (g *Game) handleReady(playerId string) {
239242
}
240243
}
241244

242-
func (g *Game) handleThrottle(playerId string, cmd cg.Command) {
245+
func (g *Game) handleControl(playerId string, cmd cg.Command) {
243246
if !g.running {
244247
return
245248
}
246249

247-
var data ThrottleCmdData
250+
var data ControlCmdData
248251
cmd.UnmarshalData(&data)
249252

250-
if data.Level > 1 {
251-
data.Level = 1
252-
} else if data.Level < -1 {
253-
data.Level = -1
253+
if data.Thrust > 1 {
254+
data.Thrust = 1
255+
} else if data.Thrust < -1 {
256+
data.Thrust = -1
254257
}
255258

256259
data.Angle = NormalizeAngle(data.Angle)
257260

258261
player := g.players[playerId]
259-
player.targetThrottle = data.Level
262+
player.targetThrust = data.Thrust
260263
player.targetAngle = data.Angle
261264
}
262265

@@ -270,8 +273,8 @@ func (g *Game) start() {
270273
player.acc = Vec{}
271274
player.angle = 0
272275
player.targetAngle = 0
273-
player.throttle = 0
274-
player.targetThrottle = 0
276+
player.thrust = 0
277+
player.targetThrust = 0
275278

276279
player.finished = false
277280
player.checkpoints = make([]Vec, len(g.checkpoints))

hoverrace/player.go

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ type Player struct {
1717

1818
checkpoints []Vec
1919

20-
throttle float64
21-
targetThrottle float64
20+
thrust float64
21+
targetThrust float64
2222

2323
angle float64
2424
targetAngle float64
@@ -43,23 +43,23 @@ func (p *Player) update(delta time.Duration) {
4343
Pos: p.pos,
4444
Checkpoints: len(p.checkpoints),
4545
Velocity: p.vel,
46-
Throttle: p.throttle,
46+
Thrust: p.thrust,
4747
Angle: p.angle,
4848
}
4949
}
5050
}
5151

5252
func (p *Player) move(delta time.Duration) {
5353
if !p.finished && p.game.running {
54-
if p.targetThrottle > p.throttle {
55-
p.throttle += p.game.config.ThrottleSpeed * delta.Seconds()
56-
if p.throttle > p.targetThrottle {
57-
p.throttle = p.targetThrottle
54+
if p.targetThrust > p.thrust {
55+
p.thrust += p.game.config.ThrottleSpeed * delta.Seconds()
56+
if p.thrust > p.targetThrust {
57+
p.thrust = p.targetThrust
5858
}
59-
} else if p.targetThrottle < p.throttle {
60-
p.throttle -= p.game.config.ThrottleSpeed * delta.Seconds()
61-
if p.throttle < p.targetThrottle {
62-
p.throttle = p.targetThrottle
59+
} else if p.targetThrust < p.thrust {
60+
p.thrust -= p.game.config.ThrottleSpeed * delta.Seconds()
61+
if p.thrust < p.targetThrust {
62+
p.thrust = p.targetThrust
6363
}
6464
}
6565

@@ -75,9 +75,9 @@ func (p *Player) move(delta time.Duration) {
7575
}
7676
}
7777

78-
p.acc = VecFromAngle(p.angle).Mul(p.game.config.MaxAcceleration * p.throttle)
78+
p.acc = VecFromAngle(p.angle).Mul(p.game.config.MaxAcceleration * p.thrust)
7979
} else {
80-
p.throttle = 0
80+
p.thrust = 0
8181

8282
if p.vel.MagnitudeSquared() != 0 {
8383
velMag := p.vel.Magnitude()

0 commit comments

Comments
 (0)