added zoom for #5

This commit is contained in:
orosmatthew 2022-02-02 21:16:59 -05:00
parent 005e34498e
commit 527b0c64f1
3 changed files with 148 additions and 3 deletions

View File

@ -7,17 +7,34 @@ public class ParticleSimulationScene : Node2D
{ {
private Node2D _particleNodes; private Node2D _particleNodes;
private Camera2D _camera;
private Tween _cameraTween;
private Vector2 _cameraZoomTarget;
private Vector2 _cameraPosTarget;
private float _maxZoom;
private Vector2 _spaceSize;
private const float CameraZoomSpeed = 0.5f;
private ParticleSimulation _particleSimulation; private ParticleSimulation _particleSimulation;
public float PhysicsInterpolationFraction; public float PhysicsInterpolationFraction;
public void Initialize(int seed, int nParticles, float zoom) public void Initialize(int seed, int nParticles, float zoom)
{ {
_maxZoom = zoom;
_camera = GetNode<Camera2D>("Camera2D");
_cameraTween = GetNode<Tween>("CameraTween");
_particleNodes = GetNode<Node2D>("ParticleNodes"); _particleNodes = GetNode<Node2D>("ParticleNodes");
_cameraZoomTarget = _camera.Zoom;
_cameraPosTarget = _camera.Position;
GD.Seed((ulong)seed); GD.Seed((ulong)seed);
GD.Print("Last Seed: " + seed); GD.Print("Last Seed: " + seed);
var viewSize = GetViewportRect().Size; var viewSize = GetViewportRect().Size;
GetNode<Camera2D>("Camera2D").Zoom = new Vector2(zoom, zoom); _camera.Zoom = new Vector2(zoom, zoom);
var spaceSize = viewSize * zoom; var spaceSize = viewSize * zoom;
_spaceSize = spaceSize;
_particleSimulation = new ParticleSimulation _particleSimulation = new ParticleSimulation
{ {
SpaceSize = spaceSize SpaceSize = spaceSize
@ -28,10 +45,92 @@ public class ParticleSimulationScene : Node2D
public override void _Process(float delta) public override void _Process(float delta)
{ {
// Game state inputs
if (Input.IsActionJustPressed("quit")) if (Input.IsActionJustPressed("quit"))
GetParent<Main>().ExitToMenu(); GetParent<Main>().ExitToMenu();
if (Input.IsActionJustPressed("reset")) if (Input.IsActionJustPressed("reset"))
GetParent<Main>().RestartSimulation(); GetParent<Main>().RestartSimulation();
var shouldTweenStop = false;
if (Input.IsActionJustReleased("zoom_in"))
{
shouldTweenStop = true;
// Zoom
_cameraZoomTarget -= new Vector2(0.1f, 0.1f);
// Movement
var mousePos = GetGlobalMousePosition();
var posDelta = _cameraPosTarget - mousePos;
posDelta = posDelta.Clamped(500f);
_cameraPosTarget -= posDelta * 0.2f;
}
if (Input.IsActionJustReleased("zoom_out"))
{
shouldTweenStop = true;
// Zoom
_cameraZoomTarget += new Vector2(0.1f, 0.1f);
// Movement
var mousePos = GetGlobalMousePosition();
var posDelta = _cameraPosTarget - mousePos;
posDelta = posDelta.Clamped(500f);
_cameraPosTarget += posDelta * 0.2f;
}
var cameraDir = new Vector2();
if (Input.IsActionPressed("up"))
cameraDir += new Vector2(0, -1);
if (Input.IsActionPressed("down"))
cameraDir += new Vector2(0, 1);
if (Input.IsActionPressed("left"))
cameraDir += new Vector2(-1, 0);
if (Input.IsActionPressed("right"))
cameraDir += new Vector2(1, 0);
if (cameraDir.LengthSquared() != 0)
{
shouldTweenStop = true;
cameraDir = cameraDir.Normalized();
_cameraPosTarget += cameraDir * 1000f * delta;
}
if (Input.IsActionPressed("key_zoom_in"))
{
shouldTweenStop = true;
_cameraZoomTarget -= new Vector2(0.01f, 0.01f);
}
if (Input.IsActionPressed("key_zoom_out"))
{
shouldTweenStop = true;
_cameraZoomTarget += new Vector2(0.01f, 0.01f);
}
if (shouldTweenStop)
{
_cameraTween.StopAll();
_cameraZoomTarget.x = Mathf.Clamp(_cameraZoomTarget.x, 0.01f, _maxZoom);
_cameraZoomTarget.y = Mathf.Clamp(_cameraZoomTarget.y, 0.01f, _maxZoom);
_cameraPosTarget.x = Mathf.Clamp(_cameraPosTarget.x,
0f - (_maxZoom - _cameraZoomTarget.x) * (_spaceSize.x / (Mathf.Sqrt2 * 2)),
0f + (_maxZoom - _cameraZoomTarget.x) * (_spaceSize.x / (Mathf.Sqrt2 * 2)));
_cameraPosTarget.y = Mathf.Clamp(_cameraPosTarget.y,
0f - (_maxZoom - _cameraZoomTarget.y) * (_spaceSize.y / (Mathf.Sqrt2 * 2)),
0f + (_maxZoom - _cameraZoomTarget.y) * (_spaceSize.y / (Mathf.Sqrt2 * 2)));
_cameraTween.InterpolateProperty(_camera, "zoom", _camera.Zoom, _cameraZoomTarget, CameraZoomSpeed,
Tween.TransitionType.Quint, Tween.EaseType.Out);
_cameraTween.InterpolateProperty(_camera, "position", _camera.Position, _cameraPosTarget, CameraZoomSpeed,
Tween.TransitionType.Quint, Tween.EaseType.Out);
_cameraTween.Start();
}
PhysicsInterpolationFraction = Engine.GetPhysicsInterpolationFraction(); PhysicsInterpolationFraction = Engine.GetPhysicsInterpolationFraction();
} }
@ -51,7 +150,7 @@ public class ParticleSimulationScene : Node2D
{ {
var simulationParticle = _particleSimulation.GetParticle(particleNode.SimulationId); var simulationParticle = _particleSimulation.GetParticle(particleNode.SimulationId);
particleNode.LastSimulationPosition = particleNode.Position; particleNode.LastSimulationPosition = particleNode.Position;
particleNode.CurrentSimulationPosition = simulationParticle.Position; particleNode.CurrentSimulationPosition = simulationParticle.Position - (_spaceSize / 2.0f);
particleNode.SetColor(simulationParticle.Type.Hue, simulationParticle.Health, particleNode.SetColor(simulationParticle.Type.Hue, simulationParticle.Health,
Mathf.Clamp(simulationParticle.AverageSpeed / 1.5f, 1f, 1f)); Mathf.Clamp(simulationParticle.AverageSpeed / 1.5f, 1f, 1f));
particleNode.WasTeleportedLast = simulationParticle.WasTeleportedLast; particleNode.WasTeleportedLast = simulationParticle.WasTeleportedLast;

View File

@ -8,6 +8,8 @@ script = ExtResource( 1 )
[node name="ParticleNodes" type="Node2D" parent="."] [node name="ParticleNodes" type="Node2D" parent="."]
[node name="Camera2D" type="Camera2D" parent="."] [node name="Camera2D" type="Camera2D" parent="."]
anchor_mode = 0
current = true current = true
zoom = Vector2( 1.35, 1.35 ) zoom = Vector2( 1.35, 1.35 )
smoothing_speed = 100.0
[node name="CameraTween" type="Tween" parent="."]

View File

@ -29,6 +29,50 @@ quit={
"events": [ Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":0,"alt":false,"shift":false,"control":false,"meta":false,"command":false,"pressed":false,"scancode":16777217,"physical_scancode":0,"unicode":0,"echo":false,"script":null) "events": [ Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":0,"alt":false,"shift":false,"control":false,"meta":false,"command":false,"pressed":false,"scancode":16777217,"physical_scancode":0,"unicode":0,"echo":false,"script":null)
] ]
} }
up={
"deadzone": 0.5,
"events": [ Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":0,"alt":false,"shift":false,"control":false,"meta":false,"command":false,"pressed":false,"scancode":87,"physical_scancode":0,"unicode":0,"echo":false,"script":null)
, Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":0,"alt":false,"shift":false,"control":false,"meta":false,"command":false,"pressed":false,"scancode":16777232,"physical_scancode":0,"unicode":0,"echo":false,"script":null)
]
}
down={
"deadzone": 0.5,
"events": [ Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":0,"alt":false,"shift":false,"control":false,"meta":false,"command":false,"pressed":false,"scancode":16777234,"physical_scancode":0,"unicode":0,"echo":false,"script":null)
, Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":0,"alt":false,"shift":false,"control":false,"meta":false,"command":false,"pressed":false,"scancode":83,"physical_scancode":0,"unicode":0,"echo":false,"script":null)
]
}
left={
"deadzone": 0.5,
"events": [ Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":0,"alt":false,"shift":false,"control":false,"meta":false,"command":false,"pressed":false,"scancode":65,"physical_scancode":0,"unicode":0,"echo":false,"script":null)
, Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":0,"alt":false,"shift":false,"control":false,"meta":false,"command":false,"pressed":false,"scancode":16777231,"physical_scancode":0,"unicode":0,"echo":false,"script":null)
]
}
right={
"deadzone": 0.5,
"events": [ Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":0,"alt":false,"shift":false,"control":false,"meta":false,"command":false,"pressed":false,"scancode":16777233,"physical_scancode":0,"unicode":0,"echo":false,"script":null)
, Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":0,"alt":false,"shift":false,"control":false,"meta":false,"command":false,"pressed":false,"scancode":68,"physical_scancode":0,"unicode":0,"echo":false,"script":null)
]
}
zoom_in={
"deadzone": 0.5,
"events": [ Object(InputEventMouseButton,"resource_local_to_scene":false,"resource_name":"","device":0,"alt":false,"shift":false,"control":false,"meta":false,"command":false,"button_mask":0,"position":Vector2( 0, 0 ),"global_position":Vector2( 0, 0 ),"factor":1.0,"button_index":4,"pressed":false,"doubleclick":false,"script":null)
]
}
zoom_out={
"deadzone": 0.5,
"events": [ Object(InputEventMouseButton,"resource_local_to_scene":false,"resource_name":"","device":0,"alt":false,"shift":false,"control":false,"meta":false,"command":false,"button_mask":0,"position":Vector2( 0, 0 ),"global_position":Vector2( 0, 0 ),"factor":1.0,"button_index":5,"pressed":false,"doubleclick":false,"script":null)
]
}
key_zoom_in={
"deadzone": 0.5,
"events": [ Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":0,"alt":false,"shift":false,"control":false,"meta":false,"command":false,"pressed":false,"scancode":69,"physical_scancode":0,"unicode":0,"echo":false,"script":null)
]
}
key_zoom_out={
"deadzone": 0.5,
"events": [ Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":0,"alt":false,"shift":false,"control":false,"meta":false,"command":false,"pressed":false,"scancode":81,"physical_scancode":0,"unicode":0,"echo":false,"script":null)
]
}
[mono] [mono]