processing_sim/ship.pde

149 lines
3.5 KiB
Plaintext

// Roboter muss abprallen, kaputt gehen
// TODO Abprallen nicht machen, wenn newx nicht erreicht sein kann, sondern wenn Robi tatsächlich die Wand berührt
class Ship {
PVector pos = new PVector();
float a;
float newX, newY;
boolean colliding = false;
boolean colliding2 = false;
boolean hasSaw = false;
float rotationSpeed = 4; // The speed of rotation
float speed; // The speed of movement
float maxSpeed = 4;
float minSpeed = 2;
int nextLog;
int health = 3;
Ship() {
pos.x = width/2;
pos.y = height/2;
a = -90;
newX = width/2;
newY = height/2;
}
void draw() {
nextLog = returnIndexOfNearestLog();
logCollide(logs);
// logCollide(trees);
sawIndicator();
simulate();
render();
// println(newX, " : ", newY);
if (health == 0) {
exit();
}
colliding2 = false;
}
void sawIndicator() {
if (hasSaw) {
if (checkDistance(logs[nextLog], ship_zero) < 40) {
fill(204, 102, 0);
circle(pos.x, pos.y -20, 20);
fill(0);
textSize(32);
textAlign(CENTER, CENTER);
text("s", pos.x, pos.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 = pos.x + cos(radians(a)) * speed;
newY = pos.y + sin(radians(a)) * speed;
// Check if the new position is within the bounds of the screen
if(colliding == false && colliding2 == false) {
pos.x = newX;
pos.y = newY;
} else {
float richtungX = pos.x-newX;
float richtungY = pos.y-newY;
pos.x = (7*richtungX) + pos.x;
pos.y = (7*richtungY) + pos.y;
health = health - 1;
}
} else if (SOUTH) {
newX = pos.x - cos(radians(a)) * speed;
newY = pos.y - sin(radians(a)) * speed;
// Check if the new position is within the bounds of the screen
if(colliding == false && colliding2 == false) {
pos.x = newX;
pos.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(pos.x, pos.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();
}
}