From b24c9e8732db2f8642ade13882ef88254716fa75 Mon Sep 17 00:00:00 2001 From: Martin Araneda Date: Mon, 1 Aug 2022 14:10:00 -0400 Subject: [PATCH] fix goal state --- godot/goal/Goal.tscn | 51 ++++++++++++++++++++++ godot/{levels => goal}/GoalNotReached.gdns | 0 godot/goal/GoalReached.gdns | 8 ++++ godot/levels/Prototype.tscn | 50 ++------------------- godot/project.godot | 1 + src/goal/GoalNotReached.cpp | 11 +---- src/goal/GoalReached.cpp | 3 +- 7 files changed, 68 insertions(+), 56 deletions(-) create mode 100644 godot/goal/Goal.tscn rename godot/{levels => goal}/GoalNotReached.gdns (100%) create mode 100644 godot/goal/GoalReached.gdns diff --git a/godot/goal/Goal.tscn b/godot/goal/Goal.tscn new file mode 100644 index 0000000..3e6e22e --- /dev/null +++ b/godot/goal/Goal.tscn @@ -0,0 +1,51 @@ +[gd_scene load_steps=9 format=2] + +[ext_resource path="res://assets/flag.png" type="Texture" id=1] +[ext_resource path="res://goal/GoalNotReached.gdns" type="Script" id=2] +[ext_resource path="res://goal/GoalReached.gdns" type="Script" id=3] +[ext_resource path="res://state_machine/StateMachine.gdns" type="Script" id=4] + +[sub_resource type="RectangleShape2D" id=5] +extents = Vector2( 10, 18 ) + +[sub_resource type="AtlasTexture" id=6] +atlas = ExtResource( 1 ) +region = Rect2( 0, 0, 18, 36 ) + +[sub_resource type="AtlasTexture" id=7] +atlas = ExtResource( 1 ) +region = Rect2( 18, 0, 18, 36 ) + +[sub_resource type="SpriteFrames" id=8] +animations = [ { +"frames": [ SubResource( 6 ), SubResource( 7 ) ], +"loop": true, +"name": "flagmove", +"speed": 5.0 +} ] + +[node name="Goal" type="Area2D"] +position = Vector2( 324, 378 ) +collision_layer = 32 + +[node name="CollisionShape2D" type="CollisionShape2D" parent="."] +position = Vector2( 18, 18 ) +shape = SubResource( 5 ) + +[node name="AnimatedSprite" type="AnimatedSprite" parent="."] +position = Vector2( 18, 18 ) +frames = SubResource( 8 ) +animation = "flagmove" +frame = 1 +playing = true + +[node name="StateMachine" type="Node" parent="."] +script = ExtResource( 4 ) + +[node name="GoalReached" type="Node" parent="StateMachine"] +script = ExtResource( 3 ) + +[node name="GoalNotReached" type="Node" parent="StateMachine"] +script = ExtResource( 2 ) + +[connection signal="body_entered" from="." to="StateMachine/GoalNotReached" method="_on_Goal_body_entered"] diff --git a/godot/levels/GoalNotReached.gdns b/godot/goal/GoalNotReached.gdns similarity index 100% rename from godot/levels/GoalNotReached.gdns rename to godot/goal/GoalNotReached.gdns diff --git a/godot/goal/GoalReached.gdns b/godot/goal/GoalReached.gdns new file mode 100644 index 0000000..eaa25ba --- /dev/null +++ b/godot/goal/GoalReached.gdns @@ -0,0 +1,8 @@ +[gd_resource type="NativeScript" load_steps=2 format=2] + +[ext_resource path="res://gdnative/alai.tres" type="GDNativeLibrary" id=1] + +[resource] +resource_name = "GoalReached" +class_name = "GoalReached" +library = ExtResource( 1 ) diff --git a/godot/levels/Prototype.tscn b/godot/levels/Prototype.tscn index 699991e..036f781 100644 --- a/godot/levels/Prototype.tscn +++ b/godot/levels/Prototype.tscn @@ -1,4 +1,4 @@ -[gd_scene load_steps=18 format=2] +[gd_scene load_steps=13 format=2] [ext_resource path="res://CameraLimit.gdns" type="Script" id=1] [ext_resource path="res://characters/player/Player.tscn" type="PackedScene" id=2] @@ -7,8 +7,7 @@ [ext_resource path="res://collectables/coin/Coin.tscn" type="PackedScene" id=5] [ext_resource path="res://assets/coin.png" type="Texture" id=6] [ext_resource path="res://hud/coin/Counter.gdns" type="Script" id=7] -[ext_resource path="res://assets/flag.png" type="Texture" id=8] -[ext_resource path="res://levels/GoalNotReached.gdns" type="Script" id=9] +[ext_resource path="res://goal/Goal.tscn" type="PackedScene" id=8] [sub_resource type="StyleBoxFlat" id=1] bg_color = Color( 0, 0, 0, 0.541176 ) @@ -29,25 +28,6 @@ animations = [ { "speed": 5.0 } ] -[sub_resource type="RectangleShape2D" id=5] -extents = Vector2( 10, 18 ) - -[sub_resource type="AtlasTexture" id=6] -atlas = ExtResource( 8 ) -region = Rect2( 0, 0, 18, 36 ) - -[sub_resource type="AtlasTexture" id=7] -atlas = ExtResource( 8 ) -region = Rect2( 18, 0, 18, 36 ) - -[sub_resource type="SpriteFrames" id=8] -animations = [ { -"frames": [ SubResource( 6 ), SubResource( 7 ) ], -"loop": true, -"name": "flagmove", -"speed": 5.0 -} ] - [node name="Prototype" type="Node2D"] [node name="Player" parent="." instance=ExtResource( 2 )] @@ -112,6 +92,7 @@ script = ExtResource( 7 ) position = Vector2( 162, 18 ) frames = SubResource( 4 ) animation = "spin" +frame = 1 playing = true centered = false @@ -120,32 +101,9 @@ centered = false [node name="coin" parent="Coins" instance=ExtResource( 5 )] position = Vector2( 72, 450 ) -[node name="AnimatedSprite" parent="Coins/coin" index="1"] -frame = 0 - [node name="coin2" parent="Coins" instance=ExtResource( 5 )] position = Vector2( 234, 450 ) -[node name="Goal" type="Area2D" parent="."] -position = Vector2( 324, 378 ) - -[node name="CollisionShape2D" type="CollisionShape2D" parent="Goal"] -position = Vector2( 18, 18 ) -shape = SubResource( 5 ) - -[node name="AnimatedSprite" type="AnimatedSprite" parent="Goal"] -position = Vector2( 18, 18 ) -frames = SubResource( 8 ) -animation = "flagmove" -playing = true - -[node name="StateMachine" type="Node" parent="Goal"] - -[node name="GoalReached" type="Node" parent="Goal/StateMachine"] - -[node name="GoalNotReached" type="Node" parent="Goal/StateMachine"] -script = ExtResource( 9 ) - -[connection signal="body_entered" from="Goal" to="Goal/StateMachine/GoalNotReached" method="_on_Goal_body_entered"] +[node name="Goal" parent="." instance=ExtResource( 8 )] [editable path="Coins/coin"] diff --git a/godot/project.godot b/godot/project.godot index 4ff88a6..92bc2da 100644 --- a/godot/project.godot +++ b/godot/project.godot @@ -96,6 +96,7 @@ right={ 2d_physics/layer_1="Player" 2d_physics/layer_2="Tiles" 2d_physics/layer_3="Collectables" +2d_physics/layer_6="End Level" [physics] diff --git a/src/goal/GoalNotReached.cpp b/src/goal/GoalNotReached.cpp index 4978c11..c4763f3 100644 --- a/src/goal/GoalNotReached.cpp +++ b/src/goal/GoalNotReached.cpp @@ -38,16 +38,9 @@ void GoalNotReached::_state_exit() void GoalNotReached::_on_Goal_body_entered(Node *node) { - Godot::print("Flag touched"); - /* auto parent_node = get_parent(); - - if (parent_node != nullptr) - { - auto goal = Object::cast_to(parent_node); - goal->set_collision_mask_bit(0, false); - } - get_state_machine()->change("GoalReached");*/ + + get_state_machine()->change("GoalReached"); } diff --git a/src/goal/GoalReached.cpp b/src/goal/GoalReached.cpp index 580aea4..239f1e4 100644 --- a/src/goal/GoalReached.cpp +++ b/src/goal/GoalReached.cpp @@ -1,4 +1,5 @@ #include "goal/GoalReached.h" +#include using namespace godot; @@ -6,7 +7,6 @@ void GoalReached::_register_methods() { register_method("_state_enter", &GoalReached::_state_enter); register_method("_state_exit", &GoalReached::_state_exit); - //register_signal("coin_collected", "amount", GODOT_VARIANT_TYPE_INT); } GoalReached::GoalReached() @@ -24,6 +24,7 @@ void GoalReached::_init() void GoalReached::_state_enter() { + Godot::print("Flag touched"); }