gg/cmd/test/player.go

130 lines
2.5 KiB
Go

package main
import (
//"math/rand"
"fmt"
"time"
)
import "vultras.su/core/gg"
type Player struct {
gg.AnimatedSprite
MoveSpeed gg.Float
ScaleSpeed gg.Float
Spawned bool
}
func NewPlayer() *Player {
ret := &Player{}
ret.Transform = gg.T()
ret.Scale = gg.V2(1)
ret.Around = gg.V2(.5)
ret.MoveSpeed = 90.
ret.ScaleSpeed = .2
ret.Animations = playerAnimations
ret.TimeBetweenFrames = time.Second/5
fmt.Println("player-walk", ret.Animate(Walk))
ret.Visible = true
ret.Layer = PlayerL
return ret
}
func (p *Player) Start(c *Context) {
}
func (p *Player) Update(c *Context) {
if p.Spawned {
return
}
dt := c.Dt().Seconds()
cam := c.Camera
keys := c.Keys()
shift := c.IsPressed(gg.KeyShift)
//p.Uniforms["Random"] = any(rand.Float32())
for _, v := range keys {
switch v {
case gg.KeyQ:
p.Scale = p.Scale.Add(gg.V(p.ScaleSpeed*dt, 0))
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:
if shift {
p.Rotation -= gg.Pi * 0.3 * dt
} else {
p.Rotation += gg.Pi * 0.3 * dt
}
case gg.KeyF:
if shift {
cam.Scale = cam.Scale.Add(gg.V2(p.ScaleSpeed * dt))
} else {
cam.Scale = cam.Scale.Add(gg.V2(-p.ScaleSpeed * dt))
}
case gg.KeyG:
if shift {
cam.Scale.Y -= gg.Pi * p.ScaleSpeed * dt
} else {
cam.Scale.Y += gg.Pi * p.ScaleSpeed * dt
}
case gg.KeyLeftBracket:
if shift {
rect.Rotation -= gg.Pi * 0.3 * dt
} else {
rect.Rotation += gg.Pi * 0.3 * dt
}
case gg.Key0:
c.Del(p)
case gg.KeyB:
case gg.Key5:
pp := *p
counter++
p.Spawned = true
c.Spawn(&pp)
}
}
for _, event := range c.Events {
switch ec := event.(type) {
case *gg.KeyDown:
switch {
case ec.Key == gg.KeyB:
if p.Layer != PlayerL {
p.Layer = PlayerL
} else {
p.Layer = HighestL
}
}
case *gg.MouseMove:
if !c.IsButtoned(gg.MouseButtonRight) {
break
}
pos := c.Camera.Position
c.Camera.Position = pos.Sub(ec.Abs)
case *gg.WheelChange:
c.Camera.Scale = c.Camera.Scale.Add(gg.V2(
ec.Offset.Y * dt * p.ScaleSpeed * 40,
))
}}
}