From 9648170a9c6a003aae90134628c9ce68c61eb492 Mon Sep 17 00:00:00 2001 From: Hamcha Date: Mon, 6 May 2019 00:39:55 +0200 Subject: [PATCH] Cards can now be created programmatically --- Scenes/Board.tscn | 25 ++++-------------- Scenes/Components/Card.gd | 29 +++++++++++++-------- Scenes/Components/Card.tscn | 20 ++++++++++++++- Scenes/Scripts/Board.gd | 51 ++++++++++++++++++++++++++----------- Scenes/Scripts/BoardUI.gd | 4 +-- Scenes/Scripts/Camera.gd | 2 +- project.godot | 9 +++++-- 7 files changed, 89 insertions(+), 51 deletions(-) diff --git a/Scenes/Board.tscn b/Scenes/Board.tscn index 91e753a..f6412bb 100644 --- a/Scenes/Board.tscn +++ b/Scenes/Board.tscn @@ -1,14 +1,13 @@ -[gd_scene load_steps=8 format=2] +[gd_scene load_steps=7 format=2] [ext_resource path="res://Scenes/Scripts/Board.gd" type="Script" id=1] [ext_resource path="res://Scenes/Scripts/Camera.gd" type="Script" id=2] -[ext_resource path="res://Scenes/Components/Card.tscn" type="PackedScene" id=3] -[ext_resource path="res://MLPAssets/Background/boardbg.jpg" type="Texture" id=4] -[ext_resource path="res://Scenes/BoardUI.tscn" type="PackedScene" id=5] +[ext_resource path="res://MLPAssets/Background/boardbg.jpg" type="Texture" id=3] +[ext_resource path="res://Scenes/BoardUI.tscn" type="PackedScene" id=4] [sub_resource type="SpatialMaterial" id=1] flags_unshaded = true -albedo_texture = ExtResource( 4 ) +albedo_texture = ExtResource( 3 ) [sub_resource type="PlaneMesh" id=2] material = SubResource( 1 ) @@ -24,25 +23,11 @@ script = ExtResource( 2 ) [node name="Hand" type="Spatial" parent="Camera"] transform = Transform( 0.4, 0, 0, 0, 0, -0.4, 0, 0.4, 0, 0, -0.552465, -0.87444 ) -[node name="Card4" parent="Camera/Hand" instance=ExtResource( 3 )] -transform = Transform( 1, 0, 0, 0, 1, -2.98023e-008, 0, 2.98023e-008, 1, 0, 0, 0 ) -inHand = true - [node name="MeshInstance" type="MeshInstance" parent="."] transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, -0.001, 0 ) mesh = SubResource( 2 ) material/0 = null -[node name="BoardUI" parent="." instance=ExtResource( 5 )] +[node name="BoardUI" parent="." instance=ExtResource( 4 )] [node name="Cards" type="Spatial" parent="."] - -[node name="Card" parent="Cards" instance=ExtResource( 3 )] -transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, 1.55435, -0.001, 1.13711 ) -[connection signal="card_dropped" from="Camera/Hand/Card4" to="." method="_card_dropped"] -[connection signal="card_picked" from="Camera/Hand/Card4" to="." method="_card_picked"] -[connection signal="card_selected" from="Camera/Hand/Card4" to="BoardUI" method="_card_selected" binds= [ "ff6" ]] -[connection signal="card_unselected" from="Camera/Hand/Card4" to="BoardUI" method="_card_unselected"] -[connection signal="card_selected" from="Cards/Card" to="BoardUI" method="_card_selected" binds= [ "ff6" ]] -[connection signal="card_unselected" from="Cards/Card" to="BoardUI" method="_card_unselected"] -[connection signal="mouse_exited" from="Cards/Card" to="." method="_mouse_blur"] diff --git a/Scenes/Components/Card.gd b/Scenes/Components/Card.gd index 93428b0..7386c53 100644 --- a/Scenes/Components/Card.gd +++ b/Scenes/Components/Card.gd @@ -1,11 +1,15 @@ extends Spatial +class_name Card + signal card_selected() signal card_unselected() signal card_picked() signal card_dropped() var lifted := false +export var cardID := "" +export var playerID := 0 export var inHand := false onready var animation := $Border/AnimationPlayer @@ -21,15 +25,6 @@ func _mouse_blur(): emit_signal("card_unselected") func _input_event(camera, event, click_position, click_normal, shape_idx): - if event is InputEventMouseButton and event.button_index == BUTTON_LEFT: - if event.pressed: - lifted = true - animation.play("lift") - emit_signal("card_picked") - else: - lifted = false - animation.play_backwards("lift") - emit_signal("card_dropped") if lifted and event is InputEventMouseMotion: var origin: Vector3 = camera.project_ray_origin(event.position) var direction: Vector3 = camera.project_ray_normal(event.position) @@ -38,4 +33,18 @@ func _input_event(camera, event, click_position, click_normal, shape_idx): else: var denom := Vector3.UP.dot(direction) var t: float = (-camera.transform.origin).dot(Vector3.UP) / denom; - transform.origin = origin + direction * t \ No newline at end of file + transform.origin = origin + direction * t + if event is InputEventMouseButton and event.button_index == BUTTON_LEFT: + if event.pressed: + animation.play("lift") + if inHand: + animation.advance(1.0) + emit_signal("card_picked") + lifted = true + else: + if inHand: + animation.play("drop") + else: + animation.play_backwards("lift") + emit_signal("card_dropped") + lifted = false \ No newline at end of file diff --git a/Scenes/Components/Card.tscn b/Scenes/Components/Card.tscn index 279adc6..aa91000 100644 --- a/Scenes/Components/Card.tscn +++ b/Scenes/Components/Card.tscn @@ -1,4 +1,4 @@ -[gd_scene load_steps=13 format=2] +[gd_scene load_steps=14 format=2] [ext_resource path="res://Scenes/Components/Card.gd" type="Script" id=1] [ext_resource path="res://MLPAssets/Models/cardborder.obj" type="ArrayMesh" id=2] @@ -23,6 +23,23 @@ tracks/0/keys = { "times": PoolRealArray( 0, 0.1 ) } +[sub_resource type="Animation" id=9] +resource_name = "drop" +length = 0.1 +step = 0.05 +tracks/0/type = "value" +tracks/0/path = NodePath(".:translation") +tracks/0/interp = 2 +tracks/0/loop_wrap = true +tracks/0/imported = false +tracks/0/enabled = true +tracks/0/keys = { +"times": PoolRealArray( 0, 0.1 ), +"transitions": PoolRealArray( 0.297302, 0.965936 ), +"update": 0, +"values": [ Vector3( 0, 0.15, 0 ), Vector3( 0, 0, 0 ) ] +} + [sub_resource type="Animation" id=2] length = 0.3 tracks/0/type = "value" @@ -132,6 +149,7 @@ material/0 = ExtResource( 3 ) [node name="AnimationPlayer" type="AnimationPlayer" parent="Border"] anims/blur = SubResource( 8 ) +anims/drop = SubResource( 9 ) anims/flip = SubResource( 2 ) anims/focus = SubResource( 3 ) anims/lift = SubResource( 4 ) diff --git a/Scenes/Scripts/Board.gd b/Scenes/Scripts/Board.gd index 2cad0d6..225c5f0 100644 --- a/Scenes/Scripts/Board.gd +++ b/Scenes/Scripts/Board.gd @@ -1,17 +1,23 @@ extends Spatial +onready var CardTemplate := preload("res://Scenes/Components/Card.tscn") + onready var camera := $Camera onready var hand := $Camera/Hand +onready var ui := $BoardUI onready var cards := $Cards -onready var handCard := $Camera/Hand/Card4 export var mouseHandThreshold = 0.9 -var holdingCard: bool +var holdingCard: Card var mouseOrigin: Vector2 var lastCameraTransform: Transform +func _ready(): + add_card("ff36", 0, true) + add_card("ff36", 0, false) + func _input(event: InputEvent): # Camera zoom if event.is_action("zoom_in"): @@ -29,26 +35,41 @@ func _process(delta: float): # If panning, translate mouse delta to camera delta if Input.is_action_pressed("pan"): var mouseDelta := absMousePos - mouseOrigin - var mousePos: Vector2 = mouseDelta * 0.0096 * (1-camera.getZoomLevel()/5) # Magic numbers everywhere + var mousePos: Vector2 = mouseDelta * 0.0096 * (1-camera.get_zoom()/5) # Magic numbers everywhere camera.transform.origin = lastCameraTransform.origin - Vector3(mousePos.x, 0, mousePos.y) # If holding a card, move it between board/hand - if holdingCard: + if holdingCard != null: var relPos: float = absMousePos.y / get_viewport().size.y var selectingHand: bool = relPos > mouseHandThreshold - if selectingHand and not handCard.inHand: - call_deferred("reparent", handCard, cards, hand) - handCard.inHand = true - elif not selectingHand and handCard.inHand: - call_deferred("reparent", handCard, hand, cards) - handCard.inHand = false + if selectingHand and not holdingCard.inHand: + holdingCard.inHand = true + call_deferred("reparent", holdingCard, cards, hand) + elif not selectingHand and holdingCard.inHand: + holdingCard.inHand = false + call_deferred("reparent", holdingCard, hand, cards) -func _card_picked(): - holdingCard = true +func _card_picked(card: Card): + holdingCard = card -func _card_dropped(): - holdingCard = false +func _card_dropped(card: Card): + holdingCard = null func reparent(object: Node, from: Node, to: Node): from.remove_child(object) to.add_child(object) - object.set_owner(to) \ No newline at end of file + object.set_owner(to) + +func add_card(cardID: String, playerID: int, inHand: bool): + var card := CardTemplate.instance() + card.cardID = cardID + card.playerID = playerID + card.inHand = inHand + card.connect("card_dropped", self, "_card_dropped", [card]) + card.connect("card_picked", self, "_card_picked", [card]) + card.connect("card_selected", ui, "_card_selected", [card]) + card.connect("card_unselected", ui, "_card_unselected", [card]) + if inHand: + # TODO if player != me, put in opponent's hand + hand.add_child(card) + else: + cards.add_child(card) \ No newline at end of file diff --git a/Scenes/Scripts/BoardUI.gd b/Scenes/Scripts/BoardUI.gd index 2125cbf..2a691e6 100644 --- a/Scenes/Scripts/BoardUI.gd +++ b/Scenes/Scripts/BoardUI.gd @@ -2,8 +2,8 @@ extends Control onready var animation = $AnimationPlayer -func _card_selected(cardID): +func _card_selected(card: Card): animation.play("fade ui card") -func _card_unselected(): +func _card_unselected(card: Card): animation.play_backwards("fade ui card") diff --git a/Scenes/Scripts/Camera.gd b/Scenes/Scripts/Camera.gd index 3d2b193..aa34218 100644 --- a/Scenes/Scripts/Camera.gd +++ b/Scenes/Scripts/Camera.gd @@ -18,5 +18,5 @@ func _process(delta: float): transform = transform.translated(Vector3.FORWARD * deltaZoom) currentZoom += deltaZoom -func getZoomLevel() -> float: +func get_zoom() -> float: return currentZoom \ No newline at end of file diff --git a/project.godot b/project.godot index 6ebeacd..64977d9 100644 --- a/project.godot +++ b/project.godot @@ -8,9 +8,14 @@ config_version=4 -_global_script_classes=[ ] +_global_script_classes=[ { +"base": "Spatial", +"class": "Card", +"language": "GDScript", +"path": "res://Scenes/Components/Card.gd" +} ] _global_script_class_icons={ - +"Card": "" } [application]