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-05-26 18:31:04 +03:00
|
|
|
"fmt"
|
2023-05-27 18:00:27 +03:00
|
|
|
"math/rand"
|
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-18 13:26:54 +03:00
|
|
|
ScaleSpeed gx.Float
|
2023-02-17 16:56:15 +03:00
|
|
|
}
|
|
|
|
|
2023-02-18 09:11:09 +03:00
|
|
|
type Debug struct{}
|
|
|
|
|
2023-05-26 18:31:04 +03:00
|
|
|
type Rect struct {
|
|
|
|
*gx.DrawableRectangle
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewRect() *Rect {
|
|
|
|
return &Rect{&gx.DrawableRectangle{
|
|
|
|
Rectangle: gx.Rectangle{
|
2023-05-30 22:35:49 +03:00
|
|
|
Transform: gx.Transform{
|
|
|
|
S: gx.Vector{
|
|
|
|
X: 200,
|
|
|
|
Y: 400,
|
|
|
|
},
|
|
|
|
},
|
2023-05-26 18:31:04 +03:00
|
|
|
},
|
|
|
|
Color: gx.Color{
|
|
|
|
gx.MaxColorV,
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
gx.MaxColorV,
|
|
|
|
},
|
|
|
|
Visible: true,
|
|
|
|
/*Shader: gx.SolidWhiteColorShader,
|
|
|
|
Options: gx.ShaderOptions{
|
|
|
|
Images: [4]*gx.Image{
|
|
|
|
playerImg,
|
|
|
|
nil,
|
|
|
|
nil,
|
|
|
|
nil,
|
|
|
|
},
|
|
|
|
},*/
|
|
|
|
}}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *Rect) Update(e *gx.Engine) error {
|
2023-05-30 22:51:50 +03:00
|
|
|
r.R += 0.3 * e.DT()
|
2023-05-26 18:31:04 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-02-17 23:51:40 +03:00
|
|
|
var (
|
|
|
|
playerImg *gx.Image
|
2023-05-30 14:34:10 +03:00
|
|
|
player *Player
|
|
|
|
rect *Rect
|
2023-02-17 23:51:40 +03:00
|
|
|
)
|
|
|
|
|
2023-02-17 16:56:15 +03:00
|
|
|
func NewPlayer() *Player {
|
2023-05-26 18:31:04 +03:00
|
|
|
ret := &Player{
|
2023-02-17 23:51:40 +03:00
|
|
|
Sprite: &gx.Sprite{
|
2023-05-30 13:24:14 +03:00
|
|
|
Transform: gx.Transform {
|
2023-05-04 19:31:33 +03:00
|
|
|
S: gx.Vector{5, 5},
|
2023-06-03 10:39:15 +03:00
|
|
|
RA: gx.Vector{.5, .5},
|
2023-02-17 23:51:40 +03:00
|
|
|
},
|
2023-05-23 16:19:55 +03:00
|
|
|
Visible: true,
|
2023-05-30 13:24:14 +03:00
|
|
|
ShaderOptions: gx.ShaderOptions {
|
|
|
|
Shader: gx.SolidWhiteColorShader,
|
|
|
|
Uniforms: make(map[string] any),
|
|
|
|
},
|
2023-02-17 23:51:40 +03:00
|
|
|
},
|
2023-02-18 09:11:09 +03:00
|
|
|
MoveSpeed: 90.,
|
2023-02-18 13:26:54 +03:00
|
|
|
ScaleSpeed: .2,
|
2023-02-17 16:56:15 +03:00
|
|
|
}
|
2023-05-26 18:31:04 +03:00
|
|
|
|
|
|
|
ret.Images[0] = playerImg
|
|
|
|
|
|
|
|
return ret
|
2023-02-17 16:40:46 +03:00
|
|
|
}
|
|
|
|
|
2023-05-30 14:34:10 +03:00
|
|
|
func (p *Player) Draw(e *gx.Engine, i *gx.Image) {
|
|
|
|
p.Sprite.Draw(e, i)
|
2023-06-03 10:39:15 +03:00
|
|
|
t := p.Transform
|
|
|
|
t.S.X *= 4.
|
|
|
|
t.S.Y *= 4.
|
2023-05-30 14:34:10 +03:00
|
|
|
r := &gx.DrawableRectangle{
|
|
|
|
Rectangle: gx.Rectangle{
|
2023-06-03 10:39:15 +03:00
|
|
|
Transform: t,
|
2023-05-30 14:34:10 +03:00
|
|
|
},
|
|
|
|
Color: gx.Color{0, 0, gx.MaxColorV, gx.MaxColorV},
|
|
|
|
}
|
|
|
|
r.Draw(e, i)
|
|
|
|
}
|
|
|
|
|
2023-05-26 18:31:04 +03:00
|
|
|
func (p *Player) Start(e *gx.Engine, v ...any) {
|
|
|
|
fmt.Println("starting")
|
2023-02-18 16:57:37 +03:00
|
|
|
c := e.Camera()
|
2023-05-30 22:35:49 +03:00
|
|
|
c.RA = gx.V(360, 240)
|
2023-02-18 15:38:28 +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()
|
2023-05-27 18:00:27 +03:00
|
|
|
|
|
|
|
p.Uniforms["Random"] = any(rand.Float32())
|
2023-02-18 09:11:09 +03:00
|
|
|
for _, v := range keys {switch v {
|
|
|
|
case ebiten.KeyArrowUp :
|
2023-05-30 13:24:14 +03:00
|
|
|
c.P.Y += p.MoveSpeed * dt
|
2023-02-18 09:11:09 +03:00
|
|
|
case ebiten.KeyArrowLeft :
|
2023-05-30 13:24:14 +03:00
|
|
|
c.P.X -= p.MoveSpeed * dt
|
2023-02-18 09:11:09 +03:00
|
|
|
case ebiten.KeyArrowDown :
|
2023-05-30 13:24:14 +03:00
|
|
|
c.P.Y -= p.MoveSpeed * dt
|
2023-02-18 09:11:09 +03:00
|
|
|
case ebiten.KeyArrowRight :
|
2023-05-30 13:24:14 +03:00
|
|
|
c.P.X += p.MoveSpeed * dt
|
2023-02-18 09:11:09 +03:00
|
|
|
case ebiten.KeyW :
|
2023-05-30 13:24:14 +03:00
|
|
|
p.P.Y += p.MoveSpeed * dt
|
2023-02-18 09:11:09 +03:00
|
|
|
case ebiten.KeyA :
|
2023-05-30 13:24:14 +03:00
|
|
|
p.P.X -= p.MoveSpeed * dt
|
2023-02-18 09:11:09 +03:00
|
|
|
case ebiten.KeyS :
|
2023-05-30 13:24:14 +03:00
|
|
|
p.P.Y -= p.MoveSpeed * dt
|
2023-02-18 09:11:09 +03:00
|
|
|
case ebiten.KeyD :
|
2023-05-30 13:24:14 +03:00
|
|
|
p.P.X += p.MoveSpeed * dt
|
2023-02-18 09:11:09 +03:00
|
|
|
case ebiten.KeyR :
|
2023-05-30 13:24:14 +03:00
|
|
|
c.R += gx.Pi * p.ScaleSpeed * dt
|
2023-02-18 09:11:09 +03:00
|
|
|
case ebiten.KeyT :
|
2023-05-30 13:24:14 +03:00
|
|
|
c.R -= gx.Pi * p.ScaleSpeed * dt
|
2023-06-03 10:39:15 +03:00
|
|
|
case ebiten.KeyRightBracket :
|
|
|
|
if e.KeyIsPressed(ebiten.KeyShift) {
|
|
|
|
p.R -= gx.Pi * 0.3 * dt
|
|
|
|
} else {
|
|
|
|
p.R += gx.Pi * 0.3 * dt
|
|
|
|
}
|
2023-02-18 13:26:54 +03:00
|
|
|
case ebiten.KeyF :
|
|
|
|
if e.KeyIsPressed(ebiten.KeyShift) {
|
2023-05-30 13:24:14 +03:00
|
|
|
c.S.X -= gx.Pi * p.ScaleSpeed * dt
|
2023-02-18 13:26:54 +03:00
|
|
|
} else {
|
2023-05-30 13:24:14 +03:00
|
|
|
c.S.X += gx.Pi * p.ScaleSpeed * dt
|
2023-02-18 13:26:54 +03:00
|
|
|
}
|
|
|
|
case ebiten.KeyG :
|
|
|
|
if e.KeyIsPressed(ebiten.KeyShift) {
|
2023-05-30 13:24:14 +03:00
|
|
|
c.S.Y -= gx.Pi * p.ScaleSpeed * dt
|
2023-02-18 13:26:54 +03:00
|
|
|
} else {
|
2023-05-30 13:24:14 +03:00
|
|
|
c.S.Y += gx.Pi * p.ScaleSpeed * dt
|
2023-02-18 13:26:54 +03:00
|
|
|
}
|
2023-02-18 15:38:28 +03:00
|
|
|
case ebiten.KeyZ :
|
|
|
|
if e.KeyIsPressed(ebiten.KeyShift) {
|
2023-05-30 13:24:14 +03:00
|
|
|
c.RA.X -= gx.Pi * p.MoveSpeed * dt
|
2023-02-18 15:38:28 +03:00
|
|
|
} else {
|
2023-05-30 13:24:14 +03:00
|
|
|
c.RA.X += gx.Pi * p.MoveSpeed * dt
|
2023-02-18 15:38:28 +03:00
|
|
|
}
|
|
|
|
case ebiten.KeyX :
|
|
|
|
if e.KeyIsPressed(ebiten.KeyShift) {
|
2023-05-30 13:24:14 +03:00
|
|
|
c.RA.Y -= gx.Pi * p.MoveSpeed * dt
|
2023-02-18 15:38:28 +03:00
|
|
|
} else {
|
2023-05-30 13:24:14 +03:00
|
|
|
c.RA.Y += gx.Pi * p.MoveSpeed * dt
|
2023-02-18 15:38:28 +03:00
|
|
|
}
|
2023-06-03 10:39:15 +03:00
|
|
|
case ebiten.KeyLeftBracket :
|
|
|
|
if e.KeyIsPressed(ebiten.KeyShift) {
|
|
|
|
rect.R -= gx.Pi * 0.3 * dt
|
|
|
|
} else {
|
|
|
|
rect.R += gx.Pi * 0.3 * dt
|
|
|
|
}
|
2023-04-28 18:21:34 +03:00
|
|
|
case ebiten.Key0 :
|
|
|
|
e.Del(p)
|
2023-02-18 09:11:09 +03:00
|
|
|
}}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *Debug) Draw(
|
|
|
|
e *gx.Engine,
|
|
|
|
i *gx.Image,
|
|
|
|
) {
|
|
|
|
keyStrs := []string{}
|
|
|
|
for _, k := range e.Keys() {
|
|
|
|
keyStrs = append(keyStrs, k.String())
|
|
|
|
}
|
2023-05-30 14:34:10 +03:00
|
|
|
|
|
|
|
if rect.ContainsPoint(player.P) {
|
|
|
|
keyStrs = append(keyStrs, "THIS IS SHIT")
|
|
|
|
}
|
|
|
|
|
|
|
|
e.DebugPrint(i,
|
|
|
|
strings.Join(keyStrs, ", "))
|
|
|
|
|
2023-02-17 23:51:40 +03:00
|
|
|
}
|
|
|
|
|
2023-05-23 16:19:55 +03:00
|
|
|
func (d *Debug) IsVisible() bool {return true}
|
|
|
|
|
2023-02-17 07:04:29 +03:00
|
|
|
func main() {
|
2023-06-03 10:39:15 +03:00
|
|
|
e := gx.NewEngine(&gx.WindowConfig{
|
2023-02-17 07:04:29 +03:00
|
|
|
Title: "Test title",
|
2023-02-18 09:11:09 +03:00
|
|
|
Width: 720,
|
|
|
|
Height: 480,
|
2023-05-04 19:31:33 +03:00
|
|
|
VSync: true,
|
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-05-30 14:34:10 +03:00
|
|
|
player = NewPlayer()
|
|
|
|
rect = NewRect()
|
|
|
|
|
2023-02-18 09:11:09 +03:00
|
|
|
e.Add(1, &Debug{})
|
2023-05-30 14:34:10 +03:00
|
|
|
e.Add(0, player)
|
|
|
|
e.Add(-1, rect)
|
|
|
|
|
2023-02-17 07:04:29 +03:00
|
|
|
e.Run()
|
|
|
|
}
|