Skip to content

Commit

Permalink
Adjust downstream code to default parameters (itest, dodge-the-creeps)
Browse files Browse the repository at this point in the history
  • Loading branch information
Bromeon committed Jun 29, 2023
1 parent b445d35 commit 01ce0bb
Show file tree
Hide file tree
Showing 11 changed files with 66 additions and 143 deletions.
2 changes: 1 addition & 1 deletion examples/dodge-the-creeps/rust/src/hud.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ impl Hud {
message_label.show();

let mut timer = self.base.get_node_as::<Timer>("MessageTimer");
timer.start(0.0);
timer.start();
}

pub fn show_game_over(&self) {
Expand Down
26 changes: 11 additions & 15 deletions examples/dodge-the-creeps/rust/src/main_scene.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
use crate::hud::Hud;
use crate::mob;
use crate::player;
use godot::engine::node::InternalMode;

use godot::engine::{Marker2D, PathFollow2D, RigidBody2D, Timer};
use godot::prelude::*;

use rand::Rng as _;
use std::f64::consts::PI;
use std::f32::consts::PI;

// Deriving GodotClass makes the class available to Godot
#[derive(GodotClass)]
Expand Down Expand Up @@ -33,7 +34,7 @@ impl Main {
hud.bind_mut().show_game_over();

self.music().stop();
self.death_sound().play(0.0);
self.death_sound().play();
}

#[func]
Expand All @@ -45,22 +46,22 @@ impl Main {
self.score = 0;

player.bind_mut().start(start_position.get_position());
start_timer.start(0.0);
start_timer.start();

let mut hud = self.base.get_node_as::<Hud>("Hud");
let hud = hud.bind_mut();
hud.update_score(self.score);
hud.show_message("Get Ready".into());

self.music().play(0.0);
self.music().play();
}

#[func]
fn on_start_timer_timeout(&self) {
let mut mob_timer = self.base.get_node_as::<Timer>("MobTimer");
let mut score_timer = self.base.get_node_as::<Timer>("ScoreTimer");
mob_timer.start(0.0);
score_timer.start(0.0);
mob_timer.start();
score_timer.start();
}

#[func]
Expand All @@ -82,19 +83,15 @@ impl Main {
let mut rng = rand::thread_rng();
let progress = rng.gen_range(u32::MIN..u32::MAX);

mob_spawn_location.set_progress(progress.into());
mob_spawn_location.set_progress(progress as f32);
mob_scene.set_position(mob_spawn_location.get_position());

let mut direction = mob_spawn_location.get_rotation() + PI / 2.0;
direction += rng.gen_range(-PI / 4.0..PI / 4.0);

mob_scene.set_rotation(direction);

self.base.add_child(
mob_scene.share().upcast(),
false,
InternalMode::INTERNAL_MODE_DISABLED,
);
self.base.add_child(mob_scene.share().upcast());

let mut mob = mob_scene.cast::<mob::Mob>();
{
Expand All @@ -103,15 +100,14 @@ impl Main {
let range = rng.gen_range(mob.min_speed..mob.max_speed);

mob.set_linear_velocity(Vector2::new(range, 0.0));
let lin_vel = mob.get_linear_velocity().rotated(real::from_f64(direction));
let lin_vel = mob.get_linear_velocity().rotated(real::from_f32(direction));
mob.set_linear_velocity(lin_vel);
}

let mut hud = self.base.get_node_as::<Hud>("Hud");
hud.bind_mut().connect(
"start_game".into(),
Callable::from_object_method(mob, "on_start_game"),
0,
);
}

Expand Down
2 changes: 1 addition & 1 deletion examples/dodge-the-creeps/rust/src/mob.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ impl RigidBody2DVirtual for Mob {
.base
.get_node_as::<AnimatedSprite2D>("AnimatedSprite2D");

sprite.play("".into(), 1.0, false);
sprite.play();
let anim_names = sprite.get_sprite_frames().unwrap().get_animation_names();

// TODO use pick_random() once implemented
Expand Down
10 changes: 5 additions & 5 deletions examples/dodge-the-creeps/rust/src/player.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,16 +74,16 @@ impl Area2DVirtual for Player {

// Note: exact=false by default, in Rust we have to provide it explicitly
let input = Input::singleton();
if input.is_action_pressed("move_right".into(), false) {
if input.is_action_pressed("move_right".into()) {
velocity += Vector2::RIGHT;
}
if input.is_action_pressed("move_left".into(), false) {
if input.is_action_pressed("move_left".into()) {
velocity += Vector2::LEFT;
}
if input.is_action_pressed("move_down".into(), false) {
if input.is_action_pressed("move_down".into()) {
velocity += Vector2::DOWN;
}
if input.is_action_pressed("move_up".into(), false) {
if input.is_action_pressed("move_up".into()) {
velocity += Vector2::UP;
}

Expand All @@ -103,7 +103,7 @@ impl Area2DVirtual for Player {
animated_sprite.set_flip_v(velocity.y > 0.0)
}

animated_sprite.play(animation.into(), 1.0, false);
animated_sprite.play_ex().name(animation.into()).done();
} else {
animated_sprite.stop();
}
Expand Down
2 changes: 1 addition & 1 deletion godot-codegen/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ keywords = ["gamedev", "godot", "engine", "codegen"]
categories = ["game-engines", "graphics"]

[features]
default = []
default = ["codegen-fmt"]
codegen-fmt = []
codegen-full = []
double-precision = []
Expand Down
14 changes: 4 additions & 10 deletions godot-core/src/engine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
use crate::builtin::{GodotString, NodePath};
use crate::obj::dom::EngineDomain;
use crate::obj::{Gd, GodotClass, Inherits};
use resource_loader::CacheMode;

pub use crate::gen::central::global;
pub use crate::gen::classes::*;
Expand All @@ -27,8 +26,6 @@ pub mod native {
pub use crate::gen::native::*;
}

use self::packed_scene::GenEditState;

/// Extension trait for convenience functions on `PackedScene`
pub trait PackedSceneExt {
/// ⚠️ Instantiates the scene as type `T`, panicking if not found or bad type.
Expand Down Expand Up @@ -56,8 +53,7 @@ impl PackedSceneExt for PackedScene {
where
T: Inherits<Node>,
{
self.instantiate(GenEditState::GEN_EDIT_STATE_DISABLED)
.and_then(|gd| gd.try_cast::<T>())
self.instantiate().and_then(|gd| gd.try_cast::<T>())
}
}

Expand Down Expand Up @@ -224,10 +220,8 @@ where
let type_hint = T::CLASS_NAME;

ResourceLoader::singleton()
.load(
path.clone(), /* TODO unclone */
type_hint.into(),
CacheMode::CACHE_MODE_REUSE,
)
.load_ex(path.clone())
.type_hint(type_hint.into())
.done() // TODO unclone
.and_then(|res| res.try_cast::<T>())
}
12 changes: 3 additions & 9 deletions itest/rust/src/array_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -367,14 +367,11 @@ fn untyped_array_pass_to_godot_func() {

#[itest]
fn untyped_array_return_from_godot_func() {
use godot::engine::node::InternalMode;
use godot::engine::Node;

// There aren't many API functions that return an untyped array.
let mut node = Node::new_alloc();
let mut child = Node::new_alloc();
child.set_name("child_node".into());
node.add_child(child.share(), false, InternalMode::INTERNAL_MODE_DISABLED);
node.add_child(child.share());
node.queue_free(); // Do not leak even if the test fails.
let result = node.get_node_and_resource("child_node".into());

Expand Down Expand Up @@ -408,15 +405,12 @@ fn typed_array_pass_to_godot_func() {

#[itest]
fn typed_array_return_from_godot_func() {
use godot::engine::node::InternalMode;
use godot::engine::Node;

let mut node = Node::new_alloc();
let mut child = Node::new_alloc();
child.set_name("child_node".into());
node.add_child(child.share(), false, InternalMode::INTERNAL_MODE_DISABLED);
node.add_child(child.share());
node.queue_free(); // Do not leak even if the test fails.
let children = node.get_children(false);
let children = node.get_children();

assert_eq!(children, array![child]);
}
Expand Down
22 changes: 5 additions & 17 deletions itest/rust/src/node_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

use crate::{itest, TestContext};
use godot::builtin::{NodePath, Variant};
use godot::engine::{global, node, Node, Node3D, NodeExt, PackedScene, SceneTree};
use godot::engine::{global, Node, Node3D, NodeExt, PackedScene, SceneTree};
use godot::obj::Share;

use std::str::FromStr;
Expand All @@ -19,19 +19,11 @@ fn node_get_node() {

let mut parent = Node3D::new_alloc();
parent.set_name("parent".into());
parent.add_child(
child.share().upcast(),
false,
node::InternalMode::INTERNAL_MODE_DISABLED,
);
parent.add_child(child.share().upcast());

let mut grandparent = Node::new_alloc();
grandparent.set_name("grandparent".into());
grandparent.add_child(
parent.share().upcast(),
false,
node::InternalMode::INTERNAL_MODE_DISABLED,
);
grandparent.add_child(parent.share().upcast());

// Directly on Gd<T>
let found = grandparent.get_node_as::<Node3D>(NodePath::from("parent/child"));
Expand Down Expand Up @@ -72,11 +64,7 @@ fn node_scene_tree() {

let mut parent = Node::new_alloc();
parent.set_name("parent".into());
parent.add_child(
child.share(),
false,
node::InternalMode::INTERNAL_MODE_DISABLED,
);
parent.add_child(child.share());

let mut scene = PackedScene::new();
let err = scene.pack(parent.share());
Expand All @@ -99,6 +87,6 @@ fn node_call_group(ctx: &TestContext) {
let mut node = ctx.scene_tree.share();
let mut tree = node.get_tree().unwrap();

node.add_to_group("group".into(), true);
node.add_to_group("group".into());
tree.call_group("group".into(), "set_name".into(), &[Variant::from("name")]);
}
7 changes: 3 additions & 4 deletions itest/rust/src/object_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ use godot::bind::{godot_api, GodotClass};
use godot::builtin::{
FromVariant, GodotString, StringName, ToVariant, Variant, VariantConversionError, Vector3,
};
use godot::engine::node::InternalMode;
use godot::engine::{
file_access, Area2D, Camera3D, FileAccess, Node, Node3D, Object, RefCounted, RefCountedVirtual,
};
Expand Down Expand Up @@ -658,9 +657,9 @@ fn object_get_scene_tree(ctx: &TestContext) {
let node = Node3D::new_alloc();

let mut tree = ctx.scene_tree.share();
tree.add_child(node.upcast(), false, InternalMode::INTERNAL_MODE_DISABLED);
tree.add_child(node.upcast());

let count = tree.get_child_count(false);
let count = tree.get_child_count();
assert_eq!(count, 1);
} // implicitly tested: node does not leak

Expand Down Expand Up @@ -838,7 +837,7 @@ fn double_use_reference() {
emitter
.share()
.upcast::<Object>()
.connect("do_use".into(), double_use.callable("use_1"), 0);
.connect("do_use".into(), double_use.callable("use_1"));

let guard = double_use.bind();

Expand Down
8 changes: 3 additions & 5 deletions itest/rust/src/signal_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,11 +81,9 @@ fn signals() {
let signal_name = format!("signal_{i}_arg");
let receiver_name = format!("receive_{i}_arg");

emitter.bind_mut().connect(
signal_name.clone().into(),
receiver.callable(receiver_name),
0,
);
emitter
.bind_mut()
.connect(signal_name.clone().into(), receiver.callable(receiver_name));

emitter.bind_mut().emit_signal(signal_name.into(), arg);

Expand Down
Loading

0 comments on commit 01ce0bb

Please sign in to comment.