Add ability for SimpleSprite to move as intended

This commit is contained in:
Aaron Franke
2021-07-06 21:43:49 -04:00
parent a6084fd911
commit 3aa846ac79
3 changed files with 20 additions and 14 deletions

View File

@@ -0,0 +1,9 @@
[gd_resource type="NativeScript" load_steps=2 format=2]
[ext_resource path="res://gdnative/simple.gdnlib" type="GDNativeLibrary" id=1]
[resource]
resource_name = "SimpleSprite"
class_name = "SimpleSprite"
library = ExtResource( 1 )

View File

@@ -1,7 +1,8 @@
[gd_scene load_steps=3 format=2]
[gd_scene load_steps=4 format=2]
[ext_resource path="res://main.gd" type="Script" id=1]
[ext_resource path="res://icon.png" type="Texture" id=2]
[ext_resource path="res://gdnative/simple_sprite.gdns" type="Script" id=3]
[node name="Node" type="Node"]
script = ExtResource( 1 )
@@ -19,8 +20,12 @@ margin_top = 360.0
margin_right = 662.0
margin_bottom = 374.0
align = 1
__meta__ = {
"_edit_use_anchors_": false
}
[node name="Sprite" type="Sprite" parent="."]
texture = ExtResource( 2 )
script = ExtResource( 3 )
[connection signal="pressed" from="Button" to="." method="_on_Button_pressed"]

View File

@@ -18,25 +18,17 @@ void SimpleSprite::_register_methods() {
godot::register_method("_process", &SimpleSprite::_process);
}
// `_init` doesn't need to be registered in `_register_methods`.
void SimpleSprite::_init() {
godot::Godot::print("Wheeeeey");
godot::Godot::print("A SimpleSprite was initialized in GDNative!");
}
void SimpleSprite::_process(double delta) {
godot::Input *input = godot::Input::get_singleton();
godot::Vector2 input_dir(0, 0);
if (godot::Input::get_singleton()->is_action_pressed("ui_right")) {
input_dir.x += 1;
}
if (godot::Input::get_singleton()->is_action_pressed("ui_left")) {
input_dir.x -= 1;
}
if (godot::Input::get_singleton()->is_action_pressed("ui_down")) {
input_dir.y += 1;
}
if (godot::Input::get_singleton()->is_action_pressed("ui_up")) {
input_dir.y -= 1;
}
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() * delta * 300);
}