gg/cmd/test/player.go

179 lines
3.3 KiB
Go
Raw Normal View History

2023-12-23 00:09:07 +03:00
package main
import (
//"math/rand"
"fmt"
"time"
2023-12-23 00:09:07 +03:00
)
2024-01-05 04:22:53 +03:00
import "vultras.su/core/gg"
2023-12-23 00:09:07 +03:00
type Player struct {
gg.AnimatedSprite
2023-12-23 00:09:07 +03:00
MoveSpeed gg.Float
ScaleSpeed gg.Float
Spawned bool
2023-12-23 00:09:07 +03:00
}
func NewPlayer() *Player {
ret := &Player{}
ret.Transform = gg.T()
2024-01-18 06:06:27 +03:00
ret.SetScale(gg.V2(1))
ret.SetAround(gg.V2(.5))
ret.MoveSpeed = 40.
2023-12-23 00:09:07 +03:00
ret.ScaleSpeed = .2
ret.Animations = playerAnimations
ret.TimeBetweenFrames = time.Second/10
ret.Shader = gg.SolidWhiteColorShader
2023-12-23 00:09:07 +03:00
ret.Visible = true
ret.Layer = PlayerL
ret.Collidable = true
ret.Resolvable = true
2023-12-23 00:09:07 +03:00
return ret
}
2023-12-25 23:41:12 +03:00
func (p *Player) Start(c *Context) {
p.Connect(rect)
2023-12-25 23:41:12 +03:00
}
func (p *Player) Draw(c *Context) []gg.EVertex {
return p.AnimatedSprite.Draw(c)
prect := &gg.DrawableRectangle{
Rectangle: p.Rectangle(),
}
prect.Color = gg.Rgba(1, 1, 1, 1)
return prect.Draw(c)
}
2023-12-23 00:09:07 +03:00
func (p *Player) Update(c *Context) {
//p.SetPosition(p.Position().Sub(gg.Y(2)))
p.Move(gg.Y(1))
if p.Spawned {
return
}
dt := c.Dt().Seconds()
2023-12-23 00:09:07 +03:00
cam := c.Camera
keys := c.Keys()
walking := false
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:
2024-01-18 06:06:27 +03:00
p.SetScale(
p.Scale().Add(
gg.X(p.ScaleSpeed*dt),
),
)
2023-12-23 00:09:07 +03:00
case gg.KeyW:
p.Move(gg.Y(-p.MoveSpeed * dt *10))
walking = true
p.Animate(Walk)
2023-12-23 00:09:07 +03:00
case gg.KeyA:
p.Move(gg.X(-p.MoveSpeed * dt))
p.SetScale(gg.V(-1, 1))
walking = true
p.Animate(Walk)
2023-12-23 00:09:07 +03:00
case gg.KeyS:
p.Move(gg.X(-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:
p.Move(gg.X(p.MoveSpeed * dt))
p.SetScale(gg.V(1, 1))
walking = true
p.Animate(Walk)
2023-12-23 00:09:07 +03:00
case gg.KeyR:
cam.Rotate(gg.Pi * p.ScaleSpeed * dt)
2023-12-23 00:09:07 +03:00
case gg.KeyT:
cam.Rotate(-gg.Pi * p.ScaleSpeed * dt)
2023-12-23 00:09:07 +03:00
case gg.KeyRightBracket:
2023-12-28 14:34:52 +03:00
if shift {
p.Rotate(-gg.Pi * 0.3 * dt)
2023-12-23 00:09:07 +03:00
} else {
p.Rotate(+gg.Pi * 0.3 * dt)
2023-12-23 00:09:07 +03:00
}
case gg.KeyF:
2023-12-28 14:34:52 +03:00
if shift {
cam.AddScale(gg.V2(p.ScaleSpeed * dt))
2023-12-23 00:09:07 +03:00
} else {
cam.AddScale(gg.V2(-p.ScaleSpeed * dt))
2023-12-23 00:09:07 +03:00
}
case gg.KeyLeftBracket:
2023-12-28 14:34:52 +03:00
if shift {
rect.Rotate(-gg.Pi * 0.3 * dt)
2023-12-23 00:09:07 +03:00
} else {
rect.Rotate(+gg.Pi * 0.3 * dt)
2023-12-23 00:09:07 +03:00
}
case gg.Key0:
c.Del(p)
case gg.KeyB:
case gg.Key5:
pp := *p
counter++
pp.Spawned = true
pp.Collidable = false
pp.Resolvable = false
c.Spawn(&pp)
2023-12-23 00:09:07 +03:00
}
}
if !walking {
p.Animate(Stand)
}
2024-01-08 07:12:35 +03:00
for _, event := range c.Events {
switch ec := event.(type) {
2023-12-23 00:09:07 +03:00
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
}
c.Camera.Move(ec.Abs)
2024-01-04 17:05:51 +03:00
case *gg.WheelChange:
c.Camera.AddScale(gg.V2(
ec.Offset.Y * dt * p.ScaleSpeed * 40,
2023-12-24 15:05:34 +03:00
))
2024-01-08 07:12:35 +03:00
}}
}
func (p *Player) GetCollisionInterest() []gg.CollisionType {
return []gg.CollisionType{
gg.CollisionStaticPhysics,
}
}
func (p *Player) Resolve(c *Context) {
col := c.Collisions[0]
if !p.Spawned {
fmt.Printf("frame[%d]: the col[0] len(%d): %T, %T\n", c.Frame(), len(c.Collisions), col.What, col.With)
}
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
}
}
}}
2024-01-08 07:12:35 +03:00
}