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 (
"github.com/surdeus/gox/src/gx"
"github.com/hajimehoshi/ebiten/v2/examples/resources/images"
"github.com/hajimehoshi/ebiten/v2"
"bytes"
"log"
"strings"
)
type Player struct {
*gx.Sprite
MoveSpeed gx.Float
}
type Debug struct{}
var (
playerImg *gx.Image
)
@ -27,23 +32,57 @@ func NewPlayer() *Player {
},
Image: playerImg,
},
MoveSpeed: 90.,
}
}
func (p *Player) Update(e *gx.Engine) {
//p.Sprite.Object.T.P.Y += 40 * e.DT()
func (p *Player) Update(e *gx.Engine) error {
dt := e.DT()
c := e.Camera()
//c.Object.T.P.X += 40 * dt
//c.Object.T.S.X += .01 * dt
c.Object.T.R += .2 * dt
keys := e.Keys()
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() {
e := gx.New(&gx.WindowConfig{
Title: "Test title",
Width: 480,
Height: 320,
Width: 720,
Height: 480,
})
var err error
@ -54,5 +93,6 @@ func main() {
e.Add(0, NewPlayer())
e.Add(1, &Debug{})
e.Run()
}

View file

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

View file

@ -1,11 +1,19 @@
package gx
import (
"math"
)
// Implements the camera component
// for the main window.
type Camera struct {
*Object
}
const (
Pi = math.Pi
)
// Returns the matrix satysfying camera
// position, scale and rotation to apply
// it to the objects to get the real
@ -13,16 +21,17 @@ type Camera struct {
func (c *Camera)Matrix(scale bool) Matrix {
g := &Matrix{}
// For rotating around.
g.Translate(
-c.Object.T.RA.X,
-c.Object.T.RA.Y,
c.Object.T.RA.Y,
)
g.Rotate(c.Object.T.R)
g.Translate(
-c.Object.T.P.X,
-c.Object.T.P.Y,
c.Object.T.P.Y,
)
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 {
Title string
Width, Height int
FullScreen bool
FixedSize bool
}
type Engine struct {
@ -90,6 +90,7 @@ func (e *Engine) AddBehaver(b Behaver) {
}
func (e *engine) Update() error {
var err error
eng := (*Engine)(e)
e.keys = inpututil.
@ -97,15 +98,16 @@ func (e *engine) Update() error {
e.dt = time.Since(e.lastTime).Seconds()
for _, v := range eng.behavers {
v.Update(eng)
//fmt.Println(v)
err = v.Update(eng)
if err != nil {
return err
}
}
e.lastTime = time.Now()
return nil
}
func (e *engine) Draw(i *ebiten.Image) {
eng := (*Engine)(e)
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) {
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.

View file

@ -2,7 +2,7 @@ package gx
type Behaver interface {
Start(*Engine)
Update(*Engine)
Update(*Engine) error
}
// The object type represents
@ -13,7 +13,9 @@ type Object struct {
}
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 {
return o
}

View file

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