Skip to content

Default parameters in Engine API + meta param/return types #322

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 11 commits into from
Jul 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
8 changes: 5 additions & 3 deletions godot-codegen/src/api_parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -217,21 +217,23 @@ pub struct MethodArg {
pub name: String,
#[nserde(rename = "type")]
pub type_: String,
// pub meta: Option<String>,
pub meta: Option<String>,
pub default_value: Option<String>,
}

// Example: get_available_point_id -> {type: "int", meta: "int64"}
#[derive(DeJson, Clone)]
pub struct MethodReturn {
#[nserde(rename = "type")]
pub type_: String,
// pub meta: Option<String>,
pub meta: Option<String>,
}

impl MethodReturn {
pub fn from_type(type_: &str) -> Self {
pub fn from_type_no_meta(type_: &str) -> Self {
Self {
type_: type_.to_owned(),
meta: None,
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion godot-codegen/src/central_generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -522,7 +522,7 @@ fn make_enumerator(
) -> (Ident, TokenStream, Literal) {
let enumerator_name = &type_names.json_builtin_name;
let pascal_name = to_pascal_case(enumerator_name);
let rust_ty = to_rust_type(enumerator_name, ctx);
let rust_ty = to_rust_type(enumerator_name, None, ctx);
let ord = Literal::i32_unsuffixed(value);

(ident(&pascal_name), rust_ty.to_token_stream(), ord)
Expand Down
Loading