Skip to content

Commit

Permalink
Part 3 - Enemy tank: pathfollowing and aiming
Browse files Browse the repository at this point in the history
  • Loading branch information
Chris Bradfield committed Apr 5, 2018
1 parent 5936b57 commit 6aa4ed6
Show file tree
Hide file tree
Showing 4 changed files with 134 additions and 3 deletions.
36 changes: 34 additions & 2 deletions maps/Map01.tscn
Original file line number Diff line number Diff line change
@@ -1,8 +1,16 @@
[gd_scene load_steps=4 format=2]
[gd_scene load_steps=6 format=2]

[ext_resource path="res://maps/Map.gd" type="Script" id=1]
[ext_resource path="res://terrain/terrain_tiles.tres" type="TileSet" id=2]
[ext_resource path="res://tanks/Player.tscn" type="PackedScene" id=3]
[ext_resource path="res://tanks/EnemyTank.tscn" type="PackedScene" id=4]

[sub_resource type="Curve2D" id=1]

bake_interval = 5.0
_data = {
"points": PoolVector2Array( 0, 0, 0, 0, 1601.45, 1012.35, -3.51986, -22.0791, 2.87988, 64.9573, 1600.03, 1781.92, -60.7975, -3.19987, 110.075, 0.639975, 1661.2, 1860.16, -83.5872, 4.75823, 69.7571, 0, 2658.03, 1860.19, -3.12715, 57.6289, -4.72708, -63.6338, 2755.59, 1786.34, 0, 0, 49.0723, -1.42539, 2823.39, 1727.87, -66.871, 0.905931, 84.0485, 6.07352, 2935.77, 1727.87, 0, 0, -9.59961, -79.6767, 3011.16, 1662.43, 0, 0, 0.959961, -61.4375, 3009.74, 1025.15, 0, 0, -53.1178, -5.43978, 2941.54, 958.792, 0, 0, -64.3174, -3.51986, 1664.04, 961.137, 0, 0, 0, 0, 1601.45, 1012.35 )
}

[node name="Map01" type="Node2D" index="0"]

