2023-12-23 00:09:07 +03:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
//"math/rand"
|
2024-01-16 04:11:42 +03:00
|
|
|
"fmt"
|
2024-01-13 23:46:12 +03:00
|
|
|
"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 {
|
2024-01-13 23:46:12 +03:00
|
|
|
gg.AnimatedSprite
|
2023-12-23 00:09:07 +03:00
|
|
|
MoveSpeed gg.Float
|
|
|
|
ScaleSpeed gg.Float
|
2024-01-11 04:45:28 +03:00
|
|
|
Spawned bool
|
2023-12-23 00:09:07 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewPlayer() *Player {
|
|
|
|
ret := &Player{}
|
|
|
|
ret.Transform = gg.T()
|
2023-12-28 02:31:43 +03:00
|
|
|
ret.Scale = gg.V2(1)
|
2023-12-26 23:31:04 +03:00
|
|
|
ret.Around = gg.V2(.5)
|
2024-01-14 03:51:11 +03:00
|
|
|
ret.MoveSpeed = 40.
|
2023-12-23 00:09:07 +03:00
|
|
|
ret.ScaleSpeed = .2
|
2024-01-13 23:46:12 +03:00
|
|
|
ret.Animations = playerAnimations
|
2024-01-14 00:03:49 +03:00
|
|
|
ret.TimeBetweenFrames = time.Second/10
|
2024-01-17 09:36:08 +03:00
|
|
|
ret.Shader = gg.SolidWhiteColorShader
|
2023-12-23 00:09:07 +03:00
|
|
|
|
|
|
|
ret.Visible = true
|
|
|
|
ret.Layer = PlayerL
|
|
|
|
|
2024-01-16 04:11:42 +03:00
|
|
|
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) {
|
|
|
|
}
|
|
|
|
|
2024-01-16 04:11:42 +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) {
|
2024-01-16 07:37:53 +03:00
|
|
|
p.Position.Y += 2
|
2024-01-11 04:45:28 +03:00
|
|
|
if p.Spawned {
|
|
|
|
return
|
|
|
|
}
|
2024-01-13 23:46:12 +03:00
|
|
|
dt := c.Dt().Seconds()
|
2023-12-23 00:09:07 +03:00
|
|
|
cam := c.Camera
|
|
|
|
keys := c.Keys()
|
|
|
|
|
2024-01-14 00:03:49 +03:00
|
|
|
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:
|
|
|
|
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:
|
2024-01-16 04:11:42 +03:00
|
|
|
p.Position.Y -= p.MoveSpeed * dt*10
|
2024-01-14 00:03:49 +03:00
|
|
|
walking = true
|
|
|
|
p.Animate(Walk)
|
2023-12-23 00:09:07 +03:00
|
|
|
case gg.KeyA:
|
|
|
|
p.Position.X -= p.MoveSpeed * dt
|
2024-01-14 00:03:49 +03:00
|
|
|
p.Scale.X = -1
|
|
|
|
walking = true
|
|
|
|
p.Animate(Walk)
|
2023-12-23 00:09:07 +03:00
|
|
|
case gg.KeyS:
|
|
|
|
p.Position.Y -= p.MoveSpeed * dt
|
2024-01-14 00:03:49 +03:00
|
|
|
walking = true
|
|
|
|
p.Animate(Walk)
|
2023-12-23 00:09:07 +03:00
|
|
|
case gg.KeyD:
|
|
|
|
p.Position.X += p.MoveSpeed * dt
|
2024-01-14 00:03:49 +03:00
|
|
|
p.Scale.X = 1
|
|
|
|
walking = true
|
|
|
|
p.Animate(Walk)
|
2023-12-23 00:09:07 +03:00
|
|
|
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.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:
|
2024-01-11 04:45:28 +03:00
|
|
|
case gg.Key5:
|
|
|
|
pp := *p
|
|
|
|
counter++
|
|
|
|
p.Spawned = true
|
2024-01-16 07:37:53 +03:00
|
|
|
p.Collidable = false
|
2024-01-11 04:45:28 +03:00
|
|
|
c.Spawn(&pp)
|
2023-12-23 00:09:07 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-14 00:03:49 +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
|
|
|
|
}
|
|
|
|
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(
|
2024-01-13 23:46:12 +03:00
|
|
|
ec.Offset.Y * dt * p.ScaleSpeed * 40,
|
2023-12-24 15:05:34 +03:00
|
|
|
))
|
2024-01-08 07:12:35 +03:00
|
|
|
}}
|
2024-01-16 04:11:42 +03:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2024-01-16 07:37:53 +03:00
|
|
|
func (p *Player) GetCollisionInterest() []gg.CollisionType {
|
|
|
|
return []gg.CollisionType{
|
|
|
|
gg.CollisionStaticPhysics,
|
|
|
|
}
|
|
|
|
}
|
2024-01-16 04:11:42 +03:00
|
|
|
|
2024-01-16 07:37:53 +03:00
|
|
|
func (p *Player) Resolve(c *Context) {
|
2024-01-16 04:11:42 +03:00
|
|
|
col := c.Collisions[0]
|
2024-01-16 07:37:53 +03:00
|
|
|
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 :
|
2024-01-16 04:11:42 +03:00
|
|
|
LOOP:
|
|
|
|
for {
|
2024-01-16 07:37:53 +03:00
|
|
|
p.Position.Y -= 1
|
|
|
|
_, collides := gg.Collide(p, col.With)
|
2024-01-16 04:11:42 +03:00
|
|
|
if !collides {
|
|
|
|
break LOOP
|
|
|
|
}
|
|
|
|
}
|
2024-01-16 07:37:53 +03:00
|
|
|
}}
|
2024-01-08 07:12:35 +03:00
|
|
|
}
|
|
|
|
|