gg/cmd/test/trianlge.go

97 lines
1.5 KiB
Go

package main
import "vultras.su/core/gg"
//import "fmt"
var (
counter int
)
type Tri struct {
gg.DrawablePolygon
Spawned bool
}
func NewTri() *Tri {
ret := &Tri{}
ret.Transform.Scale = gg.V2(1)
ret.Triangles = gg.Triangles{
gg.Triangle{
gg.V(0, 10),
gg.V(100, 0),
gg.V(0, -10),
},
}
ret.Color = gg.Rgba(1, 1, 0, 1)
ret.Visible = true
ret.Layer = TriangleL
ret.Connect(player)
return ret
}
func (t *Tri) Update(c *Context) {
dt := c.DT()
if t.ContainsPoint(c.AbsCursorPosition()) {
t.Color = gg.Rgba(0, 1, 0, 1)
} else {
t.Color = gg.Rgba(1, 0, 1, 1)
}
if t.Spawned {
return
}
keys := c.Keys()
d := +1.
if c.IsPressed(gg.KeyShift) {
d = -1
}
for _, key := range keys { switch key {
case gg.KeyM:
absPos := t.AbsPosition()
t.SetAbsPosition(
absPos.Add(gg.V(0, 100*c.DT()*d)),
)
case gg.KeyN:
absPos := t.AbsPosition()
t.SetAbsPosition(
absPos.Add(gg.V(100*c.DT()*d, 0)),
)
case gg.KeyV:
t.Rotation += d * gg.Pi * 0.3 * dt
case gg.Key2 :
if t.Spawned {
break
}
t.Triangles = append(t.Triangles, gg.Triangle{
gg.V(0, 10 + gg.Float(counter)),
gg.V(100 + gg.Float(counter), 0),
gg.V(0, -10 - gg.Float(counter)),
})
case gg.Key3 :
if t.Spawned {
break
}
tt := *t
tt.Spawned = true
tt.Disconnect()
if c.Spawn(&tt) == nil {
counter++
}
}}
for _, event := range c.Events {
switch e := event.(type) { case *gg.KeyDown:
switch e.Key {
case gg.Key1 :
if t.Connected() {
t.Disconnect()
} else {
t.Connect(player)
}
}}}
}