Expand All @@ -27,8 +35,32 @@ collision_mask = 1
occluder_light_mask = 1
format = 1
tile_data = PoolIntArray( -1, 0, 0, -65536, 0, 0, -65535, 0, 0, -65534, 0, 0, -65533, 1, 0, -65532, 0, 0, -65531, 0, 0, -65530, 0, 0, -65529, 0, 0, -65528, 0, 0, -65527, 0, 0, -65526, 0, 0, -65525, 1, 0, -65524, 1, 0, -65523, 0, 0, -65522, 0, 0, -65521, 0, 0, -65520, 0, 0, -65519, 0, 0, -65518, 0, 0, -65517, 7, 0, -65516, 30, 0, -65515, 30, 0, -65514, 30, 0, -65513, 21, 0, -65512, 30, 0, -65511, 30, 0, -65510, 30, 0, -65509, 30, 0, -65508, 30, 0, -65507, 30, 0, -65506, 30, 0, -65505, 30, 0, -65504, 30, 0, -65503, 30, 0, -65502, 30, 0, -65501, 30, 0, -65500, 30, 0, 65535, 0, 0, 0, 0, 0, 1, 0, 0, 2, 0, 0, 3, 1, 0, 4, 0, 0, 5, 0, 0, 6, 0, 0, 7, 0, 0, 8, 0, 0, 9, 0, 0, 10, 10, 0, 11, 1, 0, 12, 1, 0, 13, 10, 0, 14, 10, 0, 15, 10, 0, 16, 10, 0, 17, 10, 0, 18, 0, 0, 19, 7, 0, 20, 30, 0, 21, 30, 0, 22, 30, 0, 23, 21, 0, 24, 30, 0, 25, 30, 0, 26, 30, 0, 27, 30, 0, 28, 30, 0, 29, 30, 0, 30, 30, 0, 31, 30, 0, 32, 30, 0, 33, 30, 0, 34, 30, 0, 35, 30, 0, 36, 30, 0, 131071, 0, 0, 65536, 0, 0, 65537, 0, 0, 65538, 0, 0, 65539, 1, 0, 65540, 0, 0, 65541, 0, 0, 65542, 0, 0, 65543, 0, 0, 65544, 0, 0, 65545, 0, 0, 65546, 10, 0, 65547, 1, 0, 65548, 1, 0, 65549, 10, 0, 65550, 10, 0, 65551, 10, 0, 65552, 10, 0, 65553, 10, 0, 65554, 0, 0, 65555, 7, 0, 65556, 30, 0, 65557, 30, 0, 65558, 30, 0, 65559, 21, 0, 65560, 30, 0, 65561, 30, 0, 65562, 30, 0, 65563, 30, 0, 65564, 30, 0, 65565, 30, 0, 65566, 30, 0, 65567, 30, 0, 65568, 30, 0, 65569, 30, 0, 65570, 30, 0, 65571, 30, 0, 65572, 30, 0, 196607, 0, 0, 131072, 0, 0, 131073, 0, 0, 131074, 0, 0, 131075, 1, 0, 131076, 0, 0, 131077, 0, 0, 131078, 0, 0, 131079, 0, 0, 131080, 0, 0, 131081, 0, 0, 131082, 10, 0, 131083, 1, 0, 131084, 1, 0, 131085, 10, 0, 131086, 10, 0, 131087, 10, 0, 131088, 10, 0, 131089, 10, 0, 131090, 0, 0, 131091, 7, 0, 131092, 30, 0, 131093, 30, 0, 131094, 30, 0, 131095, 21, 0, 131096, 30, 0, 131097, 30, 0, 131098, 30, 0, 131099, 30, 0, 131100, 30, 0, 131101, 30, 0, 131102, 30, 0, 131103, 30, 0, 131104, 30, 0, 131105, 30, 0, 131106, 30, 0, 131107, 30, 0, 131108, 30, 0, 262143, 0, 0, 196608, 0, 0, 196609, 0, 0, 196610, 0, 0, 196611, 1, 0, 196612, 0, 0, 196613, 0, 0, 196614, 0, 0, 196615, 0, 0, 196616, 0, 0, 196617, 0, 0, 196618, 10, 0, 196619, 1, 0, 196620, 1, 0, 196621, 10, 0, 196622, 10, 0, 196623, 10, 0, 196624, 10, 0, 196625, 10, 0, 196626, 0, 0, 196627, 7, 0, 196628, 30, 0, 196629, 30, 0, 196630, 30, 0, 196631, 21, 0, 196632, 30, 0, 196633, 30, 0, 196634, 30, 0, 196635, 30, 0, 196636, 30, 0, 196637, 30, 0, 196638, 30, 0, 196639, 30, 0, 196640, 30, 0, 196641, 30, 0, 196642, 30, 0, 196643, 30, 0, 196644, 30, 0, 327679, 0, 0, 262144, 0, 0, 262145, 0, 0, 262146, 0, 0, 262147, 1, 0, 262148, 0, 0, 262149, 0, 0, 262150, 0, 0, 262151, 0, 0, 262152, 0, 0, 262153, 0, 0, 262154, 10, 0, 262155, 1, 0, 262156, 1, 0, 262157, 10, 0, 262158, 10, 0, 262159, 10, 0, 262160, 10, 0, 262161, 10, 0, 262162, 0, 0, 262163, 7, 0, 262164, 30, 0, 262165, 30, 0, 262166, 30, 0, 262167, 21, 0, 262168, 30, 0, 262169, 30, 0, 262170, 30, 0, 262171, 30, 0, 262172, 30, 0, 262173, 30, 0, 262174, 30, 0, 262175, 30, 0, 262176, 30, 0, 262177, 30, 0, 262178, 30, 0, 262179, 30, 0, 262180, 30, 0, 393215, 0, 0, 327680, 0, 0, 327681, 0, 0, 327682, 0, 0, 327683, 1, 0, 327684, 0, 0, 327685, 0, 0, 327686, 0, 0, 327687, 0, 0, 327688, 0, 0, 327689, 0, 0, 327690, 10, 0, 327691, 1, 0, 327692, 1, 0, 327693, 10, 0, 327694, 10, 0, 327695, 10, 0, 327696, 10, 0, 327697, 10, 0, 327698, 0, 0, 327699, 7, 0, 327700, 30, 0, 327701, 30, 0, 327702, 30, 0, 327703, 21, 0, 327704, 30, 0, 327705, 30, 0, 327706, 30, 0, 327707, 30, 0, 327708, 30, 0, 327709, 30, 0, 327710, 30, 0, 327711, 30, 0, 327712, 30, 0, 327713, 30, 0, 327714, 30, 0, 327715, 30, 0, 327716, 30, 0, 458751, 2, 0, 393216, 2, 0, 393217, 2, 0, 393218, 2, 0, 393219, 12, 0, 393220, 2, 0, 393221, 2, 0, 393222, 2, 0, 393223, 2, 0, 393224, 2, 0, 393225, 2, 0, 393226, 2, 0, 393227, 12, 0, 393228, 12, 0, 393229, 2, 0, 393230, 2, 0, 393231, 2, 0, 393232, 2, 0, 393233, 6, 0, 393234, 2, 0, 393235, 27, 0, 393236, 22, 0, 393237, 22, 0, 393238, 22, 0, 393239, 32, 0, 393240, 22, 0, 393241, 22, 0, 393242, 22, 0, 393243, 22, 0, 393244, 22, 0, 393245, 22, 0, 393246, 22, 0, 393247, 22, 0, 393248, 22, 0, 393249, 22, 0, 393250, 26, 0, 393251, 22, 0, 393252, 22, 0, 524287, 2, 0, 458752, 2, 0, 458753, 2, 0, 458754, 2, 0, 458755, 12, 0, 458756, 2, 0, 458757, 2, 0, 458758, 2, 0, 458759, 2, 0, 458760, 2, 0, 458761, 2, 0, 458762, 2, 0, 458763, 12, 0, 458764, 12, 0, 458765, 2, 0, 458766, 2, 0, 458767, 2, 0, 458768, 2, 0, 458769, 5, 0, 458770, 2, 0, 458771, 29, 0, 458772, 22, 0, 458773, 22, 0, 458774, 22, 0, 458775, 32, 0, 458776, 22, 0, 458777, 22, 0, 458778, 22, 0, 458779, 22, 0, 458780, 26, 0, 458781, 22, 0, 458782, 22, 0, 458783, 22, 0, 458784, 22, 0, 458785, 22, 0, 458786, 25, 0, 458787, 22, 0, 458788, 22, 0, 589823, 0, 0, 524288, 10, 0, 524289, 10, 0, 524290, 10, 0, 524291, 1, 0, 524292, 10, 0, 524293, 10, 0, 524294, 10, 0, 524295, 10, 0, 524296, 10, 0, 524297, 10, 0, 524298, 10, 0, 524299, 1, 0, 524300, 1, 0, 524301, 10, 0, 524302, 10, 0, 524303, 10, 0, 524304, 10, 0, 524305, 10, 0, 524306, 0, 0, 524307, 7, 0, 524308, 30, 0, 524309, 30, 0, 524310, 30, 0, 524311, 21, 0, 524312, 30, 0, 524313, 30, 0, 524314, 30, 0, 524315, 30, 0, 524316, 21, 0, 524317, 30, 0, 524318, 30, 0, 524319, 30, 0, 524320, 30, 0, 524321, 30, 0, 524322, 30, 0, 524323, 30, 0, 524324, 30, 0, 655359, 0, 0, 589824, 10, 0, 589825, 10, 0, 589826, 10, 0, 589827, 1, 0, 589828, 10, 0, 589829, 10, 0, 589830, 10, 0, 589831, 10, 0, 589832, 10, 0, 589833, 10, 0, 589834, 10, 0, 589835, 1, 0, 589836, 1, 0, 589837, 10, 0, 589838, 10, 0, 589839, 10, 0, 589840, 10, 0, 589841, 10, 0, 589842, 0, 0, 589843, 7, 0, 589844, 30, 0, 589845, 30, 0, 589846, 30, 0, 589847, 21, 0, 589848, 30, 0, 589849, 30, 0, 589850, 30, 0, 589851, 30, 0, 589852, 21, 0, 589853, 30, 0, 589854, 30, 0, 589855, 30, 0, 589856, 30, 0, 589857, 30, 0, 589858, 30, 0, 589859, 30, 0, 589860, 30, 0, 720895, 0, 0, 655360, 10, 0, 655361, 10, 0, 655362, 10, 0, 655363, 1, 0, 655364, 10, 0, 655365, 10, 0, 655366, 10, 0, 655367, 10, 0, 655368, 10, 0, 655369, 10, 0, 655370, 10, 0, 655371, 1, 0, 655372, 1, 0, 655373, 10, 0, 655374, 10, 0, 655375, 10, 0, 655376, 10, 0, 655377, 10, 0, 655378, 0, 0, 655379, 7, 0, 655380, 30, 0, 655381, 30, 0, 655382, 30, 0, 655383, 21, 0, 655384, 30, 0, 655385, 30, 0, 655386, 30, 0, 655387, 30, 0, 655388, 21, 0, 655389, 30, 0, 655390, 30, 0, 655391, 30, 0, 655392, 30, 0, 655393, 30, 0, 655394, 30, 0, 655395, 30, 0, 655396, 30, 0, 786431, 0, 0, 720896, 10, 0, 720897, 10, 0, 720898, 10, 0, 720899, 1, 0, 720900, 10, 0, 720901, 10, 0, 720902, 10, 0, 720903, 10, 0, 720904, 10, 0, 720905, 10, 0, 720906, 10, 0, 720907, 1, 0, 720908, 1, 0, 720909, 10, 0, 720910, 10, 0, 720911, 10, 0, 720912, 10, 0, 720913, 10, 0, 720914, 0, 0, 720915, 7, 0, 720916, 30, 0, 720917, 30, 0, 720918, 30, 0, 720919, 21, 0, 720920, 30, 0, 720921, 30, 0, 720922, 30, 0, 720923, 30, 0, 720924, 21, 0, 720925, 30, 0, 720926, 30, 0, 720927, 30, 0, 720928, 30, 0, 720929, 30, 0, 720930, 30, 0, 720931, 30, 0, 720932, 30, 0, 851967, 0, 0, 786432, 10, 0, 786433, 10, 0, 786434, 10, 0, 786435, 15, 0, 786436, 2, 0, 786437, 2, 0, 786438, 2, 0, 786439, 2, 0, 786440, 2, 0, 786441, 2, 0, 786442, 2, 0, 786443, 12, 0, 786444, 4, 0, 786445, 10, 0, 786446, 10, 0, 786447, 10, 0, 786448, 10, 0, 786449, 10, 0, 786450, 0, 0, 786451, 7, 0, 786452, 30, 0, 786453, 30, 0, 786454, 30, 0, 786455, 21, 0, 786456, 30, 0, 786457, 30, 0, 786458, 30, 0, 786459, 30, 0, 786460, 21, 0, 786461, 30, 0, 786462, 30, 0, 786463, 30, 0, 786464, 30, 0, 786465, 30, 0, 786466, 30, 0, 786467, 30, 0, 786468, 30, 0, 917503, 0, 0, 851968, 10, 0, 851969, 10, 0, 851970, 10, 0, 851971, 10, 0, 851972, 10, 0, 851973, 10, 0, 851974, 10, 0, 851975, 10, 0, 851976, 10, 0, 851977, 10, 0, 851978, 10, 0, 851979, 1, 0, 851980, 1, 0, 851981, 10, 0, 851982, 10, 0, 851983, 10, 0, 851984, 10, 0, 851985, 10, 0, 851986, 0, 0, 851987, 7, 0, 851988, 30, 0, 851989, 33, 0, 851990, 22, 0, 851991, 31, 0, 851992, 22, 0, 851993, 22, 0, 851994, 22, 0, 851995, 22, 0, 851996, 31, 0, 851997, 22, 0, 851998, 22, 0, 851999, 22, 0, 852000, 22, 0, 852001, 22, 0, 852002, 22, 0, 852003, 22, 0, 852004, 22, 0, 983039, 2, 0, 917504, 2, 0, 917505, 14, 0, 917506, 10, 0, 917507, 10, 0, 917508, 10, 0, 917509, 10, 0, 917510, 10, 0, 917511, 10, 0, 917512, 10, 0, 917513, 10, 0, 917514, 10, 0, 917515, 1, 0, 917516, 3, 0, 917517, 2, 0, 917518, 2, 0, 917519, 2, 0, 917520, 2, 0, 917521, 2, 0, 917522, 2, 0, 917523, 27, 0, 917524, 22, 0, 917525, 36, 0, 917526, 30, 0, 917527, 21, 0, 917528, 30, 0, 917529, 30, 0, 917530, 30, 0, 917531, 30, 0, 917532, 21, 0, 917533, 30, 0, 917534, 30, 0, 917535, 30, 0, 917536, 30, 0, 917537, 30, 0, 917538, 30, 0, 917539, 30, 0, 917540, 30, 0, 1048575, 0, 0, 983040, 10, 0, 983041, 1, 0, 983042, 10, 0, 983043, 10, 0, 983044, 10, 0, 983045, 10, 0, 983046, 10, 0, 983047, 10, 0, 983048, 10, 0, 983049, 10, 0, 983050, 10, 0, 983051, 1, 0, 983052, 1, 0, 983053, 10, 0, 983054, 10, 0, 983055, 10, 0, 983056, 10, 0, 983057, 10, 0, 983058, 0, 0, 983059, 7, 0, 983060, 30, 0, 983061, 30, 0, 983062, 30, 0, 983063, 21, 0, 983064, 30, 0, 983065, 30, 0, 983066, 30, 0, 983067, 30, 0, 983068, 21, 0, 983069, 30, 0, 983070, 30, 0, 983071, 30, 0, 983072, 30, 0, 983073, 30, 0, 983074, 30, 0, 983075, 30, 0, 983076, 30, 0, 1114111, 0, 0, 1048576, 10, 0, 1048577, 1, 0, 1048578, 10, 0, 1048579, 10, 0, 1048580, 10, 0, 1048581, 10, 0, 1048582, 10, 0, 1048583, 10, 0, 1048584, 10, 0, 1048585, 10, 0, 1048586, 10, 0, 1048587, 1, 0, 1048588, 1, 0, 1048589, 10, 0, 1048590, 10, 0, 1048591, 10, 0, 1048592, 10, 0, 1048593, 10, 0, 1048594, 0, 0, 1048595, 7, 0, 1048596, 30, 0, 1048597, 30, 0, 1048598, 30, 0, 1048599, 21, 0, 1048600, 30, 0, 1048601, 30, 0, 1048602, 30, 0, 1048603, 30, 0, 1048604, 21, 0, 1048605, 30, 0, 1048606, 30, 0, 1048607, 30, 0, 1048608, 30, 0, 1048609, 30, 0, 1048610, 30, 0, 1048611, 30, 0, 1048612, 30, 0, 1179647, 0, 0, 1114112, 0, 0, 1114113, 1, 0, 1114114, 0, 0, 1114115, 0, 0, 1114116, 0, 0, 1114117, 0, 0, 1114118, 0, 0, 1114119, 0, 0, 1114120, 0, 0, 1114121, 0, 0, 1114122, 0, 0, 1114123, 1, 0, 1114124, 1, 0, 1114125, 0, 0, 1114126, 0, 0, 1114127, 0, 0, 1114128, 0, 0, 1114129, 0, 0, 1114130, 0, 0, 1114131, 7, 0, 1114132, 30, 0, 1114133, 30, 0, 1114134, 30, 0, 1114135, 21, 0, 1114136, 30, 0, 1114137, 30, 0, 1114138, 30, 0, 1114139, 30, 0, 1114140, 21, 0, 1114141, 30, 0, 1114142, 30, 0, 1114143, 30, 0, 1114144, 30, 0, 1114145, 30, 0, 1114146, 30, 0, 1114147, 30, 0, 1114148, 30, 0 )
_sections_unfolded = [ "Cell" ]
_sections_unfolded = [ "Cell", "Visibility" ]

