func (s *State) Visible(sprite *twodee.Sprite) bool { var ( wb = s.window.View.Sub(s.env.Bounds().Min) sb = sprite.RelativeBounds(s.env) buffer = float32(256) ) wb.Min.X -= buffer wb.Min.Y -= buffer wb.Max.X += buffer wb.Max.X += buffer return sb.Overlaps(wb) }
func (s *State) UpdateSprite(sprite *twodee.Sprite, ms float32) (result int) { sprite.VelocityY += 0.005 * ms // Gravity var ( dX = sprite.VelocityX * ms dY = sprite.VelocityY * ms b = sprite.RelativeBounds(s.env) ) if b.Min.X+dX < 0 { result |= HITLEFT sprite.VelocityX = 0 sprite.Move(twodee.Pt(1, 0)) dX = 0 } if b.Max.X+dX > s.env.Width() { /* fmt.Printf("HITRIGHT\n") fmt.Printf("sprite.RelativeBounds(s.env) %v\n", sprite.RelativeBounds(s.env)) fmt.Printf("sprite.LocalBounds() %v\n", sprite.Bounds()) */ result |= HITRIGHT sprite.VelocityX = 0 sprite.Move(twodee.Pt(-1, 0)) dX = 0 } if sprite.Collide { for _, block := range s.boundaries { if dX != 0 && !sprite.TestMove(dX, 0, block) { if sprite.TestMove(dX, -block.Height(), block) { // Allows running up small bumps sprite.Move(twodee.Pt(0, -block.Height())) } else { if dX < 0 { sprite.MoveTo(twodee.Pt(block.X()+block.Width(), sprite.Y())) result |= HITLEFT } else { sprite.MoveTo(twodee.Pt(block.X()-sprite.Width(), sprite.Y())) result |= HITRIGHT } sprite.VelocityX = 0 dX = 0 } } if dY != 0 && !sprite.TestMove(0, dY, block) { if dY < 0 { sprite.MoveTo(twodee.Pt(sprite.X(), block.Y()+block.Height())) result |= HITTOP } else { sprite.MoveTo(twodee.Pt(sprite.X(), block.Y()-sprite.Height())) result |= HITBOTTOM } sprite.VelocityY = 0 dY = 0 } } } if dX != 0 || dY != 0 { sprite.Move(twodee.Pt(dX, dY)) } //sprite.MoveTo(twodee.Pt(Round(sprite.X()), Round(sprite.Y()))) return }