Paddle.cs 859 B

12345678910111213141516171819202122232425262728293031323334
  1. using Godot;
  2. using System;
  3. public class Paddle : Area2D
  4. {
  5. [Export]
  6. private int ballDir = 1;
  7. private const int MoveSpeed = 100;
  8. public override void _Process(float delta)
  9. {
  10. String which = GetName();
  11. // Move up and down based on input
  12. if (Input.IsActionPressed(which + "_move_up") && Position.y > 0)
  13. {
  14. Position -= new Vector2(0, MoveSpeed * delta);
  15. }
  16. if (Input.IsActionPressed(which + "_move_down") && Position.y < GetViewportRect().Size.y)
  17. {
  18. Position += new Vector2(0, MoveSpeed * delta);
  19. }
  20. }
  21. public void OnAreaEntered(Area2D area)
  22. {
  23. if (area is Ball ball)
  24. {
  25. // Assign new direction
  26. ball.direction = new Vector2(ballDir, (float)new Random().NextDouble() * 2 - 1).Normalized();
  27. }
  28. }
  29. }