ginger/gim/main.go

112 lines
2.1 KiB
Go
Raw Normal View History

2017-11-02 22:45:10 +00:00
package main
import (
"fmt"
"math/rand"
"os"
"strings"
"time"
"github.com/mediocregopher/ginger/gim/geo"
"github.com/mediocregopher/ginger/gim/terminal"
2017-11-02 22:45:10 +00:00
)
const (
framerate = 10
2017-11-02 22:45:10 +00:00
frameperiod = time.Second / time.Duration(framerate)
)
func debugf(str string, args ...interface{}) {
if !strings.HasSuffix(str, "\n") {
str += "\n"
}
2017-11-02 22:45:10 +00:00
fmt.Fprintf(os.Stderr, str, args...)
}
// TODO
// * Use actual gg graphs and not fake "boxes"
// - This will involve wrapping the vertices in some way, to preserve position
// * Once gg graphs are used we can use that birds-eye-view to make better
// decisions about edge placement
2017-11-02 22:45:10 +00:00
func main() {
rand.Seed(time.Now().UnixNano())
term := terminal.New()
2017-11-02 22:45:10 +00:00
type movingBox struct {
box
xRight bool
yDown bool
}
randBox := func() movingBox {
tsize := term.WindowSize()
2017-11-02 22:45:10 +00:00
return movingBox{
box: box{
pos: geo.XY{rand.Intn(tsize[0]), rand.Intn(tsize[1])},
size: geo.XY{30, 2},
2017-11-02 22:45:10 +00:00
},
xRight: rand.Intn(1) == 0,
yDown: rand.Intn(1) == 0,
}
}
boxes := []movingBox{
randBox(),
randBox(),
randBox(),
randBox(),
randBox(),
}
for range time.Tick(frameperiod) {
// update phase
termSize := term.WindowSize()
2017-11-02 22:45:10 +00:00
for i := range boxes {
b := &boxes[i]
b.body = fmt.Sprintf("%d) %v", i, b.rectCorner(geo.Left, geo.Up))
b.body += fmt.Sprintf(" | %v\n", b.rectCorner(geo.Right, geo.Up))
b.body += fmt.Sprintf(" %v", b.rectCorner(geo.Left, geo.Down))
b.body += fmt.Sprintf(" | %v", b.rectCorner(geo.Right, geo.Down))
2017-11-02 22:45:10 +00:00
size := b.rectSize()
2017-11-02 22:45:10 +00:00
if b.pos[0] <= 0 {
b.xRight = true
} else if b.pos[0]+size[0] >= termSize[0] {
2017-11-02 22:45:10 +00:00
b.xRight = false
}
if b.pos[1] <= 0 {
b.yDown = true
} else if b.pos[1]+size[1] >= termSize[1] {
2017-11-02 22:45:10 +00:00
b.yDown = false
}
if b.xRight {
b.pos[0] += 3
} else {
b.pos[0] -= 3
}
if b.yDown {
b.pos[1]++
} else {
b.pos[1]--
}
}
2017-11-02 22:45:10 +00:00
// draw phase
term.Reset()
for i := range boxes {
boxes[i].draw(term)
}
term.Flush()
for i := range boxes {
if i == 0 {
continue
}
basicLine(term, boxes[i-1].box, boxes[i].box)
2017-11-02 22:45:10 +00:00
}
term.Flush()
2017-11-02 22:45:10 +00:00
}
}