From d14cc627de350965fb7aa48ba82d235d23987b46 Mon Sep 17 00:00:00 2001 From: Martin Araneda Date: Sun, 17 Jul 2022 21:51:11 -0400 Subject: [PATCH] Flag successfully placed on lvl, hitbox detected and working --- godot/levels/GoalNotReached.gdns | 8 ++++ godot/levels/Prototype.tscn | 49 +++++++++++++++++++- src/goal/GoalNotReached.cpp | 56 +++++++++++++++++++++++ src/goal/GoalNotReached.h | 76 ++++++++++++++++++++++++++++++++ src/goal/GoalReached.cpp | 35 +++++++++++++++ src/goal/GoalReached.h | 74 +++++++++++++++++++++++++++++++ src/godot.cpp | 4 ++ 7 files changed, 300 insertions(+), 2 deletions(-) create mode 100644 godot/levels/GoalNotReached.gdns create mode 100644 src/goal/GoalNotReached.cpp create mode 100644 src/goal/GoalNotReached.h create mode 100644 src/goal/GoalReached.cpp create mode 100644 src/goal/GoalReached.h diff --git a/godot/levels/GoalNotReached.gdns b/godot/levels/GoalNotReached.gdns new file mode 100644 index 0000000..1e3a1aa --- /dev/null +++ b/godot/levels/GoalNotReached.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 = "GoalNotReached" +class_name = "GoalNotReached" +library = ExtResource( 1 ) diff --git a/godot/levels/Prototype.tscn b/godot/levels/Prototype.tscn index e3c4608..699991e 100644 --- a/godot/levels/Prototype.tscn +++ b/godot/levels/Prototype.tscn @@ -1,4 +1,4 @@ -[gd_scene load_steps=12 format=2] +[gd_scene load_steps=18 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,6 +7,8 @@ [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] [sub_resource type="StyleBoxFlat" id=1] bg_color = Color( 0, 0, 0, 0.541176 ) @@ -27,6 +29,25 @@ 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 )] @@ -91,7 +112,6 @@ script = ExtResource( 7 ) position = Vector2( 162, 18 ) frames = SubResource( 4 ) animation = "spin" -frame = 1 playing = true centered = false @@ -100,7 +120,32 @@ 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"] + [editable path="Coins/coin"] diff --git a/src/goal/GoalNotReached.cpp b/src/goal/GoalNotReached.cpp new file mode 100644 index 0000000..4978c11 --- /dev/null +++ b/src/goal/GoalNotReached.cpp @@ -0,0 +1,56 @@ +#include "goal/GoalNotReached.h" +#include + +using namespace godot; + + +void GoalNotReached::_register_methods() +{ + register_method("_state_enter", &GoalNotReached::_state_enter); + register_method("_state_exit", &GoalNotReached::_state_exit); + register_method("_on_Goal_body_entered", &GoalNotReached::_on_Goal_body_entered); +} + +GoalNotReached::GoalNotReached() +{ +} + +GoalNotReached::~GoalNotReached() +{ +} + +void GoalNotReached::_init() +{ + +} + +void GoalNotReached::_state_enter() +{ + animated_sprite = get_parent()->get_node("AnimatedSprite"); + animated_sprite->set_animation("flagmove"); + animated_sprite->play(); +} + +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");*/ + + +} + + + diff --git a/src/goal/GoalNotReached.h b/src/goal/GoalNotReached.h new file mode 100644 index 0000000..82c9628 --- /dev/null +++ b/src/goal/GoalNotReached.h @@ -0,0 +1,76 @@ +#ifndef ALAI_GOAL_NOT_REACHED +#define ALAI_GOAL_NOT_REACHED + +#include "state_machine/State.h" + +#include +#include +#include + +namespace godot +{ + /** + * @brief This class controls what happens when the Coin is in the not collected state. + * + */ + class GoalNotReached : public State + { + GODOT_CLASS(GoalNotReached, State) + + private: + /** + * @brief The animated sprite of the Coin. + * + */ + AnimatedSprite *animated_sprite; + + public: + /** + * @brief This method registers classes with Godot. + * + * @details This method registers methods, properties, and signals with the Godot engine. + */ + static void _register_methods(); + + /** + * @brief Construct a new GoalNotReached object. + * + */ + GoalNotReached(); + + /** + * @brief Destroy the GoalNotReached object. + * + */ + ~GoalNotReached(); + + /** + * @brief Initialize the class from Godot. + * + * @details This method is called just once when the Godot engine connects to the instance of the class. + */ + void _init(); + + /** + * @brief Called when the not collected state of the coin is entered. + * + */ + void _state_enter(); + + /** + * @brief Called when the not collected state of the coin is exited. + * + */ + void _state_exit(); + + /** + * @brief Method called on body entered. + * + * @param[in] node Node interacting with whoever + */ + void _on_Goal_body_entered(Node *node); + }; + +} + +#endif diff --git a/src/goal/GoalReached.cpp b/src/goal/GoalReached.cpp new file mode 100644 index 0000000..580aea4 --- /dev/null +++ b/src/goal/GoalReached.cpp @@ -0,0 +1,35 @@ +#include "goal/GoalReached.h" + +using namespace godot; + +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() +{ +} + +GoalReached::~GoalReached() +{ +} + +void GoalReached::_init() +{ + +} + +void GoalReached::_state_enter() +{ + + +} + +void GoalReached::_state_exit() +{ + +} + diff --git a/src/goal/GoalReached.h b/src/goal/GoalReached.h new file mode 100644 index 0000000..2c854bb --- /dev/null +++ b/src/goal/GoalReached.h @@ -0,0 +1,74 @@ +#ifndef ALAI_GOAL_REACHED +#define ALAI_GOAL_REACHED + +#include "state_machine/State.h" + +#include +#include +#include + +namespace godot +{ + /** + * @brief This class controls what happens when the goal flag is in the reached state. + * + */ + class GoalReached : public State + { + GODOT_CLASS(GoalReached, State) + + private: + /** + * @brief The animated sprite of the Coin. + * + */ + AnimatedSprite *animated_sprite; + + public: + /** + * @brief This method registers classes with Godot. + * + * @details This method registers methods, properties, and signals with the Godot engine. + */ + static void _register_methods(); + + /** + * @brief Construct a new GoalReached object. + * + */ + GoalReached(); + + /** + * @brief Destroy the GoalReached object. + * + */ + ~GoalReached(); + + /** + * @brief Initialize the class from Godot. + * + * @details This method is called just once when the Godot engine connects to the instance of the class. + */ + void _init(); + + /** + * @brief Called when the collected state of the coin is entered. + * + */ + void _state_enter(); + + /** + * @brief Called when the collected state of the coin is exited. + * + */ + void _state_exit(); + + /** + * @brief Called when the animation of the collected coin has finished. + * + */ + }; + +} + +#endif diff --git a/src/godot.cpp b/src/godot.cpp index 620fb1d..350403b 100644 --- a/src/godot.cpp +++ b/src/godot.cpp @@ -12,6 +12,8 @@ #include "coin/CoinNotCollected.h" #include "coin/CoinCollected.h" #include "coin/CoinCounter.h" +#include "goal/GoalReached.h" +#include "goal/GoalNotReached.h" using namespace godot; @@ -55,4 +57,6 @@ extern "C" void GDN_EXPORT godot_nativescript_init(void *handle) register_class(); register_class(); register_class(); + register_class(); + register_class(); }