From a365254312bd37286aabb8feaa06eb0d93d43c44 Mon Sep 17 00:00:00 2001 From: Makussu Date: Mon, 11 Dec 2023 00:03:26 +0100 Subject: [PATCH] all the features --- collect.pde | 43 ++++++++++++ kek2.pde | 194 ++++------------------------------------------------ keys.pde | 4 +- logs.pde | 55 +++++++++++++++ ship.pde | 122 +++++++++++++++++++++++++++++++++ 5 files changed, 235 insertions(+), 183 deletions(-) create mode 100644 collect.pde create mode 100644 logs.pde create mode 100644 ship.pde diff --git a/collect.pde b/collect.pde new file mode 100644 index 0000000..6b03354 --- /dev/null +++ b/collect.pde @@ -0,0 +1,43 @@ +class Collectable { + float x, y, cwidth, cheight; + boolean is_attached = false; + + Collectable(float xpos, float ypos, float cw, float ch) { + x = xpos; + y = ypos; + cwidth = cw; + cheight = ch; + } +} + +class Saw extends Collectable { + + Saw(float x, float y, float cw, float ch) { + super(x, y, cw, ch); + } + + void drawSaw() { + if (!is_attached) { + stroke(0); + fill(210, 210, 0); + rect(x, y, cwidth, cheight); + fill(0); + } + } +} + +class Bob extends Collectable { + + Bob(float x, float y, float cw, float ch) { + super(x, y, cw, ch); + } + + void drawBob() { + if (!is_attached) { + stroke(0); + fill(255, 209, 152); + rect(x, y, cwidth, cheight); + fill(0); + } + } +} diff --git a/kek2.pde b/kek2.pde index f145e83..ffab7e7 100644 --- a/kek2.pde +++ b/kek2.pde @@ -1,171 +1,4 @@ -class Ship { - float x, y, a; - float newX, newY; - boolean colliding = false; - boolean colliding2 = false; - boolean hasSaw = true; - float rotationSpeed = 4; // The speed of rotation - float movementSpeed = 4; // The speed of movement - Ship() { - x = width / 2; - y = height / 2; - a = -90; - } - - void draw() { - logCollide(logs); - logCollide(trees); - sawIndicator(); - simulate(); - render(); - // println(colliding); - - ship_zero.colliding2 = false; - } - - void sawIndicator() { - if (hasSaw) { - if (checkDistance(logs[returnIndexOfNearestLog()], 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() { - int longest_distance_log = 0; - float longest_distance_log_distance = 0; - for(int i = 0; i < logs.length; i++) { - if(checkDistance(logs[i], ship_zero) > longest_distance_log_distance) { - println(checkDistance(logs[i], ship_zero), " other ",longest_distance_log_distance); - longest_distance_log = i; - longest_distance_log_distance = checkDistance(logs[i], ship_zero); - } - } - return longest_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 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; - } - } - } - - 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 treeCollide(Tree[] trees) { - // for(int i = 0; i < trees.length; i++) { - - // } - // } - - 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); - popMatrix(); - } -} - -class Log { - float x, y, logwidth, logheight; - boolean sawed = false; - - color logcolor; - color strokecolor = logcolor; - - Log (float xpos, float ypos, float logw, float logh, boolean hasC) { - x = xpos; - y = ypos; - logwidth = logw; - logheight = logh; - } - - void drawLog(int logtext) { - - if (sawed) { - logcolor = color(128, 88, 60); - } else { - logcolor = color(133, 79, 51); - } - - stroke(strokecolor); // Set stroke color for the log - strokeWeight(1); // Set stroke weight for the log - fill(logcolor); // Set fill color for the log - // rectMode(CENTER); - rect(x, y, logwidth, logheight); // Draw the log as a rectangle - text(logtext, x, y); - // rectMode(CORNER); - strokeWeight(1); - stroke(0); - } - -} - -class Tree extends Log { - Tree (float x, float y, float logw, float logh, boolean hasC) { - super(x, y, logw, logh, hasC); - } - void draw() { - color treecolor = color(133, 79, 51); - color bushes = color(107, 117, 48); - - // rectMode(CENTER); - fill(treecolor); - rect(x, y, logwidth, logheight); - fill(bushes); - ellipse(x, (y-40), 170*0.3, 160*0.3); - // rectMode(CORNER); - } -} float checkDistance(Log log, Ship ship) { float testX = ship.x; @@ -192,14 +25,16 @@ float checkDistance(Log log, Ship ship) { boolean isColliding = false; Ship ship_zero; +Saw saw_zero; +Bob bob; -Log log_zero; -Log[] logs = {new Log(100, 200, 100, 100, true), new Log(500, 200, 200, 100, true) }; -Tree[] trees = {new Tree(50, 50, 15, 40, true), new Tree(400, 420, 15, 40, true) }; +Log[] logs = {new Log(100, 200, 100, 100, false), new Log(500, 200, 200, 100, false), new Log(400, 400, 20, 100, true) }; void setup() { size(600, 600); ship_zero = new Ship(); + saw_zero = new Saw(100, 100, 20, 50); + bob = new Bob(100, 400, 20, 40); } void draw() { @@ -211,24 +46,21 @@ void draw() { } - for(int i = 0; i < trees.length; i++) { - trees[i].draw(); - } - - // println(ship_zero.newX, ", ", ship_zero.newY); - // println(ship_zero.returnIndexOfNearestLog()); + // for(int i = 0; i < trees.length; i++) { + // trees[i].draw(); + // } ship_zero.draw(); - + saw_zero.drawSaw(); + ship_zero.collect(saw_zero); + bob.drawBob(); + ship_zero.collect(bob); for(int i = 0; i < logs.length; i++) { logs[i].strokecolor = logs[i].logcolor; } - logs[ship_zero.returnIndexOfNearestLog()].strokecolor = color(255, 0, 0); - - // println(checkDistance(logs[0], ship_zero), " other ", checkDistance(logs[1], ship_zero) ); - // println(ship_zero.returnIndexOfNearestLog()); + logs[ship_zero.nextLog].strokecolor = color(255, 0, 0); } diff --git a/keys.pde b/keys.pde index 1ac97a6..798934e 100644 --- a/keys.pde +++ b/keys.pde @@ -12,8 +12,8 @@ void keyPressed() { } } else if (key == 's') { // SAW if (ship_zero.hasSaw) { - if(checkDistance(logs[1], ship_zero) < 40) { - logs[1].sawed = true; + if(checkDistance(logs[ship_zero.nextLog], ship_zero) < 40) { + logs[ship_zero.nextLog].sawed = true; } } } diff --git a/logs.pde b/logs.pde new file mode 100644 index 0000000..75dd75e --- /dev/null +++ b/logs.pde @@ -0,0 +1,55 @@ +class Log { + float x, y, logwidth, logheight; + boolean sawed = false; + boolean is_tree = false; + + color logcolor; + color strokecolor = logcolor; + + Log (float xpos, float ypos, float logw, float logh, boolean ist) { + x = xpos; + y = ypos; + logwidth = logw; + logheight = logh; + is_tree = ist; + } + + void drawLog(int logtext) { + + if (sawed) { + logcolor = color(128, 88, 60); + } else { + logcolor = color(133, 79, 51); + } + + stroke(strokecolor); // Set stroke color for the log + strokeWeight(1); // Set stroke weight for the log + fill(logcolor); // Set fill color for the log + // rectMode(CENTER); + rect(x, y, logwidth, logheight); // Draw the log as a rectangle + text(logtext, x, y); + // rectMode(CORNER); + strokeWeight(1); + stroke(0); + + if(is_tree) { + color bushes = color(107, 117, 48); + fill(bushes); + ellipse(x+logwidth/2, y+5, 170*0.3, 160*0.3); + } + } + +} + +class Tree extends Log { + Tree (float x, float y, float logw, float logh, boolean hasC) { + super(x, y, logw, logh, hasC); + } + void draw() { + color treecolor = color(133, 79, 51); + + // rectMode(CENTER); + drawLog(1); + + } +} diff --git a/ship.pde b/ship.pde new file mode 100644 index 0000000..6c7a466 --- /dev/null +++ b/ship.pde @@ -0,0 +1,122 @@ +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 = 4; // 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(colliding); + + 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 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; + } + } + } + + 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(); + } +}