Started implementing the camera.

This commit is contained in:
Andrey Parhomenko 2023-02-18 05:51:43 +05:00
parent 92db008ccf
commit 95a7b27ff8
4 changed files with 60 additions and 2 deletions

View file

@ -31,7 +31,10 @@ func NewPlayer() *Player {
} }
func (p *Player) Update(e *gx.Engine) { func (p *Player) Update(e *gx.Engine) {
p.Sprite.Object.T.P.Y += 40 * e.DT() //p.Sprite.Object.T.P.Y += 40 * e.DT()
dt := e.DT()
c := e.Camera()
c.Object.T.P.X += 40 * dt
} }
func main() { func main() {

37
src/gx/camera.go Normal file
View file

@ -0,0 +1,37 @@
package gx
// Implements the camera component
// for the main window.
type Camera struct {
*Object
}
// Returns the matrix satysfying camera
// position, scale and rotation to apply
// it to the objects to get the real
// transform to display on the screen.
func (c *Camera)Matrix(scale bool) Matrix {
g := &Matrix{}
g.Translate(
c.Object.T.RA.X,
c.Object.T.RA.Y,
)
g.Rotate(c.Object.T.R)
g.Translate(
-c.Object.T.P.X,
-c.Object.T.P.Y,
)
if scale {
g.Scale(
c.Object.T.S.X,
c.Object.T.S.Y,
)
}
return *g
}

View file

@ -22,10 +22,15 @@ type Engine struct {
behavers []Behaver behavers []Behaver
lastTime time.Time lastTime time.Time
dt Float dt Float
camera *Camera
} }
type engine Engine type engine Engine
func (e *Engine) Camera() *Camera {
return e.camera
}
func New( func New(
cfg *WindowConfig, cfg *WindowConfig,
) *Engine { ) *Engine {
@ -35,6 +40,13 @@ func New(
Layer, Layer,
*[]Drawer, *[]Drawer,
](true), ](true),
camera: &Camera{
Object: &Object{
T: Transform{
S: Vector{0.1, 1},
},
},
},
} }
} }

View file

@ -7,6 +7,7 @@ import (
type Sprite struct { type Sprite struct {
*Object *Object
*Image *Image
Floating bool
} }
func (s *Sprite) Draw( func (s *Sprite) Draw(
@ -14,8 +15,13 @@ func (s *Sprite) Draw(
i *Image, i *Image,
) { ) {
op := &ebiten.DrawImageOptions{} op := &ebiten.DrawImageOptions{}
op.GeoM = s.Object.T.Matrix() m := s.Object.T.Matrix()
if e.camera != nil {
m.Concat(e.camera.Matrix(true))
}
op.GeoM = m
i.DrawImage(s.Image, op) i.DrawImage(s.Image, op)
} }