prototype

This commit is contained in:
2022-04-05 14:51:59 -04:00
parent 4599cff7b3
commit a2ad48e900
64 changed files with 7353 additions and 44 deletions

99
src/Main.cpp Normal file
View File

@@ -0,0 +1,99 @@
#include "Main.h"
#include <SceneTree.hpp>
using namespace godot;
void Main::_register_methods()
{
register_method("_ready", &Main::_ready);
register_method("_physics_process", &Main::_physics_process);
register_property<Main, bool>("full_screen", &Main::set_full_screen, &Main::get_full_screen, JUEGO_MAIN_FULL_SCREEN);
register_property<Main, Vector2>("window_size", &Main::set_window_size, &Main::get_window_size, JUEGO_MAIN_WINDOW_SIZE);
register_property<Main, int8_t>("launch_screen", &Main::set_launch_screen, &Main::get_launch_screen, JUEGO_MAIN_LAUNCH_SCREEN);
}
Main::Main()
{
}
Main::~Main()
{
}
void Main::_init()
{
_os = OS::get_singleton();
_input = Input::get_singleton();
full_screen = JUEGO_MAIN_FULL_SCREEN;
window_size = JUEGO_MAIN_WINDOW_SIZE;
launch_screen = JUEGO_MAIN_LAUNCH_SCREEN;
}
void Main::_ready()
{
if (get_full_screen())
{
_os->set_window_fullscreen(true);
}
else
{
String resolution = String("Resolution before: " + String().num(_os->get_window_size().x) + "x" + String().num(_os->get_window_size().y));
Godot::print(resolution);
_os->set_window_size(window_size);
_os->set_window_position(
_os->get_screen_position(get_launch_screen()) + _os->get_screen_size() * 0.5 - _os->get_window_size() * 0.5
);
resolution = String("Resolution after: " + String().num(_os->get_window_size().x) + "x" + String().num(_os->get_window_size().y));
Godot::print(resolution);
}
}
void Main::_physics_process(float delta)
{
if (_input->is_action_just_pressed("ui_cancel"))
{
get_tree()->quit();
}
}
void Main::set_full_screen(bool new_full_screen)
{
full_screen = new_full_screen;
}
bool Main::get_full_screen()
{
return full_screen;
}
void Main::set_window_size(Vector2 new_window_size)
{
window_size = new_window_size;
}
Vector2 Main::get_window_size()
{
return window_size;
}
void Main::set_launch_screen(int8_t new_launch_screen)
{
launch_screen = new_launch_screen;
}
int8_t Main::get_launch_screen()
{
if (launch_screen == -1)
{
return _os->get_current_screen();
}
else
{
return launch_screen;
}
}

237
src/Player.cpp Normal file
View File

