From 95a7b27ff85de298de413db17753324721c7fec2 Mon Sep 17 00:00:00 2001 From: surdeus Date: Sat, 18 Feb 2023 05:51:43 +0500 Subject: [PATCH] Started implementing the camera. --- src/cmd/test/main.go | 5 ++++- src/gx/camera.go | 37 +++++++++++++++++++++++++++++++++++++ src/gx/main.go | 12 ++++++++++++ src/gx/sprite.go | 8 +++++++- 4 files changed, 60 insertions(+), 2 deletions(-) create mode 100644 src/gx/camera.go diff --git a/src/cmd/test/main.go b/src/cmd/test/main.go index 2b370c8..af9c23e 100644 --- a/src/cmd/test/main.go +++ b/src/cmd/test/main.go @@ -31,7 +31,10 @@ func NewPlayer() *Player { } 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() { diff --git a/src/gx/camera.go b/src/gx/camera.go new file mode 100644 index 0000000..3e83153 --- /dev/null +++ b/src/gx/camera.go @@ -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 +} + diff --git a/src/gx/main.go b/src/gx/main.go index db83b08..eb43c6b 100644 --- a/src/gx/main.go +++ b/src/gx/main.go @@ -22,10 +22,15 @@ type Engine struct { behavers []Behaver lastTime time.Time dt Float + camera *Camera } type engine Engine +func (e *Engine) Camera() *Camera { + return e.camera +} + func New( cfg *WindowConfig, ) *Engine { @@ -35,6 +40,13 @@ func New( Layer, *[]Drawer, ](true), + camera: &Camera{ + Object: &Object{ + T: Transform{ + S: Vector{0.1, 1}, + }, + }, + }, } } diff --git a/src/gx/sprite.go b/src/gx/sprite.go index 12a6519..62c4651 100644 --- a/src/gx/sprite.go +++ b/src/gx/sprite.go @@ -7,6 +7,7 @@ import ( type Sprite struct { *Object *Image + Floating bool } func (s *Sprite) Draw( @@ -14,8 +15,13 @@ func (s *Sprite) Draw( i *Image, ) { 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) }