Advertisement
Guest User

Untitled

a guest
Sep 20th, 2024
15
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.83 KB | Gaming | 0 0
  1. using Godot;
  2. using System;
  3.  
  4. public partial class RotationPuzzle : Node2D
  5. {
  6.     [Export] private Sprite2D _targetImage;
  7.     [Export] private Sprite2D _workingImage;
  8.     [Export] private float _rotationSpeed = 10.0f;
  9.     [Export] private float _moveSpeed = 10.0f;
  10.     [Export] private float _scaleSpeed = 1.0f;
  11.     [Export] private float _rotationTolerance = 2f;
  12.     [Export] private float _positionTolerance = 5f;
  13.    
  14.     private Vector2 _targetPosition;
  15.     private float _targetRotation;
  16.     private float _targetScale;
  17.  
  18.     private Vector2 _workingPosition = new(960f, 540f);
  19.     private float _workingRotation;
  20.     private float _workingScale = 1;
  21.  
  22.  
  23.     public override void _Ready()
  24.     {
  25.         _workingImage.GlobalRotation = _workingRotation;
  26.         _workingImage.GlobalPosition = _workingPosition;
  27.         _workingImage.GlobalScale = new Vector2(_workingImage.Scale.X * _workingScale, _workingImage.Scale.Y * _workingScale);
  28.        
  29.         _targetImage.RotationDegrees = new RandomNumberGenerator().RandfRange(0f, 360f);
  30.         _targetRotation = _targetImage.RotationDegrees;
  31.        
  32.         _targetImage.GlobalPosition = new Vector2(new RandomNumberGenerator().RandfRange(210f, 944f), new RandomNumberGenerator().RandfRange(210f, 420f));
  33.         _targetPosition = _targetImage.GlobalPosition;
  34.        
  35.         _targetScale = new RandomNumberGenerator().RandfRange(0.5f, 1.5f);
  36.         _targetImage.GlobalScale = new Vector2(_targetImage.Scale.X * _targetScale, _targetImage.Scale.Y * _targetScale);
  37.        
  38.         GD.Print("Target rotation is " + _targetRotation);
  39.         GD.Print("Target position is " + _targetPosition);
  40.         GD.Print("Target scale is " + _targetScale);
  41.     }
  42.  
  43.     public override void _PhysicsProcess(double delta)
  44.     {
  45.         Vector2 inputVector = Input.GetVector(Globals.InputRotateCCW, Globals.InputRotateCW,
  46.                                               Globals.InputScaleUp, Globals.InputScaleDown);
  47.         Vector2 inputDirection = Input.GetVector(Globals.InputMoveLeft, Globals.InputMoveRight,
  48.                                                  Globals.InputMoveForward, Globals.InputMoveBackward);
  49.        
  50.         // Rotation
  51.         if (inputVector.X is > 0.1f or < -0.1f)
  52.         {
  53.             _workingRotation += (float)delta * inputVector.X * _rotationSpeed;
  54.         }
  55.        
  56.         _workingImage.RotationDegrees = _workingRotation;
  57.         _workingRotation = Mathf.Wrap(_workingRotation, 0, 360);
  58.         if (AlmostEqual(_targetRotation, _workingRotation, _rotationTolerance)) OnCorrectRotation();
  59.        
  60.         // Position
  61.         if (inputDirection.X is > 0.1f or < -0.1f)
  62.         {
  63.             _workingPosition.X += (float)delta * inputDirection.X * _moveSpeed;
  64.         }
  65.        
  66.         if (inputDirection.Y is > 0.1f or < -0.1f)
  67.         {
  68.             _workingPosition.Y += (float)delta * inputDirection.Y * _moveSpeed;
  69.         }
  70.        
  71.         _workingImage.GlobalPosition = _workingPosition;
  72.         if (AlmostEqual(_targetPosition.X, _workingPosition.X, _positionTolerance) &&
  73.             AlmostEqual(_targetPosition.Y, _workingPosition.Y, _positionTolerance)) OnCorrectPosition();
  74.        
  75.         // Scale
  76.         if (inputVector.Y is > 0.1f or < -0.1f)
  77.         {
  78.             _workingScale += (float)delta * inputVector.Y * _scaleSpeed;
  79.         }
  80.  
  81.         _workingImage.GlobalScale = new Vector2(_workingImage.Scale.X * _workingScale, _workingImage.Scale.Y * _workingScale);
  82.         GD.Print("Working scale: " + _workingImage.GlobalScale);
  83.     }
  84.  
  85.     private bool AlmostEqual(float a, float b, float tolerance)
  86.     {
  87.         return Math.Abs(a - b) <= tolerance;
  88.     }
  89.    
  90.     private void OnCorrectRotation()
  91.     {
  92.         GD.Print("Correct rotation");
  93.     }
  94.    
  95.     private void OnCorrectPosition()
  96.     {
  97.         GD.Print("Correct position");
  98.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement