From 358327478c2a9026c490b5f48b29282b32eb1e76 Mon Sep 17 00:00:00 2001 From: food-please Date: Sat, 10 Jun 2023 23:33:04 -0300 Subject: [PATCH] Add FieldCamera --- src/field/field.gd | 1 - src/field/field_camera.gd | 59 ++++++++++++++++++++++++++ src/main.tscn | 9 ++-- world/demo_area/default_gameboard.tres | 2 +- 4 files changed, 66 insertions(+), 5 deletions(-) create mode 100644 src/field/field_camera.gd diff --git a/src/field/field.gd b/src/field/field.gd index 6e8d061..fa2bbc3 100644 --- a/src/field/field.gd +++ b/src/field/field.gd @@ -130,7 +130,6 @@ func unmute(crescendo_time: = -1.0) -> void: # Inject essential dependencies to events. func _setup_event(event: Event) -> void: - print("Setup event ", event.name) event.music_player = music if event is Interaction: diff --git a/src/field/field_camera.gd b/src/field/field_camera.gd new file mode 100644 index 0000000..c0f5f60 --- /dev/null +++ b/src/field/field_camera.gd @@ -0,0 +1,59 @@ +class_name FieldCamera +extends Camera2D + +@export var gameboard: Gameboard + +func _ready() -> void: + limit_left = gameboard.boundaries.position.x * gameboard.cell_size.x + limit_top = gameboard.boundaries.position.y * gameboard.cell_size.y + limit_right = gameboard.boundaries.end.x * gameboard.cell_size.x + limit_bottom = gameboard.boundaries.end.y * gameboard.cell_size.y + + get_viewport().size_changed.connect(_on_viewport_resized) + _on_viewport_resized() + + +func _on_viewport_resized() -> void: + # Calculate tentative camera boundaries based on the gameboard. + var boundary_left: = gameboard.boundaries.position.x * gameboard.cell_size.x + var boundary_top: = gameboard.boundaries.position.y * gameboard.cell_size.y + var boundary_right: = gameboard.boundaries.end.x * gameboard.cell_size.x + var boundary_bottom: = gameboard.boundaries.end.y * gameboard.cell_size.y + + # We'll also want the current viewport boundary sizes. + var vp_size: = get_viewport_rect().size / global_scale + var boundary_width: = boundary_right - boundary_left + var boundary_height: = boundary_bottom - boundary_top + + # If the boundary size is less than the viewport size, the camera limits will be smaller than + # the camera dimensions (which does all kinds of crazy things in-game). + # Therefore, if this is the case we'll want to centre the camera on the gameboard and set the + # limits to be that of the viewport, locking the camera to one or both axes. + # Start by checking the x-axis. + if boundary_width < vp_size.x: + position.x = (gameboard.boundaries.position.x + gameboard.boundaries.size.x/2.0) \ + * gameboard.cell_size.x + limit_left = position.x - vp_size.x/2.0 as int + limit_right = position.x + vp_size.x/2.0 as int + + # If, however, the viewport is smaller than the gameplay area, the camera can be free to move + # as needed. + else: + limit_left = boundary_left + limit_right = boundary_right + + # Perform the same checks as above for the y-axis. + if boundary_height < vp_size.y: + position.y = (gameboard.boundaries.position.y + gameboard.boundaries.size.y/2.0) \ + * gameboard.cell_size.y + limit_top = position.y - vp_size.y/2.0 as int + limit_bottom = position.y + vp_size.y/2.0 as int + else: + limit_top = boundary_top + limit_bottom = boundary_bottom + + # Finally, the camera limits use global coordinates so adjust by the global scale. + limit_left *= global_scale.x as int + limit_right *= global_scale.x as int + limit_top *= global_scale.y as int + limit_bottom *= global_scale.y as int diff --git a/src/main.tscn b/src/main.tscn index 3d58452..932b625 100644 --- a/src/main.tscn +++ b/src/main.tscn @@ -1,4 +1,4 @@ -[gd_scene load_steps=17 format=3 uid="uid://q8xq4np3t6is"] +[gd_scene load_steps=18 format=3 uid="uid://q8xq4np3t6is"] [ext_resource type="Script" path="res://src/field/field.gd" id="2_bkxev"] [ext_resource type="PackedScene" uid="uid://u1yu8yoly723" path="res://world/old_demo/terrain.tscn" id="2_gu4g2"] @@ -9,6 +9,7 @@ [ext_resource type="Script" path="res://src/field/gameboard/events/interaction.gd" id="11_fls00"] [ext_resource type="PackedScene" uid="uid://b626e0bngfvjj" path="res://src/field/gamepieces/gamepiece.tscn" id="11_yntrj"] [ext_resource type="PackedScene" uid="uid://qxua6ekob7fa" path="res://data/field/characters/gobot_gfx.tscn" id="12_oablq"] +[ext_resource type="Script" path="res://src/field/field_camera.gd" id="13_ev55l"] [ext_resource type="Texture2D" uid="uid://b71dt8n01lhd0" path="res://assets/gui/path_destination_marker.tres" id="13_osl4o"] [ext_resource type="Script" path="res://src/field/player_path_destination_marker.gd" id="14_0j4ru"] [ext_resource type="Script" path="res://src/field/gameboard/debug_map_boundaries.gd" id="18_cqtg7"] @@ -36,7 +37,7 @@ terrain_mask = 1 focused_game_piece = NodePath("Objects/Gamepieces/Player") [node name="Terrain" parent="." instance=ExtResource("2_gu4g2")] -layer_0/tile_data = PackedInt32Array(327689, 0, 0, 262153, 0, 0, 196617, 0, 0, 131081, 0, 0, 65545, 0, 0, 9, 0, 0, 327688, 0, 0, 262152, 0, 0, 196616, 0, 0, 131080, 0, 0, 65544, 0, 0, 8, 131072, 0, 327687, 0, 0, 262151, 0, 0, 196615, 0, 0, 131079, 0, 0, 65543, 0, 0, 7, 0, 0, 327686, 0, 0, 262150, 0, 0, 196614, 0, 0, 131078, 0, 0, 65542, 0, 0, 6, 0, 0, 327685, 0, 0, 262149, 0, 0, 196613, 0, 0, 131077, 0, 0, 65541, 0, 0, 5, 0, 0, 327684, 0, 0, 262148, 0, 0, 196612, 0, 0, 131076, 0, 0, 65540, 0, 0, 4, 0, 0, 327683, 0, 0, 262147, 0, 0, 196611, 0, 0, 131075, 0, 0, 65539, 0, 0, 3, 0, 0, 327682, 0, 0, 262146, 0, 0, 196610, 0, 0, 131074, 0, 0, 65538, 131072, 0, 2, 0, 0, 327681, 0, 0, 262145, 0, 0, 196609, 0, 0, 131073, 0, 0, 65537, 0, 0, 1, 0, 0, 327680, 0, 0, 262144, 0, 0, 196608, 131072, 0, 131072, 0, 0, 65536, 0, 0, 0, 0, 0) +layer_0/tile_data = PackedInt32Array(327689, 0, 0, 262153, 0, 0, 196617, 0, 0, 131081, 0, 0, 65545, 0, 0, 9, 0, 0, 327688, 0, 0, 262152, 0, 0, 196616, 0, 0, 131080, 0, 0, 65544, 0, 0, 8, 131072, 0, 327687, 0, 0, 262151, 0, 0, 196615, 0, 0, 131079, 0, 0, 65543, 0, 0, 7, 0, 0, 327686, 0, 0, 262150, 0, 0, 196614, 0, 0, 131078, 0, 0, 65542, 0, 0, 6, 0, 0, 327685, 0, 0, 262149, 0, 0, 196613, 0, 0, 131077, 0, 0, 65541, 0, 0, 5, 0, 0, 327684, 0, 0, 262148, 0, 0, 196612, 0, 0, 131076, 0, 0, 65540, 0, 0, 4, 0, 0, 327683, 0, 0, 262147, 0, 0, 196611, 0, 0, 131075, 0, 0, 65539, 0, 0, 3, 0, 0, 327682, 0, 0, 262146, 0, 0, 196610, 0, 0, 131074, 0, 0, 65538, 131072, 0, 2, 0, 0, 327681, 0, 0, 262145, 0, 0, 196609, 0, 0, 131073, 0, 0, 65537, 0, 0, 1, 0, 0, 327680, 0, 0, 262144, 0, 0, 196608, 131072, 0, 131072, 0, 0, 65536, 0, 0, 0, 0, 0, 1245193, 0, 0, 1179657, 65536, 0, 1114121, 131072, 3, 1048585, 131072, 2, 983049, 131072, 2, 917513, 131072, 2, 851977, 131072, 1, 786441, 0, 0, 720905, 0, 0, 655369, 0, 0, 589833, 0, 0, 524297, 0, 0, 458761, 0, 0, 393225, 0, 0, 1245192, 0, 0, 1179656, 0, 0, 1114120, 65536, 3, 1048584, 65536, 2, 983048, 65536, 2, 917512, 65536, 2, 851976, 65536, 1, 786440, 0, 0, 720904, 0, 0, 655368, 0, 0, 589832, 0, 0, 524296, 0, 0, 458760, 0, 0, 393224, 0, 0, 1245191, 0, 0, 1179655, 0, 0, 1114119, 65536, 3, 1048583, 65536, 2, 983047, 65536, 2, 917511, 65536, 2, 851975, 65536, 1, 786439, 0, 0, 720903, 0, 0, 655367, 0, 0, 589831, 0, 0, 524295, 131072, 0, 458759, 0, 0, 393223, 0, 0, 1245190, 131072, 0, 1179654, 0, 0, 1114118, 0, 3, 1048582, 0, 2, 983046, 0, 2, 917510, 0, 2, 851974, 0, 1, 786438, 0, 0, 720902, 0, 0, 655366, 0, 0, 589830, 0, 0, 524294, 0, 0, 458758, 0, 0, 393222, 0, 0, 1245189, 0, 0, 1179653, 0, 0, 1114117, 0, 0, 1048581, 0, 0, 983045, 0, 0, 917509, 0, 0, 851973, 0, 0, 786437, 0, 0, 720901, 0, 0, 655365, 131072, 0, 589829, 0, 0, 524293, 0, 0, 458757, 0, 0, 393221, 0, 0, 1245188, 0, 0, 1179652, 0, 0, 1114116, 131072, 0, 1048580, 0, 0, 983044, 0, 0, 917508, 0, 0, 851972, 0, 0, 786436, 0, 0, 720900, 0, 0, 655364, 0, 0, 589828, 0, 0, 524292, 0, 0, 458756, 0, 0, 393220, 0, 0, 1245187, 0, 0, 1179651, 0, 0, 1114115, 0, 0, 1048579, 0, 0, 983043, 0, 0, 917507, 131072, 0, 851971, 65536, 0, 786435, 131072, 0, 720899, 65536, 0, 655363, 0, 0, 589827, 0, 0, 524291, 0, 0, 458755, 0, 0, 393219, 131072, 0, 1245186, 0, 0, 1179650, 0, 0, 1114114, 0, 0, 1048578, 0, 0, 983042, 0, 0, 917506, 65536, 0, 851970, 0, 0, 786434, 0, 0, 720898, 0, 0, 655362, 0, 0, 589826, 0, 0, 524290, 0, 0, 458754, 0, 0, 393218, 0, 0, 1245185, 0, 0, 1179649, 0, 0, 1114113, 0, 0, 1048577, 131072, 0, 983041, 0, 0, 917505, 0, 0, 851969, 0, 0, 786433, 0, 0, 720897, 0, 0, 655361, 0, 0, 589825, 0, 0, 524289, 0, 0, 458753, 0, 0, 393217, 0, 0, 1245184, 0, 0, 1179648, 0, 0, 1114112, 0, 0, 1048576, 0, 0, 983040, 0, 0, 917504, 0, 0, 851968, 0, 0, 786432, 0, 0, 720896, 0, 0, 655360, 0, 0, 589824, 65536, 0, 524288, 0, 0, 458752, 131072, 0, 393216, 0, 0) [node name="Objects" type="TileMap" parent="."] y_sort_enabled = true @@ -97,9 +98,11 @@ gameboard = ExtResource("6_kb2po") metadata/_edit_lock_ = true [node name="Camera2D" type="Camera2D" parent="."] -scale = Vector2(0.2, 0.2) +limit_smoothed = true position_smoothing_enabled = true position_smoothing_speed = 8.0 +script = ExtResource("13_ev55l") +gameboard = ExtResource("6_kb2po") metadata/_edit_lock_ = true [node name="MusicPlayer" parent="." instance=ExtResource("20_yreyb")] diff --git a/world/demo_area/default_gameboard.tres b/world/demo_area/default_gameboard.tres index bf2f0e3..31bd288 100644 --- a/world/demo_area/default_gameboard.tres +++ b/world/demo_area/default_gameboard.tres @@ -4,5 +4,5 @@ [resource] script = ExtResource("1_2pet3") -boundaries = Rect2i(0, 0, 10, 6) +boundaries = Rect2i(0, 0, 10, 20) cell_size = Vector2i(16, 16) -- GitLab