Browse Source

Mono Pong: C# style fixes

Rémi Verschelde 7 years ago
parent
commit
3616322c70
3 changed files with 9 additions and 22 deletions
  1. 3 12
      mono/monkey_pong/Ball.cs
  2. 1 1
      mono/monkey_pong/CeilingFloor.cs
  3. 5 9
      mono/monkey_pong/Paddle.cs

+ 3 - 12
mono/monkey_pong/Ball.cs

@@ -5,19 +5,10 @@ public class Ball : Area2D
 {
     private const int BallSpeed = 100;
 
-    private Vector2 direction = new Vector2(-1, 0);
     private int speed = BallSpeed;
-
     private Vector2 initialPos;
 
-    public void SetDirection(Vector2 newDirection)
-    {
-        direction = newDirection;
-    }
-    public Vector2 GetDirection()
-    {
-        return direction;
-    }
+    public Vector2 direction = new Vector2(-1, 0);
 
     public void Reset()
     {
@@ -28,11 +19,11 @@ public class Ball : Area2D
 
     public override void _Ready()
     {
-        initialPos = GetPosition();
+        initialPos = Position;
     }
 
     public override void _Process(float delta)
     {
-        SetPosition(GetPosition() + direction * speed * delta);
+        Position += direction * speed * delta;
     }
 }

+ 1 - 1
mono/monkey_pong/CeilingFloor.cs

@@ -10,7 +10,7 @@ public class CeilingFloor : Area2D
     {
         if (area is Ball ball)
         {
-            ball.SetDirection(ball.GetDirection() + new Vector2(0, yDirection));
+            ball.direction += new Vector2(0, yDirection);
         }
     }
 }

+ 5 - 9
mono/monkey_pong/Paddle.cs

@@ -13,17 +13,13 @@ public class Paddle : Area2D
         String which = GetName();
 
         // Move up and down based on input
-        if (Input.IsActionPressed(which + "_move_up") && GetPosition().y > 0)
+        if (Input.IsActionPressed(which + "_move_up") && Position.y > 0)
         {
-            Vector2 pos = GetPosition();
-            pos.y -= MoveSpeed * delta;
-            SetPosition(pos);
+            Position -= new Vector2(0, MoveSpeed * delta);
         }
-        if (Input.IsActionPressed(which + "_move_down") && GetPosition().y < GetViewportRect().Size.y)
+        if (Input.IsActionPressed(which + "_move_down") && Position.y < GetViewportRect().Size.y)
         {
-            Vector2 pos = GetPosition();
-            pos.y += MoveSpeed * delta;
-            SetPosition(pos);
+            Position += new Vector2(0, MoveSpeed * delta);
         }
     }
 
@@ -32,7 +28,7 @@ public class Paddle : Area2D
         if (area is Ball ball)
         {
             // Assign new direction
-            ball.SetDirection(new Vector2(ballDir, (float)new Random().NextDouble() * 2 - 1).normalized());
+            ball.direction = new Vector2(ballDir, (float)new Random().NextDouble() * 2 - 1).normalized();
         }
     }
 }