/
camera.go
42 lines (34 loc) · 887 Bytes
/
camera.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
package main
import (
"image"
)
type Camera struct {
Pos image.Point // center of the camera
SizeRect image.Rectangle // camera's size
Rect image.Rectangle // camera's bounding box
}
func NewCamera(rect image.Rectangle) Camera {
sz := rect.Size()
r := image.Rect(0, 0, sz.X, sz.Y)
c := Camera{
Pos: image.ZP,
SizeRect: r,
Rect: r,
}
return c
}
// place the camera's center at pt
func (c *Camera) SetCenter(pt image.Point) {
newpos := pt.Sub(c.SizeRect.Size().Div(2))
c.Pos = pt
c.Rect = image.Rect(newpos.X, newpos.Y, newpos.X+c.SizeRect.Dx(), newpos.Y+c.SizeRect.Dy())
}
//
func (c *Camera) Transform(pt image.Point) image.Point {
return pt.Sub(c.Rect.Min) //.Add(c.Rect.Size().Div(2))
}
// check if world tile pt is inside camera bounds c.Rect
// FIXME
func (c *Camera) ContainsWorldPoint(pt image.Point) bool {
return pt.In(c.Rect)
}