ld-39-jam/Bullet.gd

55 lines
1.1 KiB
GDScript3
Raw Normal View History

2017-07-30 19:46:44 +01:00
extends KinematicBody2D
2017-08-11 22:10:43 +01:00
var BulletImpact = load("res://BulletImpact.tscn")
2017-07-30 21:25:42 +01:00
var velocity = Vector2(1, 0)
var lifetime = 16 # seconds
2017-08-11 22:50:56 +01:00
var bullet_dmg = 25
2017-08-11 22:10:43 +01:00
2017-07-30 21:25:42 +01:00
onready var sprite = get_node("Sprite")
2017-07-30 19:46:44 +01:00
func _ready():
set_fixed_process(true)
2017-08-11 22:50:56 +01:00
2017-07-30 21:49:58 +01:00
func fire(delta, vel, dir, speed):
2017-07-30 22:47:29 +01:00
velocity = (vel.length() * dir + (dir * speed * delta))
sprite.set_rot(dir.angle())
2017-08-01 22:28:11 +01:00
func type():
2017-08-09 21:54:24 +01:00
return "Bullet"
2017-08-11 22:10:43 +01:00
func do_damage(v):
lifetime = 0
2017-08-11 22:10:43 +01:00
func on_impact(e):
# create bullet impact
var new_bi = BulletImpact.instance()
e.add_child(new_bi)
# get_tree().get_root().add_child(new_bi)
2017-08-12 15:45:22 +01:00
#new_bi.set_global_pos(get_collision_pos() + velocity/2)
new_bi.set_global_pos(get_collision_pos())
2017-08-11 22:10:43 +01:00
# align along "hit normal, the lazy way"
new_bi.set_global_rot((-get_collision_normal()).angle())
# new_bi.rotate(deg2rad(180))
# damage and free
e.do_damage(bullet_dmg)
queue_free()
2017-07-30 22:47:29 +01:00
2017-07-30 19:46:44 +01:00
func _fixed_process(delta):
2017-08-01 22:28:11 +01:00
2017-07-30 21:49:58 +01:00
move(velocity)
2017-07-30 21:25:42 +01:00
lifetime -= delta
2017-08-01 22:28:11 +01:00
if is_colliding():
var e = get_collider()
2017-08-12 15:45:22 +01:00
var e_t = e.type()
if e_t == "Asteroid" or e_t == "Enemy" or e_t == "PowerStation" or e_t == "Player":
2017-08-11 22:10:43 +01:00
on_impact(e)
2017-08-01 22:28:11 +01:00
2017-07-30 21:25:42 +01:00
if lifetime <= 0:
2017-07-30 21:49:58 +01:00
queue_free()