This commit is contained in:
Andrey Parhomenko 2023-05-30 14:34:10 +03:00
parent 308123f3d2
commit 421321cc62
10 changed files with 112 additions and 43 deletions

View file

@ -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))
}
}

View file

@ -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()
}

View file

@ -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.Translate(
-c.P.X,

View file

@ -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)
}

View file

@ -1,4 +0,0 @@
package gx
type Point Vector

View file

@ -39,26 +39,24 @@ func (r Rectangle) Corners() []Point {
// Get 2 triangles that the rectangle consists of.
func (r Rectangle) Triangles() Triangles {
return Triangles{}
}
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)
/*func MustNewImage(w, h int) (*Image) {
img, err := NewImage(w, h)
if err != nil {
panic(err)
return Triangles{
Triangle{p1, p2, p3},
Triangle{p1, p4, p3},
}
}
return img
}*/
// 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.

View file

@ -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,
))
}

View file

@ -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)

View file

@ -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(

View file

@ -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{}