132 lines
3.1 KiB
Plaintext
132 lines
3.1 KiB
Plaintext
class Ship {
|
|
float x, y, a;
|
|
float newX, newY;
|
|
boolean colliding = false;
|
|
boolean colliding2 = false;
|
|
boolean hasSaw = false;
|
|
float rotationSpeed = 4; // The speed of rotation
|
|
float movementSpeed = 2; // The speed of movement
|
|
int nextLog;
|
|
|
|
Ship() {
|
|
x = width / 2;
|
|
y = height / 2;
|
|
a = -90;
|
|
}
|
|
|
|
void draw() {
|
|
nextLog = returnIndexOfNearestLog();
|
|
|
|
logCollide(logs);
|
|
// logCollide(trees);
|
|
sawIndicator();
|
|
simulate();
|
|
render();
|
|
|
|
// println(newX, " : ", newY);
|
|
|
|
ship_zero.colliding2 = false;
|
|
}
|
|
|
|
void sawIndicator() {
|
|
if (hasSaw) {
|
|
if (checkDistance(logs[nextLog], ship_zero) < 40) {
|
|
fill(204, 102, 0);
|
|
circle(x, y -20, 20);
|
|
fill(0);
|
|
textSize(32);
|
|
textAlign(CENTER, CENTER);
|
|
text("s", x, y - 27);
|
|
}
|
|
}
|
|
}
|
|
|
|
int returnIndexOfNearestLog() {
|
|
float shortest_distance_log_distance = checkDistance(logs[0], ship_zero);
|
|
int shortest_distance_log = 0;
|
|
for(int i = 0; i < logs.length; i++) {
|
|
if(checkDistance(logs[i], ship_zero) < shortest_distance_log_distance) {
|
|
shortest_distance_log = i;
|
|
shortest_distance_log_distance = checkDistance(logs[i], ship_zero);
|
|
}
|
|
}
|
|
return shortest_distance_log;
|
|
}
|
|
|
|
void simulate() {
|
|
if(newX > 0 && newX < width && newY > 0 && newY < height) {
|
|
colliding = false;
|
|
} else {
|
|
colliding = true;
|
|
}
|
|
|
|
if (ROTATEL) {
|
|
a -= rotationSpeed;
|
|
}
|
|
if (ROTATER) {
|
|
a += rotationSpeed;
|
|
}
|
|
|
|
if (NORTH) {
|
|
newX = x + cos(radians(a)) * movementSpeed;
|
|
newY = y + sin(radians(a)) * movementSpeed;
|
|
|
|
// Check if the new position is within the bounds of the screen
|
|
if(colliding == false && colliding2 == false) {
|
|
x = newX;
|
|
y = newY;
|
|
} else {
|
|
float richtungX = x-newX;
|
|
float richtungY = y-newY;
|
|
x = (10*richtungX) + x;
|
|
y = (10*richtungY) + y;
|
|
}
|
|
} else if (SOUTH) {
|
|
newX = x - cos(radians(a)) * movementSpeed;
|
|
newY = y - sin(radians(a)) * movementSpeed;
|
|
|
|
// Check if the new position is within the bounds of the screen
|
|
if(colliding == false && colliding2 == false) {
|
|
x = newX;
|
|
y = newY;
|
|
} //else {
|
|
// x = newX - x;
|
|
// y = newY - y;
|
|
//}
|
|
}
|
|
}
|
|
|
|
void logCollide(Log[] logs) {
|
|
for(int i = 0; i < logs.length; i++) {
|
|
if(logs[i].sawed == false) {
|
|
if (newX+10 > logs[i].x && newX-10 < logs[i].x + logs[i].logwidth && newY+10 > logs[i].y && newY-10 < logs[i].y + logs[i].logheight) {
|
|
colliding2 = true;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
void collect(Collectable c) {
|
|
if (newX > c.x && newX < c.x + c.cwidth && newY > c.y && newY < c.y + c.cheight) {
|
|
if (c.getClass() == Saw.class ) hasSaw = true;
|
|
c.is_attached = true;
|
|
}
|
|
}
|
|
|
|
void render() {
|
|
pushMatrix();
|
|
translate(x, y);
|
|
rotate(radians(a + 90));
|
|
stroke(255);
|
|
noFill();
|
|
line(0, -10, 10, 10);
|
|
line(10, 10, 0, 5);
|
|
line(0, 5, -10, 10);
|
|
line(-10, 10, 0, -10);
|
|
if(hasSaw) {
|
|
rect(0+2, 0-45, 20, 50);
|
|
}
|
|
popMatrix();
|
|
}
|
|
}
|