Skip to content

Commit

Permalink
Part 05 - Enemy tank improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
Chris Bradfield committed Apr 25, 2018
1 parent 387a1b0 commit 670128e
Show file tree
Hide file tree
Showing 9 changed files with 91 additions and 20 deletions.
2 changes: 2 additions & 0 deletions bullets/EnemyBullet.gd
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
extends "res://bullets/Bullet.gd"

33 changes: 33 additions & 0 deletions bullets/EnemyBullet.tscn
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
[gd_scene load_steps=5 format=2]

[ext_resource path="res://bullets/Bullet.tscn" type="PackedScene" id=1]
[ext_resource path="res://bullets/EnemyBullet.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( 13.9515, 8.54269 )

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

collision_layer = 8
collision_mask = 3
script = ExtResource( 2 )
_sections_unfolded = [ "Collision" ]
speed = 800
damage = 10
lifetime = 0.5

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

rotation = 3.14159
texture = ExtResource( 3 )
region_rect = Rect2( 308, 375, 28, 16 )
_sections_unfolded = [ "Region", "Transform" ]

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

shape = SubResource( 1 )


16 changes: 13 additions & 3 deletions maps/Map01.tscn
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,12 @@ _sections_unfolded = [ "Cell", "Visibility" ]

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

position = Vector2( 1941.57, 1312.05 )
position = Vector2( 2313.83, 1366.39 )

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

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

editor/display_folded = true
curve = SubResource( 1 )

[node name="PathFollow2D" type="PathFollow2D" parent="Paths/Path2D" index="0"]
Expand All @@ -69,7 +68,9 @@ lookahead = 4.0

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

detect_radius = 400
max_speed = 175
turret_speed = 3.0
detect_radius = 500

[node name="Path2D2" type="Path2D" parent="Paths" index="1"]

Expand All @@ -89,6 +90,15 @@ lookahead = 4.0

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

max_speed = 150
rotation_speed = 0.0
turret_speed = 3.0
detect_radius = 450

[connection signal="shoot" from="Player" to="." method="_on_Tank_shoot"]

[connection signal="shoot" from="Paths/Path2D/PathFollow2D/EnemyTank" to="." method="_on_Tank_shoot"]

[connection signal="shoot" from="Paths/Path2D2/PathFollow2D/EnemyTank" to="." method="_on_Tank_shoot"]


10 changes: 9 additions & 1 deletion tanks/EnemyTank.gd
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,19 @@ onready var parent = get_parent()

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

var speed = 0
var target = null

func _ready():
var circle = CircleShape2D.new()
$DetectRadius/CollisionShape2D.shape = circle
$DetectRadius/CollisionShape2D.shape.radius = detect_radius

func control(delta):
if $LookAhead1.is_colliding() or $LookAhead2.is_colliding():
speed = lerp(speed, 0, 0.1)
else:
speed = lerp(speed, max_speed, 0.05)
if parent is PathFollow2D:
parent.set_offset(parent.get_offset() + speed * delta)
position = Vector2()
Expand All @@ -23,6 +29,8 @@ func _process(delta):
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()
if target_dir.dot(current_dir) > 0.9:
shoot()

func _on_DetectRadius_body_entered(body):
target = body
Expand Down
40 changes: 29 additions & 11 deletions tanks/EnemyTank.tscn
Original file line number Diff line number Diff line change
Expand Up @@ -2,33 +2,29 @@

[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]
[ext_resource path="res://bullets/EnemyBullet.tscn" type="PackedScene" id=3]
[ext_resource path="res://assets/onlyObjects_retina_rotated.png" type="Texture" id=4]

[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 )]
[node name="EnemyTank" index="0" instance=ExtResource( 1 )]

collision_layer = 4
collision_mask = 3
script = ExtResource( 2 )
_sections_unfolded = [ "Collision", "collision" ]
speed = 150
Bullet = ExtResource( 3 )
gun_cooldown = 0.5
health = 100
turret_speed = 1.0
detect_radius = 200

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

texture = ExtResource( 3 )
texture = ExtResource( 4 )
region_enabled = true
region_rect = Rect2( 268, 186, 80, 76 )
_sections_unfolded = [ "Region" ]
Expand All @@ -40,27 +36,49 @@ shape = SubResource( 1 )

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

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

[node name="Muzzle" parent="Turret" index="0"]

position = Vector2( 65, 0 )
_sections_unfolded = [ "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
collision_layer = 0
collision_mask = 2
audio_bus_override = false
audio_bus_name = "Master"
_sections_unfolded = [ "Collision" ]

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

shape = SubResource( 2 )
[node name="LookAhead1" type="RayCast2D" parent="." index="5"]

position = Vector2( 0, 30 )
enabled = true
exclude_parent = true
cast_to = Vector2( 100, 0 )
collision_mask = 7
_sections_unfolded = [ "Transform" ]

[node name="LookAhead2" type="RayCast2D" parent="." index="6"]

position = Vector2( 0, -30 )
enabled = true
exclude_parent = true
cast_to = Vector2( 100, 0 )
collision_mask = 7
_sections_unfolded = [ "Transform" ]

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

Expand Down
4 changes: 2 additions & 2 deletions tanks/Player.gd
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ func control(delta):
rotation += rotation_speed * rot_dir * delta
velocity = Vector2()
if Input.is_action_pressed('forward'):
velocity += Vector2(speed, 0).rotated(rotation)
velocity += Vector2(max_speed, 0).rotated(rotation)
if Input.is_action_pressed('back'):
velocity += Vector2(-speed, 0).rotated(rotation)
velocity += Vector2(-max_speed, 0).rotated(rotation)
velocity /= 2.0
if Input.is_action_just_pressed('click'):
shoot()
2 changes: 1 addition & 1 deletion tanks/Player.tscn
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ collision_mask = 5
script = ExtResource( 2 )
_sections_unfolded = [ "Collision", "collision" ]
Bullet = ExtResource( 3 )
speed = 200
max_speed = 200
rotation_speed = 1.0
gun_cooldown = 0.4
health = 100
Expand Down
2 changes: 1 addition & 1 deletion tanks/Tank.gd
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ signal health_changed
signal dead

export (PackedScene) var Bullet
export (int) var speed
export (int) var max_speed
export (float) var rotation_speed
export (float) var gun_cooldown
export (int) var health
Expand Down
2 changes: 1 addition & 1 deletion tanks/Tank.tscn
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ collision_mask = 1
collision/safe_margin = 0.08
script = ExtResource( 1 )
Bullet = null
speed = null
max_speed = null
rotation_speed = null
gun_cooldown = null
health = null
Expand Down

0 comments on commit 670128e

Please sign in to comment.