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 = 40. ret.ScaleSpeed = .2 ret.Animations = playerAnimations ret.TimeBetweenFrames = time.Second/10 ret.Visible = true ret.Layer = PlayerL ret.Collidable = true ret.Resolvable = true return ret } func (p *Player) Start(c *Context) { } 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) } func (p *Player) Update(c *Context) { p.Position.Y += 2 if p.Spawned { return } dt := c.Dt().Seconds() cam := c.Camera keys := c.Keys() walking := false 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*10 walking = true p.Animate(Walk) case gg.KeyA: p.Position.X -= p.MoveSpeed * dt p.Scale.X = -1 walking = true p.Animate(Walk) case gg.KeyS: p.Position.Y -= p.MoveSpeed * dt walking = true p.Animate(Walk) case gg.KeyD: p.Position.X += p.MoveSpeed * dt p.Scale.X = 1 walking = true p.Animate(Walk) 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 p.Collidable = false c.Spawn(&pp) } } if !walking { p.Animate(Stand) } 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, )) }} } 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.Position.Y -= 1 _, collides := gg.Collide(p, col.With) if !collides { break LOOP } } }} }