[node name="Player" parent="." index="1" instance=ExtResource( 3 )]

position = Vector2( 1941.57, 1312.05 )

[node name="Paths" type="Node2D" parent="." index="2"]

[node name="Path2D" type="Path2D" parent="Paths" index="0"]

curve = SubResource( 1 )

[node name="PathFollow2D" type="PathFollow2D" parent="Paths/Path2D" index="0"]

position = Vector2( 1601.45, 1012.35 )
rotation = 1.57734
offset = 0.0
h_offset = 0.0
v_offset = 0.0
rotate = true
cubic_interp = true
loop = true
lookahead = 4.0

[node name="EnemyTank" parent="Paths/Path2D/PathFollow2D" index="0" instance=ExtResource( 4 )]

detect_radius = 400


33 changes: 33 additions & 0 deletions tanks/EnemyTank.gd
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
extends "res://tanks/Tank.gd"

onready var parent = get_parent()

export (float) var turret_speed
export (int) var detect_radius

var target = null

func _ready():
$DetectRadius/CollisionShape2D.shape.radius = detect_radius

func control(delta):
if parent is PathFollow2D:
parent.set_offset(parent.get_offset() + speed * delta)
position = Vector2()
else:
# other movement code
pass

func _process(delta):
if target:
var target_dir = (target.global_position - global_position).normalized()
var current_dir = Vector2(1, 0).rotated($Turret.global_rotation)
$Turret.global_rotation = current_dir.linear_interpolate(target_dir, turret_speed * delta).angle()

