gg/cmd/test/main.go

311 lines
5.4 KiB
Go
Raw Normal View History

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"
"github.com/hajimehoshi/ebiten/v2"
2023-02-17 23:51:40 +03:00
"bytes"
"log"
"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
)
type Context = gg.Context
const (
2023-10-23 15:45:18 +03:00
HighestL gg.Layer = -iota
DebugL
TriangleL
PlayerL
RectL
LowestL
)
type Player struct {
2023-10-23 15:45:18 +03:00
*gg.Sprite
MoveSpeed gg.Float
ScaleSpeed gg.Float
gg.Layer
}
2023-10-23 15:45:18 +03:00
type Debug struct {
gg.Visibility
2023-10-23 15:45:18 +03:00
gg.Layer
}
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: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
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-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
},*/
},
RectL,
}
return ret
2023-05-26 18:31:04 +03:00
}
func (r *Rect) Update(c *Context) error {
2023-06-03 11:25:19 +03:00
//r.R += 0.3 * e.DT()
r.P = c.AbsCursorPosition()
2023-05-26 18:31:04 +03:00
return nil
}
func (r *Rect) Event(c *Context) {
}
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
)
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
},
Visible: true,
2023-10-23 15:45:18 +03:00
ShaderOptions: gg.ShaderOptions{
Shader: gg.SolidWhiteColorShader,
Uniforms: make(map[string]any),
},
2023-02-17 23:51:40 +03:00
},
2023-10-23 15:45:18 +03:00
MoveSpeed: 90.,
ScaleSpeed: .2,
}
2023-10-23 15:45:18 +03:00
2023-05-26 18:31:04 +03:00
ret.Images[0] = playerImg
ret.Layer = PlayerL
2023-10-23 15:45:18 +03:00
2023-05-26 18:31:04 +03:00
return ret
}
func (p *Player) Draw(c *Context) {
p.Sprite.Draw(c)
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(c)
2023-05-30 14:34:10 +03:00
}
func (p *Player) Start(c *Context) {
2023-05-26 18:31:04 +03:00
fmt.Println("starting")
cam := c.Camera()
cam.RA = gg.V(360, 240)
}
func (p *Player) Update(c *Context) error {
dt := c.DT()
cam := c.Camera()
keys := c.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:
cam.P.Y += p.MoveSpeed * dt
2023-10-23 15:45:18 +03:00
case ebiten.KeyArrowLeft:
cam.P.X -= p.MoveSpeed * dt
2023-10-23 15:45:18 +03:00
case ebiten.KeyArrowDown:
cam.P.Y -= p.MoveSpeed * dt
2023-10-23 15:45:18 +03:00
case ebiten.KeyArrowRight:
cam.P.X += p.MoveSpeed * dt
2023-10-23 15:45:18 +03:00
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:
cam.R += gg.Pi * p.ScaleSpeed * dt
2023-10-23 15:45:18 +03:00
case ebiten.KeyT:
cam.R -= gg.Pi * p.ScaleSpeed * dt
2023-10-23 15:45:18 +03:00
case ebiten.KeyRightBracket:
if c.IsPressed(gg.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 gg.KeyF:
if c.IsPressed(ebiten.KeyShift) {
cam.S.X -= gg.Pi * p.ScaleSpeed * dt
2023-10-23 15:45:18 +03:00
} else {
cam.S.X += gg.Pi * p.ScaleSpeed * dt
2023-10-23 15:45:18 +03:00
}
case gg.KeyG:
if c.IsPressed(ebiten.KeyShift) {
cam.S.Y -= gg.Pi * p.ScaleSpeed * dt
2023-10-23 15:45:18 +03:00
} else {
cam.S.Y += gg.Pi * p.ScaleSpeed * dt
2023-10-23 15:45:18 +03:00
}
case gg.KeyZ:
if c.IsPressed(ebiten.KeyShift) {
cam.RA.X -= gg.Pi * p.MoveSpeed * dt
2023-10-23 15:45:18 +03:00
} else {
cam.RA.X += gg.Pi * p.MoveSpeed * dt
2023-10-23 15:45:18 +03:00
}
case gg.KeyX:
if c.IsPressed(ebiten.KeyShift) {
cam.RA.Y -= gg.Pi * p.MoveSpeed * dt
2023-10-23 15:45:18 +03:00
} else {
cam.RA.Y += gg.Pi * p.MoveSpeed * dt
2023-10-23 15:45:18 +03:00
}
case gg.KeyV:
if c.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 gg.KeyLeftBracket:
if c.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 gg.Key0:
c.Del(p)
case gg.KeyB:
2023-11-23 22:05:22 +03:00
}
}
return nil
}
func (p *Player) Signal(e *gg.Engine, ev any) {
2023-11-23 22:05:22 +03:00
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-10-23 15:45:18 +03:00
}
}
func (d *Debug) Draw(c *Context) {
e := c.Engine
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
keyStrs = append(keyStrs, fmt.Sprintf("%v", e.CursorPosition()))
keyStrs = append(keyStrs, fmt.Sprintf("%v", e.AbsCursorPosition()))
e.DebugPrint(c.Image,
2023-05-30 14:34:10 +03:00
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-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,
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
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()
}