-
Notifications
You must be signed in to change notification settings - Fork 33
/
shapeClass.go
82 lines (67 loc) · 1.65 KB
/
shapeClass.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
70
71
72
73
74
75
76
77
78
79
80
81
82
package chipmunk
import (
"github.com/vova616/chipmunk/transform"
"github.com/vova616/chipmunk/vect"
)
type ShapeType int
const (
ShapeType_Circle = 0
ShapeType_Segment = 1
ShapeType_Polygon = 2
ShapeType_Box = 3
numShapes = iota
)
func (st ShapeType) ToString() string {
switch st {
case ShapeType_Circle:
return "Circle"
case ShapeType_Segment:
return "Segment"
case ShapeType_Polygon:
return "Polygon"
case ShapeType_Box:
return "Box"
default:
return "Unknown"
}
panic("never reached")
}
type ShapeClass interface {
ShapeType() ShapeType
// Update the shape with the new transform and compute the AABB.
update(xf transform.Transform) AABB
// Returns if the given point is located inside the shape.
TestPoint(point vect.Vect) bool
Moment(mass float32) vect.Float
Clone(s *Shape) ShapeClass
//marshalShape(shape *Shape) ([]byte, error)
//unmarshalShape(shape *Shape, data []byte) error
}
// Returns shape.ShapeClass as CircleShape or nil.
func (shape *Shape) GetAsCircle() *CircleShape {
if circle, ok := shape.ShapeClass.(*CircleShape); ok {
return circle
}
return nil
}
// Returns shape.ShapeClass as PolygonShape or nil.
func (shape *Shape) GetAsPolygon() *PolygonShape {
if poly, ok := shape.ShapeClass.(*PolygonShape); ok {
return poly
}
return nil
}
// Returns shape.ShapeClass as SegmentShape or nil.
func (shape *Shape) GetAsSegment() *SegmentShape {
if seg, ok := shape.ShapeClass.(*SegmentShape); ok {
return seg
}
return nil
}
// Returns shape.ShapeClass as BoxShape or nil.
func (shape *Shape) GetAsBox() *BoxShape {
if box, ok := shape.ShapeClass.(*BoxShape); ok {
return box
}
return nil
}