diff --git a/godot/characters/player/Player.tscn b/godot/characters/player/Player.tscn index 6edccfe..858efb3 100644 --- a/godot/characters/player/Player.tscn +++ b/godot/characters/player/Player.tscn @@ -12,7 +12,7 @@ extents = Vector2( 7, 12 ) [node name="Player" type="KinematicBody2D"] -collision_mask = 6 +collision_mask = 2 script = ExtResource( 5 ) [node name="AnimatedSprite" type="AnimatedSprite" parent="."] diff --git a/godot/collectables/coin/Coin.tscn b/godot/collectables/coin/Coin.tscn new file mode 100644 index 0000000..6d52bb0 --- /dev/null +++ b/godot/collectables/coin/Coin.tscn @@ -0,0 +1,71 @@ +[gd_scene load_steps=10 format=2] + +[ext_resource path="res://assets/coin.png" type="Texture" id=1] +[ext_resource path="res://state_machine/StateMachine.gdns" type="Script" id=2] +[ext_resource path="res://collectables/coin/states/CoinNotCollected.gdns" type="Script" id=3] +[ext_resource path="res://collectables/coin/states/CoinCollected.gdns" type="Script" id=4] + +[sub_resource type="CircleShape2D" id=1] +radius = 6.0 + +[sub_resource type="AtlasTexture" id=2] +atlas = ExtResource( 1 ) +region = Rect2( 0, 0, 18, 18 ) + +[sub_resource type="AtlasTexture" id=3] +atlas = ExtResource( 1 ) +region = Rect2( 18, 0, 18, 18 ) + +[sub_resource type="SpriteFrames" id=4] +animations = [ { +"frames": [ SubResource( 2 ), SubResource( 3 ) ], +"loop": true, +"name": "spin", +"speed": 5.0 +} ] + +[sub_resource type="Animation" id=5] +resource_name = "jump" +length = 0.4 +tracks/0/type = "value" +tracks/0/path = NodePath("AnimatedSprite:position") +tracks/0/interp = 1 +tracks/0/loop_wrap = true +tracks/0/imported = false +tracks/0/enabled = true +tracks/0/keys = { +"times": PoolRealArray( 0, 0.2, 0.4 ), +"transitions": PoolRealArray( 1, 1, 1 ), +"update": 0, +"values": [ Vector2( 0, 0 ), Vector2( 0, -20 ), Vector2( 0, 0 ) ] +} + +[node name="Coin" type="Area2D"] +collision_layer = 4 + +[node name="CollisionShape2D" type="CollisionShape2D" parent="."] +position = Vector2( 9, 9 ) +shape = SubResource( 1 ) + +[node name="AnimatedSprite" type="AnimatedSprite" parent="."] +frames = SubResource( 4 ) +animation = "spin" +frame = 1 +playing = true +centered = false + +[node name="StateMachine" type="Node" parent="."] +script = ExtResource( 2 ) +default_state = "CoinNotCollected" + +[node name="CoinNotCollected" type="Node" parent="StateMachine"] +script = ExtResource( 3 ) + +[node name="CoinCollected" type="Node" parent="StateMachine"] +script = ExtResource( 4 ) + +[node name="AnimationPlayer" type="AnimationPlayer" parent="."] +anims/jump = SubResource( 5 ) + +[connection signal="body_entered" from="." to="StateMachine/CoinNotCollected" method="_on_body_entered" flags=6] +[connection signal="animation_finished" from="AnimationPlayer" to="StateMachine/CoinCollected" method="_on_animation_finished" flags=6] diff --git a/godot/collectables/coin/states/CoinCollected.gdns b/godot/collectables/coin/states/CoinCollected.gdns new file mode 100644 index 0000000..4465239 --- /dev/null +++ b/godot/collectables/coin/states/CoinCollected.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 = "CoinCollected" +class_name = "CoinCollected" +library = ExtResource( 1 ) diff --git a/godot/collectables/coin/states/CoinNotCollected.gdns b/godot/collectables/coin/states/CoinNotCollected.gdns new file mode 100644 index 0000000..acc55d8 --- /dev/null +++ b/godot/collectables/coin/states/CoinNotCollected.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 = "CoinNotCollected" +class_name = "CoinNotCollected" +library = ExtResource( 1 ) diff --git a/godot/levels/Prototype.tscn b/godot/levels/Prototype.tscn index d62a938..e95ba6f 100644 --- a/godot/levels/Prototype.tscn +++ b/godot/levels/Prototype.tscn @@ -4,7 +4,7 @@ [ext_resource path="res://characters/player/Player.tscn" type="PackedScene" id=2] [ext_resource path="res://levels/Prototype.tmx" type="PackedScene" id=3] [ext_resource path="res://assets/backgrounds/mountains.png" type="Texture" id=4] -[ext_resource path="res://Collectables/Coin/Coin.tscn" type="PackedScene" id=5] +[ext_resource path="res://collectables/coin/Coin.tscn" type="PackedScene" id=5] [node name="Prototype" type="Node2D"] diff --git a/src/Coin/CoinCollected.cpp b/src/Coin/CoinCollected.cpp deleted file mode 100644 index e69de29..0000000 diff --git a/src/Coin/CoinCollected.h b/src/Coin/CoinCollected.h deleted file mode 100644 index e69de29..0000000 diff --git a/src/coin/CoinCollected.cpp b/src/coin/CoinCollected.cpp new file mode 100644 index 0000000..65c4a51 --- /dev/null +++ b/src/coin/CoinCollected.cpp @@ -0,0 +1,49 @@ +#include "coin/CoinCollected.h" +#include + +using namespace godot; + +void CoinCollected::_register_methods() +{ + register_method("_state_enter", &CoinCollected::_state_enter); + register_method("_state_exit", &CoinCollected::_state_exit); + register_method("_on_animation_finished", &CoinCollected::_on_animation_finished); +} + +CoinCollected::CoinCollected() +{ +} + +CoinCollected::~CoinCollected() +{ +} + +void CoinCollected::_init() +{ + +} + +void CoinCollected::_state_enter() +{ + + auto node = get_parent()->find_node("AnimationPlayer"); + + if (node != nullptr) + { + auto animation_player = Object::cast_to(node); + animation_player->play("jump"); + } +} + +void CoinCollected::_state_exit() +{ + +} + +void CoinCollected::_on_animation_finished(String anim_name) +{ + this->get_parent()->queue_free(); + +} + + diff --git a/src/coin/CoinCollected.h b/src/coin/CoinCollected.h new file mode 100644 index 0000000..0e8f359 --- /dev/null +++ b/src/coin/CoinCollected.h @@ -0,0 +1,75 @@ +#ifndef ALAI_COIN_COLLECTED +#define ALAI_COIN_COLLECTED + +#include "state_machine/State.h" + +#include +#include +#include + +namespace godot +{ + /** + * @brief This class controls what happens when the Coin is in the collected state. + * + */ + class CoinCollected : public State + { + GODOT_CLASS(CoinCollected, 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 CoinCollected object. + * + */ + CoinCollected(); + + /** + * @brief Destroy the CoinCollected object. + * + */ + ~CoinCollected(); + + /** + * @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. + * + */ + void _on_animation_finished(String anim_name); + }; + +} + +#endif diff --git a/src/Coin/CoinNotCollected.cpp b/src/coin/CoinNotCollected.cpp similarity index 55% rename from src/Coin/CoinNotCollected.cpp rename to src/coin/CoinNotCollected.cpp index 41a4732..beb34e2 100644 --- a/src/Coin/CoinNotCollected.cpp +++ b/src/coin/CoinNotCollected.cpp @@ -1,4 +1,5 @@ -#include "Coin/CoinNotCollected.h" +#include "coin/CoinNotCollected.h" +#include using namespace godot; @@ -6,7 +7,7 @@ void CoinNotCollected::_register_methods() { register_method("_state_enter", &CoinNotCollected::_state_enter); register_method("_state_exit", &CoinNotCollected::_state_exit); - register_method("_physics_process", &CoinNotCollected::_physics_process); + register_method("_on_body_entered", &CoinNotCollected::_on_body_entered); } CoinNotCollected::CoinNotCollected() @@ -34,8 +35,20 @@ void CoinNotCollected::_state_exit() } -void CoinNotCollected::_physics_process(float delta) +void CoinNotCollected::_on_body_entered(Node *node) { + Godot::print("Coin touched"); + auto parent_node = get_parent(); + if (parent_node != nullptr) + { + auto coin = Object::cast_to(parent_node); + coin->set_collision_mask_bit(0, false); + } + get_state_machine()->change("CoinCollected"); + + } + + diff --git a/src/Coin/CoinNotCollected.h b/src/coin/CoinNotCollected.h similarity index 87% rename from src/Coin/CoinNotCollected.h rename to src/coin/CoinNotCollected.h index ab4488f..4739cf0 100644 --- a/src/Coin/CoinNotCollected.h +++ b/src/coin/CoinNotCollected.h @@ -1,5 +1,5 @@ -#ifndef JUEGO_COIN_COINNOTCOLLECTED -#define JUEGO_COIN_COINNOTCOLLECTED +#ifndef ALAI_COIN_NOT_COLLECTED +#define ALAI_COIN_NOT_COLLECTED #include "state_machine/State.h" @@ -64,11 +64,11 @@ namespace godot void _state_exit(); /** - * @brief The physics processed every delta time. + * @brief Method called on body entered. * - * @param[in] delta The time since the method was last run. + * @param[in] node Node interacting with whoever */ - void _physics_process(float delta); + void _on_body_entered(Node *node); }; } diff --git a/src/godot.cpp b/src/godot.cpp index 481a009..2e5d9a6 100644 --- a/src/godot.cpp +++ b/src/godot.cpp @@ -9,7 +9,8 @@ #include "player/states/PlayerMove.h" #include "player/states/PlayerJump.h" #include "player/states/PlayerFall.h" -#include "Coin/CoinNotCollected.h" +#include "coin/CoinNotCollected.h" +#include "coin/CoinCollected.h" using namespace godot; @@ -50,5 +51,6 @@ extern "C" void GDN_EXPORT godot_nativescript_init(void *handle) register_class(); register_class(); register_class(); - register_class(); + register_class(); + register_class(); }