From 92ccf7279dd6e6a5858a0030ec59a61ace823388 Mon Sep 17 00:00:00 2001 From: Mathilde Grapin Date: Mon, 12 Jun 2023 22:06:48 +0200 Subject: [PATCH] Instanciate a ball Make the enemy throw a ball. It just instanciate a ball in the scene but does not move it. --- scenes/ball.tscn | 15 ++++ scenes/enemy.tscn | 122 +++++++++++++++++----------- scenes/enemy_behavior_tree.tscn | 13 ++- scripts/can_throw_ball_condition.gd | 7 ++ scripts/enemy.gd | 7 ++ scripts/throw_ball_action.gd | 25 ++++++ scripts/wait_action.gd | 2 +- 7 files changed, 143 insertions(+), 48 deletions(-) create mode 100644 scenes/ball.tscn create mode 100644 scripts/can_throw_ball_condition.gd create mode 100644 scripts/throw_ball_action.gd diff --git a/scenes/ball.tscn b/scenes/ball.tscn new file mode 100644 index 0000000..4358fc0 --- /dev/null +++ b/scenes/ball.tscn @@ -0,0 +1,15 @@ +[gd_scene load_steps=3 format=3 uid="uid://cy0ko2cawudia"] + +[ext_resource type="Texture2D" uid="uid://dl8qfp3u18nkx" path="res://art/pilko.png" id="1_20u67"] + +[sub_resource type="CircleShape2D" id="CircleShape2D_srlvs"] +radius = 5.0 + +[node name="Ball" type="Area2D"] +z_index = 1 + +[node name="Sprite2D" type="Sprite2D" parent="."] +texture = ExtResource("1_20u67") + +[node name="CollisionShape2D" type="CollisionShape2D" parent="."] +shape = SubResource("CircleShape2D_srlvs") diff --git a/scenes/enemy.tscn b/scenes/enemy.tscn index f585a5d..84c3347 100644 --- a/scenes/enemy.tscn +++ b/scenes/enemy.tscn @@ -1,50 +1,13 @@ -[gd_scene load_steps=10 format=3 uid="uid://bktg1ypaav3q3"] +[gd_scene load_steps=12 format=3 uid="uid://bktg1ypaav3q3"] [ext_resource type="Texture2D" uid="uid://bbwsdd0jddmck" path="res://art/Pink_Monster_Walk_6.png" id="1_e5603"] [ext_resource type="Script" path="res://scripts/enemy.gd" id="1_l5lyv"] [ext_resource type="Texture2D" uid="uid://dylr4xf3far68" path="res://art/Pink_Monster_Idle_4.png" id="2_duxxr"] +[ext_resource type="Texture2D" uid="uid://bydnrc6ixlv8w" path="res://art/Pink_Monster_Throw_4.png" id="2_iscwr"] [ext_resource type="PackedScene" uid="uid://b51tdt5kunai" path="res://scenes/enemy_behavior_tree.tscn" id="3_jk76t"] -[sub_resource type="Animation" id="Animation_etqki"] -resource_name = "walk" -length = 0.6 -loop_mode = 1 -tracks/0/type = "value" -tracks/0/imported = false -tracks/0/enabled = true -tracks/0/path = NodePath("Sprite2D:texture") -tracks/0/interp = 1 -tracks/0/loop_wrap = true -tracks/0/keys = { -"times": PackedFloat32Array(0), -"transitions": PackedFloat32Array(1), -"update": 1, -"values": [ExtResource("1_e5603")] -} -tracks/1/type = "value" -tracks/1/imported = false -tracks/1/enabled = true -tracks/1/path = NodePath("Sprite2D:hframes") -tracks/1/interp = 1 -tracks/1/loop_wrap = true -tracks/1/keys = { -"times": PackedFloat32Array(0), -"transitions": PackedFloat32Array(1), -"update": 1, -"values": [6] -} -tracks/2/type = "value" -tracks/2/imported = false -tracks/2/enabled = true -tracks/2/path = NodePath("Sprite2D:frame") -tracks/2/interp = 1 -tracks/2/loop_wrap = true -tracks/2/keys = { -"times": PackedFloat32Array(0, 0.1, 0.2, 0.3, 0.4, 0.5), -"transitions": PackedFloat32Array(1, 1, 1, 1, 1, 1), -"update": 1, -"values": [0, 1, 2, 3, 4, 5] -} +[sub_resource type="Animation" id="Animation_ouyrp"] +length = 0.001 [sub_resource type="Animation" id="Animation_6vdwd"] resource_name = "idle" @@ -87,25 +50,92 @@ tracks/2/keys = { "values": [0, 1, 2, 3] } -[sub_resource type="Animation" id="Animation_ouyrp"] -length = 0.001 +[sub_resource type="Animation" id="Animation_etqki"] +resource_name = "walk" +length = 0.6 +loop_mode = 1 tracks/0/type = "value" tracks/0/imported = false tracks/0/enabled = true -tracks/0/path = NodePath("Sprite2D:frame") +tracks/0/path = NodePath("Sprite2D:texture") tracks/0/interp = 1 tracks/0/loop_wrap = true tracks/0/keys = { "times": PackedFloat32Array(0), "transitions": PackedFloat32Array(1), "update": 1, -"values": [0] +"values": [ExtResource("1_e5603")] +} +tracks/1/type = "value" +tracks/1/imported = false +tracks/1/enabled = true +tracks/1/path = NodePath("Sprite2D:hframes") +tracks/1/interp = 1 +tracks/1/loop_wrap = true +tracks/1/keys = { +"times": PackedFloat32Array(0), +"transitions": PackedFloat32Array(1), +"update": 1, +"values": [6] +} +tracks/2/type = "value" +tracks/2/imported = false +tracks/2/enabled = true +tracks/2/path = NodePath("Sprite2D:frame") +tracks/2/interp = 1 +tracks/2/loop_wrap = true +tracks/2/keys = { +"times": PackedFloat32Array(0, 0.1, 0.2, 0.3, 0.4, 0.5), +"transitions": PackedFloat32Array(1, 1, 1, 1, 1, 1), +"update": 1, +"values": [0, 1, 2, 3, 4, 5] +} + +[sub_resource type="Animation" id="Animation_jt5bg"] +resource_name = "throw" +length = 0.4 +tracks/0/type = "value" +tracks/0/imported = false +tracks/0/enabled = true +tracks/0/path = NodePath("Sprite2D:texture") +tracks/0/interp = 1 +tracks/0/loop_wrap = true +tracks/0/keys = { +"times": PackedFloat32Array(0), +"transitions": PackedFloat32Array(1), +"update": 1, +"values": [ExtResource("2_iscwr")] +} +tracks/1/type = "value" +tracks/1/imported = false +tracks/1/enabled = true +tracks/1/path = NodePath("Sprite2D:hframes") +tracks/1/interp = 1 +tracks/1/loop_wrap = true +tracks/1/keys = { +"times": PackedFloat32Array(0), +"transitions": PackedFloat32Array(1), +"update": 1, +"values": [4] +} +tracks/2/type = "value" +tracks/2/imported = false +tracks/2/enabled = true +tracks/2/path = NodePath("Sprite2D:frame") +tracks/2/interp = 1 +tracks/2/loop_wrap = true +tracks/2/keys = { +"times": PackedFloat32Array(0, 0.1, 0.2, 0.3), +"transitions": PackedFloat32Array(1, 1, 1, 1), +"update": 1, +"values": [0, 1, 2, 3] } [sub_resource type="AnimationLibrary" id="AnimationLibrary_gnukq"] _data = { "RESET": SubResource("Animation_ouyrp"), "idle": SubResource("Animation_6vdwd"), +"throw": SubResource("Animation_jt5bg"), "walk": SubResource("Animation_etqki") } @@ -117,7 +147,7 @@ script = ExtResource("1_l5lyv") [node name="Sprite2D" type="Sprite2D" parent="."] position = Vector2(0, -16) -texture = ExtResource("2_duxxr") +texture = ExtResource("2_iscwr") flip_h = true hframes = 4 diff --git a/scenes/enemy_behavior_tree.tscn b/scenes/enemy_behavior_tree.tscn index 9befc4e..3515f30 100644 --- a/scenes/enemy_behavior_tree.tscn +++ b/scenes/enemy_behavior_tree.tscn @@ -1,4 +1,4 @@ -[gd_scene load_steps=10 format=3 uid="uid://b51tdt5kunai"] +[gd_scene load_steps=12 format=3 uid="uid://b51tdt5kunai"] [ext_resource type="Script" path="res://addons/beehave/nodes/beehave_tree.gd" id="1_b2pc4"] [ext_resource type="Script" path="res://addons/beehave/nodes/composites/sequence.gd" id="2_80fm4"] @@ -8,7 +8,9 @@ [ext_resource type="Script" path="res://scripts/can_wait_condition.gd" id="4_x5ium"] [ext_resource type="Script" path="res://addons/beehave/nodes/decorators/time_limiter.gd" id="5_012bh"] [ext_resource type="Script" path="res://scripts/wait_action.gd" id="5_xel2n"] +[ext_resource type="Script" path="res://scripts/can_throw_ball_condition.gd" id="7_qj5mg"] [ext_resource type="Script" path="res://scripts/get_random_top_cell_action.gd" id="7_uo3i6"] +[ext_resource type="Script" path="res://scripts/throw_ball_action.gd" id="8_sunpr"] [node name="EnemyBehaviorTree" type="Node"] script = ExtResource("1_b2pc4") @@ -29,6 +31,15 @@ wait_time = 2.0 [node name="Wait" type="Node" parent="MainSelector/WaitSequence/TimeLimiterDecorator"] script = ExtResource("5_xel2n") +[node name="ThrowBallSequence" type="Node" parent="MainSelector"] +script = ExtResource("2_80fm4") + +[node name="CanThrowBall" type="Node" parent="MainSelector/ThrowBallSequence"] +script = ExtResource("7_qj5mg") + +[node name="ThrowBall" type="Node" parent="MainSelector/ThrowBallSequence"] +script = ExtResource("8_sunpr") + [node name="ReturnBallSequence" type="Node" parent="MainSelector"] script = ExtResource("2_80fm4") diff --git a/scripts/can_throw_ball_condition.gd b/scripts/can_throw_ball_condition.gd new file mode 100644 index 0000000..617d059 --- /dev/null +++ b/scripts/can_throw_ball_condition.gd @@ -0,0 +1,7 @@ +class_name CanThrowBallCondition +extends ConditionLeaf + +func tick(actor, _blackboard): + if !actor.has_thrown_ball: + return SUCCESS + return FAILURE diff --git a/scripts/enemy.gd b/scripts/enemy.gd index 0b15ff3..2e2867b 100644 --- a/scripts/enemy.gd +++ b/scripts/enemy.gd @@ -3,6 +3,8 @@ extends CharacterBody2D const Y_SPAWN_OFFSET = -8 @export var speed = 80 +var has_thrown_ball = false +var ball_scene = preload("res://scenes/ball.tscn") @onready var tile_map: TileMap = get_parent() @onready var animation_player = $AnimationPlayer @onready var sprite = $Sprite2D @@ -11,3 +13,8 @@ func _ready(): var spawn_cell: Vector2i = tile_map.get_top_spawn_cell() position = tile_map.map_to_local(spawn_cell) position.y += Y_SPAWN_OFFSET + +func throw_ball(): + var ball = ball_scene.instantiate() + ball.position = position + tile_map.add_child(ball) diff --git a/scripts/throw_ball_action.gd b/scripts/throw_ball_action.gd new file mode 100644 index 0000000..868fc15 --- /dev/null +++ b/scripts/throw_ball_action.gd @@ -0,0 +1,25 @@ +class_name ThrowBallAction +extends ActionLeaf + +var has_animation_finished = false + +func before_run(actor, _blackboard): + actor.animation_player.animation_finished.connect(_on_animation_finished) + actor.animation_player.speed_scale = 0.8 + +func tick(actor, _blackboard): + actor.animation_player.play("throw") + + if !has_animation_finished: + return RUNNING + else: + actor.throw_ball() + actor.has_thrown_ball = true + return SUCCESS + +func after_run(actor, _blackboard): + actor.animation_player.play("idle") + actor.animation_player.speed_scale = 1 + +func _on_animation_finished(_anim_name): + has_animation_finished = true diff --git a/scripts/wait_action.gd b/scripts/wait_action.gd index 3e90044..9d3ea7e 100644 --- a/scripts/wait_action.gd +++ b/scripts/wait_action.gd @@ -4,5 +4,5 @@ extends ActionLeaf func before_run(actor, _blackboard): actor.animation_player.play("idle") -func tick(actor, blackboard): +func tick(_actor, _blackboard): return RUNNING