@@ -0,0 +1,237 @@
#include "Player.h"
#include <Camera2D.hpp>
#include <TileMap.hpp>
#include <VisibilityNotifier2D.hpp>
#include <SceneTree.hpp>
#include <Texture.hpp>
using namespace godot;
void Player::_register_methods()
{
register_method("_ready", &Player::_ready);
register_method("_physics_process", &Player::_physics_process);
register_method("_on_Player_player_moved", &Player::_on_Player_player_moved);
//register_property<Player, Ref<SpriteFrames>>("sprite_frames", &Player::set_sprite_frames, &Player::get_sprite_frames, Ref<SpriteFrames>(), GODOT_METHOD_RPC_MODE_DISABLED, GODOT_PROPERTY_USAGE_DEFAULT, GODOT_PROPERTY_HINT_RESOURCE_TYPE, String("SpriteFrames"));
register_property<Player, float>("speed", &Player::set_speed, &Player::get_speed, JUEGO_PLAYER_SPEED);
register_property<Player, float>("jump_force", &Player::set_jump_force, &Player::get_jump_force, JUEGO_PLAYER_JUMP_FORCE);
register_property<Player, float>("gravity", &Player::set_gravity, &Player::get_gravity, JUEGO_PLAYER_GRAVITY);
register_property<Player, float>("run_speed", &Player::set_run_speed, &Player::get_run_speed, JUEGO_PLAYER_RUN_SPEED);
register_signal<Player>("player_moved", "position", GODOT_VARIANT_TYPE_VECTOR2);
}
Player::Player()
{
}
Player::~Player()
{
}
void Player::_init()
{
_os = OS::get_singleton();
_input = Input::get_singleton();
_resource_loader = ResourceLoader::get_singleton();
//sprite_frames = _resource_loader->load(JUEGO_PLAYER_SPRITE_FRAMES);
set_speed(JUEGO_PLAYER_SPEED);
set_jump_force(JUEGO_PLAYER_JUMP_FORCE);
set_gravity(JUEGO_PLAYER_GRAVITY);
set_run_speed(JUEGO_PLAYER_RUN_SPEED);
coins = 0;
velocity = Vector2();
jumping = 0;
}
void Player::_ready()
{
animated_sprite = get_node<AnimatedSprite>("AnimatedSprite");
if (!animated_sprite)
{
WARN_PRINT("AnimateSprite not found!");
animated_sprite = AnimatedSprite()._new();
}
//animated_sprite->set_sprite_frames(sprite_frames);
auto node = get_parent()->find_node("Middleground");
if (node != nullptr)
{
auto tile_map = Object::cast_to<TileMap>(node);
get_parent()->call_deferred("remove_child", this);
tile_map->call_deferred("add_child", this);
}
else
{
WARN_PRINT("Middleground not found!");
}
}
void Player::_physics_process(float delta)
{
auto current_speed = get_speed();
velocity.x = 0;
if (_input->is_action_pressed("run"))
{
current_speed *= get_run_speed();
}
if (_input->is_action_pressed("right"))
{
velocity.x += current_speed;
}
if (_input->is_action_pressed("left"))
{
velocity.x += -current_speed;
}
if (velocity.x > 0)
{
animated_sprite->play("move");
animated_sprite->set_flip_h(true);
}
else if (velocity.x < 0)
{
animated_sprite->play("move");
animated_sprite->set_flip_h(false);
}
else
{
animated_sprite->stop();
animated_sprite->set_animation("idle");
}
if (jumping > 0 && is_on_floor())
{
animated_sprite->set_frame(1);
jumping = 0;
}
if (!is_on_floor())
{
animated_sprite->stop();
animated_sprite->set_animation("air");
if (jumping == 0)
{
jumping = 2;
}
}
velocity.y += get_gravity();
if (_input->is_action_just_pressed("jump") && jumping < 2)
{
velocity.y = -get_jump_force();
jumping++;
}
if (_input->is_action_just_released("jump"))
{
if (velocity.y < -100)
{
velocity.y = -100;
}
}
if (velocity.x < 0 || velocity.x > 0)
{
//emit_signal("player_moved", get_position());
}
velocity = move_and_slide(velocity, Vector2::UP, true);
velocity.x = Math::lerp((float) velocity.x, (float) 0, (float) 0.2);
// Clamp the player's position inside the camera's limits
auto camera = get_node<Camera2D>("Camera2D");
auto position = get_global_position();
auto sprite_node = get_node<AnimatedSprite>("AnimatedSprite");
if (sprite_node != nullptr)
{
position.x = Math::clamp((float) position.x, (float) camera->get_limit(0), (float) camera->get_limit(2) - sprite_node->get_sprite_frames()->get_frame("idle", 0)->get_size().x);
position.y = Math::clamp((float) position.y, (float) camera->get_limit(1), (float) camera->get_limit(3) + sprite_node->get_sprite_frames()->get_frame("idle", 0)->get_size().y);
}
else {
WARN_PRINT("Could not clamp player based on sprite frame size!");
position.x = Math::clamp((float) position.x, (float) camera->get_limit(0), (float) camera->get_limit(2));
position.y = Math::clamp((float) position.y, (float) camera->get_limit(1), (float) camera->get_limit(3));
}
set_global_position(position);
// If the player is off screen reload the scene
auto notifier = get_node<VisibilityNotifier2D>("Camera2D/VisibilityNotifier2D");
if (notifier != nullptr)
{
if (!notifier->is_on_screen())
{
if (get_parent()->get_class() == "TileMap")
{
Godot::print("Off screen");
auto error = get_tree()->change_scene("res://Main.tscn");
if (error != Error::OK)
{
ERR_PRINT(String().num((int) error) + " Could not load scene!");
}
}
}
}
}
void Player::set_sprite_frames(Ref<SpriteFrames> new_sprite_frames)
{
sprite_frames = new_sprite_frames;
}
Ref<SpriteFrames> Player::get_sprite_frames()
{
return sprite_frames;
}
void Player::set_speed(float new_speed)
{
speed = new_speed;
}
float Player::get_speed()
{
return speed;
}
void Player::set_jump_force(float new_jump_force)
{
jump_force = new_jump_force;
}
float Player::get_jump_force()
{
return jump_force;
}
void Player::set_gravity(float new_gravity)
{
gravity = new_gravity;
}
float Player::get_gravity()
{
return gravity;
}
void Player::set_run_speed(float new_run_speed)
{
run_speed = new_run_speed;
}
float Player::get_run_speed()
{
return run_speed;
}
void Player::_on_Player_player_moved(Vector2 position)
{
Godot::print(position);
}

