2023-02-17 07:04:29 +03:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/surdeus/gox/src/gx"
|
2023-02-17 23:51:40 +03:00
|
|
|
"github.com/hajimehoshi/ebiten/v2/examples/resources/images"
|
2023-02-18 09:11:09 +03:00
|
|
|
"github.com/hajimehoshi/ebiten/v2"
|
2023-02-17 23:51:40 +03:00
|
|
|
"bytes"
|
|
|
|
"log"
|
2023-02-18 09:11:09 +03:00
|
|
|
"strings"
|
2023-02-17 07:04:29 +03:00
|
|
|
)
|
|
|
|
|
2023-02-17 16:40:46 +03:00
|
|
|
type Player struct {
|
2023-02-17 23:51:40 +03:00
|
|
|
*gx.Sprite
|
2023-02-18 09:11:09 +03:00
|
|
|
MoveSpeed gx.Float
|
2023-02-17 16:56:15 +03:00
|
|
|
}
|
|
|
|
|
2023-02-18 09:11:09 +03:00
|
|
|
type Debug struct{}
|
|
|
|
|
2023-02-17 23:51:40 +03:00
|
|
|
var (
|
|
|
|
playerImg *gx.Image
|
|
|
|
)
|
|
|
|
|
2023-02-17 16:56:15 +03:00
|
|
|
func NewPlayer() *Player {
|
|
|
|
return &Player{
|
2023-02-17 23:51:40 +03:00
|
|
|
Sprite: &gx.Sprite{
|
|
|
|
Object: &gx.Object{
|
|
|
|
T: gx.Transform {
|
2023-02-18 02:03:28 +03:00
|
|
|
P: gx.Vector{100, 150},
|
2023-02-18 00:17:51 +03:00
|
|
|
S: gx.Vector{5, 5},
|
2023-02-18 02:03:28 +03:00
|
|
|
RA: gx.Vector{200, 200},
|
2023-02-17 23:51:40 +03:00
|
|
|
},
|
|
|
|
},
|
|
|
|
Image: playerImg,
|
|
|
|
},
|
2023-02-18 09:11:09 +03:00
|
|
|
MoveSpeed: 90.,
|
2023-02-17 16:56:15 +03:00
|
|
|
}
|
2023-02-17 16:40:46 +03:00
|
|
|
}
|
|
|
|
|
2023-02-18 09:11:09 +03:00
|
|
|
func (p *Player) Update(e *gx.Engine) error {
|
2023-02-18 03:51:43 +03:00
|
|
|
dt := e.DT()
|
|
|
|
c := e.Camera()
|
2023-02-18 09:11:09 +03:00
|
|
|
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, ", "))
|
2023-02-17 23:51:40 +03:00
|
|
|
}
|
|
|
|
|
2023-02-17 07:04:29 +03:00
|
|
|
func main() {
|
|
|
|
e := gx.New(&gx.WindowConfig{
|
|
|
|
Title: "Test title",
|
2023-02-18 09:11:09 +03:00
|
|
|
Width: 720,
|
|
|
|
Height: 480,
|
2023-02-17 07:04:29 +03:00
|
|
|
})
|
|
|
|
|
2023-02-17 23:51:40 +03:00
|
|
|
var err error
|
|
|
|
playerImg, err = gx.LoadImage(bytes.NewReader(images.Runner_png))
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-02-17 16:56:15 +03:00
|
|
|
e.Add(0, NewPlayer())
|
2023-02-18 09:11:09 +03:00
|
|
|
e.Add(1, &Debug{})
|
2023-02-17 07:04:29 +03:00
|
|
|
e.Run()
|
|
|
|
}
|