Started implementing colliders. Simplified structure for basic types like Camera.
This commit is contained in:
parent
a6db4d1417
commit
fb10beda75
9 changed files with 110 additions and 58 deletions
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
16
src/gx/circle.go
Normal file
16
src/gx/circle.go
Normal 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,
|
||||
}
|
||||
}
|
||||
|
12
src/gx/collider-simplifier.go
Normal file
12
src/gx/collider-simplifier.go
Normal 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
|
||||
}
|
||||
|
|
@ -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))
|
||||
}
|
||||
|
||||
|
|
|
@ -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
|
||||
}
|
||||
|
||||
|
|
16
src/gx/rect.go
Normal file
16
src/gx/rect.go
Normal 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
|
||||
}
|
||||
|
|
@ -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)
|
||||
}
|
||||
|
||||
|
|
6
src/gx/triangle.go
Normal file
6
src/gx/triangle.go
Normal file
|
@ -0,0 +1,6 @@
|
|||
package gx
|
||||
|
||||
type Triangle struct {
|
||||
Points [3]Point
|
||||
}
|
||||
|
Loading…
Reference in a new issue