From e18f50ee98ed9ca813bdaea9977dfe574bd42228 Mon Sep 17 00:00:00 2001 From: Gary Wang Date: Mon, 23 Nov 2020 23:27:10 +0800 Subject: [PATCH] first camera test, not happy with the result --- Player.gd | 31 +++++++++++++++++++++++++++++++ TestLevel.tscn | 23 +++++++++++++++++++++-- 2 files changed, 52 insertions(+), 2 deletions(-) diff --git a/Player.gd b/Player.gd index 6e1b80f..a0f2561 100644 --- a/Player.gd +++ b/Player.gd @@ -30,6 +30,8 @@ var var_jump = 0 onready var sprite = $Sprite +onready var camera = $Camera2D +onready var area_camera = $AreaCamera2D func _physics_process(delta): @@ -83,3 +85,32 @@ func _physics_process(delta): # Apply movement speed = move_and_slide(speed, Vector2.UP) + + +func _on_CameraLimitArea_body_shape_entered(body_id, body, body_shape, area_shape): + if body is TileMap: + return + var body_shape_owner_id = body.shape_find_owner(body_shape) + var body_shape_owner = body.shape_owner_get_owner(body_shape_owner_id) + var body_shape_2d = body.shape_owner_get_shape(body_shape_owner_id, 0) + var body_global_transform = body_shape_owner.global_transform + + var area_shape_owner_id = shape_find_owner(area_shape) + var area_shape_owner = shape_owner_get_owner(area_shape_owner_id) + var area_shape_2d = shape_owner_get_shape(area_shape_owner_id, 0) + var area_global_transform = area_shape_owner.global_transform + + var collision_points = area_shape_2d.collide_and_get_contacts(area_global_transform, + body_shape_2d, + body_global_transform) + print(body_shape_2d.extents, area_shape_2d.extents) + area_camera.limit_left = 0 + area_camera.limit_top = 0 + area_camera.limit_right = 320 + area_camera.limit_bottom = 176 + area_camera.make_current() + print(area_global_transform.x.x, area_global_transform.y.y, area_global_transform.y.x, area_global_transform.x.y) + + +func _on_CameraLimitArea_body_shape_exited(body_id, body, body_shape, area_shape): + camera.make_current() diff --git a/TestLevel.tscn b/TestLevel.tscn index 7d35327..0d25a64 100644 --- a/TestLevel.tscn +++ b/TestLevel.tscn @@ -1,4 +1,4 @@ -[gd_scene load_steps=65 format=2] +[gd_scene load_steps=66 format=2] [ext_resource path="res://Player.tscn" type="PackedScene" id=1] [ext_resource path="res://Assets/Tilesets/template.png" type="Texture" id=2] @@ -578,13 +578,32 @@ points = PoolVector2Array( 0, 0, 8, 0, 8, 8, 0, 8 ) } ] 0/z_index = 0 +[sub_resource type="RectangleShape2D" id=63] +extents = Vector2( 160, 88 ) + [node name="World" type="Node2D"] [node name="Player" parent="." instance=ExtResource( 1 )] position = Vector2( 120.764, 151.995 ) +[node name="Camera2D" type="Camera2D" parent="Player"] +current = true +limit_smoothed = true +smoothing_enabled = true + +[node name="AreaCamera2D" type="Camera2D" parent="Player"] +smoothing_enabled = true + [node name="TileMap" type="TileMap" parent="."] tile_set = SubResource( 62 ) cell_size = Vector2( 8, 8 ) format = 1 -tile_data = PoolIntArray( -393216, 0, 393218, -327680, 0, 327680, -327641, 0, 393216, -262144, 0, 327683, -262105, 0, 327683, -196608, 0, 327682, -196569, 0, 327681, -131072, 0, 327682, -131033, 0, 327681, -65536, 0, 327681, -65497, 0, 327682, 0, 0, 327683, 39, 0, 327680, 65536, 0, 327680, 65575, 0, 327681, 131072, 0, 327681, 131111, 0, 327682, 196608, 0, 327683, 196622, 0, 524291, 196623, 0, 262145, 196624, 0, 262145, 196625, 0, 589827, 196630, 0, 524289, 196631, 0, 262145, 196632, 0, 262145, 196633, 0, 589825, 196647, 0, 327682, 262144, 0, 131075, 262145, 0, 262145, 262146, 0, 3, 262147, 0, 262147, 262148, 0, 786434, 262152, 0, 720896, 262153, 0, 786432, 262183, 0, 327683, 327680, 0, 327681, 327682, 0, 327682, 327684, 0, 327682, 327688, 0, 131072, 327689, 0, 196608, 327708, 0, 524288, 327709, 0, 262146, 327710, 0, 262145, 327711, 0, 589825, 327719, 0, 327680, 393216, 0, 327680, 393218, 0, 327680, 393220, 0, 327683, 393224, 0, 131072, 393225, 0, 196608, 393255, 0, 327680, 458752, 0, 327682, 458754, 0, 327683, 458756, 0, 327683, 458760, 0, 131072, 458761, 0, 196608, 458791, 0, 327683, 524288, 0, 327681, 524290, 0, 851971, 524291, 0, 262144, 524292, 0, 196611, 524296, 0, 131072, 524297, 0, 196608, 524327, 0, 327682, 589824, 0, 327682, 589828, 0, 327683, 589832, 0, 131072, 589833, 0, 196608, 589857, 0, 524288, 589858, 0, 262147, 589859, 0, 589827, 589863, 0, 327683, 655360, 0, 327681, 655362, 0, 393217, 655364, 0, 327680, 655368, 0, 131072, 655369, 0, 196608, 655399, 0, 327680, 720896, 0, 327681, 720898, 0, 851970, 720899, 0, 262144, 720900, 0, 196611, 720904, 0, 131072, 720905, 0, 196608, 720935, 0, 327680, 786432, 0, 327681, 786436, 0, 327682, 786440, 0, 131072, 786441, 0, 196608, 786471, 0, 327683, 851968, 0, 327680, 851970, 0, 720898, 851971, 0, 262144, 851972, 0, 196611, 851976, 0, 131072, 851977, 0, 196608, 852001, 0, 524289, 852002, 0, 262147, 852003, 0, 262145, 852004, 0, 262146, 852005, 0, 262145, 852006, 0, 262146, 852007, 0, 196611, 917504, 0, 327681, 917506, 0, 327681, 917508, 0, 327682, 917512, 0, 131072, 917513, 0, 196608, 917543, 0, 327681, 983040, 0, 131075, 983041, 0, 262146, 983042, 0, 65539, 983043, 0, 262146, 983044, 0, 917506, 983048, 0, 851969, 983049, 0, 917504, 983079, 0, 327680, 1048576, 0, 327681, 1048602, 0, 524291, 1048603, 0, 262144, 1048604, 0, 262146, 1048605, 0, 262146, 1048606, 0, 589826, 1048615, 0, 327681, 1114112, 0, 327681, 1114151, 0, 327682, 1179648, 0, 327683, 1179687, 0, 327683, 1245184, 0, 327680, 1245223, 0, 327681, 1310720, 0, 131074, 1310721, 0, 0, 1310722, 0, 0, 1310723, 0, 0, 1310724, 0, 0, 1310725, 0, 0, 1310726, 0, 0, 1310727, 0, 0, 1310728, 0, 0, 1310729, 0, 0, 1310730, 0, 0, 1310731, 0, 0, 1310732, 0, 0, 1310733, 0, 0, 1310734, 0, 0, 1310735, 0, 0, 1310736, 0, 0, 1310737, 0, 0, 1310738, 0, 0, 1310739, 0, 0, 1310740, 0, 0, 1310741, 0, 0, 1310742, 0, 0, 1310743, 0, 0, 1310744, 0, 0, 1310745, 0, 0, 1310746, 0, 0, 1310747, 0, 0, 1310748, 0, 0, 1310749, 0, 0, 1310750, 0, 0, 1310751, 0, 0, 1310752, 0, 0, 1310753, 0, 0, 1310754, 0, 0, 1310755, 0, 0, 1310756, 0, 0, 1310757, 0, 0, 1310758, 0, 0, 1310759, 0, 196609, 1376256, 0, 131072, 1376257, 0, 5, 1376258, 0, 5, 1376259, 0, 5, 1376260, 0, 5, 1376261, 0, 5, 1376262, 0, 5, 1376263, 0, 5, 1376264, 0, 5, 1376265, 0, 5, 1376266, 0, 5, 1376267, 0, 5, 1376268, 0, 5, 1376269, 0, 5, 1376270, 0, 5, 1376271, 0, 5, 1376272, 0, 5, 1376273, 0, 5, 1376274, 0, 5, 1376275, 0, 5, 1376276, 0, 5, 1376277, 0, 5, 1376278, 0, 5, 1376279, 0, 5, 1376280, 0, 5, 1376281, 0, 5, 1376282, 0, 5, 1376283, 0, 5, 1376284, 0, 5, 1376285, 0, 5, 1376286, 0, 5, 1376287, 0, 5, 1376288, 0, 5, 1376289, 0, 5, 1376290, 0, 5, 1376291, 0, 5, 1376292, 0, 5, 1376293, 0, 5, 1376294, 0, 5, 1376295, 0, 196608, 1441792, 0, 851968, 1441793, 0, 65536, 1441794, 0, 65536, 1441795, 0, 65536, 1441796, 0, 65536, 1441797, 0, 65536, 1441798, 0, 65536, 1441799, 0, 65536, 1441800, 0, 65536, 1441801, 0, 65536, 1441802, 0, 65536, 1441803, 0, 65536, 1441804, 0, 65536, 1441805, 0, 65536, 1441806, 0, 65536, 1441807, 0, 65536, 1441808, 0, 65536, 1441809, 0, 65536, 1441810, 0, 65536, 1441811, 0, 65536, 1441812, 0, 65536, 1441813, 0, 65536, 1441814, 0, 65536, 1441815, 0, 65536, 1441816, 0, 65536, 1441817, 0, 65536, 1441818, 0, 65536, 1441819, 0, 65536, 1441820, 0, 65536, 1441821, 0, 65536, 1441822, 0, 65536, 1441823, 0, 65536, 1441824, 0, 65536, 1441825, 0, 65536, 1441826, 0, 65536, 1441827, 0, 65536, 1441828, 0, 65536, 1441829, 0, 65536, 1441830, 0, 65536, 1441831, 0, 917504 ) +tile_data = PoolIntArray( -393216, 0, 393218, -327680, 0, 327680, -327641, 0, 393216, -262144, 0, 327683, -262105, 0, 327683, -196608, 0, 327682, -196569, 0, 327681, -131072, 0, 327682, -131033, 0, 327681, -65536, 0, 327681, -65497, 0, 327682, 0, 0, 327683, 39, 0, 327680, 65536, 0, 327680, 65575, 0, 327681, 131072, 0, 327681, 131111, 0, 327682, 196608, 0, 327683, 196622, 0, 524291, 196623, 0, 262145, 196624, 0, 262145, 196625, 0, 589827, 196630, 0, 524289, 196631, 0, 262145, 196632, 0, 262145, 196633, 0, 589825, 196647, 0, 327682, 262144, 0, 131075, 262145, 0, 262145, 262146, 0, 3, 262147, 0, 262147, 262148, 0, 786434, 262152, 0, 720896, 262153, 0, 786432, 262183, 0, 327683, 327680, 0, 327681, 327682, 0, 327682, 327684, 0, 327682, 327688, 0, 131072, 327689, 0, 196608, 327708, 0, 524288, 327709, 0, 262146, 327710, 0, 262145, 327711, 0, 589825, 327719, 0, 327680, 393216, 0, 327680, 393218, 0, 327680, 393220, 0, 327683, 393224, 0, 131072, 393225, 0, 196608, 393255, 0, 327680, 458752, 0, 327682, 458754, 0, 327683, 458756, 0, 327683, 458760, 0, 131072, 458761, 0, 196608, 458791, 0, 327683, 458810, 0, 524291, 458811, 0, 786435, 524288, 0, 327681, 524290, 0, 851971, 524291, 0, 262144, 524292, 0, 196611, 524296, 0, 131072, 524297, 0, 196608, 524327, 0, 327682, 524347, 0, 327681, 589824, 0, 327682, 589828, 0, 327683, 589832, 0, 131072, 589833, 0, 196608, 589857, 0, 524288, 589858, 0, 262147, 589859, 0, 589827, 589863, 0, 327683, 589883, 0, 327681, 655360, 0, 327681, 655362, 0, 393217, 655364, 0, 327680, 655368, 0, 131072, 655369, 0, 196608, 655399, 0, 327680, 655419, 0, 327681, 720896, 0, 327681, 720898, 0, 851970, 720899, 0, 262144, 720900, 0, 196611, 720904, 0, 131072, 720905, 0, 196608, 720935, 0, 327680, 720955, 0, 327680, 786432, 0, 327681, 786436, 0, 327682, 786440, 0, 131072, 786441, 0, 196608, 786471, 0, 327683, 786491, 0, 327680, 851968, 0, 327680, 851970, 0, 720898, 851971, 0, 262144, 851972, 0, 196611, 851976, 0, 131072, 851977, 0, 196608, 852001, 0, 524289, 852002, 0, 262147, 852003, 0, 262145, 852004, 0, 262146, 852005, 0, 262145, 852006, 0, 262146, 852007, 0, 196611, 852027, 0, 327682, 917504, 0, 327681, 917506, 0, 327681, 917508, 0, 327682, 917512, 0, 131072, 917513, 0, 196608, 917543, 0, 327681, 917563, 0, 327681, 983040, 0, 131075, 983041, 0, 262146, 983042, 0, 65539, 983043, 0, 262146, 983044, 0, 917506, 983048, 0, 851969, 983049, 0, 917504, 983079, 0, 327680, 983099, 0, 327682, 1048576, 0, 327681, 1048602, 0, 524291, 1048603, 0, 262144, 1048604, 0, 262146, 1048605, 0, 262146, 1048606, 0, 589826, 1048615, 0, 458755, 1048635, 0, 327682, 1114112, 0, 327681, 1114171, 0, 327683, 1179648, 0, 327683, 1179707, 0, 327682, 1245184, 0, 327680, 1245243, 0, 327680, 1310720, 0, 131074, 1310721, 0, 0, 1310722, 0, 0, 1310723, 0, 0, 1310724, 0, 0, 1310725, 0, 0, 1310726, 0, 0, 1310727, 0, 0, 1310728, 0, 0, 1310729, 0, 0, 1310730, 0, 0, 1310731, 0, 0, 1310732, 0, 0, 1310733, 0, 0, 1310734, 0, 0, 1310735, 0, 0, 1310736, 0, 0, 1310737, 0, 0, 1310738, 0, 0, 1310739, 0, 0, 1310740, 0, 0, 1310741, 0, 0, 1310742, 0, 0, 1310743, 0, 0, 1310744, 0, 0, 1310745, 0, 0, 1310746, 0, 0, 1310747, 0, 0, 1310748, 0, 0, 1310749, 0, 0, 1310750, 0, 0, 1310751, 0, 0, 1310752, 0, 0, 1310753, 0, 0, 1310754, 0, 0, 1310755, 0, 0, 1310756, 0, 0, 1310757, 0, 0, 1310758, 0, 0, 1310759, 0, 2, 1310760, 0, 262145, 1310761, 0, 262145, 1310762, 0, 262144, 1310763, 0, 262145, 1310764, 0, 262144, 1310765, 0, 262146, 1310766, 0, 262147, 1310767, 0, 786435, 1310779, 0, 327681, 1376256, 0, 131072, 1376257, 0, 5, 1376258, 0, 5, 1376259, 0, 5, 1376260, 0, 5, 1376261, 0, 5, 1376262, 0, 5, 1376263, 0, 5, 1376264, 0, 5, 1376265, 0, 5, 1376266, 0, 5, 1376267, 0, 5, 1376268, 0, 5, 1376269, 0, 5, 1376270, 0, 5, 1376271, 0, 5, 1376272, 0, 5, 1376273, 0, 5, 1376274, 0, 5, 1376275, 0, 5, 1376276, 0, 5, 1376277, 0, 5, 1376278, 0, 5, 1376279, 0, 5, 1376280, 0, 5, 1376281, 0, 5, 1376282, 0, 5, 1376283, 0, 5, 1376284, 0, 5, 1376285, 0, 5, 1376286, 0, 5, 1376287, 0, 5, 1376288, 0, 5, 1376289, 0, 5, 1376290, 0, 5, 1376291, 0, 5, 1376292, 0, 5, 1376293, 0, 5, 1376294, 0, 5, 1376295, 0, 196608, 1376303, 0, 327681, 1376315, 0, 327681, 1441792, 0, 851968, 1441793, 0, 65536, 1441794, 0, 65536, 1441795, 0, 65536, 1441796, 0, 65536, 1441797, 0, 65536, 1441798, 0, 65536, 1441799, 0, 65536, 1441800, 0, 65536, 1441801, 0, 65536, 1441802, 0, 65536, 1441803, 0, 65536, 1441804, 0, 65536, 1441805, 0, 65536, 1441806, 0, 65536, 1441807, 0, 65536, 1441808, 0, 65536, 1441809, 0, 65536, 1441810, 0, 131076, 1441811, 0, 4, 1441812, 0, 65536, 1441813, 0, 65536, 1441814, 0, 65536, 1441815, 0, 65536, 1441816, 0, 65536, 1441817, 0, 65536, 1441818, 0, 65536, 1441819, 0, 65536, 1441820, 0, 65536, 1441821, 0, 65536, 1441822, 0, 65536, 1441823, 0, 65536, 1441824, 0, 65536, 1441825, 0, 65536, 1441826, 0, 65536, 1441827, 0, 65536, 1441828, 0, 65536, 1441829, 0, 65536, 1441830, 0, 65536, 1441831, 0, 917504, 1441839, 0, 327683, 1441851, 0, 327682, 1507346, 0, 131072, 1507347, 0, 196608, 1507375, 0, 851970, 1507376, 0, 262144, 1507377, 0, 589826, 1507387, 0, 327683, 1572882, 0, 131072, 1572883, 0, 196608, 1572917, 0, 524290, 1572918, 0, 262146, 1572919, 0, 262147, 1572920, 0, 262145, 1572921, 0, 589826, 1572923, 0, 327681, 1638418, 0, 131072, 1638419, 0, 196608, 1638459, 0, 327682, 1703954, 0, 131072, 1703955, 0, 196608, 1703995, 0, 327681, 1769490, 0, 131072, 1769491, 0, 196608, 1769531, 0, 327683, 1835026, 0, 131072, 1835027, 0, 196608, 1835050, 0, 524290, 1835051, 0, 262144, 1835052, 0, 262146, 1835053, 0, 262147, 1835054, 0, 262147, 1835055, 0, 262145, 1835056, 0, 262145, 1835057, 0, 262144, 1835058, 0, 262145, 1835059, 0, 262145, 1835060, 0, 262147, 1835061, 0, 262145, 1835062, 0, 262145, 1835063, 0, 262144, 1835064, 0, 589825, 1835067, 0, 327681, 1900562, 0, 131072, 1900563, 0, 196608, 1900603, 0, 327680, 1966098, 0, 131072, 1966099, 0, 196608, 1966139, 0, 327681, 2031634, 0, 131072, 2031635, 0, 196608, 2031675, 0, 327683, 2097170, 0, 131072, 2097171, 0, 196608, 2097211, 0, 327683, 2162706, 0, 131072, 2162707, 0, 65540, 2162708, 0, 0, 2162709, 0, 0, 2162710, 0, 0, 2162711, 0, 0, 2162712, 0, 0, 2162713, 0, 0, 2162714, 0, 0, 2162715, 0, 0, 2162716, 0, 0, 2162717, 0, 0, 2162718, 0, 0, 2162719, 0, 0, 2162720, 0, 0, 2162721, 0, 0, 2162722, 0, 0, 2162723, 0, 0, 2162724, 0, 0, 2162725, 0, 0, 2162726, 0, 0, 2162727, 0, 0, 2162728, 0, 0, 2162729, 0, 0, 2162730, 0, 0, 2162731, 0, 0, 2162732, 0, 0, 2162733, 0, 0, 2162734, 0, 0, 2162735, 0, 0, 2162736, 0, 0, 2162737, 0, 0, 2162738, 0, 0, 2162739, 0, 0, 2162740, 0, 0, 2162741, 0, 0, 2162742, 0, 0, 2162743, 0, 0, 2162744, 0, 0, 2162745, 0, 0, 2162746, 0, 0, 2162747, 0, 196609, 2228242, 0, 131072, 2228243, 0, 5, 2228244, 0, 5, 2228245, 0, 5, 2228246, 0, 5, 2228247, 0, 5, 2228248, 0, 5, 2228249, 0, 5, 2228250, 0, 5, 2228251, 0, 5, 2228252, 0, 5, 2228253, 0, 5, 2228254, 0, 5, 2228255, 0, 5, 2228256, 0, 5, 2228257, 0, 5, 2228258, 0, 5, 2228259, 0, 5, 2228260, 0, 5, 2228261, 0, 5, 2228262, 0, 5, 2228263, 0, 5, 2228264, 0, 5, 2228265, 0, 5, 2228266, 0, 5, 2228267, 0, 5, 2228268, 0, 5, 2228269, 0, 5, 2228270, 0, 5, 2228271, 0, 5, 2228272, 0, 5, 2228273, 0, 5, 2228274, 0, 5, 2228275, 0, 5, 2228276, 0, 5, 2228277, 0, 5, 2228278, 0, 5, 2228279, 0, 5, 2228280, 0, 5, 2228281, 0, 5, 2228282, 0, 5, 2228283, 0, 196608, 2293778, 0, 851969, 2293779, 0, 65536, 2293780, 0, 65536, 2293781, 0, 65536, 2293782, 0, 65536, 2293783, 0, 65536, 2293784, 0, 65536, 2293785, 0, 65536, 2293786, 0, 65536, 2293787, 0, 65536, 2293788, 0, 65536, 2293789, 0, 65536, 2293790, 0, 65536, 2293791, 0, 65536, 2293792, 0, 65536, 2293793, 0, 65536, 2293794, 0, 65536, 2293795, 0, 65536, 2293796, 0, 65536, 2293797, 0, 65536, 2293798, 0, 65536, 2293799, 0, 65536, 2293800, 0, 65536, 2293801, 0, 65536, 2293802, 0, 65536, 2293803, 0, 65536, 2293804, 0, 65536, 2293805, 0, 65536, 2293806, 0, 65536, 2293807, 0, 65536, 2293808, 0, 65536, 2293809, 0, 65536, 2293810, 0, 65536, 2293811, 0, 65536, 2293812, 0, 65536, 2293813, 0, 65536, 2293814, 0, 65536, 2293815, 0, 65536, 2293816, 0, 65536, 2293817, 0, 65536, 2293818, 0, 65536, 2293819, 0, 917504 ) + +[node name="CameraLimitArea" type="Area2D" parent="."] + +[node name="CollisionShape2D" type="CollisionShape2D" parent="CameraLimitArea"] +position = Vector2( 160, 88 ) +shape = SubResource( 63 ) +[connection signal="body_shape_entered" from="CameraLimitArea" to="Player" method="_on_CameraLimitArea_body_shape_entered"] +[connection signal="body_shape_exited" from="CameraLimitArea" to="Player" method="_on_CameraLimitArea_body_shape_exited"]