From 81cd7e0a8fa1ad64fe7a73ff512a71b94498ed56 Mon Sep 17 00:00:00 2001 From: Makussu Date: Sun, 10 Dec 2023 01:54:49 +0100 Subject: [PATCH] project --- kek2.pde | 234 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ keys.pde | 27 +++++++ 2 files changed, 261 insertions(+) create mode 100644 kek2.pde create mode 100644 keys.pde diff --git a/kek2.pde b/kek2.pde new file mode 100644 index 0000000..f145e83 --- /dev/null +++ b/kek2.pde @@ -0,0 +1,234 @@ +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; + float testY = ship.y; + + // which edge is closest? + if (ship.x < log.x) testX = log.x; + else if (ship.x > log.x+log.logwidth) testX = log.x+log.logwidth; + if (ship.y < log.y) testY = log.y; + else if (ship.y > log.y+log.logheight) testY = log.y+log.logheight; + + // get distant + float distX = ship.x-testX; + float distY = ship.y-testY; + float distance = sqrt( (distX*distX) + (distY*distY) ); + + line(testX, testY, ship_zero.x, ship_zero.y); + + // if the distance is less than the radius, collision! + return distance; +} + +// ----- +boolean isColliding = false; + +Ship ship_zero; + +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) }; + +void setup() { + size(600, 600); + ship_zero = new Ship(); +} + +void draw() { + background(50); + + + for(int i = 0; i < logs.length; i++) { + logs[i].drawLog(i); + } + + + for(int i = 0; i < trees.length; i++) { + trees[i].draw(); + } + + // println(ship_zero.newX, ", ", ship_zero.newY); + // println(ship_zero.returnIndexOfNearestLog()); + + ship_zero.draw(); + + + 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()); + +} diff --git a/keys.pde b/keys.pde new file mode 100644 index 0000000..1ac97a6 --- /dev/null +++ b/keys.pde @@ -0,0 +1,27 @@ +boolean NORTH, SOUTH, ROTATEL, ROTATER; +void keyPressed() { + if (key == CODED) { + if(keyCode == UP) { + NORTH = true; + } else if (keyCode == DOWN) { + SOUTH = true; + } else if (keyCode == LEFT) { + ROTATEL = true; + } else if (keyCode == RIGHT) { + ROTATER = true; + } + } else if (key == 's') { // SAW + if (ship_zero.hasSaw) { + if(checkDistance(logs[1], ship_zero) < 40) { + logs[1].sawed = true; + } + } + } +} + +void keyReleased() { + if(keyCode == UP) {NORTH = false; } + else if (keyCode == DOWN) SOUTH = false; + else if (keyCode == LEFT) {ROTATEL = false; } + else if (keyCode == RIGHT) {ROTATER = false; } +}