Skip to content

Commit 111c0d4

Browse files
committed
Code enhancements for mono pong demo.
- Use pattern matching instead of check-and-cast. - Rename constants to fit the usual c# code-style.
1 parent 5df74f5 commit 111c0d4

File tree

4 files changed

+9
-12
lines changed

4 files changed

+9
-12
lines changed

mono/monkey_pong/Ball.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@
33

44
public class Ball : Area2D
55
{
6-
private const int BALL_SPEED = 100;
6+
private const int BallSpeed = 100;
77

88
private Vector2 direction = new Vector2(-1, 0);
9-
private int speed = BALL_SPEED;
9+
private int speed = BallSpeed;
1010

1111
private Vector2 initialPos;
1212

@@ -22,7 +22,7 @@ public Vector2 GetDirection()
2222
public void Reset()
2323
{
2424
SetPosition(initialPos);
25-
speed = BALL_SPEED;
25+
speed = BallSpeed;
2626
direction = new Vector2(-1, 0);
2727
}
2828

mono/monkey_pong/CeilingFloor.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,8 @@ public class CeilingFloor : Area2D
88

99
public void OnAreaEntered(Area2D area)
1010
{
11-
if (area is Ball)
11+
if (area is Ball ball)
1212
{
13-
Ball ball = (Ball)area;
1413
ball.SetDirection(ball.GetDirection() + new Vector2(0, yDirection));
1514
}
1615
}

mono/monkey_pong/Paddle.cs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ public class Paddle : Area2D
66
[Export]
77
private int ballDir = 1;
88

9-
private const int MOVE_SPEED = 100;
9+
private const int MoveSpeed = 100;
1010

1111
public override void _Process(float delta)
1212
{
@@ -16,23 +16,22 @@ public override void _Process(float delta)
1616
if (Input.IsActionPressed(which + "_move_up") && GetPosition().y > 0)
1717
{
1818
Vector2 pos = GetPosition();
19-
pos.y -= MOVE_SPEED * delta;
19+
pos.y -= MoveSpeed * delta;
2020
SetPosition(pos);
2121
}
2222
if (Input.IsActionPressed(which + "_move_down") && GetPosition().y < GetViewportRect().Size.y)
2323
{
2424
Vector2 pos = GetPosition();
25-
pos.y += MOVE_SPEED * delta;
25+
pos.y += MoveSpeed * delta;
2626
SetPosition(pos);
2727
}
2828
}
2929

3030
public void OnAreaEntered(Area2D area)
3131
{
32-
if (area is Ball)
32+
if (area is Ball ball)
3333
{
3434
// Assign new direction
35-
Ball ball = (Ball)area;
3635
ball.SetDirection(new Vector2(ballDir, (float)new Random().NextDouble() * 2 - 1).normalized());
3736
}
3837
}

mono/monkey_pong/Wall.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,9 @@ public class Wall : Area2D
55
{
66
public void OnWallAreaEntered(Area2D area)
77
{
8-
if (area is Ball)
8+
if (area is Ball ball)
99
{
1010
// Oops, ball went out of game place, reset
11-
Ball ball = (Ball)area;
1211
ball.Reset();
1312
}
1413
}

0 commit comments

Comments
 (0)