-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathTank.gd
60 lines (51 loc) · 1.5 KB
/
Tank.gd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
extends KinematicBody2D
signal health_changed
signal died
export (PackedScene) var Bullet
export (int) var speed
export (int) var rot_speed
export (int) var damage
export (int) var start_health
export (float) var bullet_lifetime
var velocity = Vector2()
var rot_dir
var can_shoot = true
var health
func _ready():
health = start_health
emit_signal("health_changed", health)
func _input(event):
if event.is_action_pressed("tank_fire") and can_shoot:
can_shoot = false
$AnimationPlayer.play("MuzzleFlash")
#yield($AnimationPlayer, "animation_finished")
$GunTimer.start()
var b = Bullet.instance()
b.start_at($"Turret/Muzzle".global_position, $Turret.global_rotation,
'blue', damage, bullet_lifetime)
$Bullets.add_child(b)
func get_input():
if Input.is_action_pressed("tank_forward"):
velocity = Vector2(speed, 0).rotated(rotation)
if Input.is_action_pressed("tank_back"):
velocity = Vector2(-speed/2, 0).rotated(rotation)
if Input.is_action_pressed("tank_right"):
rot_dir += 1
if Input.is_action_pressed("tank_left"):
rot_dir -= 1
func _physics_process(delta):
var mpos = get_global_mouse_position()
$Turret.global_rotation = mpos.angle_to_point(position)
velocity = Vector2()
rot_dir = 0
get_input()
rotation += rot_speed * rot_dir * delta
move_and_collide(velocity * delta)
func _on_GunTimer_timeout():
can_shoot = true
func take_damage(amount):
health -= amount
emit_signal("health_changed", (health * 100 / start_health))
if health <= 0:
emit_signal("died")
print("Dead!")