camera.go (718B)
1 package main 2 3 import ( 4 "math" 5 ) 6 7 type Camera struct { 8 x, y float64 // current position (top left corner) 9 maxWidth, maxHeight float64 // global browsable area size 10 width, height float64 // viewport size 11 } 12 13 func (cam *Camera) Init(width, height, maxWidth, maxHeight float64) { 14 cam.width = width 15 cam.height = height 16 cam.maxWidth = maxWidth - width 17 cam.maxHeight = maxHeight - height 18 } 19 20 func (cam *Camera) ChangeX(diff float64) { 21 if diff < 0 { 22 cam.x = math.Max(cam.x+diff, 0) 23 } else { 24 cam.x = math.Min(cam.x+diff, cam.maxWidth) 25 } 26 } 27 28 func (cam *Camera) ChangeY(diff float64) { 29 if diff < 0 { 30 cam.y = math.Max(cam.y+diff, 0) 31 } else { 32 cam.y = math.Min(cam.y+diff, cam.maxHeight) 33 } 34 }