2 Komitmen 1f8c63d8f1 ... 7ac248f9ec

Pembuat SHA1 Pesan Tanggal
  Marien Raat 7ac248f9ec Change ball radius to 40 pixels 8 tahun lalu
  Marien Raat 621a4150dc Add ball reset upon a goal 8 tahun lalu
5 mengubah file dengan 25 tambahan dan 1 penghapusan
  1. 3 0
      include/ball.h
  2. 1 1
      include/config.h
  3. 19 0
      src/ball.cpp
  4. 1 0
      src/game.cpp
  5. 1 0
      src/server.cpp

+ 3 - 0
include/ball.h

@@ -27,6 +27,7 @@ class Ball {
 public:
     Ball() { }
     void initialize(b2World *world, float radius);
+    void update();
     void draw(sf::RenderTarget *target);
 
     void setPosition(sf::Vector2f position);
@@ -40,6 +41,8 @@ private:
     b2World *world;
 
     sf::CircleShape ballCircle;
+
+    void reset();
 };
 
 sf::Packet &operator <<(sf::Packet &packet, Ball &ball);

+ 1 - 1
include/config.h

@@ -24,7 +24,7 @@ along with this program.  If not, see <http://www.gnu.org/licenses/>.
 #define RAD_TO_DEG 57.2957795130824
 
 #define FIELD_SIZE sf::Vector2f(3000, 2000)
-#define BALL_RADIUS 30
+#define BALL_RADIUS 40
 
 #define ROTATE_B2VEC2(vec, angle)                       \
   (b2Vec2(vec.x * cos(angle) - vec.y * sin(angle),      \

+ 19 - 0
src/ball.cpp

@@ -42,6 +42,17 @@ void Ball::initialize(b2World *world, float radius)
     body->CreateFixture(&fixtureDef);
 }
 
+void Ball::update()
+{
+    float x = body->GetPosition().x * SCALE_F;
+    if (x < -BALL_RADIUS) {
+        reset();
+    }
+    if (x > FIELD_SIZE.x + BALL_RADIUS) {
+        reset();
+    }
+}
+
 void Ball::draw(sf::RenderTarget *target)
 {
     ballCircle.setPosition(getPosition());
@@ -74,6 +85,14 @@ float Ball::getRotation()
     return RAD_TO_DEG * body->GetAngle();
 }
 
+void Ball::reset()
+{
+    setPosition(FIELD_SIZE / 2.0f);
+    setRotation(0);
+    body->SetLinearVelocity(b2Vec2(0, 0));
+    body->SetAngularVelocity(0);
+}
+
 sf::Packet &operator <<(sf::Packet &packet, Ball &ball)
 {
     sf::Vector2f ballPosition = ball.getPosition();

+ 1 - 0
src/game.cpp

@@ -196,6 +196,7 @@ void handleInput()
 
 void updateGame()
 {
+    ball.update();
     for (int i = 0; i < maxCars; i++)
         playerCars[i].update();
 

+ 1 - 0
src/server.cpp

@@ -116,6 +116,7 @@ void initializeNetwork(int argc, char **argv)
 
 void updateGame()
 {
+    ball.update();
     for (int i = 0; i < maxCars; i++)
         playerCars[i].update();