81 lines
2.1 KiB
GDScript
81 lines
2.1 KiB
GDScript
extends Camera2D
|
|
|
|
const MOVEMENT_SPEED = 500
|
|
const RUN_MULT = 2
|
|
const ZOOM_SPEED = 10
|
|
const ZOOM_WINDOW = [0.5, 2]
|
|
const SCREEN_BORDER_THRESHOLD = 40
|
|
|
|
var grabbing = false
|
|
var mouse_last_global_position = Vector2()
|
|
|
|
signal mouse_motion
|
|
|
|
func _process(delta):
|
|
var direction = Vector2()
|
|
var zoom_change = 0
|
|
var movement = Vector2()
|
|
|
|
if Input.is_action_pressed("grab"):
|
|
var grabbing_movement = mouse_last_global_position - get_global_mouse_position()
|
|
movement += grabbing_movement
|
|
|
|
var mouse_pos = get_viewport().get_mouse_position()
|
|
var viewport_size = get_viewport().get_visible_rect().size
|
|
|
|
if (
|
|
Input.is_action_pressed("right")
|
|
or mouse_pos.x > viewport_size.x - SCREEN_BORDER_THRESHOLD and mouse_pos.x <= viewport_size.x
|
|
) :
|
|
direction.x = 1
|
|
if (
|
|
Input.is_action_pressed("left")
|
|
or mouse_pos.x < 0 + SCREEN_BORDER_THRESHOLD and mouse_pos.x >= 0
|
|
):
|
|
direction.x = -1
|
|
if (
|
|
Input.is_action_pressed("up")
|
|
or mouse_pos.y < 0 + SCREEN_BORDER_THRESHOLD and mouse_pos.y >= 0
|
|
):
|
|
direction.y = -1
|
|
if (
|
|
Input.is_action_pressed("down")
|
|
or mouse_pos.y > viewport_size.y - SCREEN_BORDER_THRESHOLD and mouse_pos.y <= viewport_size.y
|
|
):
|
|
direction.y = 1
|
|
|
|
direction = direction.normalized()
|
|
|
|
if Input.is_action_just_pressed("zoom"):
|
|
zoom_change = 1
|
|
if Input.is_action_just_pressed("dezoom"):
|
|
zoom_change = -1
|
|
|
|
var zoom_value = max(min(zoom.x + zoom_change*ZOOM_SPEED*delta, ZOOM_WINDOW[1]), ZOOM_WINDOW[0])
|
|
zoom = Vector2(zoom_value, zoom_value)
|
|
|
|
movement += direction*MOVEMENT_SPEED*delta
|
|
|
|
if Input.is_action_pressed("run"):
|
|
movement *= RUN_MULT
|
|
|
|
move_camera_on_map(movement)
|
|
mouse_last_global_position = get_global_mouse_position()
|
|
|
|
func move_camera_on_map(movement):
|
|
var new_position = Vector2(
|
|
max(
|
|
min(position.x+movement.x, GameTerrain.TERRAIN_SIZE.x * GameTerrain.MAP_RATIO),
|
|
0
|
|
),
|
|
max(
|
|
min(position.y+movement.y, GameTerrain.TERRAIN_SIZE.y * GameTerrain.MAP_RATIO),
|
|
0
|
|
)
|
|
)
|
|
position = lerp(position, new_position, 0.8)
|
|
|
|
func _input(event):
|
|
if event is InputEventMouseMotion:
|
|
emit_signal("mouse_motion", get_global_mouse_position())
|