Move input and random number generator to headers and _ready()

This commit is contained in:
Aaron Franke
2021-07-12 14:01:06 -04:00
parent b83f498855
commit 6ccec9e3ed
5 changed files with 15 additions and 14 deletions

View File

@@ -1,7 +1,5 @@
#include "Simple.hpp"
#include "Input.hpp"
void Simple::_register_methods() {
godot::register_method("get_data", &Simple::get_data);
}
@@ -22,14 +20,14 @@ void SimpleSprite::_register_methods() {
// `_init` is always required, if you exclude it then Godot will crash.
void SimpleSprite::_init() {
godot::Godot::print("A SimpleSprite was initialized in GDNative!");
_input = godot::Input::get_singleton();
}
void SimpleSprite::_process(const double p_delta) {
godot::Input *input = godot::Input::get_singleton();
godot::Vector2 input_dir(0, 0);
input_dir.x = input->get_action_strength("ui_right") - input->get_action_strength("ui_left");
input_dir.y = input->get_action_strength("ui_down") - input->get_action_strength("ui_up");
input_dir.x = _input->get_action_strength("ui_right") - _input->get_action_strength("ui_left");
input_dir.y = _input->get_action_strength("ui_down") - _input->get_action_strength("ui_up");
set_position(get_position() + input_dir.normalized() * (real_t)p_delta * 300);
}

View File

@@ -3,6 +3,7 @@
#include <Godot.hpp>
#include <Input.hpp>
#include <Reference.hpp>
#include <Sprite.hpp>
@@ -22,6 +23,8 @@ public:
class SimpleSprite : public godot::Sprite {
GODOT_CLASS(SimpleSprite, godot::Sprite)
godot::Input *_input;
public:
static void _register_methods();