Skip to content

Commit

Permalink
changed Option<Gd<T>> -> OnReady<Gd<T>> where reasonable
Browse files Browse the repository at this point in the history
  • Loading branch information
Lamby777 committed Jan 23, 2024
1 parent cbd146f commit 10d148b
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 33 deletions.
4 changes: 1 addition & 3 deletions pets-lib/src/battle/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,9 +98,7 @@ impl INode2D for BattleEngine {
fn ready(&mut self) {
// The node that contains the text labels below
let cont = self.base().get_node_as("%Choices");

use crate::wrapped::from_children_of;
self.choices = from_children_of(cont);
self.choices = crate::wrapped::from_children_of(cont);
}

fn process(&mut self, _delta: f64) {
Expand Down
2 changes: 0 additions & 2 deletions pets-lib/src/main_menu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,6 @@ impl INode2D for TitleScreen {
}

fn ready(&mut self) {
// use MainMenuChoice::*;

// The node that contains the text labels below
let cont = self.base().get_node_as("Background/MenuChoices");
self.choices = crate::wrapped::from_children_of(cont);
Expand Down
29 changes: 16 additions & 13 deletions pets-lib/src/world/interaction/manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ use crate::world::interaction::zone::InteractionZone;
use crate::world::playercb::PlayerCB;

#[derive(GodotClass)]
#[class(init, base=Node2D)]
#[class(base=Node2D)]
pub struct InteractionManager {
#[base]
node: Base<Node2D>,
prompt_txt: Option<Gd<RichTextLabel>>,
prompt_txt: OnReady<Gd<RichTextLabel>>,

/// All interaction zones the player is inside
zones: Vec<Gd<InteractionZone>>,
Expand All @@ -33,10 +33,6 @@ impl InteractionManager {
self.zones.retain(|v| *v != obj);
}

pub fn prompt_txt(&mut self) -> &mut Gd<RichTextLabel> {
self.prompt_txt.as_mut().unwrap()
}

/// "ummm ackshually, this is not a singleton"
pub fn singleton() -> Gd<InteractionManager> {
// using this cool godot feature I just found...
Expand Down Expand Up @@ -73,25 +69,32 @@ impl InteractionManager {

#[godot_api]
impl INode2D for InteractionManager {
fn init(node: Base<Node2D>) -> Self {
Self {
node,
prompt_txt: OnReady::manual(),
zones: vec![],
}
}

fn ready(&mut self) {
self.prompt_txt = Some(self.base().get_node_as("Prompt"));
let prompt_txt = self.base().get_node_as("Prompt");
self.prompt_txt.init(prompt_txt);
}

fn process(&mut self, _delta: f64) {
self.sort_zones_by_distance();

let Some(zone) = self.closest_zone() else {
// if no zones, hide the prompt
self.prompt_txt().hide();

self.prompt_txt.hide();
return;
};

let txt = self.prompt_txt();
txt.show();

// move the prompt to the zone
txt.set_position(zone.get_position() + Vector2::new(0.0, -50.0));
self.prompt_txt.show();
self.prompt_txt
.set_position(zone.get_position() + Vector2::new(0.0, -50.0));
}

fn unhandled_input(&mut self, event: Gd<InputEvent>) {
Expand Down
40 changes: 25 additions & 15 deletions pets-lib/src/world/pchar_node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@ use godot::engine::Sprite2D;
use godot::prelude::*;

#[derive(GodotClass)]
#[class(init, base=Node2D)]
#[class(base=Node2D)]
pub struct PCharNode {
#[base]
node: Base<Node2D>,
base: Base<Node2D>,

sprite: Option<Gd<Sprite2D>>,
anim_player: Option<Gd<AnimationPlayer>>,
anim_tree: Option<Gd<AnimationTree>>,
anim_state: Option<Gd<AnimationNodeStateMachinePlayback>>,
sprite: OnReady<Gd<Sprite2D>>,
anim_player: OnReady<Gd<AnimationPlayer>>,
anim_tree: OnReady<Gd<AnimationTree>>,
anim_state: OnReady<Gd<AnimationNodeStateMachinePlayback>>,
}

#[godot_api]
Expand All @@ -23,11 +23,8 @@ impl PCharNode {
let mode_str = if moving { "Run" } else { "Idle" };
let anim_path = format!("parameters/{mode_str}/blend_position");

self.anim_tree
.as_mut()
.unwrap()
.set(anim_path.into(), Variant::from(inputs));
self.anim_state.as_mut().unwrap().travel(mode_str.into());
self.anim_tree.set(anim_path.into(), Variant::from(inputs));
self.anim_state.travel(mode_str.into());
}
}

Expand Down Expand Up @@ -57,14 +54,27 @@ macro_rules! load_pchar_scene_under {

#[godot_api]
impl INode2D for PCharNode {
fn init(base: Base<Node2D>) -> Self {
Self {
base,
sprite: OnReady::manual(),
anim_player: OnReady::manual(),
anim_tree: OnReady::manual(),
anim_state: OnReady::manual(),
}
}

fn ready(&mut self) {
self.sprite = Some(self.base().get_node_as("Sprite2D"));
self.anim_player = Some(self.base().get_node_as("AnimationPlayer"));
let sprite = self.base().get_node_as("Sprite2D");
let anim_player = self.base().get_node_as("AnimationPlayer");
self.sprite.init(sprite);
self.anim_player.init(anim_player);

let mut tree = self.base().get_node_as::<AnimationTree>("AnimationTree");
tree.set_active(true);
self.anim_state = tree.get("parameters/playback".into()).to();
let anim_state = tree.get("parameters/playback".into()).to();
self.anim_state.init(anim_state);

self.anim_tree = Some(tree);
self.anim_tree.init(tree);
}
}

0 comments on commit 10d148b

Please sign in to comment.