frame-of-mind/src/logic-scenes/board/card.gd

255 lines
7.9 KiB
GDScript

@tool
extends Area2D
class_name Card
var card_id
#FIXME remove this legacy stuff without loosing the evil notes ...
var compatible_sticky_notes: Array[StickyNote] = []
@export var evil_sticky_notes: Array[StickyNote] = []
var own_sticky_notes: Array[StickyNote] = []
var current_sticky_note: StickyNote = null
var wiggle_pos: float = 0
var wiggle_intensity: float = 0
var noise: Noise = FastNoiseLite.new()
var wiggle_tween
var scale_tween
var transfor_arr: Array[Transform2D] = [
Transform2D(0.9, Vector2(-125, -83)),
Transform2D(-0.3, Vector2(-126, -75)),
Transform2D(-0.3, Vector2(-126, -74)),
Transform2D(-0.3, Vector2(-126, -73)),
Transform2D(0.5, Vector2(-126, -77))
]
@export var text: String = "" :
set(value):
text = value
_on_text_updated()
var label:Label
var background_sprite: AnimatedSprite2D
@export var wiggle_strength: float = 0.2
@export var wiggle_speed: float = 5
@export_range(1, 2) var scale_bump: float = 1.05
@export_range(1.0, 10.0) var bounce_speed: float = 5
@export var highlighted: bool = false:
set(highlight):
if highlight != highlighted:
highlighted = highlight
if is_inside_tree() and is_node_ready():
if scale_tween: scale_tween.kill()
if wiggle_tween: wiggle_tween.kill()
if highlighted:
scale_tween = get_tree().create_tween()
scale_tween.tween_property(self, "scale", Vector2(scale_bump, scale_bump), 0.1)
wiggle_tween = get_tree().create_tween()
wiggle_tween.tween_property(self, "wiggle_intensity", 1, 0.2)
else:
scale_tween = get_tree().create_tween()
scale_tween.tween_property(self, "scale", Vector2(1, 1), 0.3)
wiggle_tween = get_tree().create_tween()
wiggle_tween.tween_property(self, "wiggle_intensity", 0, 0.5)
else:
if highlighted:
scale = Vector2(scale_bump, scale_bump)
wiggle_intensity = 1
else:
scale = Vector2(1,1)
wiggle_intensity = 0
@export var voice_line: AudioStream = null
@export var is_dragable: bool = false
@export var diameter = 336.0
var sticky_note_position: Vector2 = Vector2(-66, 83)
var collider: Shape2D
var is_dragged: bool = false:
set(dragged):
is_dragged = dragged
z_index = int(dragged)
var is_mouse_entered: bool = false
var mouse_offset: Vector2
func _init(card_name: String = "card", own_id:StringName = "-1") -> void:
if card_name != "void":
text = card_name
card_id = own_id
name = "c_%s" % card_name
func _ready():
input_event.connect(_on_input_event)
mouse_entered.connect(_on_mouse_entered)
mouse_exited.connect(_on_mouse_exited)
background_sprite = AnimatedSprite2D.new()
background_sprite.sprite_frames = preload("res://logic-scenes/board/card-textures/card-sprites.tres")
label = Label.new()
label.theme_type_variation = "card_text"
label.size = Vector2(273.0, 131.0)
label.position = Vector2(-135.0, -82.0)
label.autowrap_mode = TextServer.AUTOWRAP_WORD
var collision_shape: = CollisionShape2D.new()
collider = CapsuleShape2D.new()
collider.height = diameter
collider.radius = 110
collision_shape.shape = collider
add_child(collision_shape, false, Node.INTERNAL_MODE_FRONT)
add_child(background_sprite, false, Node.INTERNAL_MODE_FRONT)
add_child(label, false, Node.INTERNAL_MODE_FRONT)
collision_shape.rotation = PI/2
_handle_wiggle(0)
_on_text_updated()
func _on_text_updated():
if is_node_ready():
var curr_frame = text.hash() % background_sprite.sprite_frames.get_frame_count(background_sprite.animation)
background_sprite.frame = curr_frame
background_sprite.scale = Vector2(0.6, 0.6)
if text == "":
if background_sprite.get_child_count() == 0:
background_sprite.add_child(load("res://logic-scenes/board/void_stuff.tscn").instantiate(), false, Node.INTERNAL_MODE_DISABLED)
background_sprite.clip_children = CanvasItem.CLIP_CHILDREN_AND_DRAW
else:
if background_sprite.get_child_count() > 0:
background_sprite.get_child(0).queue_free()
label.text = text
wiggle_pos = float(text.hash() % 100)
label.rotation = deg_to_rad(transfor_arr[curr_frame].get_rotation())
label.position = transfor_arr[curr_frame].origin
if not Engine.is_editor_hint():
_handle_wiggle(0)
func _process(delta: float) -> void:
if highlighted:
_handle_wiggle(delta)
if get_overlapping_areas().size() > 0 and is_dragable:
for area in get_overlapping_areas():
if area is Card or area is CardCollider:
if area is CardCollider:
position += area.direction * delta
elif not (area.highlighted or self.highlighted) and area.is_dragable:
var diff:Vector2 = position - area.position
position -= diff.normalized() * ((diff.length()-diameter)/diameter) * bounce_speed * (delta/(1.0/60))
_move_card()
func _handle_wiggle(delta):
wiggle_pos += delta * wiggle_speed * wiggle_intensity
rotation = noise.get_noise_1d(wiggle_pos)*wiggle_strength
## Deprecated
func replace_with(card: Card):
self.text = card.text
self.compatible_sticky_notes = card.compatible_sticky_notes
self.own_sticky_notes = card.own_sticky_notes
self.voice_line = card.voice_line
self.name = card.name
func _input(event: InputEvent) -> void:
if event is InputEventMouseButton:
if event.button_index == MOUSE_BUTTON_LEFT and not event.pressed:
is_dragged = false
func _on_mouse_entered():
is_mouse_entered = true
if not Input.is_action_pressed("mouse_left"):
# Do nothing if mouse hovers over sticky_note
if has_sticky_note_attached():
if current_sticky_note.highlighted:
return
highlighted = true
if "handle_hover" in owner:
owner.handle_hover(self)
func _on_mouse_exited():
highlighted = false
is_mouse_entered = false
func _on_input_event(_viewport, event, _shape_idx):
if event is InputEventMouseMotion:
_move_card()
if event is InputEventMouseButton:
# Mouse Button Left is being handled by card_board to prevent events being missed when mouse moves outside card
if event.button_index == MOUSE_BUTTON_LEFT and event.pressed:
if "handle_mouse_button" in owner:
mouse_offset = (get_viewport().get_mouse_position() - position)
if highlighted:
owner.handle_mouse_button(event, self)
func _move_card():
if is_dragged:
position += (get_viewport().get_mouse_position() - position) - mouse_offset
func has_sticky_note_attached() -> bool:
return get_child_count(false) > 0
func get_attached_sticky_note() -> StickyNote:
return null if not has_sticky_note_attached() else get_child(0)
func preview_sticky_note(sticky_note: StickyNote):
sticky_note.reparent(self.get_parent())
sticky_note.attached_to = self
sticky_note.tween_transform_to(Transform2D(0, global_position + sticky_note_position + 0 * Vector2(sticky_note.diameter, sticky_note.diameter)))
func attach_sticky_note(sticky_note: StickyNote) -> bool:
if has_sticky_note_attached():
return false
sticky_note.reparent(self)
sticky_note.position = sticky_note_position
sticky_note.on_board = false
current_sticky_note = sticky_note
sticky_note.attached_to = self
if text == "hit" and sticky_note.text == "effort":
Steam.setAchievement("FIGHT_FOR_GOOD")
Steam.storeStats()
return true
func remove_sticky_note() -> StickyNote:
if not is_instance_valid(current_sticky_note): return null
var former_child:StickyNote = current_sticky_note
current_sticky_note = null
former_child.reparent(get_parent())
former_child.owner = self.owner
former_child.on_board = true
former_child.attached_to = owner
return former_child
func exchange_sticky_note_with(new_note: StickyNote) -> StickyNote:
var tmp = remove_sticky_note()
attach_sticky_note(new_note)
return tmp
# This makes sure this node highlights itself when focus has left the sticky note.
func check_hover():
if is_mouse_entered:
_on_mouse_entered()
func reclaim_sticky_note():
current_sticky_note.on_board = false
current_sticky_note.tween_transform_to(Transform2D(0, to_global(sticky_note_position)))
await current_sticky_note.transform_tween_finished
current_sticky_note.reparent(self)
current_sticky_note.owner = self.owner