Skip to content
Snippets Groups Projects
Commit a905421d authored by Faruk Hammoud's avatar Faruk Hammoud
Browse files

ball mechanics

parent 731f3ba0
No related branches found
No related tags found
No related merge requests found
No preview for this file type
......@@ -3,8 +3,9 @@ class Ball {
float pos_y;
float vel_x;
float vel_y;
int linked_to = -1;
Ball() {
this.set_velocity(0,-10);
this.set_velocity(0,0);
}
void set_position(float pos_x, float pos_y) {
this.pos_x = pos_x;
......@@ -16,6 +17,7 @@ class Ball {
this.vel_y = vel_y;
}
void run(float delta_t) {
if(linked_to == -1)
this.pos_x += this.vel_x*delta_t;
this.pos_y += this.vel_y*delta_t;
this.verify_borders();
......
......@@ -3,6 +3,10 @@ class Game {
Team team_1;
Team team_2;
Ball ball;
int linked_to = -1;
float timestamp = 0;
Game() {
this.team_1 = new Team(1);
this.team_2 = new Team(2);
......@@ -61,10 +65,70 @@ class Game {
input[21] = this.ball.get_pos_y();
return input;
}
void run(float delta_t) {
void ball_mechanic(float delta_t) {
if (this.linked_to == -1) {
this.ball.run(delta_t);
} else {
if (this.linked_to < 5) {
float x = this.team_1.players[this.linked_to].opening_x();
float y = this.team_1.players[this.linked_to].opening_y();
this.ball.set_position(x, y);
if (this.team_1.kick_catch != 0) { // kick
float vx = this.team_1.kick_catch*cos(this.team_1.players[this.linked_to].get_angle());
float vy = this.team_1.kick_catch*sin(this.team_1.players[this.linked_to].get_angle());
this.ball.set_velocity(vx, vy);
this.team_1.kick_catch = 0;
this.linked_to = -1;
}
} else {
float x = this.team_2.players[this.linked_to-5].opening_x();
float y = this.team_2.players[this.linked_to-5].opening_y();
this.ball.set_position(x, y);
if (this.team_2.kick_catch != 0) { //kick
float vx = this.team_2.kick_catch*cos(this.team_2.players[this.linked_to-5].get_angle());
float vy = this.team_2.kick_catch*sin(this.team_2.players[this.linked_to-5].get_angle());
this.ball.set_velocity(vx, vy);
this.team_2.kick_catch = 0;
this.linked_to = -1;
}
}
}
//catch
if (this.linked_to == -1 || this.timestamp - millis() > 500) {
if (random(1)>0.5) {
if (this.team_1.kick_catch != 0) {
for (int i=0; i<5; i++) {
float dx = this.team_1.players[i].opening_x() - this.ball.get_pos_x();
float dy = this.team_1.players[i].opening_y() - this.ball.get_pos_y();
float n = sqrt(pow(dx, 2)+pow(dy, 2));
if (n<1) {
this.team_1.kick_catch = 0;
this.linked_to = i;
this.timestamp = millis();
}
}
}
} else {
if (this.team_2.kick_catch != 0) {
for (int i=0; i<5; i++) {
float dx = this.team_2.players[i].opening_x() - this.ball.get_pos_x();
float dy = this.team_2.players[i].opening_y() - this.ball.get_pos_y();
float n = sqrt(pow(dx, 2)+pow(dy, 2));
if (n<1) {
this.team_2.kick_catch = 0;
this.linked_to = 5+i;
this.timestamp = millis();
}
}
}
}
}
}
void run(float delta_t) {
this.team_1.run(delta_t);
this.team_2.run(delta_t);
this.ball_mechanic(delta_t);
this.verify_colision();
}
void show() {
......
......@@ -38,6 +38,15 @@ class Player {
float get_pos_y() {
return this.pos_y;
}
float get_angle() {
return this.angle;
}
float opening_x() {
return this.get_pos_x()+6*cos(this.angle);
}
float opening_y() {
return this.get_pos_y()+6*sin(this.angle);
}
void verify_borders() {
if (this.pos_x < -93) {
this.pos_x = -93;
......
......@@ -3,6 +3,7 @@ class Team {
int team = 1;
Player[] players = new Player[5];
int score = 0;
float kick_catch = 0;
Team(int team) {
......@@ -19,6 +20,7 @@ class Team {
this.players[i].set_velocity(output[i*2]);
this.players[i].set_direction(output[i*2+1]);
}
this.kick_catch = output[10];
}
void initialize_players() {
int s = 1;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment