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 {
*gx.DrawableTriangles
*gx.DrawablePolygon
}
func NewTri() *Tri {
ret := &Tri{}
ret.DrawableTriangles = &gx.DrawableTriangles{}
ret.DrawablePolygon = &gx.DrawablePolygon{}
ret.Transform.S = gx.V(1, 1)
ret.Triangles = gx.Triangles{
gx.Triangle{
@ -88,6 +89,7 @@ var (
player *Player
rectMove gx.Rectangle
rect *Rect
tri *Tri
)
func NewPlayer() *Player {
@ -190,6 +192,12 @@ func (p *Player) Update(e *gx.Engine) error {
} else {
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 :
if e.KeyIsPressed(ebiten.KeyShift) {
rect.R -= gx.Pi * 0.3 * dt
@ -241,7 +249,7 @@ func main() {
player = NewPlayer()
rect = NewRect()
tri := NewTri()
tri = NewTri()
e.Add(1, &Debug{})
e.Add(0, player)

View file

@ -1,29 +1,52 @@
package gx
import (
)
type PolygonTriangle struct {
P, S int
T, S int
}
// Grouped triangles type.
type Polygon struct {
Transform
base Triangle
triangles []PolygonTriangle
Triangles
}
// Polygon that can be drawn.
type DrawablePolygon struct {
Polygon
ShaderOptions
Visibility
Colority
}
func NewPolygon(base Triangle) *Polygon {
ret := &Polygon{
Transform: T(),
base: base,
func (p *Polygon) MakeTriangles() Triangles {
mv := p.Matrix()
m := &mv
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
}
func (p *Polygon) Triangles() Triangles {
return Triangles{}
func (p *DrawablePolygon) Draw(
e *Engine,
i *Image,
) {
(&DrawableTriangles{
Visibility: p.Visibility,
Colority: p.Colority,
ShaderOptions: p.ShaderOptions,
Triangles: p.MakeTriangles(),
}).Draw(e, i)
}