// 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 { // Movement PVector pos = new PVector(-width/2, height/2); // position PVector speed = new PVector(0, 0); // The speed of movement PVector acceleration = new PVector(); float a; // angle // For the collision PVector newPos = new PVector(width/2, height/2); float newX, newY; boolean colliding = false; boolean colliding_logs = false; boolean hasSaw = false; int nextLog; int health = 3; Ship() { // pos.x = width/2; // pos.y = height/2; a = 0; newX = width/2; newY = height/2; } void draw() { nextLog = returnIndexOfNearestLog(); logCollide(logs); logCollide(walls); sawIndicator(); simulate(); render(); // println(newX, " : ", newY); if (health == 0) { exit(); } colliding_logs = 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++) { // only logs that are not sawed already if (!logs[i].sawed) { 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() { // First do the Math but dont move something // Rotate PLayer if (ROTATEL) { a -= 0.03; } if (ROTATER) { a += 0.03; } PVector direction = new PVector(); direction.x = cos(a); direction.y = sin(a); direction.mult(0.07); acceleration = direction; // add speed if pressing NORTH if(NORTH) { speed.add(acceleration); speed.limit(1.3); } else { // decrease it if not speed.mult(0.95); } // Check new pos newPos = pos.add(speed); // Collide with outside walls if( newPos.x > width || newPos.y < 0 || newPos.y > height) { colliding = true; // println("collide"); } else { colliding = false; } // Check if the new position is within the bounds of the screen // - Handle it more like https://processing.org/examples/circlecollision.html if(colliding == false && colliding_logs == false) { pos.add(speed); } else { speed.limit(0); speed.sub(acceleration); pos.add(0, 0); pos.add(speed.mult(2)); } // println(speed); } void logCollide(Log[] loggers) { for(int i = 0; i < loggers.length; i++) { if(loggers[i].sawed == false) { if (newPos.x+10 > loggers[i].x && newPos.x-10 < loggers[i].x + loggers[i].logwidth && newPos.y+10 > loggers[i].y - loggers[i].logheight && newPos.y-10 < loggers[i].y) { colliding_logs = true; } } } } void collect(Collectable c) { if (pos.dist(c.pos) < 20) { if (c.getClass() == Saw.class ) hasSaw = true; c.is_attached = true; } } void render() { pushMatrix(); translate(pos.x, pos.y); rotate(a); stroke(255); noFill(); // line(0, -10, 10, 10); // line(10, 10, 0, 5); // line(0, 5, -10, 10); // line(-10, 10, 0, -10); // triangle(- 5, - 5, - 5, 5, 10, 0); imageMode(CENTER); image(player_sprite, 0, 0); if(hasSaw) { image(saw_sprite, 20, 0); } popMatrix(); } }