2023-02-17 07:04:29 +03:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2023-12-14 22:01:07 +03:00
|
|
|
"github.com/di4f/gg"
|
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-08-31 11:29:37 +03:00
|
|
|
const (
|
2023-10-23 15:45:18 +03:00
|
|
|
HighestL gg.Layer = -iota
|
2023-08-31 11:29:37 +03:00
|
|
|
DebugL
|
|
|
|
TriangleL
|
|
|
|
PlayerL
|
|
|
|
RectL
|
|
|
|
LowestL
|
|
|
|
)
|
|
|
|
|
2023-02-17 16:40:46 +03:00
|
|
|
type Player struct {
|
2023-10-23 15:45:18 +03:00
|
|
|
*gg.Sprite
|
|
|
|
MoveSpeed gg.Float
|
|
|
|
ScaleSpeed gg.Float
|
|
|
|
gg.Layer
|
2023-02-17 16:56:15 +03:00
|
|
|
}
|
|
|
|
|
2023-10-23 15:45:18 +03:00
|
|
|
type Debug struct {
|
|
|
|
gg.Layer
|
2023-08-31 11:29:37 +03:00
|
|
|
}
|
2023-02-18 09:11:09 +03:00
|
|
|
|
2023-05-26 18:31:04 +03:00
|
|
|
type Rect struct {
|
2023-10-23 15:45:18 +03:00
|
|
|
*gg.DrawableRectangle
|
|
|
|
gg.Layer
|
2023-05-26 18:31:04 +03:00
|
|
|
}
|
|
|
|
|
2023-06-15 20:18:58 +03:00
|
|
|
type Tri struct {
|
2023-10-23 15:45:18 +03:00
|
|
|
*gg.DrawablePolygon
|
|
|
|
gg.Layer
|
2023-06-15 20:18:58 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewTri() *Tri {
|
|
|
|
ret := &Tri{}
|
2023-10-23 15:45:18 +03:00
|
|
|
ret.DrawablePolygon = &gg.DrawablePolygon{}
|
|
|
|
ret.Transform.S = gg.V(1, 1)
|
|
|
|
|
|
|
|
ret.Triangles = gg.Triangles{
|
|
|
|
gg.Triangle{
|
|
|
|
gg.V(0, 0),
|
|
|
|
gg.V(100, 100),
|
|
|
|
gg.V(0, -50),
|
2023-06-15 20:18:58 +03:00
|
|
|
},
|
2023-10-23 15:45:18 +03:00
|
|
|
gg.Triangle{
|
|
|
|
gg.V(0, 0),
|
|
|
|
gg.V(-100, -100),
|
|
|
|
gg.V(0, 50),
|
2023-06-15 20:25:16 +03:00
|
|
|
},
|
2023-06-15 20:18:58 +03:00
|
|
|
}
|
2023-10-23 15:45:18 +03:00
|
|
|
ret.Color = gg.Color{gg.MaxColorV, gg.MaxColorV, 0, gg.MaxColorV}
|
2023-06-15 20:18:58 +03:00
|
|
|
ret.Visible = true
|
2023-08-31 11:29:37 +03:00
|
|
|
ret.Layer = TriangleL
|
2023-10-23 15:45:18 +03:00
|
|
|
|
2023-06-15 20:18:58 +03:00
|
|
|
return ret
|
|
|
|
}
|
|
|
|
|
2023-05-26 18:31:04 +03:00
|
|
|
func NewRect() *Rect {
|
2023-10-23 15:45:18 +03:00
|
|
|
ret := &Rect{&gg.DrawableRectangle{
|
|
|
|
Rectangle: gg.Rectangle{
|
|
|
|
Transform: gg.Transform{
|
|
|
|
S: gg.Vector{
|
|
|
|
X: 200,
|
|
|
|
Y: 400,
|
2023-05-30 22:35:49 +03:00
|
|
|
},
|
2023-05-26 18:31:04 +03:00
|
|
|
},
|
2023-10-23 15:45:18 +03:00
|
|
|
},
|
|
|
|
Color: gg.Color{
|
|
|
|
gg.MaxColorV,
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
gg.MaxColorV,
|
|
|
|
},
|
|
|
|
Visible: true,
|
|
|
|
/*Shader: gg.SolidWhiteColorShader,
|
|
|
|
Options: gg.ShaderOptions{
|
|
|
|
Images: [4]*gg.Image{
|
|
|
|
playerImg,
|
|
|
|
nil,
|
|
|
|
nil,
|
|
|
|
nil,
|
2023-05-26 18:31:04 +03:00
|
|
|
},
|
2023-10-23 15:45:18 +03:00
|
|
|
},*/
|
2023-08-31 11:29:37 +03:00
|
|
|
},
|
|
|
|
RectL,
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret
|
2023-05-26 18:31:04 +03:00
|
|
|
}
|
|
|
|
|
2023-10-23 15:45:18 +03:00
|
|
|
func (r *Rect) Update(e *gg.Engine) error {
|
2023-06-03 11:25:19 +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 (
|
2023-10-23 15:45:18 +03:00
|
|
|
playerImg *gg.Image
|
|
|
|
player *Player
|
|
|
|
rectMove gg.Rectangle
|
|
|
|
rect *Rect
|
|
|
|
tri *Tri
|
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-10-23 15:45:18 +03:00
|
|
|
Sprite: &gg.Sprite{
|
|
|
|
Transform: gg.Transform{
|
|
|
|
S: gg.Vector{5, 5},
|
|
|
|
RA: gg.Vector{.5, .5},
|
2023-02-17 23:51:40 +03:00
|
|
|
},
|
2023-05-23 16:19:55 +03:00
|
|
|
Visible: true,
|
2023-10-23 15:45:18 +03:00
|
|
|
ShaderOptions: gg.ShaderOptions{
|
|
|
|
Shader: gg.SolidWhiteColorShader,
|
|
|
|
Uniforms: make(map[string]any),
|
2023-05-30 13:24:14 +03:00
|
|
|
},
|
2023-02-17 23:51:40 +03:00
|
|
|
},
|
2023-10-23 15:45:18 +03:00
|
|
|
MoveSpeed: 90.,
|
2023-02-18 13:26:54 +03:00
|
|
|
ScaleSpeed: .2,
|
2023-02-17 16:56:15 +03:00
|
|
|
}
|
2023-10-23 15:45:18 +03:00
|
|
|
|
2023-05-26 18:31:04 +03:00
|
|
|
ret.Images[0] = playerImg
|
2023-08-31 11:29:37 +03:00
|
|
|
ret.Layer = PlayerL
|
2023-10-23 15:45:18 +03:00
|
|
|
|
2023-05-26 18:31:04 +03:00
|
|
|
return ret
|
2023-02-17 16:40:46 +03:00
|
|
|
}
|
|
|
|
|
2023-10-23 15:45:18 +03:00
|
|
|
func (p *Player) Draw(e *gg.Engine, i *gg.Image) {
|
2023-05-30 14:34:10 +03:00
|
|
|
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-10-23 15:45:18 +03:00
|
|
|
rectMove = gg.Rectangle{
|
2023-06-03 11:25:19 +03:00
|
|
|
Transform: t,
|
|
|
|
}
|
2023-10-23 15:45:18 +03:00
|
|
|
r := &gg.DrawableRectangle{
|
2023-06-03 11:25:19 +03:00
|
|
|
Rectangle: rectMove,
|
2023-10-23 15:45:18 +03:00
|
|
|
Color: gg.Color{0, 0, gg.MaxColorV, gg.MaxColorV},
|
2023-05-30 14:34:10 +03:00
|
|
|
}
|
|
|
|
r.Draw(e, i)
|
|
|
|
}
|
|
|
|
|
2023-10-23 15:45:18 +03:00
|
|
|
func (p *Player) Start(e *gg.Engine, v ...any) {
|
2023-05-26 18:31:04 +03:00
|
|
|
fmt.Println("starting")
|
2023-02-18 16:57:37 +03:00
|
|
|
c := e.Camera()
|
2023-10-23 15:45:18 +03:00
|
|
|
c.RA = gg.V(360, 240)
|
2023-02-18 15:38:28 +03:00
|
|
|
}
|
|
|
|
|
2023-10-23 15:45:18 +03:00
|
|
|
func (p *Player) Update(e *gg.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-10-23 15:45:18 +03:00
|
|
|
|
2023-05-27 18:00:27 +03:00
|
|
|
p.Uniforms["Random"] = any(rand.Float32())
|
2023-10-23 15:45:18 +03:00
|
|
|
for _, v := range keys {
|
|
|
|
switch v {
|
|
|
|
case ebiten.KeyArrowUp:
|
|
|
|
c.P.Y += p.MoveSpeed * dt
|
|
|
|
case ebiten.KeyArrowLeft:
|
|
|
|
c.P.X -= p.MoveSpeed * dt
|
|
|
|
case ebiten.KeyArrowDown:
|
|
|
|
c.P.Y -= p.MoveSpeed * dt
|
|
|
|
case ebiten.KeyArrowRight:
|
|
|
|
c.P.X += p.MoveSpeed * dt
|
|
|
|
case ebiten.KeyW:
|
|
|
|
p.P.Y += p.MoveSpeed * dt
|
|
|
|
case ebiten.KeyA:
|
|
|
|
p.P.X -= p.MoveSpeed * dt
|
|
|
|
case ebiten.KeyS:
|
|
|
|
p.P.Y -= p.MoveSpeed * dt
|
|
|
|
case ebiten.KeyD:
|
|
|
|
p.P.X += p.MoveSpeed * dt
|
|
|
|
case ebiten.KeyR:
|
|
|
|
c.R += gg.Pi * p.ScaleSpeed * dt
|
|
|
|
case ebiten.KeyT:
|
|
|
|
c.R -= gg.Pi * p.ScaleSpeed * dt
|
|
|
|
case ebiten.KeyRightBracket:
|
2023-11-23 22:05:22 +03:00
|
|
|
if e.IsPressed(ebiten.KeyShift) {
|
2023-10-23 15:45:18 +03:00
|
|
|
p.R -= gg.Pi * 0.3 * dt
|
|
|
|
} else {
|
|
|
|
p.R += gg.Pi * 0.3 * dt
|
|
|
|
}
|
|
|
|
case ebiten.KeyF:
|
2023-11-23 22:05:22 +03:00
|
|
|
if e.IsPressed(ebiten.KeyShift) {
|
2023-10-23 15:45:18 +03:00
|
|
|
c.S.X -= gg.Pi * p.ScaleSpeed * dt
|
|
|
|
} else {
|
|
|
|
c.S.X += gg.Pi * p.ScaleSpeed * dt
|
|
|
|
}
|
|
|
|
case ebiten.KeyG:
|
2023-11-23 22:05:22 +03:00
|
|
|
if e.IsPressed(ebiten.KeyShift) {
|
2023-10-23 15:45:18 +03:00
|
|
|
c.S.Y -= gg.Pi * p.ScaleSpeed * dt
|
|
|
|
} else {
|
|
|
|
c.S.Y += gg.Pi * p.ScaleSpeed * dt
|
|
|
|
}
|
|
|
|
case ebiten.KeyZ:
|
2023-11-23 22:05:22 +03:00
|
|
|
if e.IsPressed(ebiten.KeyShift) {
|
2023-10-23 15:45:18 +03:00
|
|
|
c.RA.X -= gg.Pi * p.MoveSpeed * dt
|
|
|
|
} else {
|
|
|
|
c.RA.X += gg.Pi * p.MoveSpeed * dt
|
|
|
|
}
|
|
|
|
case ebiten.KeyX:
|
2023-11-23 22:05:22 +03:00
|
|
|
if e.IsPressed(ebiten.KeyShift) {
|
2023-10-23 15:45:18 +03:00
|
|
|
c.RA.Y -= gg.Pi * p.MoveSpeed * dt
|
|
|
|
} else {
|
|
|
|
c.RA.Y += gg.Pi * p.MoveSpeed * dt
|
|
|
|
}
|
|
|
|
case ebiten.KeyV:
|
2023-11-23 22:05:22 +03:00
|
|
|
if e.IsPressed(ebiten.KeyShift) {
|
2023-10-23 15:45:18 +03:00
|
|
|
tri.R -= gg.Pi * 0.3 * dt
|
|
|
|
} else {
|
|
|
|
tri.R += gg.Pi * 0.3 * dt
|
|
|
|
}
|
|
|
|
case ebiten.KeyLeftBracket:
|
2023-11-23 22:05:22 +03:00
|
|
|
if e.IsPressed(ebiten.KeyShift) {
|
2023-10-23 15:45:18 +03:00
|
|
|
rect.R -= gg.Pi * 0.3 * dt
|
|
|
|
} else {
|
|
|
|
rect.R += gg.Pi * 0.3 * dt
|
|
|
|
}
|
|
|
|
case ebiten.Key0:
|
|
|
|
e.Del(p)
|
|
|
|
case ebiten.KeyB:
|
2023-11-23 22:05:22 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Player) Event(e *gg.Engine, ev any) {
|
|
|
|
fmt.Println("event:", ev)
|
|
|
|
switch ec := ev.(type) {
|
|
|
|
case *gg.KeyDown:
|
|
|
|
switch {
|
2023-12-14 22:01:07 +03:00
|
|
|
case ec.Key == gg.KeyB:
|
2023-10-23 15:45:18 +03:00
|
|
|
if p.Layer != PlayerL {
|
|
|
|
p.Layer = PlayerL
|
|
|
|
} else {
|
|
|
|
p.Layer = HighestL
|
|
|
|
}
|
2023-02-18 15:38:28 +03:00
|
|
|
}
|
2023-10-23 15:45:18 +03:00
|
|
|
}
|
2023-02-18 09:11:09 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (d *Debug) Draw(
|
2023-10-23 15:45:18 +03:00
|
|
|
e *gg.Engine,
|
|
|
|
i *gg.Image,
|
2023-02-18 09:11:09 +03:00
|
|
|
) {
|
|
|
|
keyStrs := []string{}
|
|
|
|
for _, k := range e.Keys() {
|
|
|
|
keyStrs = append(keyStrs, k.String())
|
|
|
|
}
|
2023-10-23 15:45:18 +03:00
|
|
|
|
2023-12-14 22:01:07 +03:00
|
|
|
if rectMove.ContainsPoint(e.AbsCursorPosition()) {
|
|
|
|
keyStrs = append(keyStrs, "contains cursor")
|
|
|
|
}
|
|
|
|
|
2023-06-03 22:41:00 +03:00
|
|
|
if rectMove.Vertices().Contained(rect).Len() > 0 ||
|
2023-10-23 15:45:18 +03:00
|
|
|
rect.Vertices().Contained(rectMove).Len() > 0 {
|
2023-05-30 14:34:10 +03:00
|
|
|
keyStrs = append(keyStrs, "THIS IS SHIT")
|
|
|
|
}
|
2023-10-23 15:45:18 +03:00
|
|
|
|
2023-11-23 23:12:41 +03:00
|
|
|
keyStrs = append(keyStrs, fmt.Sprintf("%v", e.CursorPosition()))
|
|
|
|
keyStrs = append(keyStrs, fmt.Sprintf("%v", e.AbsCursorPosition()))
|
|
|
|
|
2023-05-30 14:34:10 +03:00
|
|
|
e.DebugPrint(i,
|
|
|
|
strings.Join(keyStrs, ", "))
|
2023-10-23 15:45:18 +03:00
|
|
|
|
2023-02-17 23:51:40 +03:00
|
|
|
}
|
|
|
|
|
2023-10-23 15:45:18 +03:00
|
|
|
func (d *Debug) IsVisible() bool { return true }
|
2023-05-23 16:19:55 +03:00
|
|
|
|
2023-02-17 07:04:29 +03:00
|
|
|
func main() {
|
2023-10-23 15:45:18 +03:00
|
|
|
e := gg.NewEngine(&gg.WindowConfig{
|
|
|
|
Title: "Test title",
|
|
|
|
Width: 720,
|
2023-02-18 09:11:09 +03:00
|
|
|
Height: 480,
|
2023-10-23 15:45:18 +03:00
|
|
|
VSync: true,
|
2023-02-17 07:04:29 +03:00
|
|
|
})
|
|
|
|
|
2023-02-17 23:51:40 +03:00
|
|
|
var err error
|
2023-10-23 15:45:18 +03:00
|
|
|
playerImg, err = gg.LoadImage(bytes.NewReader(images.Runner_png))
|
2023-02-17 23:51:40 +03:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2023-05-30 14:34:10 +03:00
|
|
|
player = NewPlayer()
|
|
|
|
rect = NewRect()
|
2023-06-20 14:04:55 +03:00
|
|
|
tri = NewTri()
|
2023-10-23 15:45:18 +03:00
|
|
|
|
2023-08-31 11:29:37 +03:00
|
|
|
e.Add(&Debug{})
|
|
|
|
e.Add(player)
|
|
|
|
e.Add(rect)
|
|
|
|
e.Add(tri)
|
|
|
|
fmt.Println(rect.GetLayer(), player.GetLayer())
|
2023-10-23 15:45:18 +03:00
|
|
|
|
2023-02-17 07:04:29 +03:00
|
|
|
e.Run()
|
|
|
|
}
|