2017-11-02 22:45:10 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"math/rand"
|
|
|
|
"os"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
2017-11-04 21:29:15 +00:00
|
|
|
"github.com/mediocregopher/ginger/gim/geo"
|
|
|
|
"github.com/mediocregopher/ginger/gim/terminal"
|
2017-11-02 22:45:10 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2017-11-04 21:29:15 +00:00
|
|
|
framerate = 10
|
2017-11-02 22:45:10 +00:00
|
|
|
frameperiod = time.Second / time.Duration(framerate)
|
|
|
|
)
|
|
|
|
|
|
|
|
func debugf(str string, args ...interface{}) {
|
2017-11-04 21:29:15 +00:00
|
|
|
if !strings.HasSuffix(str, "\n") {
|
|
|
|
str += "\n"
|
|
|
|
}
|
2017-11-02 22:45:10 +00:00
|
|
|
fmt.Fprintf(os.Stderr, str, args...)
|
|
|
|
}
|
|
|
|
|
2017-11-04 21:29:15 +00:00
|
|
|
// 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())
|
2017-11-04 21:29:15 +00:00
|
|
|
term := terminal.New()
|
2017-11-02 22:45:10 +00:00
|
|
|
|
|
|
|
type movingBox struct {
|
|
|
|
box
|
|
|
|
xRight bool
|
|
|
|
yDown bool
|
|
|
|
}
|
|
|
|
|
|
|
|
randBox := func() movingBox {
|
2017-11-04 21:29:15 +00:00
|
|
|
tsize := term.WindowSize()
|
2017-11-02 22:45:10 +00:00
|
|
|
return movingBox{
|
|
|
|
box: box{
|
2017-11-04 21:29:15 +00:00
|
|
|
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) {
|
|
|
|
|
2017-11-04 21:29:15 +00:00
|
|
|
// update phase
|
|
|
|
termSize := term.WindowSize()
|
2017-11-02 22:45:10 +00:00
|
|
|
for i := range boxes {
|
|
|
|
b := &boxes[i]
|
2017-11-04 21:29:15 +00:00
|
|
|
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
|
|
|
|
2017-11-04 21:29:15 +00:00
|
|
|
size := b.rectSize()
|
2017-11-02 22:45:10 +00:00
|
|
|
if b.pos[0] <= 0 {
|
|
|
|
b.xRight = true
|
2017-11-04 21:29:15 +00:00
|
|
|
} 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
|
2017-11-04 21:29:15 +00:00
|
|
|
} 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-04 21:29:15 +00:00
|
|
|
}
|
2017-11-02 22:45:10 +00:00
|
|
|
|
2017-11-04 21:29:15 +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
|
|
|
}
|
2017-11-04 21:29:15 +00:00
|
|
|
term.Flush()
|
2017-11-02 22:45:10 +00:00
|
|
|
}
|
|
|
|
}
|