gg/cmd/test/player.go

147 lines
2.8 KiB
Go
Raw Normal View History

2023-12-23 00:09:07 +03:00
package main
import (
//"math/rand"
"fmt"
2023-12-23 00:09:07 +03:00
)
2024-01-04 17:05:51 +03:00
import "github.com/vultras/gg"
2023-12-23 00:09:07 +03:00
type Player struct {
gg.Sprite
MoveSpeed gg.Float
ScaleSpeed gg.Float
gg.Layer
}
func NewPlayer() *Player {
ret := &Player{}
ret.Transform = gg.T()
fmt.Println("transform:", ret.Transform)
//ret.Parent = rect
ret.Scale = gg.V2(1)
2023-12-23 00:09:07 +03:00
// Around center.
ret.Around = gg.V2(.5)
2024-01-04 17:05:51 +03:00
ret.MoveSpeed = 90.
2023-12-23 00:09:07 +03:00
ret.ScaleSpeed = .2
ret.Visible = true
ret.Images[0] = playerImg
ret.Layer = PlayerL
return ret
}
2023-12-25 23:41:12 +03:00
func (p *Player) Start(c *Context) {
}
2023-12-23 00:09:07 +03:00
// Custom drawing function.
func (p *Player) Draw(c *Context) {
p.Sprite.Draw(c)
t := p.Transform
t.Scale.X *= 4.
t.Scale.Y *= 4.
r := &gg.DrawableRectangle{}
r.Color = gg.Rgba(0, 0, 1, 1)
r.Rectangle = gg.Rectangle{
Transform: t,
}
r.Draw(c)
}
func (p *Player) Update(c *Context) {
dt := c.DT()
cam := c.Camera
keys := c.Keys()
2023-12-28 14:34:52 +03:00
shift := c.IsPressed(gg.KeyShift)
2023-12-23 00:09:07 +03:00
//p.Uniforms["Random"] = any(rand.Float32())
for _, v := range keys {
switch v {
2024-01-04 17:05:51 +03:00
case gg.KeyQ:
p.Scale = p.Scale.Add(gg.V(p.ScaleSpeed*dt, 0))
2023-12-23 00:09:07 +03:00
case gg.KeyArrowUp:
cam.Position.Y += p.MoveSpeed * dt
case gg.KeyArrowLeft:
cam.Position.X -= p.MoveSpeed * dt
case gg.KeyArrowDown:
cam.Position.Y -= p.MoveSpeed * dt
case gg.KeyArrowRight:
cam.Position.X += p.MoveSpeed * dt
case gg.KeyW:
p.Position.Y += p.MoveSpeed * dt
case gg.KeyA:
p.Position.X -= p.MoveSpeed * dt
case gg.KeyS:
p.Position.Y -= p.MoveSpeed * dt
case gg.KeyD:
p.Position.X += p.MoveSpeed * dt
case gg.KeyR:
cam.Rotation += gg.Pi * p.ScaleSpeed * dt
case gg.KeyT:
cam.Rotation -= gg.Pi * p.ScaleSpeed * dt
case gg.KeyRightBracket:
2023-12-28 14:34:52 +03:00
if shift {
2023-12-23 00:09:07 +03:00
p.Rotation -= gg.Pi * 0.3 * dt
} else {
p.Rotation += gg.Pi * 0.3 * dt
}
case gg.KeyF:
2023-12-28 14:34:52 +03:00
if shift {
2023-12-24 15:05:34 +03:00
cam.Scale = cam.Scale.Add(gg.V2(p.ScaleSpeed * dt))
2023-12-23 00:09:07 +03:00
} else {
2023-12-24 15:05:34 +03:00
cam.Scale = cam.Scale.Add(gg.V2(-p.ScaleSpeed * dt))
2023-12-23 00:09:07 +03:00
}
case gg.KeyG:
2023-12-28 14:34:52 +03:00
if shift {
2023-12-23 00:09:07 +03:00
cam.Scale.Y -= gg.Pi * p.ScaleSpeed * dt
} else {
cam.Scale.Y += gg.Pi * p.ScaleSpeed * dt
}
case gg.KeyV:
2023-12-28 14:34:52 +03:00
if shift {
2023-12-23 00:09:07 +03:00
tri.Rotation -= gg.Pi * 0.3 * dt
} else {
tri.Rotation += gg.Pi * 0.3 * dt
}
case gg.KeyLeftBracket:
2023-12-28 14:34:52 +03:00
if shift {
2023-12-23 00:09:07 +03:00
rect.Rotation -= gg.Pi * 0.3 * dt
} else {
rect.Rotation += gg.Pi * 0.3 * dt
}
case gg.Key0:
c.Del(p)
case gg.KeyB:
}
}
}
func (p *Player) Event(c *gg.Context) {
switch ec := c.Event.(type) {
case *gg.KeyDown:
switch {
case ec.Key == gg.KeyB:
if p.Layer != PlayerL {
p.Layer = PlayerL
} else {
p.Layer = HighestL
}
}
2024-01-04 17:05:51 +03:00
case *gg.MouseMove:
2023-12-24 15:05:34 +03:00
if !c.IsButtoned(gg.MouseButtonRight) {
break
}
pos := c.Camera.Position
c.Camera.Position = pos.Sub(ec.Abs)
2024-01-04 17:05:51 +03:00
case *gg.WheelChange:
2023-12-24 15:05:34 +03:00
c.Camera.Scale = c.Camera.Scale.Add(gg.V2(
ec.Offset.Y * c.DT() * p.ScaleSpeed * 40,
2023-12-24 15:05:34 +03:00
))
2023-12-23 00:09:07 +03:00
}
2023-12-24 15:05:34 +03:00
2023-12-23 00:09:07 +03:00
}