26
src/godot.cpp Normal file
View File

@@ -0,0 +1,26 @@
#include <Godot.hpp>
#include "Main.h"
#include "Player.h"
using namespace godot;
extern "C" void GDN_EXPORT godot_gdnative_init(godot_gdnative_init_options *o)
{
Godot::gdnative_init(o);
}
extern "C" void GDN_EXPORT godot_gdnative_terminate(godot_gdnative_terminate_options *o)
{
// This next line is a workaround to fix bug:
// https://github.com/godotengine/godot/issues/48295
Godot::nativescript_terminate(_RegisterState::nativescript_handle);
Godot::gdnative_terminate(o);
}
extern "C" void GDN_EXPORT godot_nativescript_init(void *handle)
{
Godot::nativescript_init(handle);
register_class<Main>();
register_class<Player>();
}

143
src/include/Main.h Normal file
View File

@@ -0,0 +1,143 @@
#ifndef JUEGO_MAIN_H
#define JUEGO_MAIN_H
#include <Godot.hpp>
#include <Node.hpp>
#include <OS.hpp>
#include <Input.hpp>
/**
* @brief If the game should be full screen or not by default.
*/
#define JUEGO_MAIN_FULL_SCREEN false
/**
* @brief The default size of the window.
* @details This is ignored if full screen is true.
*/
#define JUEGO_MAIN_WINDOW_SIZE Vector2(1280, 720)
/**
* @brief Which screen to launch the game on.
* @details If -1 it will launch the game on the "active" screen. Anything between 0 and N represents the screen number to show the game on when opened.
*/
#define JUEGO_MAIN_LAUNCH_SCREEN -1
/**
* @brief This is the godot namespace for all the code included in the library.
* @details This namespace is used a prefix when the Godot engine looks for classes, methods, and properties.
*/
namespace godot
{
/**
* @brief This class controls the Main node.
* @details The main node is responsible for controling the window and the game iteself is a child of it.
*/
class Main : public Node
{
GODOT_CLASS(Main, Node)
private:
/**
* @brief OS singleton.
*/
OS *_os;
/**
* @brief Input singleton.
*/
Input *_input;
/**
* @brief If the window is full screen or not.
*/
bool full_screen;
/**
* @brief The size of the window.
*/
Vector2 window_size;
/**
* @brief The screen to launch the game on.
*/
int8_t launch_screen;
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 Main object.
*/
Main();
/**
* @brief Destroy the Main object.
*/
~Main();
/**
* @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 This class handles the physics processing.
* @details Every delta time, this function is called to check for input and update positioning.
*
* @param[in] delta The difference in time that passed since the last call to this method.
*/
void _physics_process(float delta);
/**
* @brief Set the full screen object.
*
* @param[in] new_full_screen The new full screen state.
*/
void set_full_screen(bool new_full_screen);
/**
* @brief Get the full screen object.
*
* @return true If full screen.
* @return false If not full screen.
*/
bool get_full_screen();
/**
* @brief Set the window size object.
*
* @param[in] new_window_size The new window size.
*/
void set_window_size(Vector2 new_window_size);
/**
* @brief Get the window size object.
*
* @return Vector2 The window size.
*/
Vector2 get_window_size();
/**
* @brief Set the launch screen object.
*
* @param[in] new_launch_screen The launch screen to use.
*/
void set_launch_screen(int8_t new_launch_screen);
/**
* @brief Get the launch screen object.
*
* @return int8_t The launch screen.
*/
int8_t get_launch_screen();
};
}
#endif

219
src/include/Player.h Normal file
View File

