camera movement improvement
This commit is contained in:
parent
0a5af3879d
commit
a74dfd4527
@ -41,3 +41,32 @@ dezoom={
|
||||
"events": [Object(InputEventMouseButton,"resource_local_to_scene":false,"resource_name":"","device":-1,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"button_mask":0,"position":Vector2(0, 0),"global_position":Vector2(0, 0),"factor":1.0,"button_index":5,"canceled":false,"pressed":false,"double_click":false,"script":null)
|
||||
]
|
||||
}
|
||||
up={
|
||||
"deadzone": 0.5,
|
||||
"events": [Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":-1,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":0,"physical_keycode":87,"key_label":0,"unicode":122,"location":0,"echo":false,"script":null)
|
||||
, Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":-1,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":0,"physical_keycode":4194320,"key_label":0,"unicode":0,"location":0,"echo":false,"script":null)
|
||||
]
|
||||
}
|
||||
right={
|
||||
"deadzone": 0.5,
|
||||
"events": [Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":-1,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":0,"physical_keycode":4194321,"key_label":0,"unicode":0,"location":0,"echo":false,"script":null)
|
||||
, Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":-1,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":0,"physical_keycode":68,"key_label":0,"unicode":100,"location":0,"echo":false,"script":null)
|
||||
]
|
||||
}
|
||||
left={
|
||||
"deadzone": 0.5,
|
||||
"events": [Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":-1,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":0,"physical_keycode":65,"key_label":0,"unicode":113,"location":0,"echo":false,"script":null)
|
||||
, Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":-1,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":0,"physical_keycode":4194319,"key_label":0,"unicode":0,"location":0,"echo":false,"script":null)
|
||||
]
|
||||
}
|
||||
down={
|
||||
"deadzone": 0.5,
|
||||
"events": [Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":-1,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":0,"physical_keycode":83,"key_label":0,"unicode":115,"location":0,"echo":false,"script":null)
|
||||
, Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":-1,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":0,"physical_keycode":4194322,"key_label":0,"unicode":0,"location":0,"echo":false,"script":null)
|
||||
]
|
||||
}
|
||||
grab={
|
||||
"deadzone": 0.5,
|
||||
"events": [Object(InputEventMouseButton,"resource_local_to_scene":false,"resource_name":"","device":-1,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"button_mask":4,"position":Vector2(165, 21),"global_position":Vector2(174, 67),"factor":1.0,"button_index":3,"canceled":false,"pressed":true,"double_click":false,"script":null)
|
||||
]
|
||||
}
|
||||
|
||||
@ -1,34 +1,71 @@
|
||||
extends Camera2D
|
||||
|
||||
const MOVEMENT_SPEED = 200
|
||||
const MOVEMENT_SPEED = 500
|
||||
const ZOOM_SPEED = 10
|
||||
const ZOOM_WINDOW = [0.5, 2]
|
||||
const BORDER_THRESHOLD = 50
|
||||
|
||||
var movement = Vector2()
|
||||
var zoom_change = 0
|
||||
const SCREEN_BORDER_THRESHOLD = 10
|
||||
|
||||
var grabbing = false
|
||||
var mouse_last_global_position = Vector2()
|
||||
|
||||
func _process(delta):
|
||||
movement = Vector2()
|
||||
zoom_change = 0
|
||||
var direction = Vector2()
|
||||
var zoom_change = 0
|
||||
var movement = Vector2()
|
||||
|
||||
if Input.is_action_pressed("ui_right"):
|
||||
movement.x = 1
|
||||
if Input.is_action_pressed("ui_left"):
|
||||
movement.x = -1
|
||||
if Input.is_action_pressed("ui_up"):
|
||||
movement.y = -1
|
||||
if Input.is_action_pressed("ui_down"):
|
||||
movement.y = 1
|
||||
if Input.is_action_pressed("grab"):
|
||||
var grabbing_movement = mouse_last_global_position - get_global_mouse_position()
|
||||
print(grabbing_movement)
|
||||
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
|
||||
) :
|
||||
direction.x = 1
|
||||
if (
|
||||
Input.is_action_pressed("left")
|
||||
or mouse_pos.x < 0 + SCREEN_BORDER_THRESHOLD
|
||||
):
|
||||
direction.x = -1
|
||||
if (
|
||||
Input.is_action_pressed("up")
|
||||
or mouse_pos.y < 0 + SCREEN_BORDER_THRESHOLD
|
||||
):
|
||||
direction.y = -1
|
||||
if (
|
||||
Input.is_action_pressed("down")
|
||||
or mouse_pos.y > viewport_size.y - SCREEN_BORDER_THRESHOLD
|
||||
):
|
||||
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
|
||||
|
||||
movement = movement.normalized()
|
||||
|
||||
var zoom_value = max(min(zoom.x + zoom_change*ZOOM_SPEED*delta, ZOOM_WINDOW[1]), ZOOM_WINDOW[0])
|
||||
zoom = Vector2(zoom_value, zoom_value)
|
||||
position = position+movement*MOVEMENT_SPEED*delta
|
||||
|
||||
movement += direction*MOVEMENT_SPEED*delta
|
||||
|
||||
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.7)
|
||||
|
||||
Loading…
Reference in New Issue
Block a user