boolet impacts

This commit is contained in:
Robin Hübner 2017-08-11 23:10:43 +02:00
parent efbcd5cf22
commit dcdc9171cd
9 changed files with 69 additions and 9 deletions

View File

@ -30,8 +30,11 @@ func wrap(v, v_min, v_max):
else: else:
return v return v
func explode(): func on_explode():
queue_free() queue_free()
func do_damage(v):
health -= v
func _fixed_process(delta): func _fixed_process(delta):
@ -40,13 +43,8 @@ func _fixed_process(delta):
# cur_pos.y = wrap(cur_pos.y, 1, get_viewport().get_rect().size.y) # cur_pos.y = wrap(cur_pos.y, 1, get_viewport().get_rect().size.y)
# set_pos(cur_pos) # set_pos(cur_pos)
if is_colliding():
var e = get_collider()
if e.type() == "Bullet":
health -= 25
if health <= 0: if health <= 0:
explode() on_explode()
self.move(velocity) self.move(velocity)
self.rotate(rot_vel) self.rotate(rot_vel)

View File

@ -1,8 +1,12 @@
extends KinematicBody2D extends KinematicBody2D
var BulletImpact = load("res://BulletImpact.tscn")
var velocity = Vector2(1, 0) var velocity = Vector2(1, 0)
var lifetime = 16 # seconds var lifetime = 16 # seconds
var bullet_dmg = 0.25
onready var sprite = get_node("Sprite") onready var sprite = get_node("Sprite")
func _ready(): func _ready():
@ -14,6 +18,23 @@ func fire(delta, vel, dir, speed):
func type(): func type():
return "Bullet" return "Bullet"
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)
new_bi.set_global_pos(get_collision_pos() + velocity/2)
# 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()
func _fixed_process(delta): func _fixed_process(delta):
@ -22,6 +43,8 @@ func _fixed_process(delta):
if is_colliding(): if is_colliding():
var e = get_collider() var e = get_collider()
if e.type() == "Asteroid":
on_impact(e)
if lifetime <= 0: if lifetime <= 0:
queue_free() queue_free()

View File

@ -11,7 +11,6 @@ extents = Vector2( 0.99762, 3.98857 )
[node name="Bullet" type="KinematicBody2D"] [node name="Bullet" type="KinematicBody2D"]
transform/rot = 180.0 transform/rot = 180.0
transform/scale = Vector2( 2, 2 )
input/pickable = false input/pickable = false
shapes/0/shape = SubResource( 1 ) shapes/0/shape = SubResource( 1 )
shapes/0/transform = Matrix32( 1, 0, 0, 1, 0, 0 ) shapes/0/transform = Matrix32( 1, 0, 0, 1, 0, 0 )

16
BulletImpact.gd Normal file
View File

@ -0,0 +1,16 @@
extends Sprite
var current_time = 0
var frames_per_second = 0.05
func _ready():
set_process(true)
func _process(delta):
if int(current_time / frames_per_second) >= get_hframes() - 1:
queue_free()
set_frame(int(current_time / frames_per_second))
current_time += delta

14
BulletImpact.tscn Normal file
View File

@ -0,0 +1,14 @@
[gd_scene load_steps=3 format=1]
[ext_resource path="res://raw/bullet_impact.png" type="Texture" id=1]
[ext_resource path="res://BulletImpact.gd" type="Script" id=2]
[node name="BulletImpact" type="Sprite"]
transform/scale = Vector2( 0.25, 0.25 )
texture = ExtResource( 1 )
hframes = 5
region_rect = Rect2( 0, 0, 32, 48 )
script/script = ExtResource( 2 )

View File

@ -1,4 +1,4 @@
[gd_scene load_steps=8 format=1] [gd_scene load_steps=9 format=1]
[ext_resource path="res://Game.gd" type="Script" id=1] [ext_resource path="res://Game.gd" type="Script" id=1]
[ext_resource path="res://Minimap.tscn" type="PackedScene" id=2] [ext_resource path="res://Minimap.tscn" type="PackedScene" id=2]
@ -7,6 +7,7 @@
[ext_resource path="res://Player.tscn" type="PackedScene" id=5] [ext_resource path="res://Player.tscn" type="PackedScene" id=5]
[ext_resource path="res://raw/space.png" type="Texture" id=6] [ext_resource path="res://raw/space.png" type="Texture" id=6]
[ext_resource path="res://EnemyGrunt.tscn" type="PackedScene" id=7] [ext_resource path="res://EnemyGrunt.tscn" type="PackedScene" id=7]
[ext_resource path="res://BulletImpact.tscn" type="PackedScene" id=8]
[node name="Game" type="Node"] [node name="Game" type="Node"]
@ -71,4 +72,8 @@ region_rect = Rect2( 0, 0, 8192, 8192 )
transform/pos = Vector2( 127.878, -20.3627 ) transform/pos = Vector2( 127.878, -20.3627 )
transform/scale = Vector2( 1.5, 1.5 ) transform/scale = Vector2( 1.5, 1.5 )
[node name="BulletImpact" parent="." instance=ExtResource( 8 )]
transform/pos = Vector2( 53.4955, -25.9372 )

BIN
raw/bullet_impact.ase (Stored with Git LFS) Normal file

Binary file not shown.

BIN
raw/bullet_impact.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.1 KiB

View File

@ -0,0 +1,2 @@
filter=false
gen_mipmaps=false