oh-my-git/scenes/card.gd

119 lines
2.9 KiB
GDScript3
Raw Normal View History

2020-10-13 14:04:12 +03:00
extends Node2D
var hovered = false
2020-10-13 14:04:12 +03:00
var dragged = false
var drag_offset
2020-10-14 01:27:37 +03:00
2020-10-13 18:08:37 +03:00
export var arg_number = 0
export var command = "" setget set_command
2020-10-14 01:27:37 +03:00
export var description = "" setget set_description
2020-10-15 16:22:38 +03:00
export var energy = 0 setget set_energy
2020-10-14 01:27:37 +03:00
2020-10-13 18:08:37 +03:00
var _first_argument = null
var _home_position = null
var _home_rotation = null
2020-10-13 18:08:37 +03:00
2020-10-15 16:22:38 +03:00
onready var energy_label = $Sprite/Energy
2020-10-13 14:04:12 +03:00
func _ready():
set_process_unhandled_input(true)
2020-10-15 16:22:38 +03:00
set_energy(energy)
2020-10-13 14:04:12 +03:00
func _process(delta):
2020-10-15 16:22:38 +03:00
if game.energy >= energy:
energy_label.modulate = Color(0.5, 1, 0.5)
else:
energy_label.modulate = Color(1, 1, 1)
modulate = Color(1, 0.5, 0.5)
2020-10-13 14:04:12 +03:00
if dragged:
var mousepos = get_viewport().get_mouse_position()
2020-10-14 15:48:38 +03:00
global_position = mousepos - drag_offset
var target_scale = 1
if hovered and not dragged:
target_scale = 1.5
scale = lerp(scale, Vector2(target_scale, target_scale), 10*delta)
2020-10-13 14:04:12 +03:00
func _unhandled_input(event):
if event is InputEventMouseButton:
if event.button_index == BUTTON_LEFT and event.pressed and hovered:
2020-10-13 14:04:12 +03:00
dragged = true
game.dragged_object = self
2020-10-22 17:19:22 +03:00
$PickupSound.play()
2020-10-13 14:04:12 +03:00
drag_offset = get_viewport().get_mouse_position() - global_position
get_tree().set_input_as_handled()
modulate.a = 0.5
2020-10-13 18:08:37 +03:00
elif event.button_index == BUTTON_LEFT and !event.pressed and dragged:
2020-10-13 14:04:12 +03:00
dragged = false
game.dragged_object = null
modulate.a = 1
2020-10-13 18:29:24 +03:00
2020-10-14 15:48:38 +03:00
if get_viewport().get_mouse_position().y < get_viewport().size.y/3*2:
2020-10-13 18:29:24 +03:00
if arg_number == 0 :
try_play($Label.text)
2020-10-13 18:29:24 +03:00
else:
move_back()
else:
2020-10-14 01:04:47 +03:00
move_back()
func _mouse_entered():
hovered = true
z_index = 1
func _mouse_exited():
hovered = false
z_index = 0
2020-10-13 18:08:37 +03:00
func set_command(new_command):
command = new_command
2020-10-14 01:27:37 +03:00
$Label.text = command
func set_description(new_description):
description = new_description
$Description.text = description
2020-10-13 18:08:37 +03:00
2020-10-15 16:22:38 +03:00
func set_energy(new_energy):
energy = new_energy
if energy_label:
energy_label.text = str(energy)
2020-10-13 18:08:37 +03:00
func move_back():
position = _home_position
rotation_degrees = _home_rotation
2020-10-22 17:19:22 +03:00
$ReturnSound.play()
2020-10-13 18:29:24 +03:00
func buuurn():
2020-10-15 17:11:02 +03:00
move_back()
func dropped_on(other):
2020-10-13 18:08:37 +03:00
var full_command = ""
match arg_number:
2020-10-13 19:13:42 +03:00
1:
var argument = other.id
if ($Label.text.begins_with("git checkout") or $Label.text.begins_with("git rebase")) and other.id.begins_with("refs/heads"):
argument = Array(other.id.split("/")).pop_back()
full_command = $Label.text + " " + argument
try_play(full_command)
2020-10-14 01:57:30 +03:00
# 2:
# if _first_argument:
# full_command = $Label.text + " " + _first_argument + " " + other.id
# $"../Terminal".send_command(full_command)
# buuurn()
# else:
# _first_argument = other.id
2020-10-26 21:56:52 +03:00
func try_play(full_command):
if game.energy >= energy:
2020-10-22 17:19:22 +03:00
$PlaySound.play()
var particles = preload("res://scenes/card_particles.tscn").instance()
2020-10-22 17:19:22 +03:00
particles.position = position
get_parent().add_child(particles)
2020-10-26 21:56:52 +03:00
$"../../..".terminal.send_command(full_command)
buuurn()
game.energy -= energy
else:
move_back()