From ee0d75fb37ed9e626fdab0aaeaa722e2a2e275a1 Mon Sep 17 00:00:00 2001 From: Paul Norberger Date: Mon, 20 Apr 2020 01:13:59 +0200 Subject: [PATCH] Added states to boss state machine --- src/Boss/SlimeBoss/SlimeBoss.gd | 3 -- src/Boss/SlimeBoss/SlimeBoss.tscn | 52 ++++++++++++++----- src/Boss/SlimeBoss/SlimeBossStateMachine.gd | 37 +++++++++++-- src/Boss/SlimeBoss/States/Motion/Dead.gd | 4 ++ src/Boss/SlimeBoss/States/Motion/Idle.gd | 16 ++---- .../SlimeBoss/States/Motion/JumpTeleport.gd | 4 ++ src/Boss/SlimeBoss/States/Motion/Pursue.gd | 4 ++ src/Boss/SlimeBoss/States/Motion/SplitUp.gd | 18 ++----- src/Boss/SlimeBoss/States/Motion/Stagger.gd | 18 ++----- src/Boss/SlimeBoss/States/Motion/Wander.gd | 18 ++----- 10 files changed, 97 insertions(+), 77 deletions(-) delete mode 100644 src/Boss/SlimeBoss/SlimeBoss.gd create mode 100644 src/Boss/SlimeBoss/States/Motion/Dead.gd create mode 100644 src/Boss/SlimeBoss/States/Motion/JumpTeleport.gd create mode 100644 src/Boss/SlimeBoss/States/Motion/Pursue.gd diff --git a/src/Boss/SlimeBoss/SlimeBoss.gd b/src/Boss/SlimeBoss/SlimeBoss.gd deleted file mode 100644 index 83bf13e..0000000 --- a/src/Boss/SlimeBoss/SlimeBoss.gd +++ /dev/null @@ -1,3 +0,0 @@ -extends KinematicBody2D -class_name SlimeBoss - diff --git a/src/Boss/SlimeBoss/SlimeBoss.tscn b/src/Boss/SlimeBoss/SlimeBoss.tscn index 9549180..88eea10 100644 --- a/src/Boss/SlimeBoss/SlimeBoss.tscn +++ b/src/Boss/SlimeBoss/SlimeBoss.tscn @@ -1,8 +1,9 @@ -[gd_scene load_steps=18 format=2] +[gd_scene load_steps=23 format=2] [ext_resource path="res://Overlap/HurtHit_Box/Hurtbox.tscn" type="PackedScene" id=1] [ext_resource path="res://Overlap/HurtHit_Box/Hitbox.tscn" type="PackedScene" id=2] [ext_resource path="res://Overlap/Stats/Stats.tscn" type="PackedScene" id=3] +[ext_resource path="res://Boss/SlimeBoss/SlimeBossStateMachine.gd" type="Script" id=4] [ext_resource path="res://Boss/SlimeBoss/Animations/move_down.png" type="Texture" id=5] [ext_resource path="res://Boss/SlimeBoss/Animations/move_right.png" type="Texture" id=6] [ext_resource path="res://Boss/SlimeBoss/States/Motion/Idle.gd" type="Script" id=7] @@ -10,6 +11,9 @@ [ext_resource path="res://Boss/SlimeBoss/States/Motion/Wander.gd" type="Script" id=9] [ext_resource path="res://Boss/SlimeBoss/States/Motion/SplitUp.gd" type="Script" id=10] [ext_resource path="res://Boss/SlimeBoss/Animations/move_up.png" type="Texture" id=11] +[ext_resource path="res://Boss/SlimeBoss/States/Motion/JumpTeleport.gd" type="Script" id=12] +[ext_resource path="res://Boss/SlimeBoss/States/Motion/Dead.gd" type="Script" id=13] +[ext_resource path="res://Boss/SlimeBoss/States/Motion/Pursue.gd" type="Script" id=14] [sub_resource type="CapsuleShape2D" id=1] radius = 18.0 @@ -19,12 +23,14 @@ height = 18.0 radius = 18.0 height = 18.0 -[sub_resource type="CapsuleShape2D" id=3] +[sub_resource type="CircleShape2D" id=3] +radius = 150.0 + +[sub_resource type="CapsuleShape2D" id=4] radius = 8.0 height = 15.0 -[sub_resource type="Animation" id=4] -resource_name = "MoveDown" +[sub_resource type="Animation" id=5] length = 2.66666 loop = true step = 0.0111111 @@ -102,7 +108,6 @@ tracks/5/keys = { } [sub_resource type="Animation" id=6] -resource_name = "MoveLeft" length = 1.28333 loop = true step = 0.0166667 @@ -179,8 +184,7 @@ tracks/5/keys = { "values": [ true ] } -[sub_resource type="Animation" id=5] -resource_name = "MoveRight" +[sub_resource type="Animation" id=7] length = 1.28333 loop = true step = 0.0166667 @@ -257,8 +261,7 @@ tracks/5/keys = { "values": [ false ] } -[sub_resource type="Animation" id=7] -resource_name = "MoveUp" +[sub_resource type="Animation" id=8] length = 1.28333 loop = true step = 0.0166667 @@ -324,9 +327,14 @@ tracks/4/keys = { } [node name="SlimeBoss" type="KinematicBody2D"] +collision_layer = 4 +collision_mask = 0 +script = ExtResource( 4 ) __meta__ = { "_edit_group_": true } +STATES_COLLECTION = NodePath("States") +START_STATE = NodePath("States/Idle") [node name="Sprite" type="Sprite" parent="."] position = Vector2( 0, -20 ) @@ -337,7 +345,7 @@ frame = 227 [node name="Hitbox" parent="." instance=ExtResource( 2 )] visible = false -collision_layer = 0 +collision_layer = 4 [node name="CollisionShape2D" parent="Hitbox" index="0"] position = Vector2( 0, -15 ) @@ -351,10 +359,16 @@ collision_mask = 0 position = Vector2( 0, -15 ) shape = SubResource( 2 ) +[node name="HeroCloseZone" type="Area2D" parent="."] +collision_layer = 0 + +[node name="CollisionShape2D" type="CollisionShape2D" parent="HeroCloseZone"] +shape = SubResource( 3 ) + [node name="CollisionShape2D" type="CollisionShape2D" parent="."] position = Vector2( -1.52588e-05, 0 ) rotation = 1.5708 -shape = SubResource( 3 ) +shape = SubResource( 4 ) [node name="DebugLabel" type="Label" parent="."] margin_left = -42.2456 @@ -382,12 +396,22 @@ script = ExtResource( 10 ) [node name="Stagger" type="Node" parent="States"] script = ExtResource( 8 ) +[node name="JumpTeleport" type="Node" parent="States"] +script = ExtResource( 12 ) + +[node name="Dead" type="Node" parent="States"] +script = ExtResource( 13 ) + +[node name="Pursue" type="Node" parent="States"] +script = ExtResource( 14 ) + [node name="AnimationPlayer" type="AnimationPlayer" parent="."] autoplay = "MoveRight" -anims/MoveDown = SubResource( 4 ) +anims/MoveDown = SubResource( 5 ) anims/MoveLeft = SubResource( 6 ) -anims/MoveRight = SubResource( 5 ) -anims/MoveUp = SubResource( 7 ) +anims/MoveRight = SubResource( 7 ) +anims/MoveUp = SubResource( 8 ) +[connection signal="area_entered" from="HeroCloseZone" to="States/Idle" method="hero_close"] [editable path="Hitbox"] diff --git a/src/Boss/SlimeBoss/SlimeBossStateMachine.gd b/src/Boss/SlimeBoss/SlimeBossStateMachine.gd index d480320..c783ada 100644 --- a/src/Boss/SlimeBoss/SlimeBossStateMachine.gd +++ b/src/Boss/SlimeBoss/SlimeBossStateMachine.gd @@ -1,9 +1,38 @@ extends "res://Overlap/StateMachine/StateMachine.gd" +onready var hero_close_zone = $HeroCloseZone +onready var debug_label = $DebugLabel + func _ready(): states_map = { - "idle": $Idle, - "wander": $Wander, - "split_up": $SplitUp, - "stagger": $Stagger + "idle": $States/Idle, + "wander": $States/Wander, + "pursue": $States/Pursue, + "split_up": $States/SplitUp, + "stagger": $States/Stagger, + "jump_teleport": $States/JumpTeleport, + "dead": $States/Dead } + + debug_label.text = "IDLE" + +func change_state(state_name): + current_state.exit() + debug_label.text = state_name.to_upper() + + if state_name == "previous": + states_stack.pop_front() + elif state_name in ["stagger"]: + states_stack.push_front(states_map[state_name]) + elif state_name == "dead": + queue_free() + return + else: + var new_state = states_map[state_name] + states_stack[0] = new_state + + current_state = states_stack[0] + if state_name != "previous": + # We don"t want to reinitialize the state if we"re going back to the previous state + current_state.enter() + emit_signal("state_changed", states_stack) diff --git a/src/Boss/SlimeBoss/States/Motion/Dead.gd b/src/Boss/SlimeBoss/States/Motion/Dead.gd new file mode 100644 index 0000000..576756c --- /dev/null +++ b/src/Boss/SlimeBoss/States/Motion/Dead.gd @@ -0,0 +1,4 @@ +extends "res://Overlap/StateMachine/State.gd" + +func enter(): + print("dead state") diff --git a/src/Boss/SlimeBoss/States/Motion/Idle.gd b/src/Boss/SlimeBoss/States/Motion/Idle.gd index 992b176..b5ee51b 100644 --- a/src/Boss/SlimeBoss/States/Motion/Idle.gd +++ b/src/Boss/SlimeBoss/States/Motion/Idle.gd @@ -1,13 +1,7 @@ -extends "res://Overlap/StateMachine/MotionState.gd" +extends "res://Overlap/StateMachine/State.gd" func enter(): - owner.get_node("AnimationPlayer").play("MoveDown") # TODO: Replace animation - -func handle_input(event): - return .handle_input(event) - -func update(delta): - var input_direction = get_input_direction() - if input_direction: - # emit_signal("finished", "move") - pass + owner.get_node("AnimationPlayer").play("MoveDown") + +func hero_close(area): + emit_signal("finished", "pursue") diff --git a/src/Boss/SlimeBoss/States/Motion/JumpTeleport.gd b/src/Boss/SlimeBoss/States/Motion/JumpTeleport.gd new file mode 100644 index 0000000..5ccf4ea --- /dev/null +++ b/src/Boss/SlimeBoss/States/Motion/JumpTeleport.gd @@ -0,0 +1,4 @@ +extends "res://Overlap/StateMachine/State.gd" + +func enter(): + print("jump teleport state") diff --git a/src/Boss/SlimeBoss/States/Motion/Pursue.gd b/src/Boss/SlimeBoss/States/Motion/Pursue.gd new file mode 100644 index 0000000..9d604c4 --- /dev/null +++ b/src/Boss/SlimeBoss/States/Motion/Pursue.gd @@ -0,0 +1,4 @@ +extends "res://Overlap/StateMachine/State.gd" + +func enter(): + print("pursue state") diff --git a/src/Boss/SlimeBoss/States/Motion/SplitUp.gd b/src/Boss/SlimeBoss/States/Motion/SplitUp.gd index 1eccaec..845fd18 100644 --- a/src/Boss/SlimeBoss/States/Motion/SplitUp.gd +++ b/src/Boss/SlimeBoss/States/Motion/SplitUp.gd @@ -1,16 +1,4 @@ -extends Node +extends "res://Overlap/StateMachine/State.gd" - -# Declare member variables here. Examples: -# var a = 2 -# var b = "text" - - -# Called when the node enters the scene tree for the first time. -func _ready(): - pass # Replace with function body. - - -# Called every frame. 'delta' is the elapsed time since the previous frame. -#func _process(delta): -# pass +func enter(): + print("split up state") diff --git a/src/Boss/SlimeBoss/States/Motion/Stagger.gd b/src/Boss/SlimeBoss/States/Motion/Stagger.gd index 1eccaec..05d8da0 100644 --- a/src/Boss/SlimeBoss/States/Motion/Stagger.gd +++ b/src/Boss/SlimeBoss/States/Motion/Stagger.gd @@ -1,16 +1,4 @@ -extends Node +extends "res://Overlap/StateMachine/State.gd" - -# Declare member variables here. Examples: -# var a = 2 -# var b = "text" - - -# Called when the node enters the scene tree for the first time. -func _ready(): - pass # Replace with function body. - - -# Called every frame. 'delta' is the elapsed time since the previous frame. -#func _process(delta): -# pass +func enter(): + print("stagger state") diff --git a/src/Boss/SlimeBoss/States/Motion/Wander.gd b/src/Boss/SlimeBoss/States/Motion/Wander.gd index 1eccaec..c0c535b 100644 --- a/src/Boss/SlimeBoss/States/Motion/Wander.gd +++ b/src/Boss/SlimeBoss/States/Motion/Wander.gd @@ -1,16 +1,4 @@ -extends Node +extends "res://Overlap/StateMachine/State.gd" - -# Declare member variables here. Examples: -# var a = 2 -# var b = "text" - - -# Called when the node enters the scene tree for the first time. -func _ready(): - pass # Replace with function body. - - -# Called every frame. 'delta' is the elapsed time since the previous frame. -#func _process(delta): -# pass +func enter(): + print("wander state")