func _on_DetectRadius_body_entered(body):
if body.name == "Player":
target = body

func _on_DetectRadius_body_exited(body):
if body == target:
target = null
66 changes: 66 additions & 0 deletions tanks/EnemyTank.tscn
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
[gd_scene load_steps=6 format=2]

[ext_resource path="res://tanks/Tank.tscn" type="PackedScene" id=1]
[ext_resource path="res://tanks/EnemyTank.gd" type="Script" id=2]
[ext_resource path="res://assets/onlyObjects_retina_rotated.png" type="Texture" id=3]

[sub_resource type="RectangleShape2D" id=1]

custom_solver_bias = 0.0
extents = Vector2( 39.7858, 35.5505 )

[sub_resource type="CircleShape2D" id=2]

custom_solver_bias = 0.0
radius = 350.571

[node name="EnemyTank" instance=ExtResource( 1 )]

script = ExtResource( 2 )
Bullet = null
speed = 150
rotation_speed = null
gun_cooldown = 0.5
health = 100
turret_speed = 1.0
detect_radius = 200

[node name="Body" parent="." index="0"]

texture = ExtResource( 3 )
region_enabled = true
region_rect = Rect2( 268, 186, 80, 76 )
_sections_unfolded = [ "Region" ]

[node name="CollisionShape2D" parent="." index="1"]

