PirateTreasureHunt/scenes/Coin.gd

65 lines
1.4 KiB
GDScript
Raw Normal View History

2022-08-02 16:26:23 +02:00
extends KinematicBody2D
2022-01-23 11:47:18 +01:00
onready var coin_sprite = get_node("CoinSprite")
2022-08-02 16:26:23 +02:00
const COIN_INITIAL_LIFETIME = 10
2022-01-23 11:47:18 +01:00
var velocity = Vector2(0, 0)
var z_pos = 0
var z_vel = 0
var gravity = 1500
var bounciness = 0.5
2022-01-24 22:12:50 +01:00
var lifetime = 10
2022-08-02 16:26:23 +02:00
var pickup_timeout = 3
2022-01-23 11:47:18 +01:00
# Called when the node enters the scene tree for the first time.
func _ready():
2022-08-02 16:26:23 +02:00
lifetime = COIN_INITIAL_LIFETIME
set_collision_layer_bit(2, false)
2022-01-23 11:47:18 +01:00
pass # Replace with function body.
func _draw():
coin_sprite.offset.y = -z_pos
2022-01-24 22:12:50 +01:00
draw_circle(Vector2.ZERO, 5, Color(0.3, 0.3, 0.3, coin_sprite.modulate.a))
2022-01-23 11:47:18 +01:00
pass
func _process(delta):
2022-08-02 16:26:23 +02:00
print (COIN_INITIAL_LIFETIME - lifetime)
if COIN_INITIAL_LIFETIME - lifetime >= pickup_timeout:
set_collision_layer_bit(2, true)
2022-01-23 11:47:18 +01:00
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
2022-01-24 22:12:50 +01:00
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()
2022-01-23 11:47:18 +01:00
update()
# Called every frame. 'delta' is the elapsed time since the previous frame.
#func _process(delta):
# pass