-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHarvester.pde
140 lines (123 loc) · 4.15 KB
/
Harvester.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
class Harvester {
float x, y, speed;
boolean hasMoved = false;
boolean moving = false;
int movingSteps = 0;
int dx = 0;
int dy = 0;
// Generates a harvester and initializes its sped
Harvester() {
this.generate(wurm);
speed = board.getBoxSize() / 7;
}
// Generates a new harvester in a random location while avoiding generating inside of any wurm
void generate(Sandworm wurm) {
boolean inWurm = false;
boolean first = true;
while(true) {
int rand = int(random(board.numOfBoxes-3)) + 2;
x = board.getXPos() - (board.getBoxSize()/2) + board.getBoxSize() * rand;
if (first) x = wurm.body.get(1).x;
rand = int(random(board.numOfBoxes-3)) + 2;
y = board.getYPos() - (board.getBoxSize()/2) + board.getBoxSize() * rand;
if (first) y = wurm.body.get(1).y;
first = false;
for (int i = 0; i < wurm.body.size(); i++) {
Body curr = wurm.body.get(i);
if (curr.x > this.getBound("left") && curr.x < this.getBound("right") && curr.y < this.getBound("bottom") && curr.y > this.getBound("top")) {
inWurm = true;
}
if (inWurm) break;
}
if (!inWurm && wurm2 != null) {
for (int i = 0; i < wurm2.body.size(); i++) {
Body curr = wurm2.body.get(i);
if (curr.x > this.getBound("left") && curr.x < this.getBound("right") && curr.y < this.getBound("bottom") && curr.y > this.getBound("top")) {
inWurm = true;
}
if (inWurm) break;
}
}
// If the harvester was generated inside the wurm, reset inWurm and retry
if (inWurm) {
inWurm = false;
continue;
}
hasMoved = false;
break;
}
}
// Displays the harvester on the board and calls the move function if the game mode is hard
void display() {
if (!gc.isEasy) move();
fill(255, 234, 0);
ellipse(x, y, 10, 10);
imageMode(CENTER);
image(harvesterImage, x, y, board.getBoxSize()-4, board.getBoxSize()-4);
}
// Checks whether a wurm is within a specified radius and moves one tile if it has not moved already
void move() {
Sandworm wurmToCheck = wurm;
int checks = 1;
if (wurm2 != null) checks++;
for (int i=0; i < checks; i++) {
if (i == 1) wurmToCheck = wurm2;
if (!hasMoved) {
float radius = board.getBoxSize() * 3;
Body head = wurmToCheck.body.get(0);
if (head.x > x-radius && head.x < x+radius && head.y > y-radius && head.y < y+radius) {
moving = true;
hasMoved = true;
int rand = int(random(0, 4));
if (rand == 0) { //<>//
dx = -1;
dy = 0;
} else if (rand == 1) {
dx = 1;
dy = 0;
} else if (rand == 2) {
dx = 0;
dy = -1;
} else {
dx = 0;
dy = 1;
}
// Temporarily change harvester position and check if it will move into the wurm
// If it will, just abort the move
x += (dx * board.getBoxSize());
y += (dy * board.getBoxSize());
for (int j=0; j<wurmToCheck.body.size(); j++) {
Body curr = wurmToCheck.body.get(j);
if (curr.x > getBound("left") && curr.x < getBound("right") && curr.y > getBound("top") && curr.y < getBound("bottom")) {
moving = false;
}
}
x -= (dx * board.getBoxSize());
y -= (dy * board.getBoxSize());
}
}
if (moving) {
x += (dx * speed);
y += (dy * speed);
movingSteps++;
if (movingSteps >= 8) {
movingSteps = 0;
moving = false;
}
}
}
}
// Returns the bounds of the harvester based on position and size
float getBound(String side) {
if (side == "left") {
return this.x - (board.boxSize/2);
} else if (side == "right") {
return this.x + (board.boxSize/2);
} else if (side == "top") {
return this.y - (board.boxSize/2);
} else if (side == "bottom") {
return this.y + (board.boxSize/2);
}
return -1.0;
}
}