Skip to content

Commit 3616322

Browse files
committed
Mono Pong: C# style fixes
1 parent faf120e commit 3616322

File tree

3 files changed

+9
-22
lines changed

3 files changed

+9
-22
lines changed

mono/monkey_pong/Ball.cs

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,10 @@ public class Ball : Area2D
55
{
66
private const int BallSpeed = 100;
77

8-
private Vector2 direction = new Vector2(-1, 0);
98
private int speed = BallSpeed;
10-
119
private Vector2 initialPos;
1210

13-
public void SetDirection(Vector2 newDirection)
14-
{
15-
direction = newDirection;
16-
}
17-
public Vector2 GetDirection()
18-
{
19-
return direction;
20-
}
11+
public Vector2 direction = new Vector2(-1, 0);
2112

2213
public void Reset()
2314
{
@@ -28,11 +19,11 @@ public void Reset()
2819

2920
public override void _Ready()
3021
{
31-
initialPos = GetPosition();
22+
initialPos = Position;
3223
}
3324

3425
public override void _Process(float delta)
3526
{
36-
SetPosition(GetPosition() + direction * speed * delta);
27+
Position += direction * speed * delta;
3728
}
3829
}

mono/monkey_pong/CeilingFloor.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ public void OnAreaEntered(Area2D area)
1010
{
1111
if (area is Ball ball)
1212
{
13-
ball.SetDirection(ball.GetDirection() + new Vector2(0, yDirection));
13+
ball.direction += new Vector2(0, yDirection);
1414
}
1515
}
1616
}

mono/monkey_pong/Paddle.cs

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,13 @@ public override void _Process(float delta)
1313
String which = GetName();
1414

1515
// Move up and down based on input
16-
if (Input.IsActionPressed(which + "_move_up") && GetPosition().y > 0)
16+
if (Input.IsActionPressed(which + "_move_up") && Position.y > 0)
1717
{
18-
Vector2 pos = GetPosition();
19-
pos.y -= MoveSpeed * delta;
20-
SetPosition(pos);
18+
Position -= new Vector2(0, MoveSpeed * delta);
2119
}
22-
if (Input.IsActionPressed(which + "_move_down") && GetPosition().y < GetViewportRect().Size.y)
20+
if (Input.IsActionPressed(which + "_move_down") && Position.y < GetViewportRect().Size.y)
2321
{
24-
Vector2 pos = GetPosition();
25-
pos.y += MoveSpeed * delta;
26-
SetPosition(pos);
22+
Position += new Vector2(0, MoveSpeed * delta);
2723
}
2824
}
2925

@@ -32,7 +28,7 @@ public void OnAreaEntered(Area2D area)
3228
if (area is Ball ball)
3329
{
3430
// Assign new direction
35-
ball.SetDirection(new Vector2(ballDir, (float)new Random().NextDouble() * 2 - 1).normalized());
31+
ball.direction = new Vector2(ballDir, (float)new Random().NextDouble() * 2 - 1).normalized();
3632
}
3733
}
3834
}

0 commit comments

Comments
 (0)