PirateTreasureHunt/scenes/Coin.gd

65 lines
1.4 KiB
GDScript

extends KinematicBody2D
onready var coin_sprite = get_node("CoinSprite")
const COIN_INITIAL_LIFETIME = 10
var velocity = Vector2(0, 0)
var z_pos = 0
var z_vel = 0
var gravity = 1500
var bounciness = 0.5
var lifetime = 10
var pickup_timeout = 3
# Called when the node enters the scene tree for the first time.
func _ready():
lifetime = COIN_INITIAL_LIFETIME
set_collision_layer_bit(2, false)
pass # Replace with function body.
func _draw():
coin_sprite.offset.y = -z_pos
draw_circle(Vector2.ZERO, 5, Color(0.3, 0.3, 0.3, coin_sprite.modulate.a))
pass
func _process(delta):
print (COIN_INITIAL_LIFETIME - lifetime)
if COIN_INITIAL_LIFETIME - lifetime >= pickup_timeout:
set_collision_layer_bit(2, true)
z_vel = z_vel - gravity * delta
z_pos = z_pos + z_vel * delta
if z_pos < 0:
z_vel = - z_vel * bounciness
z_pos = 0
if z_vel < 0.01:
z_vel = 0
velocity = velocity * 0.5
position = position + velocity * delta
lifetime = lifetime - delta
if lifetime > 1:
if lifetime < 3:
if int(lifetime * 5) % 2 == 0:
coin_sprite.modulate = Color("AAffffff")
else:
coin_sprite.modulate = Color("ffffffff")
else:
coin_sprite.modulate = lerp (Color("0fff"), Color("ffff"), lifetime)
if lifetime < 0:
queue_free()
update()
# Called every frame. 'delta' is the elapsed time since the previous frame.
#func _process(delta):
# pass