Newer
Older
DungeonShooting / DungeonShooting_Godot / src / test / TestPerfectPixelScene.cs
  1. using Godot;
  2. using System;
  3.  
  4. public partial class TestPerfectPixelScene : Node2D
  5. {
  6. public enum HandlerType
  7. {
  8. Normal,
  9. UseHandler
  10. }
  11.  
  12. [Export]
  13. public Label FpsLabel;
  14. [Export]
  15. public Camera2D Camera2D;
  16.  
  17. [Export]
  18. public float Speed = 50;
  19.  
  20. [Export]
  21. public SubViewportContainer SubViewportContainer;
  22.  
  23. [Export]
  24. public HandlerType Type;
  25. private ShaderMaterial _shaderMaterial;
  26. private Vector2 _cameraPos;
  27.  
  28. public override void _Ready()
  29. {
  30. if (SubViewportContainer != null)
  31. {
  32. _shaderMaterial = (ShaderMaterial)SubViewportContainer.Material;
  33. }
  34. }
  35.  
  36. public override void _Process(double delta)
  37. {
  38. FpsLabel.Text = "FPS: " + Engine.GetFramesPerSecond();
  39. InputManager.Update((float)delta);
  40. var dir = InputManager.MoveAxis;
  41. if (dir != Vector2.Zero)
  42. {
  43. _cameraPos += dir * Speed * (float)delta;
  44. }
  45.  
  46. if (Type == HandlerType.Normal)
  47. {
  48. Camera2D.GlobalPosition = _cameraPos.Round();
  49. }
  50. else if (Type == HandlerType.UseHandler)
  51. {
  52. if (_shaderMaterial != null)
  53. {
  54. var cameraPosition = _cameraPos;
  55. var offset = cameraPosition.Round() - cameraPosition;
  56. _shaderMaterial.SetShaderParameter("offset", offset);
  57. Camera2D.GlobalPosition = cameraPosition.Round();
  58. }
  59. }
  60. //Debug.Log("CameraPos: " + cameraPosition);
  61. }
  62. }