camera no move out of window

This commit is contained in:
Victor 2024-09-01 12:54:18 +02:00
parent 21b694d4b6
commit f9e1a56615
3 changed files with 15 additions and 6 deletions

View File

@ -70,3 +70,8 @@ grab={
"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) "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)
] ]
} }
run={
"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":4194325,"key_label":0,"unicode":0,"location":0,"echo":false,"script":null)
]
}

View File

@ -3,7 +3,7 @@
[ext_resource type="Script" path="res://scripts/map.gd" id="1_3np0o"] [ext_resource type="Script" path="res://scripts/map.gd" id="1_3np0o"]
[ext_resource type="PackedScene" uid="uid://6ferubyu2uy1" path="res://scenes/Scanners.tscn" id="1_6mlj0"] [ext_resource type="PackedScene" uid="uid://6ferubyu2uy1" path="res://scenes/Scanners.tscn" id="1_6mlj0"]
[ext_resource type="Texture2D" uid="uid://dtvde6oxrfuk1" path="res://assets/texture/ground.jpg" id="3_20ci8"] [ext_resource type="Texture2D" uid="uid://dtvde6oxrfuk1" path="res://assets/texture/ground.jpg" id="3_20ci8"]
[ext_resource type="PackedScene" path="res://objects/Animal.tscn" id="4_pkphc"] [ext_resource type="PackedScene" uid="uid://cj457q2fx5mim" path="res://objects/Animal.tscn" id="4_pkphc"]
[node name="Map" type="Node2D"] [node name="Map" type="Node2D"]
script = ExtResource("1_3np0o") script = ExtResource("1_3np0o")

View File

@ -1,9 +1,10 @@
extends Camera2D extends Camera2D
const MOVEMENT_SPEED = 500 const MOVEMENT_SPEED = 500
const RUN_MULT = 2
const ZOOM_SPEED = 10 const ZOOM_SPEED = 10
const ZOOM_WINDOW = [0.5, 2] const ZOOM_WINDOW = [0.5, 2]
const SCREEN_BORDER_THRESHOLD = 10 const SCREEN_BORDER_THRESHOLD = 40
var grabbing = false var grabbing = false
var mouse_last_global_position = Vector2() var mouse_last_global_position = Vector2()
@ -23,22 +24,22 @@ func _process(delta):
if ( if (
Input.is_action_pressed("right") Input.is_action_pressed("right")
or mouse_pos.x > viewport_size.x - SCREEN_BORDER_THRESHOLD or mouse_pos.x > viewport_size.x - SCREEN_BORDER_THRESHOLD and mouse_pos.x <= viewport_size.x
) : ) :
direction.x = 1 direction.x = 1
if ( if (
Input.is_action_pressed("left") Input.is_action_pressed("left")
or mouse_pos.x < 0 + SCREEN_BORDER_THRESHOLD or mouse_pos.x < 0 + SCREEN_BORDER_THRESHOLD and mouse_pos.x >= 0
): ):
direction.x = -1 direction.x = -1
if ( if (
Input.is_action_pressed("up") Input.is_action_pressed("up")
or mouse_pos.y < 0 + SCREEN_BORDER_THRESHOLD or mouse_pos.y < 0 + SCREEN_BORDER_THRESHOLD and mouse_pos.y >= 0
): ):
direction.y = -1 direction.y = -1
if ( if (
Input.is_action_pressed("down") Input.is_action_pressed("down")
or mouse_pos.y > viewport_size.y - SCREEN_BORDER_THRESHOLD or mouse_pos.y > viewport_size.y - SCREEN_BORDER_THRESHOLD and mouse_pos.y <= viewport_size.y
): ):
direction.y = 1 direction.y = 1
@ -54,6 +55,9 @@ func _process(delta):
movement += direction*MOVEMENT_SPEED*delta movement += direction*MOVEMENT_SPEED*delta
if Input.is_action_pressed("run"):
movement *= RUN_MULT
move_camera_on_map(movement) move_camera_on_map(movement)
mouse_last_global_position = get_global_mouse_position() mouse_last_global_position = get_global_mouse_position()