processing_sim/ship.pde
Maximilian Ruhm d309b0f45d main
2023-12-22 14:17:38 +01:00

179 lines
3.9 KiB
Plaintext

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 = 150;
Ship() {
// pos.x = width/2;
// pos.y = height/2;
a = 0;
newX = width/2;
newY = height/2;
}
// Tarek
void draw() {
nextLog = returnIndexOfNearestLog();
logCollide(logs);
logCollide(walls);
sawIndicator();
simulate();
render();
// println(newX, " : ", newY);
if (health == 0) {
isgame = false;
isend = true;
}
colliding_logs = false;
}
// Max, julia
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);
}
}
}
// Max
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;
}
// Max
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 if (SOUTH) {
speed.mult(0.80);
} else { // decrease it if not
speed.mult(0.90);
}
// 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));
health = health - 1;
}
// println(speed);
}
// Tarek
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;
}
}
}
}
// Tarek, Marla
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);
if(bob.is_attached) {
image(player_sprite_bob, 0, 0);
} else {
image(player_sprite, 0, 0);
}
if(hasSaw) {
image(saw_sprite, 20, 0);
}
popMatrix();
}
}