Started implementing colliders. Simplified structure for basic types like Camera.

This commit is contained in:
Andrey Parhomenko 2023-05-04 19:31:33 +03:00
parent a6db4d1417
commit fb10beda75
9 changed files with 110 additions and 58 deletions

View file

@ -24,13 +24,11 @@ var (
func NewPlayer() *Player { func NewPlayer() *Player {
return &Player{ return &Player{
Sprite: &gx.Sprite{ Sprite: &gx.Sprite{
Object: &gx.Object{ T: gx.Transform {
T: gx.Transform { S: gx.Vector{5, 5},
S: gx.Vector{5, 5}, RA: gx.Vector{320, 240},
RA: gx.Vector{320, 240},
},
}, },
Image: playerImg, I: playerImg,
}, },
MoveSpeed: 90., MoveSpeed: 90.,
ScaleSpeed: .2, ScaleSpeed: .2,
@ -39,7 +37,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, -240) c.T.RA = gx.V(360, -240)
} }
func (p *Player) Update(e *gx.Engine) error { 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 { for _, v := range keys {switch v {
case ebiten.KeyArrowUp : case ebiten.KeyArrowUp :
c.Object.T.P.Y += p.MoveSpeed * dt c.T.P.Y += p.MoveSpeed * dt
case ebiten.KeyArrowLeft : case ebiten.KeyArrowLeft :
c.Object.T.P.X -= p.MoveSpeed * dt c.T.P.X -= p.MoveSpeed * dt
case ebiten.KeyArrowDown : case ebiten.KeyArrowDown :
c.Object.T.P.Y -= p.MoveSpeed * dt c.T.P.Y -= p.MoveSpeed * dt
case ebiten.KeyArrowRight : case ebiten.KeyArrowRight :
c.Object.T.P.X += p.MoveSpeed * dt c.T.P.X += p.MoveSpeed * dt
case ebiten.KeyW : case ebiten.KeyW :
p.Object.T.P.Y += p.MoveSpeed * dt p.T.P.Y += p.MoveSpeed * dt
case ebiten.KeyA : case ebiten.KeyA :
p.Object.T.P.X -= p.MoveSpeed * dt p.T.P.X -= p.MoveSpeed * dt
case ebiten.KeyS : case ebiten.KeyS :
p.Object.T.P.Y -= p.MoveSpeed * dt p.T.P.Y -= p.MoveSpeed * dt
case ebiten.KeyD : case ebiten.KeyD :
p.Object.T.P.X += p.MoveSpeed * dt p.T.P.X += p.MoveSpeed * dt
case ebiten.KeyR : case ebiten.KeyR :
c.Object.T.R += gx.Pi * p.ScaleSpeed * dt c.T.R += gx.Pi * p.ScaleSpeed * dt
case ebiten.KeyT : case ebiten.KeyT :
c.Object.T.R -= gx.Pi * p.ScaleSpeed * dt c.T.R -= gx.Pi * p.ScaleSpeed * dt
case ebiten.KeyF : case ebiten.KeyF :
if e.KeyIsPressed(ebiten.KeyShift) { 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 { } else {
c.Object.T.S.X += gx.Pi * p.ScaleSpeed * dt c.T.S.X += gx.Pi * p.ScaleSpeed * dt
} }
case ebiten.KeyG : case ebiten.KeyG :
if e.KeyIsPressed(ebiten.KeyShift) { 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 { } else {
c.Object.T.S.Y += gx.Pi * p.ScaleSpeed * dt c.T.S.Y += gx.Pi * p.ScaleSpeed * dt
} }
case ebiten.KeyZ : case ebiten.KeyZ :
if e.KeyIsPressed(ebiten.KeyShift) { 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 { } 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 : case ebiten.KeyX :
if e.KeyIsPressed(ebiten.KeyShift) { 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 { } 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 : case ebiten.Key0 :
e.Del(p) e.Del(p)
}} }}
@ -117,6 +115,7 @@ func main() {
Title: "Test title", Title: "Test title",
Width: 720, Width: 720,
Height: 480, Height: 480,
VSync: true,
}) })
var err error var err error

View file

@ -7,7 +7,7 @@ import (
// Implements the camera component // Implements the camera component
// for the main window. // for the main window.
type Camera struct { type Camera struct {
*Object T Transform
} }
const ( const (
@ -18,6 +18,8 @@ const (
// position, scale and rotation to apply // position, scale and rotation to apply
// it to the objects to get the real // it to the objects to get the real
// transform to display on the screen. // 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( func (c *Camera)RealMatrix(
e *Engine, e *Engine,
scale bool, scale bool,
@ -27,20 +29,20 @@ func (c *Camera)RealMatrix(
if scale { if scale {
g.Scale( g.Scale(
c.Object.T.S.X, c.T.S.X,
c.Object.T.S.Y, c.T.S.Y,
) )
} }
g.Translate( g.Translate(
-c.Object.T.P.X, -c.T.P.X,
c.Object.T.P.Y, c.T.P.Y,
) )
g.Rotate(-c.Object.T.R) g.Rotate(-c.T.R)
g.Translate( g.Translate(
c.Object.T.RA.X, c.T.RA.X,
-c.Object.T.RA.Y, -c.T.RA.Y,
) )
return *g return *g

16
src/gx/circle.go Normal file
View file

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

View file

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

View file

@ -17,6 +17,7 @@ type WindowConfig struct {
Title string Title string
Width, Height int Width, Height int
FixedSize bool FixedSize bool
VSync bool
} }
// The main structure that represents current state of [game] engine. // The main structure that represents current state of [game] engine.
@ -58,10 +59,8 @@ func New(
*poolx.Pool[Drawer], *poolx.Pool[Drawer],
](true), ](true),
camera: &Camera{ camera: &Camera{
Object: &Object{ T: Transform{
T: Transform{
S: Vector{1, 1}, S: Vector{1, 1},
},
}, },
}, },
behavers: poolx.New[Behaver](), behavers: poolx.New[Behaver](),
@ -74,7 +73,6 @@ func (e *Engine) Add(l Layer, b any) {
beh, ok := b.(Behaver) beh, ok := b.(Behaver)
if ok { if ok {
e.AddBehaver(beh) e.AddBehaver(beh)
beh.Start(e)
} }
drw, ok := b.(Drawer) drw, ok := b.(Drawer)
@ -116,6 +114,7 @@ func (e *Engine) AddDrawer(l Layer, d Drawer) {
func (e *Engine) AddBehaver(b Behaver) { func (e *Engine) AddBehaver(b Behaver) {
e.behavers.Append(b) e.behavers.Append(b)
b.Start(e)
} }
func (e *engine) Update() error { func (e *engine) Update() error {
@ -163,9 +162,10 @@ 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) ebiten.SetWindowSizeLimits(1, 1, e.wcfg.Width, e.wcfg.Height)
ebiten.SetVsyncEnabled(e.wcfg.VSync)
e.lastTime = time.Now() e.lastTime = time.Now()
return ebiten.RunGame((*engine)(e)) return ebiten.RunGame((*engine)(e))
} }

View file

@ -1,22 +1,23 @@
package gx 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) Start(*Engine)
}
// Implementing the interface type
// will call the function on each
// engine iteration.
type Updater interface {
Update(*Engine) error Update(*Engine) error
} }
// The object type represents // The general interface for
// basic information for interaction type Behaver interface {
// with the engine. Starter
type Object struct { Updater
T Transform
}
func (o *Object) Start(e *Engine) {}
func (o *Object) Update(e *Engine) error {
return nil
}
func (o *Object) GetObject() *Object {
return o
} }

16
src/gx/rect.go Normal file
View file

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

View file

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

6
src/gx/triangle.go Normal file
View file

@ -0,0 +1,6 @@
package gx
type Triangle struct {
Points [3]Point
}