-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathCamera.cs
79 lines (63 loc) · 1.63 KB
/
Camera.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
using System;
using CityBuilder.GUI;
using Godot;
namespace CityBuilder;
public class Camera : Camera2D
{
[Export(PropertyHint.Range, "0, 1024")]
public int Speed { get; private set; }
[Export(PropertyHint.Range, "0, 1024")]
public float MinimumZoom { get; private set; }
[Export(PropertyHint.Range, "0, 1024")]
public float MaximumZoom { get; private set; }
[Export(PropertyHint.Range, "0, 1")]
public float ZoomStep { get; private set; }
public override void _Ready()
{
Current = true;
if (Speed <= 0)
{
throw new ArgumentOutOfRangeException(nameof(Speed));
}
if (MinimumZoom <= 0)
{
throw new ArgumentOutOfRangeException(nameof(MinimumZoom));
}
if (MaximumZoom <= 0)
{
throw new ArgumentOutOfRangeException(nameof(MaximumZoom));
}
if (MinimumZoom > MaximumZoom)
{
throw new ArgumentException($"{nameof(MinimumZoom)} can't be bigger than {nameof(MaximumZoom)}.");
}
if (ZoomStep is <= 0 or >= 1)
{
throw new ArgumentOutOfRangeException(nameof(ZoomStep));
}
}
public override void _UnhandledInput(InputEvent @event)
{
if (@event.IsActionPressed(InputAction.ZoomIn))
{
Zoom *= 1 - ZoomStep;
}
else if (@event.IsActionPressed(InputAction.ZoomOut))
{
Zoom /= 1 - ZoomStep;
}
else if (@event is InputEventMouseMotion motion && Input.IsActionPressed(InputAction.MouseclickRight))
{
GlobalPosition -= motion.Relative * Zoom;
}
}
public override void _Process(float delta)
{
var direction = Input.GetVector(
InputAction.CameraLeft,
InputAction.CameraRight,
InputAction.CameraUp,
InputAction.CameraDown);
Position += Speed * delta * direction;
}
}