-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPGraphical.pde
123 lines (96 loc) · 3.18 KB
/
PGraphical.pde
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
import java.util.*;
static enum Shape{
ELLIPSE,
RECTANGLE,
TRIANGLE
}
public class ShapesInitializerInstance{
public void init(){
if (basicShapes.size() == 0){
PShape createdShape = null;
createdShape = createShape();
createdShape.beginShape();
for (float a = 0; a < TWO_PI; a += 0.1) {
createdShape.vertex(cos(a)*100, sin(a)*100);
}
createdShape.endShape();
basicShapes.put(Shape.ELLIPSE, createdShape);
createdShape = null;
createdShape = createShape();
createdShape.beginShape(QUADS);
createdShape.vertex(0, 0);
createdShape.vertex(0, 20);
createdShape.vertex(20, 20);
createdShape.vertex(20, 0);
createdShape.endShape();
basicShapes.put(Shape.RECTANGLE, createdShape);
createdShape = null;
createdShape = createShape();
createdShape.beginShape(TRIANGLES);
createdShape.vertex(0, 0);
createdShape.vertex(10, 20);
createdShape.vertex(20, 0);
createdShape.endShape();
basicShapes.put(Shape.TRIANGLE, createdShape);
}
}
}
public class PGraphical extends Graphical{
PShape myShape;
color myStrokeColor;
color myFillColor;
void scale(PVector _relativeScale) {
relativeScale = _relativeScale;
//myShape.scale(scale.x * gameObject.transform.scale.x, scale.y * gameObject.transform.scale.y);
}
void setRotation(float _relativeRotation) {
relativeAngle = _relativeRotation;
}
PShape createBasicShape(Shape shape){
PShape createdShape = null;
switch (shape){
case ELLIPSE :
return basicShapes.get(Shape.ELLIPSE);
case RECTANGLE :
return basicShapes.get(Shape.RECTANGLE);
case TRIANGLE :
return basicShapes.get(Shape.TRIANGLE);
}
return null;
}
void setPShape(Shape shape){
PShape basicShape = basicShapes.get(shape);
if (basicShape == null)
basicShape = createBasicShape(shape);
myShape = basicShape;
}
PGraphical(GameObject _gameObject, PVector _relativeScale, color strokeColor, color fillColor, int _layer, Shape shape, Camera camera) {
super(_gameObject, PGraphical.class, _layer, camera);
myStrokeColor = strokeColor;
myFillColor = fillColor;
stroke(red(myStrokeColor), green(myStrokeColor), blue(myStrokeColor));
fill(red(myFillColor), green(myFillColor), blue(myFillColor));
setPShape(shape);
relativeScale = new PVector(1, 1);
relativePos = PVector.zero();
}
void display(Camera camera){
shapeMode(CENTER);
float sx = gameObject.transform.size.x;
float sy = gameObject.transform.size.y;
float rx = gameObject.transform.getPosition().x + sx / 2f + camera.getPosition().x;//TEST
float ry = gameObject.transform.getPosition().y + sy / 2f + camera.getPosition().y;
pushMatrix();
/// Go to the point around which the shape must rotate
translate(rx, ry);
/// Rotate the coordinate system
rotate(gameObject.transform.rotation + relativeAngle);
pushMatrix();
/// Translate a bit to center the shape on a given point
translate(0, 0);
/// Draw the shape
scale(relativeScale);
shape(myShape, 0, 0, sx, sy);
popMatrix();
}
}