From fb10beda7588cd303b9da3593ccd509abff20189 Mon Sep 17 00:00:00 2001 From: surdeus Date: Thu, 4 May 2023 19:31:33 +0300 Subject: [PATCH] Started implementing colliders. Simplified structure for basic types like Camera. --- src/cmd/test/main.go | 53 +++++++++++++++++------------------ src/gx/camera.go | 18 ++++++------ src/gx/circle.go | 16 +++++++++++ src/gx/collider-simplifier.go | 12 ++++++++ src/gx/main.go | 10 +++---- src/gx/object.go | 29 ++++++++++--------- src/gx/rect.go | 16 +++++++++++ src/gx/sprite.go | 8 +++--- src/gx/triangle.go | 6 ++++ 9 files changed, 110 insertions(+), 58 deletions(-) create mode 100644 src/gx/circle.go create mode 100644 src/gx/collider-simplifier.go create mode 100644 src/gx/rect.go create mode 100644 src/gx/triangle.go diff --git a/src/cmd/test/main.go b/src/cmd/test/main.go index 2ac24f7..cfc932f 100644 --- a/src/cmd/test/main.go +++ b/src/cmd/test/main.go @@ -24,13 +24,11 @@ var ( func NewPlayer() *Player { return &Player{ Sprite: &gx.Sprite{ - Object: &gx.Object{ - T: gx.Transform { - S: gx.Vector{5, 5}, - RA: gx.Vector{320, 240}, - }, + T: gx.Transform { + S: gx.Vector{5, 5}, + RA: gx.Vector{320, 240}, }, - Image: playerImg, + I: playerImg, }, MoveSpeed: 90., ScaleSpeed: .2, @@ -39,7 +37,7 @@ func NewPlayer() *Player { func (p *Player) Start(e *gx.Engine) { c := e.Camera() - c.Object.T.RA = gx.V(360, -240) + c.T.RA = gx.V(360, -240) } func (p *Player) Update(e *gx.Engine) error { @@ -49,51 +47,51 @@ func (p *Player) Update(e *gx.Engine) error { for _, v := range keys {switch v { case ebiten.KeyArrowUp : - c.Object.T.P.Y += p.MoveSpeed * dt + c.T.P.Y += p.MoveSpeed * dt case ebiten.KeyArrowLeft : - c.Object.T.P.X -= p.MoveSpeed * dt + c.T.P.X -= p.MoveSpeed * dt case ebiten.KeyArrowDown : - c.Object.T.P.Y -= p.MoveSpeed * dt + c.T.P.Y -= p.MoveSpeed * dt case ebiten.KeyArrowRight : - c.Object.T.P.X += p.MoveSpeed * dt + c.T.P.X += p.MoveSpeed * dt case ebiten.KeyW : - p.Object.T.P.Y += p.MoveSpeed * dt + p.T.P.Y += p.MoveSpeed * dt case ebiten.KeyA : - p.Object.T.P.X -= p.MoveSpeed * dt + p.T.P.X -= p.MoveSpeed * dt case ebiten.KeyS : - p.Object.T.P.Y -= p.MoveSpeed * dt + p.T.P.Y -= p.MoveSpeed * dt case ebiten.KeyD : - p.Object.T.P.X += p.MoveSpeed * dt + p.T.P.X += p.MoveSpeed * dt case ebiten.KeyR : - c.Object.T.R += gx.Pi * p.ScaleSpeed * dt + c.T.R += gx.Pi * p.ScaleSpeed * dt case ebiten.KeyT : - c.Object.T.R -= gx.Pi * p.ScaleSpeed * dt + c.T.R -= gx.Pi * p.ScaleSpeed * dt case ebiten.KeyF : if e.KeyIsPressed(ebiten.KeyShift) { - c.Object.T.S.X -= gx.Pi * p.ScaleSpeed * dt + c.T.S.X -= gx.Pi * p.ScaleSpeed * dt } else { - c.Object.T.S.X += gx.Pi * p.ScaleSpeed * dt + c.T.S.X += gx.Pi * p.ScaleSpeed * dt } case ebiten.KeyG : if e.KeyIsPressed(ebiten.KeyShift) { - c.Object.T.S.Y -= gx.Pi * p.ScaleSpeed * dt + c.T.S.Y -= gx.Pi * p.ScaleSpeed * dt } else { - c.Object.T.S.Y += gx.Pi * p.ScaleSpeed * dt + c.T.S.Y += gx.Pi * p.ScaleSpeed * dt } case ebiten.KeyZ : if e.KeyIsPressed(ebiten.KeyShift) { - c.Object.T.RA.X -= gx.Pi * p.MoveSpeed * dt + c.T.RA.X -= gx.Pi * p.MoveSpeed * dt } else { - c.Object.T.RA.X += gx.Pi * p.MoveSpeed * dt + c.T.RA.X += gx.Pi * p.MoveSpeed * dt } - log.Println(c.Object.T.RA.X) + log.Println(c.T.RA.X) case ebiten.KeyX : if e.KeyIsPressed(ebiten.KeyShift) { - c.Object.T.RA.Y -= gx.Pi * p.MoveSpeed * dt + c.T.RA.Y -= gx.Pi * p.MoveSpeed * dt } else { - c.Object.T.RA.Y += gx.Pi * p.MoveSpeed * dt + c.T.RA.Y += gx.Pi * p.MoveSpeed * dt } - log.Println(c.Object.T.RA.Y) + log.Println(c.T.RA.Y) case ebiten.Key0 : e.Del(p) }} @@ -117,6 +115,7 @@ func main() { Title: "Test title", Width: 720, Height: 480, + VSync: true, }) var err error diff --git a/src/gx/camera.go b/src/gx/camera.go index 6556a7b..a6ac4b4 100644 --- a/src/gx/camera.go +++ b/src/gx/camera.go @@ -7,7 +7,7 @@ import ( // Implements the camera component // for the main window. type Camera struct { - *Object + T Transform } const ( @@ -18,6 +18,8 @@ const ( // position, scale and rotation to apply // it to the objects to get the real // transform to display on the screen. +// (Should implement buffering it so we do not +// need to calculate it each time for each object. ) func (c *Camera)RealMatrix( e *Engine, scale bool, @@ -27,20 +29,20 @@ func (c *Camera)RealMatrix( if scale { g.Scale( - c.Object.T.S.X, - c.Object.T.S.Y, + c.T.S.X, + c.T.S.Y, ) } g.Translate( - -c.Object.T.P.X, - c.Object.T.P.Y, + -c.T.P.X, + c.T.P.Y, ) - g.Rotate(-c.Object.T.R) + g.Rotate(-c.T.R) g.Translate( - c.Object.T.RA.X, - -c.Object.T.RA.Y, + c.T.RA.X, + -c.T.RA.Y, ) return *g diff --git a/src/gx/circle.go b/src/gx/circle.go new file mode 100644 index 0000000..8a6f43f --- /dev/null +++ b/src/gx/circle.go @@ -0,0 +1,16 @@ +package gx + +type Circle struct { + // Position + P Point + // Radius + R Float +} + +func (c Circle) ColliderSimplify() Rect { + return Rect{ + W: c.R * 2, + H: c.R * 2, + } +} + diff --git a/src/gx/collider-simplifier.go b/src/gx/collider-simplifier.go new file mode 100644 index 0000000..fa7b686 --- /dev/null +++ b/src/gx/collider-simplifier.go @@ -0,0 +1,12 @@ +package gx + +// Implementing the interface lets +// the engine to work faster about +// collisions because it first checks +// if the the bigger rectangles that +// contain more complicated structure +// do collide. +type ColliderSimplifier interface { + ColliderSimplify() Rect +} + diff --git a/src/gx/main.go b/src/gx/main.go index 95468c6..e7e55e6 100644 --- a/src/gx/main.go +++ b/src/gx/main.go @@ -17,6 +17,7 @@ type WindowConfig struct { Title string Width, Height int FixedSize bool + VSync bool } // The main structure that represents current state of [game] engine. @@ -58,10 +59,8 @@ func New( *poolx.Pool[Drawer], ](true), camera: &Camera{ - Object: &Object{ - T: Transform{ + T: Transform{ S: Vector{1, 1}, - }, }, }, behavers: poolx.New[Behaver](), @@ -74,7 +73,6 @@ func (e *Engine) Add(l Layer, b any) { beh, ok := b.(Behaver) if ok { e.AddBehaver(beh) - beh.Start(e) } drw, ok := b.(Drawer) @@ -116,6 +114,7 @@ func (e *Engine) AddDrawer(l Layer, d Drawer) { func (e *Engine) AddBehaver(b Behaver) { e.behavers.Append(b) + b.Start(e) } func (e *engine) Update() error { @@ -163,9 +162,10 @@ func (e *Engine) Run() error { ebiten.SetWindowTitle(e.wcfg.Title) ebiten.SetWindowSize(e.wcfg.Width, e.wcfg.Height) ebiten.SetWindowSizeLimits(1, 1, e.wcfg.Width, e.wcfg.Height) + + ebiten.SetVsyncEnabled(e.wcfg.VSync) e.lastTime = time.Now() - return ebiten.RunGame((*engine)(e)) } diff --git a/src/gx/object.go b/src/gx/object.go index 0f5d472..178d6a9 100644 --- a/src/gx/object.go +++ b/src/gx/object.go @@ -1,22 +1,23 @@ package gx -type Behaver interface { +// Implementing the interface type +// will call the function OnStart +// when first appear on scene BEFORE +// the OnUpdate. +type Starter interface { Start(*Engine) +} + +// Implementing the interface type +// will call the function on each +// engine iteration. +type Updater interface { Update(*Engine) error } -// The object type represents -// basic information for interaction -// with the engine. -type Object struct { - T Transform -} - -func (o *Object) Start(e *Engine) {} -func (o *Object) Update(e *Engine) error { - return nil -} -func (o *Object) GetObject() *Object { - return o +// The general interface for +type Behaver interface { + Starter + Updater } diff --git a/src/gx/rect.go b/src/gx/rect.go new file mode 100644 index 0000000..dcd9901 --- /dev/null +++ b/src/gx/rect.go @@ -0,0 +1,16 @@ +package gx + +// The type describes rectangle geometry. +type Rect struct { + // Position and rotate around. + P, RA Point + // Width and height. + W, H Float + // Rotation in radians. + R Float +} + +func (r Rect) ColliderSimplify() Rect { + return r +} + diff --git a/src/gx/sprite.go b/src/gx/sprite.go index e001408..4527fbb 100644 --- a/src/gx/sprite.go +++ b/src/gx/sprite.go @@ -5,8 +5,8 @@ import ( ) type Sprite struct { - *Object - *Image + I *Image + T Transform Floating bool } @@ -17,7 +17,7 @@ func (s *Sprite) Draw( op := &ebiten.DrawImageOptions{} m := &Matrix{} - m.Concat(s.Object.T.Matrix(e)) + m.Concat(s.T.Matrix(e)) if e.camera != nil { m.Concat(e.camera.RealMatrix( e, @@ -26,6 +26,6 @@ func (s *Sprite) Draw( } op.GeoM = *m - i.DrawImage(s.Image, op) + i.DrawImage(s.I, op) } diff --git a/src/gx/triangle.go b/src/gx/triangle.go new file mode 100644 index 0000000..a3d374f --- /dev/null +++ b/src/gx/triangle.go @@ -0,0 +1,6 @@ +package gx + +type Triangle struct { + Points [3]Point +} +