From a905421d7e1e7565fad308aa54ad43ddcffed59a Mon Sep 17 00:00:00 2001
From: Faruk Hammoud <farukhammoud@gmail.com>
Date: Fri, 20 Mar 2020 10:22:23 +0100
Subject: [PATCH] ball mechanics

---
 ias/__pycache__/ia_0000001.cpython-37.pyc | Bin 960 -> 960 bytes
 stadium/ball.pde                          |   4 +-
 stadium/game.pde                          |  70 +++++++++++++++++++++-
 stadium/player.pde                        |  19 ++++--
 stadium/team.pde                          |  10 ++--
 5 files changed, 90 insertions(+), 13 deletions(-)

diff --git a/ias/__pycache__/ia_0000001.cpython-37.pyc b/ias/__pycache__/ia_0000001.cpython-37.pyc
index e02aa5a2def2e8e1079fdb021a4b0ed2bd45f74a..b62a8cd8bcd5fa43633b386fde4069f845336e53 100644
GIT binary patch
delta 20
bcmX@Wet@0ZiI<m)fq{X+NVaGr_f}>AER6&;

delta 20
bcmX@Wet@0ZiI<m)fq{Xc^GD%E?ybxKGz$f0

diff --git a/stadium/ball.pde b/stadium/ball.pde
index 54a698b..4775bb5 100644
--- a/stadium/ball.pde
+++ b/stadium/ball.pde
@@ -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();
diff --git a/stadium/game.pde b/stadium/game.pde
index e5c6227..aa3d147 100644
--- a/stadium/game.pde
+++ b/stadium/game.pde
@@ -3,19 +3,23 @@ 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);
     this.ball = new Ball();
   }
   void keep_distance(Player player_1, Player player_2) {
-    
+
     float xm = player_1.get_pos_x()/2.0 + player_2.get_pos_x()/2.0;
     float ym = player_1.get_pos_y()/2.0 + player_2.get_pos_y()/2.0;
     float dx = player_1.get_pos_x() - xm;
     float dy = player_1.get_pos_y() - ym;
     float  n = sqrt(pow(dx, 2)+pow(dy, 2));
-    
+
     if (n < 6) {
       dx *= 6/n;
       dy *= 6/n;
@@ -61,10 +65,70 @@ class Game {
     input[21] = this.ball.get_pos_y();
     return input;
   }
+  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.ball.run(delta_t); 
+
     this.team_1.run(delta_t);
     this.team_2.run(delta_t);
+    this.ball_mechanic(delta_t);
     this.verify_colision();
   }
   void show() {
diff --git a/stadium/player.pde b/stadium/player.pde
index 88c9cbc..fad6d17 100644
--- a/stadium/player.pde
+++ b/stadium/player.pde
@@ -29,8 +29,8 @@ class Player {
     this.pos_x = pos_x;
     this.pos_y = pos_y;
   }
-  void set_angle(float angle){
-     this.angle = angle; 
+  void set_angle(float angle) {
+    this.angle = angle;
   }
   float get_pos_x() {
     return this.pos_x;
@@ -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;
@@ -57,10 +66,10 @@ class Player {
     this.pos_y += this.vel*sin(angle)*delta_t;
     this.verify_borders();
   }
-  void showPos(){
+  void showPos() {
     textSize(2);
-    fill(255,0,0);
-    text(str(this.pos_x)+","+str(this.pos_y),0,0);
+    fill(255, 0, 0);
+    text(str(this.pos_x)+","+str(this.pos_y), 0, 0);
   }
   void show() {
     noStroke();
diff --git a/stadium/team.pde b/stadium/team.pde
index 5d7cea5..73fbfb5 100644
--- a/stadium/team.pde
+++ b/stadium/team.pde
@@ -1,9 +1,10 @@
 class Team {
-  
+
   int team = 1;
   Player[] players = new Player[5];
   int score = 0;
- 
+  float kick_catch = 0;
+
 
   Team(int team) {
     this.team = team;
@@ -19,11 +20,12 @@ 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;
-    if(this.team == 2){
-       s = -1; 
+    if (this.team == 2) {
+      s = -1;
     }
     this.players[0].set_position(-80*s, 0);
     this.players[0].set_angle((PI-(s*PI))/2.0);
-- 
GitLab