Skip to content

Commit

Permalink
Bullets the way you expect.
Browse files Browse the repository at this point in the history
Add timer to get rid of bullets.
Recognize when objects are hit.
Still issue with show and hide of Tanks.
  • Loading branch information
bblodget committed Jul 31, 2020
1 parent b6a9862 commit 8db95ec
Show file tree
Hide file tree
Showing 4 changed files with 103 additions and 16 deletions.
15 changes: 8 additions & 7 deletions Bullet.tscn
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
[gd_scene load_steps=4 format=2]

[ext_resource path="res://bullet.png" type="Texture" id=1]
[ext_resource path="res://Bullet.gd" type="Script" id=2]
[ext_resource path="res://Bullet_rigid.gd" type="Script" id=3]

[sub_resource type="RectangleShape2D" id=1]
extents = Vector2( 1.02081, 1.02438 )

[node name="Bullet" type="KinematicBody2D"]
script = ExtResource( 2 )
__meta__ = {
"_edit_group_": true
}
[node name="Bullet_Rigid" type="RigidBody2D"]
gravity_scale = 0.0
contacts_reported = 1
contact_monitor = true
script = ExtResource( 3 )

[node name="bullet" type="Sprite" parent="."]
position = Vector2( 0.0633631, -0.0659924 )
texture = ExtResource( 1 )

[node name="CollisionShape2D" type="CollisionShape2D" parent="."]
position = Vector2( -0.0633631, 0.0659924 )
shape = SubResource( 1 )
[connection signal="body_entered" from="." to="." method="_on_Bullet_Rigid_body_entered"]
44 changes: 44 additions & 0 deletions Bullet_area2D.gd
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
extends Area2D

# Max speeds
const BULLET_SPEED = 40

# Global Vars
var velocity = Vector2.ZERO
var heading = 0

# Called when the node enters the scene tree for the first time.
func _ready():
pass

func set_velocity(new_velocity):
velocity = new_velocity

func set_heading(new_heading):
heading = new_heading

func _physics_process(delta):

#velocity.x = delta * BULLET_SPEED * cos(heading * (PI/180))
#velocity.y = delta * BULLET_SPEED * sin(heading * (PI/180))
#move_and_collide(velocity)

#if position.y < 10:
# queue_free()

move_and_collide(velocity * delta)


# Declare member variables here. Examples:
# var a = 2
# var b = "text"


# Called when the node enters the scene tree for the first time.
func _ready():
pass # Replace with function body.


# Called every frame. 'delta' is the elapsed time since the previous frame.
#func _process(delta):
# pass
36 changes: 36 additions & 0 deletions Bullet_rigid.gd
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
extends RigidBody2D

# Global Vars
var projectile_speed = 400
var life_time = 0.9
var deadTimer


# Called when the node enters the scene tree for the first time.
func _ready():
deadTimer = Timer.new()
deadTimer.set_wait_time(1.0)
add_child(deadTimer)
apply_impulse(Vector2(), Vector2(projectile_speed, 0).rotated(rotation))
SelfDestruct()

func SelfDestruct():
yield(get_tree().create_timer(life_time),"timeout")
queue_free()


# Called every frame. 'delta' is the elapsed time since the previous frame.
#func _process(delta):
# pass


func _on_Bullet_Rigid_body_entered(body):
#print("body: ", body.name)
#self.hide()
if body.name == "Tank1" or body.name == "Tank2":
body.hide()
deadTimer.start()
yield(deadTimer, "timeout")
deadTimer.stop()
body.show()

24 changes: 15 additions & 9 deletions TankPlayer.gd
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ var heading
var adj_heading
var fire = 0
var last_fire = 0
var bullet_scene
var can_fire = true
var rate_of_fire = 0.4
var bullet_scene = preload("res://Bullet.tscn")

# Instance variables
export var tank_sprite : Texture
Expand All @@ -26,14 +28,9 @@ export var initial_heading : float = 0.0

func _ready():
heading = get_rotation_degrees()

$tank.texture = tank_sprite
bullet_scene = load("res://Bullet.tscn")

func _physics_process(delta):




var turn = TURN_SPEED * (Input.get_action_strength(ui_right) - Input.get_action_strength(ui_left))

Expand All @@ -44,10 +41,14 @@ func _physics_process(delta):
set_rotation_degrees(heading)

fire = Input.get_action_strength(ui_fire)
if (fire==1) and (last_fire==0):
if (fire==1) and (can_fire == true) and (last_fire==0):
can_fire = false
shoot()
#yield(get_tree().create_timer(rate_of_fire), "timeout")
can_fire = true
last_fire = fire



var fwd = FWD_SPEED * delta * (Input.get_action_strength(ui_down) - Input.get_action_strength(ui_up))

Expand All @@ -71,10 +72,15 @@ func shoot():
var spawn_point = gpos + (direction * distance_from_me)

var bullet = bullet_scene.instance()
bullet.set_global_position(spawn_point)
bullet.rotation = (adj_heading - 90)* (PI/180)
print("heading: ", heading)
print("adj_heading: ", adj_heading)
owner.add_child(bullet)

bullet.set_global_position(spawn_point)
bullet.set_velocity(direction * 100)


#bullet.set_velocity(direction * 100)
#bullet.transform = transform


Expand Down

0 comments on commit 8db95ec

Please sign in to comment.