integrate obelisk into alai

This commit is contained in:
2023-02-26 21:45:25 -03:00
parent 937e173fce
commit b84ed69198
24 changed files with 1619 additions and 36 deletions

View File

@@ -12,6 +12,7 @@ void alai::Event::_register_methods()
godot::register_signal<Event>("player_died");
godot::register_signal<Event>("player_won");
godot::register_signal<Event>("player_touched", "damage", GODOT_VARIANT_TYPE_INT);
godot::register_signal<Event>("report_object", "name", GODOT_VARIANT_TYPE_STRING, "state", GODOT_VARIANT_TYPE_STRING, "position", GODOT_VARIANT_TYPE_VECTOR2, "velocity", GODOT_VARIANT_TYPE_VECTOR2);
}
alai::Event::Event()

View File

@@ -1,5 +1,6 @@
#include "Event.h"
#include "Main.h"
#include "obelisk.h"
#include <SceneTree.hpp>
@@ -39,6 +40,10 @@ void alai::Main::_init()
void alai::Main::_ready()
{
auto obelisk = std::unique_ptr<obelisk::Obelisk> {new obelisk::Obelisk("alai.kb")};
godot::Godot::print("Obelisk version: " + godot::String(obelisk->getVersion().c_str()));
godot::Godot::print("Obelisk library version: " + godot::String(std::to_string(obelisk->getLibVersion()).c_str()));
auto success = _project_settings->load_resource_pack("monitor.pck");
if (success)
{

View File

@@ -8,6 +8,7 @@
#include "goal/GoalReached.h"
#include "gui/game_over/GameOverScreen.h"
#include "gui/game_won/GameWonScreen.h"
#include "player/AI.h"
#include "player/Player.h"
#include "player/states/PlayerFall.h"
#include "player/states/PlayerIdle.h"
@@ -20,7 +21,7 @@
/**
* @brief This function connects the gdnative init function.
*
*
*/
extern "C" void GDN_EXPORT godot_gdnative_init(godot_gdnative_init_options *o)
{
@@ -29,7 +30,7 @@ extern "C" void GDN_EXPORT godot_gdnative_init(godot_gdnative_init_options *o)
/**
* @brief This function connects the gdnative terminate function.
*
*
*/
extern "C" void GDN_EXPORT godot_gdnative_terminate(godot_gdnative_terminate_options *o)
{
@@ -41,7 +42,7 @@ extern "C" void GDN_EXPORT godot_gdnative_terminate(godot_gdnative_terminate_opt
/**
* @brief This function connects the init methods in the classes to godot's gdnative.
*
*
*/
extern "C" void GDN_EXPORT godot_nativescript_init(void *handle)
{
@@ -53,6 +54,7 @@ extern "C" void GDN_EXPORT godot_nativescript_init(void *handle)
godot::register_class<alai::Main>();
godot::register_class<alai::CameraLimit>();
godot::register_class<alai::player::Player>();
godot::register_class<alai::player::AI>();
godot::register_class<alai::player::PlayerIdle>();
godot::register_class<alai::player::PlayerMove>();
godot::register_class<alai::player::PlayerJump>();

104
src/player/AI.cpp Normal file
View File

@@ -0,0 +1,104 @@
#include "player/AI.h"
void alai::player::AI::_register_methods()
{
godot::register_method("_ready", &AI::_ready);
godot::register_method("_physics_process", &AI::_physics_process);
godot::register_method("_on_object_report", &AI::_on_object_report);
}
alai::player::AI::AI()
{
}
alai::player::AI::~AI()
{
}
void alai::player::AI::_init()
{
_input = godot::Input::get_singleton();
obelisk = std::unique_ptr<obelisk::Obelisk> {new obelisk::Obelisk("alai.kb")};
}
void alai::player::AI::_ready()
{
parent = Object::cast_to<Player>(get_parent());
event = get_node<alai::Event>("/root/Event");
event->connect("report_object", this, "_on_object_report");
}
void alai::player::AI::_physics_process(float delta)
{
while (!entities.empty())
{
const auto& entity = entities.front();
if (obelisk->query(entity.name.utf8().get_data(), "is", "touchable") > 0)
{
auto player_position = parent->get_global_position();
if (entity.position.x > player_position.x)
{
_input->action_press("right");
}
else
{
_input->action_press("left");
}
}
if (obelisk->query(entity.name.utf8().get_data(), "is", "bouncable") > 0)
{
auto player_position = parent->get_global_position();
auto distance = sqrt(pow(player_position.x - entity.position.x, 2) + pow(player_position.y - entity.position.y, 2));
int jump_distance = 0;
auto player_direction = parent->get_velocity();
auto action = obelisk->queryAction(entity.name.utf8().get_data(), "is", "bouncable");
if (action == "jump on")
{
jump_distance = 100;
}
else if (action == "jump over")
{
jump_distance = 50;
}
if (distance < jump_distance)
{
if (_input->is_action_pressed("right") && player_position.x < entity.position.x)
{
_input->action_press("jump");
}
else if (_input->is_action_pressed("left") && player_position.x > entity.position.x)
{
_input->action_press("jump");
}
}
}
if (obelisk->query(entity.name.utf8().get_data(), "is", "gold") > 0 && obelisk->query(std::string("gold ") + entity.name.utf8().get_data(), "is", "collectable") > 0)
{
auto player_position = parent->get_global_position();
float distance = player_position.x - entity.position.x;
if (distance > -10 && distance < 10 && player_position.y - entity.position.y > 20)
{
_input->action_press("jump");
}
}
entities.pop();
}
}
void alai::player::AI::_on_object_report(godot::String name, godot::String state, godot::Vector2 position, godot::Vector2 velocity)
{
auto entity = Entity();
entity.name = name.to_lower().rstrip("0123456789");
entity.state = state;
entity.position = position;
entity.velocity = velocity;
entities.push(entity);
}

88
src/player/AI.h Normal file
View File

@@ -0,0 +1,88 @@
#ifndef ALAI_PLAYER_AI_H
#define ALAI_PLAYER_AI_H
#include "Event.h"
#include "obelisk.h"
#include "player/Player.h"
#include <Godot.hpp>
#include <Input.hpp>
#include <Node.hpp>
#include <Vector2.hpp>
#include <queue>
namespace alai
{
namespace player
{
class Entity
{
public:
godot::String name;
godot::String state;
godot::Vector2 position;
godot::Vector2 velocity;
};
class AI : public godot::Node
{
GODOT_CLASS(AI, godot::Node)
private:
/**
* @brief Input singleton.
*
*/
godot::Input* _input;
/**
* @brief The obelisk object.
*
*/
std::unique_ptr<obelisk::Obelisk> obelisk;
alai::Event* event;
std::queue<alai::player::Entity> entities;
alai::player::Player* parent;
public:
/**
* @brief This method registers classes with Godot.
*
* @details This method registers methods, properties, and signals with the Godot engine.
*/
static void
_register_methods();
AI();
~AI();
/**
* @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 Code to be run when ready.
*
* @details This method is run when all the children of this node are ready.
*/
void _ready();
/**
* @brief The physics processed every delta time.
*
* @param[in] delta The time since the method was last run.
*/
void _physics_process(float delta);
void _on_object_report(godot::String name, godot::String state, godot::Vector2 position, godot::Vector2 velocity);
};
} // namespace player
} // namespace alai
#endif