-
Notifications
You must be signed in to change notification settings - Fork 0
/
wall.go
69 lines (59 loc) · 1.65 KB
/
wall.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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package main
import (
"github.com/faiface/pixel"
"github.com/faiface/pixel/imdraw"
"github.com/faiface/pixel/pixelgl"
"golang.org/x/image/colornames"
)
const (
wallThick = 10
halfThick = wallThick / 2
)
type Wall struct {
points []pixel.Vec
}
func NewWall(points []pixel.Vec) Wall {
var linePoints []pixel.Vec
for i, point := range points {
if i == len(points)-1 {
prevPoint := points[i-1]
angle := prevPoint.Sub(point).Angle()
linePoints = append(linePoints, pixel.V(-halfThick, -halfThick).Rotated(angle).Add(point))
linePoints = append(linePoints, pixel.V(-halfThick, halfThick).Rotated(angle).Add(point))
} else {
nextPoint := points[i+1]
angle := nextPoint.Sub(point).Angle()
linePoints = append(linePoints, pixel.V(-halfThick, halfThick).Rotated(angle).Add(point))
}
}
for i := range points {
ri := len(points) - i - 1
point := points[ri]
if ri == 0 {
prevPoint := points[ri+1]
angle := prevPoint.Sub(point).Angle()
linePoints = append(linePoints, pixel.V(-halfThick, -halfThick).Rotated(angle).Add(point))
linePoints = append(linePoints, pixel.V(-halfThick, halfThick).Rotated(angle).Add(point))
} else {
nextPoint := points[ri-1]
angle := nextPoint.Sub(point).Angle()
linePoints = append(linePoints, pixel.V(halfThick, halfThick).Rotated(angle).Add(point))
}
}
return Wall{
points: linePoints,
}
}
func (w *Wall) Move(move pixel.Vec) {
for i, point := range w.points {
w.points[i] = point.Add(move)
}
}
func (w *Wall) Draw(win *pixelgl.Window) {
imd := imdraw.New(nil)
imd.Color = colornames.Gray
imd.EndShape = imdraw.RoundEndShape
imd.Push(w.points...)
imd.Line(1)
imd.Draw(win)
}