Merge pull request #1 from surdeus/master

Implemented polygons.
This commit is contained in:
Andreo Parhomenka 2023-06-20 16:08:16 +05:00 committed by GitHub
commit f081e9e6b4
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 43 additions and 12 deletions

View file

@ -24,12 +24,13 @@ type Rect struct {
} }
type Tri struct { type Tri struct {
*gx.DrawableTriangles *gx.DrawablePolygon
} }
func NewTri() *Tri { func NewTri() *Tri {
ret := &Tri{} ret := &Tri{}
ret.DrawableTriangles = &gx.DrawableTriangles{} ret.DrawablePolygon = &gx.DrawablePolygon{}
ret.Transform.S = gx.V(1, 1)
ret.Triangles = gx.Triangles{ ret.Triangles = gx.Triangles{
gx.Triangle{ gx.Triangle{
@ -88,6 +89,7 @@ var (
player *Player player *Player
rectMove gx.Rectangle rectMove gx.Rectangle
rect *Rect rect *Rect
tri *Tri
) )
func NewPlayer() *Player { func NewPlayer() *Player {
@ -190,6 +192,12 @@ func (p *Player) Update(e *gx.Engine) error {
} else { } else {
c.RA.Y += gx.Pi * p.MoveSpeed * dt c.RA.Y += gx.Pi * p.MoveSpeed * dt
} }
case ebiten.KeyV :
if e.KeyIsPressed(ebiten.KeyShift) {
tri.R -= gx.Pi * 0.3 * dt
} else {
tri.R += gx.Pi * 0.3 * dt
}
case ebiten.KeyLeftBracket : case ebiten.KeyLeftBracket :
if e.KeyIsPressed(ebiten.KeyShift) { if e.KeyIsPressed(ebiten.KeyShift) {
rect.R -= gx.Pi * 0.3 * dt rect.R -= gx.Pi * 0.3 * dt
@ -241,7 +249,7 @@ func main() {
player = NewPlayer() player = NewPlayer()
rect = NewRect() rect = NewRect()
tri := NewTri() tri = NewTri()
e.Add(1, &Debug{}) e.Add(1, &Debug{})
e.Add(0, player) e.Add(0, player)

View file

@ -1,29 +1,52 @@
package gx package gx
import (
)
type PolygonTriangle struct { type PolygonTriangle struct {
P, S int T, S int
} }
// Grouped triangles type.
type Polygon struct { type Polygon struct {
Transform Transform
base Triangle Triangles
triangles []PolygonTriangle
} }
// Polygon that can be drawn.
type DrawablePolygon struct { type DrawablePolygon struct {
Polygon Polygon
ShaderOptions ShaderOptions
Visibility
Colority
} }
func NewPolygon(base Triangle) *Polygon { func (p *Polygon) MakeTriangles() Triangles {
ret := &Polygon{ mv := p.Matrix()
Transform: T(), m := &mv
base: base,
ret := make(Triangles, len(p.Triangles))
for i, t := range p.Triangles {
ret[i] = Triangle{
t[0].Apply(m),
t[1].Apply(m),
t[2].Apply(m),
}
} }
return ret return ret
} }
func (p *Polygon) Triangles() Triangles { func (p *DrawablePolygon) Draw(
return Triangles{} e *Engine,
i *Image,
) {
(&DrawableTriangles{
Visibility: p.Visibility,
Colority: p.Colority,
ShaderOptions: p.ShaderOptions,
Triangles: p.MakeTriangles(),
}).Draw(e, i)
} }