-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmap_camera2D.gd
More file actions
76 lines (54 loc) · 2.17 KB
/
map_camera2D.gd
File metadata and controls
76 lines (54 loc) · 2.17 KB
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
extends Camera2D
## Source: https://gitlab.com/realrobots/rimworldripoff/-/blob/1ee8ef79c7ac03e56628dbece33f855c2e090523/CameraController.gd
@export var zoomSpeed : float = 10;
var zoomTarget :Vector2
var dragStartMousePos = Vector2.ZERO
var dragStartCameraPos = Vector2.ZERO
var isDragging : bool = false
# Called when the node enters the scene tree for the first time.
func _ready():
zoomTarget = zoom
#The camera's position is fixed at the top-left corner (0,0), but the map
# image is displayed relative to that position, so the image ends up with
# its corner starting in the middle of the screen. Set the cameras# position
# so it is in the center of screen so the map image origin is in the
# tje upper left corner
# Get the screen size
var screen_size = get_viewport().size
# Calculate the center position
var center_position = Vector2(screen_size.x / 2, screen_size.y / 2)
# Set the camera's position to the center
set_position(center_position)
# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta):
Zoom(delta)
SimplePan(delta)
ClickAndDrag()
func Zoom(delta):
if Input.is_action_just_pressed("camera_zoom_in"):
zoomTarget *= 1.1
if Input.is_action_just_pressed("camera_zoom_out"):
zoomTarget *= 0.9
zoom = zoom.slerp(zoomTarget, zoomSpeed * delta)
func SimplePan(delta):
var moveAmount = Vector2.ZERO
if Input.is_action_pressed("camera_move_right"):
moveAmount.x += 1
if Input.is_action_pressed("camera_move_left"):
moveAmount.x -= 1
if Input.is_action_pressed("camera_move_up"):
moveAmount.y -= 1
if Input.is_action_pressed("camera_move_down"):
moveAmount.y += 1
moveAmount = moveAmount.normalized()
position += moveAmount * delta * 1000 * (1/zoom.x)
func ClickAndDrag():
if !isDragging and Input.is_action_just_pressed("camera_pan"):
dragStartMousePos = get_viewport().get_mouse_position()
dragStartCameraPos = position
isDragging = true
if isDragging and Input.is_action_just_released("camera_pan"):
isDragging = false
if isDragging:
var moveVector = get_viewport().get_mouse_position() - dragStartMousePos
position = dragStartCameraPos - moveVector * 1/zoom.x