-
Notifications
You must be signed in to change notification settings - Fork 0
/
MovableBoardItem.pde
207 lines (174 loc) · 4.31 KB
/
MovableBoardItem.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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
public class MovableBoardItem {
protected PVector pos, velocity, accel;
protected PVector GRAVITY;
protected int maxRows, maxCols;
protected int[][] glyphData;
protected int numOfJumps;
protected boolean LIFE;
protected boolean BOUNDS;
public MovableBoardItem(int colAt, int rowAt) {
pos = new PVector(colAt, rowAt);
velocity = new PVector(0, 0);
accel = new PVector(0, 0);
GRAVITY = new PVector(0, 3);
LIFE = true;
BOUNDS = true;
numOfJumps = 0;
}
public void update() {
if (LIFE) {
if (velocity.x > 3 || velocity.x < -3)
accel.set(0, accel.y);
if (velocity.x > 3)
velocity.set(3, velocity.y);
if (velocity.x < -3)
velocity.set(-3, velocity.y);
if (velocity.y > 2)
velocity.set(velocity.x, 2);
accel.add(GRAVITY);
if (accel.mag()>0.01) {
velocity.add(accel.mult(5/frameRate));
}
if (velocity.mag()<0.15) {
velocity.mult(0);
}
if (BOUNDS) {
if (onRightEdge() && velocity.x>0)
velocity.set(0, velocity.y);
if (onLeftEdge() && velocity.x<0)
velocity.set(0, velocity.y);
}
pos.add(velocity);
if (pos.y>=maxRows - glyphData.length) {
pos.set(pos.x, maxRows - glyphData.length);
numOfJumps = 0;
}
}
}
public void applyForce(PVector force) {
accel.add(force);
}
public void show(int x, int y, int cellSize) {
for (int row = 0; row < glyphData.length; row++) {
for (int col = 0; col < glyphData[row].length; col++) {
int fillColor = glyphData[row][col];
if (fillColor != -1) {
fill(fillColor);
rect(x + col*cellSize, y + row*cellSize, cellSize, cellSize);
}
}
}
}
public ArrayList<Cell> getEdge(char direction) {
ArrayList<Cell> result= new ArrayList<Cell>();
if (direction == 'u') {
for (int i = 0; i < glyphData[0].length; i++) {
int colVal = i + (int)pos.x;
int rowVal= (int)pos.y;
Cell c = new Cell(rowVal, colVal);
result.add(c);
}
}
if (direction == 'd') {
for (int i = 0; i < glyphData[0].length; i++) {
int colVal = i + (int)pos.x;
int rowVal= (int)pos.y + glyphData[0].length - 1;
Cell c = new Cell(rowVal, colVal);
result.add(c);
}
}
if (direction == 'r') {
for (int i = 0; i < glyphData.length; i++) {
int colVal = (int)pos.x + glyphData[0].length - 1;
int rowVal= (int)pos.y + i;
Cell c = new Cell(rowVal, colVal);
result.add(c);
}
}
if (direction == 'l') {
for (int i = 0; i < glyphData.length; i++) {
int colVal = (int)pos.x;
int rowVal= (int)pos.y + i;
Cell c = new Cell(rowVal, colVal);
result.add(c);
}
}
return result;
}
public void setData(int[][] data) {
glyphData = data;
}
public void setBounds(int rows, int cols) {
maxRows = rows;
maxCols = cols;
}
public boolean onGround() {
if (((maxRows - glyphData.length) - pos.y) <= 0)
return true;
else
return false;
}
public boolean onLeftEdge() {
if (pos.x <= 1)
return true;
else
return false;
}
public boolean onRightEdge() {
if ((maxCols - glyphData[0].length - pos.x) <= 0)
return true;
else
return false;
}
public int getRowId() {
return (int)pos.y;
}
public int getColId() {
return (int)pos.x;
}
public PVector getAccel() {
return accel;
}
public void setAccel(PVector a) {
accel.set(a);
}
public PVector getVelocity() {
return velocity;
}
public void setVelocity(PVector v) {
velocity.set(v);
}
public PVector getPos() {
return pos;
}
public void setPos(PVector p) {
pos.set(p);
}
public PVector getGravity() {
return GRAVITY;
}
public int getNumOfJumps() {
return numOfJumps;
}
public void setNumOfJumps(int j) {
numOfJumps = j;
}
public int getHeight() {
return glyphData.length;
}
public int getWidth() {
return glyphData[0].length;
}
public boolean getLife() {
return LIFE;
}
public void setLife(boolean l) {
LIFE = l;
}
public boolean getBounds() {
return BOUNDS;
}
public void setBounds(boolean b) {
BOUNDS = b;
}
}