diff --git a/src/cmd/math/main.go b/src/cmd/math/main.go index bcaac25..c538014 100644 --- a/src/cmd/math/main.go +++ b/src/cmd/math/main.go @@ -35,16 +35,19 @@ func main() { fmt.Println(l1.Crosses(l2)) fmt.Println(l1.ContainsPoint(gx.Point{1, 4})) - t := gx.Triangle{ - gx.Point{0, 0}, - gx.Point{0, 100}, - gx.Point{100, 0}, + t := gx.Rectangle{ + Transform: gx.Transform{ + S: gx.Vector{1, 1}, + P: gx.Point{0, 200}, + }, + W: 100, + H: 200, } points := []gx.Point{ gx.Point{}, gx.Point{100, 0}, - gx.Point{0, 100}, + gx.Point{0, 99}, gx.Point{.1, .1}, gx.Point{-1, -1}, gx.Point{1, 1}, @@ -53,9 +56,14 @@ func main() { gx.Point{50, 1}, } + ts := t.Triangles() + t1 := ts[0] + t2 := ts[1] + fmt.Printf("Rectangle triangles:\n\t%v\n\t%v\n", t1, t2) for _, p := range points { - fmt.Println(t, p, t.ContainsPoint(p)) + fmt.Println(p, t.ContainsPoint(p)) } + } diff --git a/src/cmd/test/main.go b/src/cmd/test/main.go index 64ed6b2..9cf2f81 100644 --- a/src/cmd/test/main.go +++ b/src/cmd/test/main.go @@ -55,6 +55,8 @@ func (r *Rect) Update(e *gx.Engine) error { var ( playerImg *gx.Image + player *Player + rect *Rect ) func NewPlayer() *Player { @@ -79,6 +81,19 @@ func NewPlayer() *Player { return ret } +func (p *Player) Draw(e *gx.Engine, i *gx.Image) { + p.Sprite.Draw(e, i) + r := &gx.DrawableRectangle{ + Rectangle: gx.Rectangle{ + Transform: p.Transform, + W: 10, + H: 10, + }, + Color: gx.Color{0, 0, gx.MaxColorV, gx.MaxColorV}, + } + r.Draw(e, i) +} + func (p *Player) Start(e *gx.Engine, v ...any) { fmt.Println("starting") c := e.Camera() @@ -153,7 +168,14 @@ func (d *Debug) Draw( for _, k := range e.Keys() { keyStrs = append(keyStrs, k.String()) } - e.DebugPrint(i, strings.Join(keyStrs, ", ")) + + if rect.ContainsPoint(player.P) { + keyStrs = append(keyStrs, "THIS IS SHIT") + } + + e.DebugPrint(i, + strings.Join(keyStrs, ", ")) + } func (d *Debug) IsVisible() bool {return true} @@ -173,8 +195,12 @@ func main() { } - e.Add(0, NewPlayer()) + player = NewPlayer() + rect = NewRect() + e.Add(1, &Debug{}) - e.Add(-1, NewRect()) + e.Add(0, player) + e.Add(-1, rect) + e.Run() } diff --git a/src/gx/camera.go b/src/gx/camera.go index 4388aea..58d070b 100644 --- a/src/gx/camera.go +++ b/src/gx/camera.go @@ -14,17 +14,13 @@ type Camera struct { // need to calculate it each time for each object. ) func (c *Camera)RealMatrix( e *Engine, - scale bool, ) Matrix { g := &Matrix{} - - if scale { - g.Scale( - c.S.X, - c.S.Y, - ) - } + g.Scale( + c.S.X, + c.S.Y, + ) g.Translate( -c.P.X, diff --git a/src/gx/img.go b/src/gx/img.go index ed551fc..5b3fcc5 100644 --- a/src/gx/img.go +++ b/src/gx/img.go @@ -37,6 +37,11 @@ func LoadImage(input io.Reader) (*Image, error) { return ret, nil } +func NewImage(w, h int) (*Image) { + return ebiten.NewImage(w, h) +} + + func (c Color) RGBA() (r, g, b, a uint32) { return uint32(c.R), uint32(c.G), uint32(c.B), uint32(c.A) } diff --git a/src/gx/point.go b/src/gx/point.go deleted file mode 100644 index 704e26c..0000000 --- a/src/gx/point.go +++ /dev/null @@ -1,4 +0,0 @@ -package gx - -type Point Vector - diff --git a/src/gx/rect.go b/src/gx/rect.go index 2220f7f..c1caf63 100644 --- a/src/gx/rect.go +++ b/src/gx/rect.go @@ -39,26 +39,24 @@ func (r Rectangle) Corners() []Point { // Get 2 triangles that the rectangle consists of. func (r Rectangle) Triangles() Triangles { - return Triangles{} -} - -/*func MustNewImage(w, h int) (*Image) { - img, err := NewImage(w, h) - if err != nil { - panic(err) - } + m := r.Matrix() + p1 := r.P.Apply(&m) + p2 := r.P.Add(Vector{r.W, 0}).Apply(&m) + p3 := r.P.Add(Vector{r.W, -r.H}).Apply(&m) + p4 := r.P.Add(Vector{0, -r.H}).Apply(&m) - return img -}*/ + return Triangles{ + Triangle{p1, p2, p3}, + Triangle{p1, p4, p3}, + } +} +// Check whether the rectangle contains the point. func (r Rectangle) ContainsPoint(p Point) bool { - return false -} - -func NewImage(w, h int) (*Image) { - return ebiten.NewImage(w, h) + return r.Triangles().ContainsPoint(p) } +// Check whether the drawable rectangle should be drawn. func (r *DrawableRectangle) IsVisible() bool { return r.Visible } @@ -77,8 +75,8 @@ func (r *DrawableRectangle) Draw( t.S.X *= r.W t.S.Y *= r.H - m := t.Matrix(e) - rm := e.Camera().RealMatrix(e, true) + m := t.Matrix() + rm := e.Camera().RealMatrix(e) m.Concat(rm) @@ -108,8 +106,8 @@ func (r *DrawableRectangle) Draw( } - rm := e.Camera().RealMatrix(e, true) - m := t.Matrix(e) + rm := e.Camera().RealMatrix(e) + m := t.Matrix() m.Concat(rm) // Drawing with shader. diff --git a/src/gx/sprite.go b/src/gx/sprite.go index ac5b0e4..0ac95c1 100644 --- a/src/gx/sprite.go +++ b/src/gx/sprite.go @@ -21,11 +21,10 @@ func (s *Sprite) Draw( m := &Matrix{} - m.Concat(s.Matrix(e)) + m.Concat(s.Matrix()) if !s.Floating { m.Concat(e.Camera().RealMatrix( e, - true, )) } diff --git a/src/gx/transform.go b/src/gx/transform.go index 080e7b0..a8f51bb 100644 --- a/src/gx/transform.go +++ b/src/gx/transform.go @@ -22,7 +22,7 @@ func T() Transform { // Returns the GeoM with corresponding // to the transfrom transformation -func (t Transform)Matrix(e *Engine) Matrix { +func (t Transform)Matrix() Matrix { g := &Matrix{} g.Scale(t.S.X, t.S.Y) diff --git a/src/gx/triangle.go b/src/gx/triangle.go index e1e5bac..8228e65 100644 --- a/src/gx/triangle.go +++ b/src/gx/triangle.go @@ -55,6 +55,16 @@ func (t Triangle) Sgn() Float { (t[1].X - t[2].X) * (t[0].Y - t[2].Y) } +func (ts Triangles) ContainsPoint(p Point) bool { + for _, t := range ts { + if t.ContainsPoint(p) { + return true + } + } + + return false +} + //func (t Triangle) /* func (r *DrawableRectangle) Draw( diff --git a/src/gx/vector.go b/src/gx/vector.go index 8ecc726..1cecca8 100644 --- a/src/gx/vector.go +++ b/src/gx/vector.go @@ -8,6 +8,9 @@ import ( type Vector struct { X, Y Float } +type Point = Vector + +type Vectors []Vector func V(x, y Float) Vector { return Vector{x, y} @@ -19,6 +22,34 @@ func (v Vector) Apply(m *Matrix) Vector { return V(x, y) } +// Adds the vector to other one returning the result. +func (v Vector) Add(a ...Vector) Vector { + for _, r := range a { + v.X += r.X + v.Y += r.Y + } + + return v +} + +// Returns the subtraction of all the vectors from the current one. +func (v Vector) Sub(s ...Vector) Vector { + for _, r := range s { + v.X -= r.X + v.Y -= r.Y + } + + return v +} + +// Returns the negative version of the vector. +func (v Vector) Neg() Vector { + return Vector{ + -v.X, + -v.Y, + } +} + // Returns the vector rotated by "a" angle in radians. func (v Vector) Rotate(a Float) Vector { m := &Matrix{}