gg/cmd/test/trianlge.go

102 lines
1.5 KiB
Go

package main
import "surdeus.su/core/gg"
import "surdeus.su/core/gg/ox"
import "surdeus.su/core/gg/mx"
//import "fmt"
import "log"
var (
counter int
)
type Tri struct {
ox.DrawablePolygon
Spawned bool
}
func NewTri(t *ox.Transform) *Tri {
ret := &Tri{}
ret.Transform = *t
ret.Triangles = mx.Triangles{
mx.Triangle{
mx.V(0, 10),
mx.V(100, 0),
mx.V(0, -10),
},
}
ret.Color = gg.RGBA(1, 1, 0, 1)
ret.Visible = true
ret.Layer = LayerTriangle
return ret
}
func (t *Tri) GetTags(c Context) gg.TagMap {
tags := gg.TagMap{
TagTriangle: struct{}{},
}
if t.IsConnected() {
tags[TagConnectedToPlayer] = struct{}{}
}
return tags
}
func (t *Tri) OnStart(c Context) {
log.Println("added a tri")
}
func (t *Tri) OnUpdate(c Context) {
e := c.Engine()
dt := e.DT().Seconds()
keys := e.GetKeyboardKeys()
d := +1.
if e.IsPressed(gg.KeyShift) {
d = -1
}
for _, key := range keys {
switch key {
case gg.KeyM:
t.Move(mx.VY(100 * dt * d))
case gg.KeyN:
t.Move(mx.VX(100 * dt * d))
case gg.KeyV:
t.Rotate(d * mx.Pi * 0.3 * dt)
case gg.Key2:
if t.Spawned {
break
}
case gg.Key3:
if t.Spawned {
break
}
tt := *t
tt.Spawned = true
tt.Disconnect()
if e.Spawn(&tt) {
counter++
}
}
}
for _, down := range c.Events().Keyboard.Downs {
switch down.Key {
case gg.Key1:
if t.IsConnected() {
t.Disconnect()
} else {
t.Connect(player)
}
}
}
}
func (tri *Tri) Draw(c Context) *gg.Drawing {
return tri.DrawablePolygon.Draw(c)
}