235 lines
5.4 KiB
Plaintext
235 lines
5.4 KiB
Plaintext
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());
|
|
|
|
}
|