This commit is contained in:
Andrey Parhomenko 2023-02-18 21:35:38 +05:00
parent e03c1479e2
commit 4f42968d2d
5 changed files with 32 additions and 15 deletions

View file

@ -39,7 +39,7 @@ func NewPlayer() *Player {
func (p *Player) Start(e *gx.Engine) { func (p *Player) Start(e *gx.Engine) {
c := e.Camera() c := e.Camera()
c.Object.T.RA = gx.V(360, -120) c.Object.T.RA = gx.V(360, -240)
} }
func (p *Player) Update(e *gx.Engine) error { func (p *Player) Update(e *gx.Engine) error {

View file

@ -24,11 +24,13 @@ func (c *Camera)RealMatrix(
) Matrix { ) Matrix {
g := &Matrix{} g := &Matrix{}
// For rotating around.
g.Translate( if scale {
-c.Object.T.RA.X, g.Scale(
c.Object.T.RA.Y, c.Object.T.S.X,
) c.Object.T.S.Y,
)
}
g.Translate( g.Translate(
-c.Object.T.P.X, -c.Object.T.P.X,
@ -41,13 +43,6 @@ func (c *Camera)RealMatrix(
-c.Object.T.RA.Y, -c.Object.T.RA.Y,
) )
if scale {
g.Scale(
c.Object.T.S.X,
c.Object.T.S.Y,
)
}
return *g return *g
} }

View file

@ -137,6 +137,7 @@ func (e *Engine) DT() Float {
func (e *Engine) Run() error { func (e *Engine) Run() error {
ebiten.SetWindowTitle(e.wcfg.Title) ebiten.SetWindowTitle(e.wcfg.Title)
ebiten.SetWindowSize(e.wcfg.Width, e.wcfg.Height) ebiten.SetWindowSize(e.wcfg.Width, e.wcfg.Height)
ebiten.SetWindowSizeLimits(1, 1, e.wcfg.Width, e.wcfg.Height)
e.lastTime = time.Now() e.lastTime = time.Now()

View file

@ -15,8 +15,9 @@ func (s *Sprite) Draw(
i *Image, i *Image,
) { ) {
op := &ebiten.DrawImageOptions{} op := &ebiten.DrawImageOptions{}
m := s.Object.T.Matrix(e) m := &Matrix{}
m.Concat(s.Object.T.Matrix(e))
if e.camera != nil { if e.camera != nil {
m.Concat(e.camera.RealMatrix( m.Concat(e.camera.RealMatrix(
e, e,
@ -24,7 +25,7 @@ func (s *Sprite) Draw(
)) ))
} }
op.GeoM = m op.GeoM = *m
i.DrawImage(s.Image, op) i.DrawImage(s.Image, op)
} }

View file

@ -2,6 +2,7 @@ package gx
import ( import (
"github.com/hajimehoshi/ebiten/v2" "github.com/hajimehoshi/ebiten/v2"
"math"
) )
type Matrix = ebiten.GeoM type Matrix = ebiten.GeoM
@ -22,6 +23,25 @@ func V(x, y Float) Vector {
return Vector{x, y} return Vector{x, y}
} }
// Returns the vector with the matrix applied
func (v Vector) Apply(m *Matrix) Vector {
x, y := m.Apply(v.X, v.Y)
return V(x, y)
}
// Returns the vector rotated by "a" angle in radians.
func (v Vector) Rotate(a Float) Vector {
m := &Matrix{}
m.Rotate(a)
return v.Apply(m)
}
// Returns the normalized vector.
func (v Vector) Norm() Vector {
l := math.Sqrt(v.X*v.X + v.Y*v.Y)
return V(v.X / l, v.Y / l)
}
func T() Transform { func T() Transform {
ret := Transform{} ret := Transform{}
return ret return ret