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.SetScale(gg.V2(1)) ret.SetAround(gg.V2(.5)) ret.MoveSpeed = 40. ret.ScaleSpeed = .2 ret.Animations = playerAnimations ret.TimeBetweenFrames = time.Second/10 ret.Shader = gg.SolidWhiteColorShader ret.Visible = true ret.Layer = PlayerL ret.Collidable = true ret.Resolvable = true return ret } func (p *Player) Start(c *Context) { p.Connect(rect) } 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.SetPosition(p.Position().Sub(gg.Y(2))) p.Move(gg.Y(1)) 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.SetScale( p.Scale().Add( gg.X(p.ScaleSpeed*dt), ), ) case gg.KeyW: p.Move(gg.Y(-p.MoveSpeed * dt *10)) walking = true p.Animate(Walk) case gg.KeyA: p.Move(gg.X(-p.MoveSpeed * dt)) p.SetScale(gg.V(-1, 1)) walking = true p.Animate(Walk) case gg.KeyS: p.Move(gg.X(-p.MoveSpeed * dt)) //p.Position.Y -= p.MoveSpeed * dt walking = true p.Animate(Walk) case gg.KeyD: p.Move(gg.X(p.MoveSpeed * dt)) p.SetScale(gg.V(1, 1)) walking = true p.Animate(Walk) case gg.KeyR: cam.Rotate(gg.Pi * p.ScaleSpeed * dt) case gg.KeyT: cam.Rotate(-gg.Pi * p.ScaleSpeed * dt) case gg.KeyRightBracket: if shift { p.Rotate(-gg.Pi * 0.3 * dt) } else { p.Rotate(+gg.Pi * 0.3 * dt) } case gg.KeyF: if shift { cam.AddScale(gg.V2(p.ScaleSpeed * dt)) } else { cam.AddScale(gg.V2(-p.ScaleSpeed * dt)) } case gg.KeyLeftBracket: if shift { rect.Rotate(-gg.Pi * 0.3 * dt) } else { rect.Rotate(+gg.Pi * 0.3 * dt) } 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) } } 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 } c.Camera.Move(ec.Abs) case *gg.WheelChange: c.Camera.AddScale(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.Move(gg.Y(-1)) _, collides := gg.Collide(p, col.With) if !collides { break LOOP } } }} }