gg/cmd/test/player.go
2024-06-02 00:01:17 +05:00

164 lines
3 KiB
Go

package main
import "surdeus.su/core/gg"
import "surdeus.su/core/gg/ox"
import "surdeus.su/core/gg/mx"
import "time"
//import "log"
type Player struct {
ox.AnimatedSprite
MoveSpeed mx.Float
ScaleSpeed mx.Float
Spawned bool
}
func NewPlayer() *Player {
ret := &Player{}
//ret.Transform = ox.T()
ret.AnimatedSprite = ox.NewAnimatedSprite(
playerAnimations,
time.Second / 10,
)
ret.SetScale(mx.V2(1))
ret.SetAround(mx.V2(.5))
ret.MoveSpeed = 40.
ret.ScaleSpeed = .2
ret.Shader = gg.SolidWhiteColorShader
ret.Visible = true
ret.Layer = LayerPlayer
//ret.Collidable = true
//ret.Resolvable = true
return ret
}
func (p *Player) OnStart(c Context) {
p.Connect(rect)
}
func (p *Player) Draw(c Context) *gg.Drawing {
return p.AnimatedSprite.Draw(c)
prect := &ox.DrawableRectangle{
Rectangle: p.GetRectangle(),
}
prect.Color = gg.RGBA(1, 1, 1, 1)
return prect.Draw(c)
}
func (p *Player) OnUpdate(c Context) {
//log.Println("OnUpdate(...)")
if p.Spawned {
return
}
e := c.Engine()
dt := e.DT().Seconds()
//cam := c.Camera
keys := e.GetKeyboardKeys()
/*r := gg.Rectangle{
Transform: gg.T(),
}
r.Width = 150
r.Height = 150
col, _ := gg.Collide(p, r)
fmt.Println(col.Crosses)*/
//p.SetPosition(p.Position().Sub(gg.Y(2)))
//p.Move(gg.Y(1))
walking := false
shiftPressed := e.IsPressed(gg.KeyShift)
d := float64(1)
if shiftPressed {
d *= -1
}
//p.Uniforms["Random"] = any(rand.Float32())
for _, key := range keys {
switch key {
case gg.KeyQ:
p.AddScale(mx.VX(d*p.ScaleSpeed * dt))
case gg.KeyW:
p.Move(mx.VY(-p.MoveSpeed * dt))
walking = true
p.Animate(Walk)
case gg.KeyA:
p.Move(mx.VX(-p.MoveSpeed * dt))
p.SetScale(mx.V(-1, 1))
walking = true
p.Animate(Walk)
case gg.KeyS:
p.Move(mx.VY(p.MoveSpeed * dt))
//p.Position.Y -= p.MoveSpeed * dt
walking = true
p.Animate(Walk)
case gg.KeyD:
p.Move(mx.VX(p.MoveSpeed * dt))
p.SetScale(mx.V(1, 1))
walking = true
p.Animate(Walk)
case gg.KeyE :
p.Rotate(d*mx.Pi * 0.3 * dt)
case gg.KeyX:
c.Engine().Delete(p)
case gg.KeyC:
pp := *p
counter++
pp.Spawned = true
//pp.Collidable = false
//pp.Resolvable = false
c.Engine().Spawn(&pp)
}
}
if !walking {
p.Animate(Stand)
}
for _, down := range c.Events().Keyboard.KeyDowns {
switch {
case down.Key == gg.KeyZ:
if p.Layer != LayerPlayer {
p.Layer = LayerPlayer
} else {
p.Layer = LayerHighest
}
}
}
}
/*func (p *Player) GetCollisionInterest() []gg.CollisionType {
return []gg.CollisionType{
gg.CollisionStaticPhysics,
}
}*/
/*func (p *Player) Resolve(c *Context) {
col := c.Collisions[0]
if !p.Spawned && false {
fmt.Printf(
"frame[%d]: the col[0] len(%d): %T, %T, %d %d\n",
c.Frame(), len(c.Collisions), col.What, col.With, len(col.Crosses), len(col.Points),
)
}
for _, col := range c.Collisions {
switch col.Type {
case gg.CollisionStaticPhysics:
LOOP:
for {
p.Move(gg.Y(-1))
_, collides := gg.Collide(p, col.With)
if !collides {
break LOOP
}
}
}
}
}*/