Implemented the example for testing the engine.

This commit is contained in:
Andrey Parhomenko 2023-02-18 11:11:09 +05:00
parent f35332a822
commit af8949725a
7 changed files with 87 additions and 18 deletions

View file

@ -3,14 +3,19 @@ package main
import ( import (
"github.com/surdeus/gox/src/gx" "github.com/surdeus/gox/src/gx"
"github.com/hajimehoshi/ebiten/v2/examples/resources/images" "github.com/hajimehoshi/ebiten/v2/examples/resources/images"
"github.com/hajimehoshi/ebiten/v2"
"bytes" "bytes"
"log" "log"
"strings"
) )
type Player struct { type Player struct {
*gx.Sprite *gx.Sprite
MoveSpeed gx.Float
} }
type Debug struct{}
var ( var (
playerImg *gx.Image playerImg *gx.Image
) )
@ -27,23 +32,57 @@ func NewPlayer() *Player {
}, },
Image: playerImg, Image: playerImg,
}, },
MoveSpeed: 90.,
} }
} }
func (p *Player) Update(e *gx.Engine) { func (p *Player) Update(e *gx.Engine) error {
//p.Sprite.Object.T.P.Y += 40 * e.DT()
dt := e.DT() dt := e.DT()
c := e.Camera() c := e.Camera()
//c.Object.T.P.X += 40 * dt keys := e.Keys()
//c.Object.T.S.X += .01 * dt
c.Object.T.R += .2 * dt for _, v := range keys {switch v {
case ebiten.KeyArrowUp :
c.Object.T.P.Y += p.MoveSpeed * dt
case ebiten.KeyArrowLeft :
c.Object.T.P.X -= p.MoveSpeed * dt
case ebiten.KeyArrowDown :
c.Object.T.P.Y -= p.MoveSpeed * dt
case ebiten.KeyArrowRight :
c.Object.T.P.X += p.MoveSpeed * dt
case ebiten.KeyW :
p.Object.T.P.Y += p.MoveSpeed * dt
case ebiten.KeyA :
p.Object.T.P.X -= p.MoveSpeed * dt
case ebiten.KeyS :
p.Object.T.P.Y -= p.MoveSpeed * dt
case ebiten.KeyD :
p.Object.T.P.X += p.MoveSpeed * dt
case ebiten.KeyR :
c.Object.T.R += gx.Pi * .3 * dt
case ebiten.KeyT :
c.Object.T.R -= gx.Pi * .3 * dt
}}
return nil
}
func (d *Debug) Draw(
e *gx.Engine,
i *gx.Image,
) {
keyStrs := []string{}
for _, k := range e.Keys() {
keyStrs = append(keyStrs, k.String())
}
e.DebugPrint(i, strings.Join(keyStrs, ", "))
} }
func main() { func main() {
e := gx.New(&gx.WindowConfig{ e := gx.New(&gx.WindowConfig{
Title: "Test title", Title: "Test title",
Width: 480, Width: 720,
Height: 320, Height: 480,
}) })
var err error var err error
@ -54,5 +93,6 @@ func main() {
e.Add(0, NewPlayer()) e.Add(0, NewPlayer())
e.Add(1, &Debug{})
e.Run() e.Run()
} }

View file

@ -1,5 +1,6 @@
package gix package gix
import ( import (
"github.com/hajimehoshi/ebiten/v2/inpututil"
) )

View file

@ -1,11 +1,19 @@
package gx package gx
import (
"math"
)
// Implements the camera component // Implements the camera component
// for the main window. // for the main window.
type Camera struct { type Camera struct {
*Object *Object
} }
const (
Pi = math.Pi
)
// Returns the matrix satysfying camera // Returns the matrix satysfying camera
// 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
@ -13,16 +21,17 @@ type Camera struct {
func (c *Camera)Matrix(scale bool) Matrix { func (c *Camera)Matrix(scale bool) Matrix {
g := &Matrix{} g := &Matrix{}
// For rotating around.
g.Translate( g.Translate(
-c.Object.T.RA.X, -c.Object.T.RA.X,
-c.Object.T.RA.Y, c.Object.T.RA.Y,
) )
g.Rotate(c.Object.T.R) g.Rotate(c.Object.T.R)
g.Translate( g.Translate(
-c.Object.T.P.X, -c.Object.T.P.X,
-c.Object.T.P.Y, c.Object.T.P.Y,
) )
if scale { if scale {

12
src/gx/debug.go Normal file
View file

@ -0,0 +1,12 @@
package gx
import (
"github.com/hajimehoshi/ebiten/v2/ebitenutil"
)
func (e *Engine)DebugPrint(
i *Image,
str string,
) {
ebitenutil.DebugPrint(i, str)
}

View file

@ -14,7 +14,7 @@ type Layer int
type WindowConfig struct { type WindowConfig struct {
Title string Title string
Width, Height int Width, Height int
FullScreen bool FixedSize bool
} }
type Engine struct { type Engine struct {
@ -90,6 +90,7 @@ func (e *Engine) AddBehaver(b Behaver) {
} }
func (e *engine) Update() error { func (e *engine) Update() error {
var err error
eng := (*Engine)(e) eng := (*Engine)(e)
e.keys = inpututil. e.keys = inpututil.
@ -97,15 +98,16 @@ func (e *engine) Update() error {
e.dt = time.Since(e.lastTime).Seconds() e.dt = time.Since(e.lastTime).Seconds()
for _, v := range eng.behavers { for _, v := range eng.behavers {
v.Update(eng) err = v.Update(eng)
//fmt.Println(v) if err != nil {
return err
}
} }
e.lastTime = time.Now() e.lastTime = time.Now()
return nil return nil
} }
func (e *engine) Draw(i *ebiten.Image) { func (e *engine) Draw(i *ebiten.Image) {
eng := (*Engine)(e) eng := (*Engine)(e)
for p := range e.layers.Vals() { for p := range e.layers.Vals() {
@ -116,7 +118,10 @@ func (e *engine) Draw(i *ebiten.Image) {
} }
func (e *engine) Layout(ow, oh int) (int, int) { func (e *engine) Layout(ow, oh int) (int, int) {
return e.wcfg.Width, e.wcfg.Height if e.wcfg.FixedSize {
return e.wcfg.Width, e.wcfg.Height
}
return ow, oh
} }
// Return the delta time duration value. // Return the delta time duration value.

View file

@ -2,7 +2,7 @@ package gx
type Behaver interface { type Behaver interface {
Start(*Engine) Start(*Engine)
Update(*Engine) Update(*Engine) error
} }
// The object type represents // The object type represents
@ -13,7 +13,9 @@ type Object struct {
} }
func (o *Object) Start(e *Engine) {} func (o *Object) Start(e *Engine) {}
func (o *Object) Update(e *Engine) {} func (o *Object) Update(e *Engine) error {
return nil
}
func (o *Object) GetObject() *Object { func (o *Object) GetObject() *Object {
return o return o
} }

View file

@ -31,7 +31,7 @@ func (t Transform)Matrix() Matrix {
g.Scale(t.S.X, t.S.Y) g.Scale(t.S.X, t.S.Y)
g.Translate(-t.RA.X, -t.RA.Y) g.Translate(-t.RA.X, -t.RA.Y)
g.Rotate(t.R) g.Rotate(t.R)
g.Translate(t.P.X, t.P.Y) g.Translate(t.P.X, -t.P.Y)
return *g return *g
} }