integrate obelisk into alai
This commit is contained in:
104
src/player/AI.cpp
Normal file
104
src/player/AI.cpp
Normal 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
88
src/player/AI.h
Normal 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
|
||||
Reference in New Issue
Block a user