travel progress indicator

master
Jordan Orelli 6 years ago
parent 8cdd7bdaa6
commit aa6bd1ca9e

@ -104,6 +104,7 @@ are farther away take longer to communicate with.
} }
type status struct { type status struct {
GameCode string
State string State string
Balance int Balance int
Bombs int Bombs int
@ -113,7 +114,8 @@ type status struct {
} }
var statusTemplate = template.Must(template.New("status").Parse(` var statusTemplate = template.Must(template.New("status").Parse(`
******************************************************************************** --------------------------------------------------------------------------------
Current Game: {{.GameCode}}
Current State: {{.State}} Current State: {{.State}}
Balance: {{.Balance}} Balance: {{.Balance}}
Bombs: {{.Bombs}} Bombs: {{.Bombs}}
@ -121,7 +123,7 @@ Kills: {{.Kills}}
Location: {{.Location}} Location: {{.Location}}
{{.Description}} {{.Description}}
********************************************************************************
`)) `))
var statusCommand = Command{ var statusCommand = Command{

@ -59,18 +59,7 @@ func (c *Connection) RunCommand(name string, args ...string) {
}() }()
switch name { switch name {
case "commands": case "commands":
c.Line() c.ListCommands()
commands := c.Commands()
names := make([]string, len(commands))
for i := range commands {
names[i] = commands[i].name
}
sort.Strings(names)
for _, name := range names {
cmd := c.GetCommand(name)
c.Printf("%-20s%s\n", name, cmd.help)
}
c.Line()
return return
} }
@ -82,6 +71,24 @@ func (c *Connection) RunCommand(name string, args ...string) {
cmd.handler(c, args...) cmd.handler(c, args...)
} }
func (c *Connection) ListCommands() {
c.Printf("\n")
c.Line()
c.Printf("- Available Commands\n")
c.Line()
commands := c.Commands()
names := make([]string, len(commands))
for i := range commands {
names[i] = commands[i].name
}
sort.Strings(names)
for _, name := range names {
cmd := c.GetCommand(name)
c.Printf("%-20s%s\n", name, cmd.help)
}
c.Printf("\n")
}
func (c *Connection) SetState(s ConnectionState) { func (c *Connection) SetState(s ConnectionState) {
if c.ConnectionState == s { if c.ConnectionState == s {
return return
@ -92,8 +99,8 @@ func (c *Connection) SetState(s ConnectionState) {
c.ConnectionState.Exit(c) c.ConnectionState.Exit(c)
} }
log_info("enter state: %v", s) log_info("enter state: %v", s)
s.Enter(c)
c.ConnectionState = s c.ConnectionState = s
s.Enter(c)
} }
func (c *Connection) ReadLines(out chan []string) { func (c *Connection) ReadLines(out chan []string) {

@ -32,6 +32,7 @@ var banner = `
O * ' . O * ' .
A game of dark cunning in the vast unknown of space by Jordan Orelli.
############################################################################################## ##############################################################################################
` `
@ -87,7 +88,7 @@ func (st *LobbyState) Enter(c *Connection) {
} }
break break
} }
c.ListCommands()
} }
func (st *LobbyState) Tick(c *Connection, frame int64) ConnectionState { return st } func (st *LobbyState) Tick(c *Connection, frame int64) ConnectionState { return st }

@ -1,6 +1,7 @@
package main package main
import ( import (
"bytes"
"fmt" "fmt"
"text/template" "text/template"
"time" "time"
@ -45,8 +46,6 @@ var enterTravelTemplate = template.Must(template.New("enter-travel").Parse(`
Departing: {{.Departing}} Departing: {{.Departing}}
Destination: {{.Destination}} Destination: {{.Destination}}
Total Trip Time: {{.Duration}} Total Trip Time: {{.Duration}}
Current Time: {{.Time}}
ETA: {{.ETA}}
`)) `))
func (t *TravelState) Enter(c *Connection) { func (t *TravelState) Enter(c *Connection) {
@ -54,20 +53,45 @@ func (t *TravelState) Enter(c *Connection) {
Departing *System Departing *System
Destination *System Destination *System
Duration time.Duration Duration time.Duration
Time time.Time
ETA time.Time
}{ }{
t.start, t.start,
t.dest, t.dest,
t.tripTime(), t.tripTime(),
time.Now(),
t.eta(),
}) })
t.start.Leave(c) t.start.Leave(c)
} }
func (t *TravelState) Tick(c *Connection, frame int64) ConnectionState { func (t *TravelState) Tick(c *Connection, frame int64) ConnectionState {
t.travelled += options.playerSpeed * options.lightSpeed dt := options.playerSpeed * options.lightSpeed
segmentLength := t.dist / 18
x := t.travelled
for x > segmentLength {
x -= segmentLength
}
if x < dt {
c.Printf("%v", t.start.name)
var buf bytes.Buffer
segment := int(t.travelled / t.dist * 18)
buf.WriteRune('|')
for i := 0; i < 18; i++ {
switch {
case i == segment:
buf.WriteRune('>')
case i == segment-1:
buf.WriteRune('=')
case i < segment:
buf.WriteRune('-')
default:
buf.WriteRune(' ')
}
}
buf.WriteRune('|')
c.Write(buf.Bytes())
c.Printf("at %v in %v\n", t.dest.name, t.remaining())
}
t.travelled += dt
if t.travelled >= t.dist { if t.travelled >= t.dist {
return Idle(t.dest) return Idle(t.dest)
} }
@ -86,6 +110,7 @@ func (t *TravelState) String() string {
func (t *TravelState) PrintStatus(c *Connection) { func (t *TravelState) PrintStatus(c *Connection) {
desc := fmt.Sprintf("Traveling from %v to %v", t.start, t.dest) desc := fmt.Sprintf("Traveling from %v to %v", t.start, t.dest)
statusTemplate.Execute(c, status{ statusTemplate.Execute(c, status{
GameCode: c.game.id,
State: "In Transit", State: "In Transit",
Balance: c.money, Balance: c.money,
Bombs: c.bombs, Bombs: c.bombs,

Loading…
Cancel
Save