@@ -0,0 +1,219 @@
#ifndef JUEGO_PLAYER_H
#define JUEGO_PLAYER_H
/**
* @brief This resource is loaded by default for the AnimatedSprite node.
*/
#define JUEGO_PLAYER_SPRITE_FRAMES "res://characters/player/sprites/green.tres"
/**
* @brief The speed the player should move it.
*/
#define JUEGO_PLAYER_SPEED 100.0
/**
* @brief The force applied to the player when jumping.
*/
#define JUEGO_PLAYER_JUMP_FORCE 300.0
/**
* @brief The gravity applied to the player.
*/
#define JUEGO_PLAYER_GRAVITY 9.81
/**
* @brief The multiplier used to change the speed of the player when running.
*/
#define JUEGO_PLAYER_RUN_SPEED 2.0
#include <Godot.hpp>
#include <KinematicBody2D.hpp>
#include <OS.hpp>
#include <Sprite.hpp>
#include <Vector2.hpp>
#include <Input.hpp>
#include <AnimatedSprite.hpp>
#include <SpriteFrames.hpp>
#include <ResourceLoader.hpp>
#include <PackedScene.hpp>
/**
* @brief This is the godot namespace for all the code included in the library.
* @details This namespace is used a prefix when the Godot engine looks for classes, methods, and properties.
*/
namespace godot
{
/**
* @brief This class is used to control the player.
* @details This class allows the player to move, run, and jump as well as controls the sprite displayed for those actions.
*/
class Player : public KinematicBody2D
{
GODOT_CLASS(Player, KinematicBody2D)
private:
/**
* @brief OS singleton.
*/
OS *_os;
/**
* @brief Input singleton.
*/
Input *_input;
/**
* @brief ResourceLoader singleton.
*/
ResourceLoader *_resource_loader;
/**
* @brief The animated sprite connected to the KinematicBody2D.
*/
AnimatedSprite *animated_sprite;
/**
* @brief The sprite frames used in the animated sprite.
*/
Ref<SpriteFrames> sprite_frames;
/**
* @brief The coins the player has collected.
*/
uint8_t coins;
/**
* @brief The velocity at which moves the player moves.
*/
Vector2 velocity;
/**
* @brief The speed that the player moves in.
*/
float speed;
/**
* @brief The force applied to the player when jumping.
*/
float jump_force;
/**
* @brief The gravity applied to the player.
*/
float gravity;
/**
* @brief The speed multiplier used to make the player move faster.
*/
float run_speed;
/**
* @brief State of jumping of the player. To be replaced with a state machine in the future.
*/
uint8_t jumping;
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 Player object.
*/
Player();
/**
* @brief Destroy the Player object.
*/
~Player();
/**
* @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 This class handles the physics processing.
* @details Every delta time, this function is called to check for input and update positioning.
*
* @param[in] delta The difference in time that passed since the last call to this method.
*/
void _physics_process(float delta);
/**
* @brief Set the sprite frames object.
*
* @param[in] new_sprite_frames The new sprite frame.
*/
void set_sprite_frames(Ref<SpriteFrames> new_sprite_frames);
/**
* @brief Get the sprite frames object.
*
* @return Ref<SpriteFrames> A reference to the sprite frames object.
*/
Ref<SpriteFrames> get_sprite_frames();
/**
* @brief Set the speed object.
*
* @param[in] new_speed The new speed.
*/
void set_speed(float new_speed);
/**
* @brief Get the speed object.
*
* @return float The current speed of the player.
*/
float get_speed();
/**
* @brief Set the jump force object.
*
* @param[in] new_jump_force The new force applied to the player to make him jump.
*/
void set_jump_force(float new_jump_force);
/**
* @brief Get the jump force object.
*
* @return float The current force being applied to the player.
*/
float get_jump_force();
/**
* @brief Set the gravity object.
*
* @param[in] new_gravity The new gravity to apply to the player.
*/
void set_gravity(float new_gravity);
/**
* @brief Get the gravity object.
*
* @return float The current gravity applied to the player.
*/
float get_gravity();
/**
* @brief Set the run speed object.
*
* @param[in] new_run_speed The new speed for running.
*/
void set_run_speed(float new_run_speed);
/**
* @brief Get the run speed object.
*
* @return float The current run speed of the player.
*/
float get_run_speed();
/**
* @brief This signal is called when the player moves.
* @details The jump action is not included in this signal, just moving left or right.
*
* @param[in] position The new position of the player.
*/
void _on_Player_player_moved(Vector2 position);
};
}
#endif