gg/cmd/test/player.go

165 lines
3 KiB
Go
Raw Normal View History

2023-12-23 00:09:07 +03:00
package main
2024-05-19 21:11:58 +03:00
import "surdeus.su/core/gg"
2024-06-01 16:07:28 +03:00
import "surdeus.su/core/gg/ox"
import "surdeus.su/core/gg/mx"
import "time"
2024-06-01 22:01:17 +03:00
//import "log"
2023-12-23 00:09:07 +03:00
type Player struct {
2024-06-01 16:07:28 +03:00
ox.AnimatedSprite
MoveSpeed mx.Float
ScaleSpeed mx.Float
2024-05-19 21:11:58 +03:00
Spawned bool
2023-12-23 00:09:07 +03:00
}
func NewPlayer() *Player {
ret := &Player{}
2024-06-01 16:07:28 +03:00
//ret.Transform = ox.T()
ret.AnimatedSprite = ox.NewAnimatedSprite(
playerAnimations,
2024-06-01 22:01:17 +03:00
time.Second / 10,
2024-06-01 16:07:28 +03:00
)
ret.SetScale(mx.V2(1))
ret.SetAround(mx.V2(.5))
ret.MoveSpeed = 40.
2023-12-23 00:09:07 +03:00
ret.ScaleSpeed = .2
ret.Shader = gg.SolidWhiteColorShader
2023-12-23 00:09:07 +03:00
ret.Visible = true
2024-06-01 16:07:28 +03:00
ret.Layer = LayerPlayer
2023-12-23 00:09:07 +03:00
//ret.Collidable = true
//ret.Resolvable = true
2023-12-23 00:09:07 +03:00
return ret
}
2024-06-01 16:07:28 +03:00
func (p *Player) OnStart(c Context) {
p.Connect(rect)
2023-12-25 23:41:12 +03:00
}
2024-06-01 16:07:28 +03:00
func (p *Player) Draw(c Context) *gg.Drawing {
return p.AnimatedSprite.Draw(c)
2024-06-01 16:07:28 +03:00
prect := &ox.DrawableRectangle{
Rectangle: p.GetRectangle(),
}
2024-06-01 16:07:28 +03:00
prect.Color = gg.RGBA(1, 1, 1, 1)
return prect.Draw(c)
}
2024-06-01 16:07:28 +03:00
func (p *Player) OnUpdate(c Context) {
2024-06-01 22:01:17 +03:00
//log.Println("OnUpdate(...)")
2024-06-01 16:07:28 +03:00
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)))
2024-06-01 16:07:28 +03:00
//p.Move(gg.Y(1))
2023-12-23 00:09:07 +03:00
walking := false
2024-06-01 16:07:28 +03:00
shiftPressed := e.IsPressed(gg.KeyShift)
d := float64(1)
if shiftPressed {
d *= -1
}
2023-12-23 00:09:07 +03:00
//p.Uniforms["Random"] = any(rand.Float32())
2024-06-01 16:07:28 +03:00
for _, key := range keys {
switch key {
2024-01-04 17:05:51 +03:00
case gg.KeyQ:
2024-06-01 16:07:28 +03:00
p.AddScale(mx.VX(d*p.ScaleSpeed * dt))
2023-12-23 00:09:07 +03:00
case gg.KeyW:
2024-06-01 16:07:28 +03:00
p.Move(mx.VY(-p.MoveSpeed * dt))
walking = true
p.Animate(Walk)
2023-12-23 00:09:07 +03:00
case gg.KeyA:
2024-06-01 16:07:28 +03:00
p.Move(mx.VX(-p.MoveSpeed * dt))
p.SetScale(mx.V(-1, 1))
walking = true
p.Animate(Walk)
2023-12-23 00:09:07 +03:00
case gg.KeyS:
2024-06-01 16:07:28 +03:00
p.Move(mx.VY(p.MoveSpeed * dt))
//p.Position.Y -= p.MoveSpeed * dt
walking = true
p.Animate(Walk)
2023-12-23 00:09:07 +03:00
case gg.KeyD:
2024-06-01 16:07:28 +03:00
p.Move(mx.VX(p.MoveSpeed * dt))
p.SetScale(mx.V(1, 1))
walking = true
p.Animate(Walk)
2024-06-01 16:07:28 +03:00
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
2024-06-01 16:07:28 +03:00
//pp.Collidable = false
//pp.Resolvable = false
c.Engine().Spawn(&pp)
2023-12-23 00:09:07 +03:00
}
}
if !walking {
p.Animate(Stand)
}
2024-06-01 16:07:28 +03:00
for _, down := range c.Events().Keyboard.KeyDowns {
switch {
case down.Key == gg.KeyZ:
if p.Layer != LayerPlayer {
p.Layer = LayerPlayer
} else {
p.Layer = LayerHighest
2024-05-19 21:11:58 +03:00
}
2023-12-23 00:09:07 +03:00
}
2024-05-19 21:11:58 +03:00
}
}
2024-06-01 16:07:28 +03:00
/*func (p *Player) GetCollisionInterest() []gg.CollisionType {
return []gg.CollisionType{
gg.CollisionStaticPhysics,
}
2024-06-01 16:07:28 +03:00
}*/
2024-06-01 16:07:28 +03:00
/*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),
)
}
2024-05-19 21:11:58 +03:00
for _, col := range c.Collisions {
switch col.Type {
case gg.CollisionStaticPhysics:
LOOP:
2024-05-19 21:11:58 +03:00
for {
p.Move(gg.Y(-1))
_, collides := gg.Collide(p, col.With)
if !collides {
break LOOP
}
}
}
2024-05-19 21:11:58 +03:00
}
2024-06-01 16:07:28 +03:00
}*/