visible = false
shape = SubResource( 1 )

[node name="Turret" parent="." index="2"]

texture = ExtResource( 3 )
offset = Vector2( 20, 0 )
region_enabled = true
region_rect = Rect2( 371, 14, 60, 24 )
_sections_unfolded = [ "Offset", "Region", "Transform" ]

[node name="DetectRadius" type="Area2D" parent="." index="4"]

input_pickable = true
gravity_vec = Vector2( 0, 1 )
gravity = 98.0
linear_damp = 0.1
angular_damp = 1.0
audio_bus_override = false
audio_bus_name = "Master"

[node name="CollisionShape2D" type="CollisionShape2D" parent="DetectRadius" index="0"]

shape = SubResource( 2 )

[connection signal="body_entered" from="DetectRadius" to="." method="_on_DetectRadius_body_entered"]

[connection signal="body_exited" from="DetectRadius" to="." method="_on_DetectRadius_body_exited"]


2 changes: 1 addition & 1 deletion terrain/TileSetMaker.tscn
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
[ext_resource path="res://terrain/TileSetMaker.gd" type="Script" id=1]
[ext_resource path="res://assets/terrainTiles_retina.png" type="Texture" id=2]

[node name="TileSetMaker" type="Node" index="0"]
[node name="TileSetMaker" type="Node"]

script = ExtResource( 1 )

Expand Down

0 comments on commit 6aa4ed6

Please sign in to comment.