Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using Godot;
- using System;
- public partial class RotationPuzzle : Node2D
- {
- [Export] private Sprite2D _targetImage;
- [Export] private Sprite2D _workingImage;
- [Export] private float _rotationSpeed = 10.0f;
- [Export] private float _moveSpeed = 10.0f;
- [Export] private float _scaleSpeed = 1.0f;
- [Export] private float _rotationTolerance = 2f;
- [Export] private float _positionTolerance = 5f;
- private Vector2 _targetPosition;
- private float _targetRotation;
- private float _targetScale;
- private Vector2 _workingPosition = new(960f, 540f);
- private float _workingRotation;
- private float _workingScale = 1;
- public override void _Ready()
- {
- _workingImage.GlobalRotation = _workingRotation;
- _workingImage.GlobalPosition = _workingPosition;
- _workingImage.GlobalScale = new Vector2(_workingImage.Scale.X * _workingScale, _workingImage.Scale.Y * _workingScale);
- _targetImage.RotationDegrees = new RandomNumberGenerator().RandfRange(0f, 360f);
- _targetRotation = _targetImage.RotationDegrees;
- _targetImage.GlobalPosition = new Vector2(new RandomNumberGenerator().RandfRange(210f, 944f), new RandomNumberGenerator().RandfRange(210f, 420f));
- _targetPosition = _targetImage.GlobalPosition;
- _targetScale = new RandomNumberGenerator().RandfRange(0.5f, 1.5f);
- _targetImage.GlobalScale = new Vector2(_targetImage.Scale.X * _targetScale, _targetImage.Scale.Y * _targetScale);
- GD.Print("Target rotation is " + _targetRotation);
- GD.Print("Target position is " + _targetPosition);
- GD.Print("Target scale is " + _targetScale);
- }
- public override void _PhysicsProcess(double delta)
- {
- Vector2 inputVector = Input.GetVector(Globals.InputRotateCCW, Globals.InputRotateCW,
- Globals.InputScaleUp, Globals.InputScaleDown);
- Vector2 inputDirection = Input.GetVector(Globals.InputMoveLeft, Globals.InputMoveRight,
- Globals.InputMoveForward, Globals.InputMoveBackward);
- // Rotation
- if (inputVector.X is > 0.1f or < -0.1f)
- {
- _workingRotation += (float)delta * inputVector.X * _rotationSpeed;
- }
- _workingImage.RotationDegrees = _workingRotation;
- _workingRotation = Mathf.Wrap(_workingRotation, 0, 360);
- if (AlmostEqual(_targetRotation, _workingRotation, _rotationTolerance)) OnCorrectRotation();
- // Position
- if (inputDirection.X is > 0.1f or < -0.1f)
- {
- _workingPosition.X += (float)delta * inputDirection.X * _moveSpeed;
- }
- if (inputDirection.Y is > 0.1f or < -0.1f)
- {
- _workingPosition.Y += (float)delta * inputDirection.Y * _moveSpeed;
- }
- _workingImage.GlobalPosition = _workingPosition;
- if (AlmostEqual(_targetPosition.X, _workingPosition.X, _positionTolerance) &&
- AlmostEqual(_targetPosition.Y, _workingPosition.Y, _positionTolerance)) OnCorrectPosition();
- // Scale
- if (inputVector.Y is > 0.1f or < -0.1f)
- {
- _workingScale += (float)delta * inputVector.Y * _scaleSpeed;
- }
- _workingImage.GlobalScale = new Vector2(_workingImage.Scale.X * _workingScale, _workingImage.Scale.Y * _workingScale);
- GD.Print("Working scale: " + _workingImage.GlobalScale);
- }
- private bool AlmostEqual(float a, float b, float tolerance)
- {
- return Math.Abs(a - b) <= tolerance;
- }
- private void OnCorrectRotation()
- {
- GD.Print("Correct rotation");
- }
- private void OnCorrectPosition()
- {
- GD.Print("Correct position");
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement