Skip to content

Commit

Permalink
Export nodes directly (#43)
Browse files Browse the repository at this point in the history
  • Loading branch information
TitanNano authored Aug 20, 2024
1 parent 1a92ba5 commit 166e27e
Show file tree
Hide file tree
Showing 14 changed files with 74 additions and 143 deletions.
20 changes: 10 additions & 10 deletions native/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion native/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,4 @@ rand = "0.8.5"
pomsky-macro = "0.11.0"
regex = "1.10.5"

godot-rust-script = { git = "https://github.com/titannano/godot-rust-script", rev = "501596ccbec7019632221d2218ab1806f973c412" }
godot-rust-script = { git = "https://github.com/titannano/godot-rust-script", rev = "91c51296bc47a99db4c632f8b50780bed3d512b2" }
19 changes: 5 additions & 14 deletions native/src/scripts/objects/building.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
use godot::{
builtin::{math::ApproxEq, Array, NodePath, Transform3D, Vector3},
builtin::{math::ApproxEq, Array, Transform3D, Vector3},
engine::{MeshInstance3D, Node, Node3D, PackedScene, Time},
meta::ToGodot,
obj::{Gd, Inherits},
tools::load,
};
use godot_rust_script::{godot_script_impl, CastToScript, Context, GodotScript, RsRef};
use godot_rust_script::{godot_script_impl, CastToScript, GodotScript, RsRef};
use rand::Rng;
use std::{any::Any, fmt::Debug};

Expand Down Expand Up @@ -203,32 +203,23 @@ struct Building {
#[export(flags = ["Fire:1"])]
pub events: u8,

#[export(node_path = ["MeshInstance3D"])]
pub mesh_path: NodePath,
#[export]
pub mesh: Option<Gd<MeshInstance3D>>,

pub tile_coords_array: Array<u32>,

tile_coords: TileCoords,

mesh: Option<Gd<MeshInstance3D>>,
features: Features<dyn BuildingFeature<Node>>,

base: Gd<Node>,
}

#[godot_script_impl]
impl Building {
pub fn _ready(&mut self, mut context: Context<Self>) {
pub fn _ready(&mut self) {
let events = BuildingEventFlags(self.events);

self.mesh = {
let mesh_path = self.mesh_path.clone();

context.reentrant_scope(self, |base: Gd<Node>| {
base.try_get_node_as(mesh_path.to_owned())
})
};

self.tile_coords = (
self.tile_coords_array.get(0).unwrap_or(0),
self.tile_coords_array.get(1).unwrap_or(0),
Expand Down
29 changes: 3 additions & 26 deletions native/src/scripts/objects/canon_upgrade.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use anyhow::{bail, Context};
use godot::{
builtin::{NodePath, StringName},
builtin::StringName,
engine::{GpuParticles3D, Node3D},
obj::Gd,
};
Expand Down Expand Up @@ -70,41 +70,18 @@ struct CanonUpgrade {
#[prop(set = Self::set_mode)]
pub mode: u8,

#[export(node_path = ["GPUParticles3D"])]
#[prop(set = Self::set_water_jet_path)]
pub water_jet_path: NodePath,

water_jet: Option<Gd<GpuParticles3D>>,
#[export]
pub water_jet: Option<Gd<GpuParticles3D>>,

base: Gd<Node3D>,
}

#[godot_script_impl]
impl CanonUpgrade {
pub fn _ready(&mut self) {
self.water_jet = self.base.try_get_node_as(self.water_jet_path.clone());

if self.water_jet.is_none() {
logger::error!("Failed to resolve node path: {}", self.water_jet_path);
}

self.set_mode(self.mode);
}

pub fn set_water_jet_path(&mut self, value: NodePath) {
self.water_jet_path = value.clone();

if !self.base.is_node_ready() {
return;
}

self.water_jet = self.base.try_get_node_as(value.clone());

if self.water_jet.is_none() {
logger::error!("Failed to resolve node path: {}", value);
}
}

pub fn set_mode(&mut self, value: u8) {
self.mode = value;

Expand Down
14 changes: 3 additions & 11 deletions native/src/scripts/objects/debugger_3_d.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use godot::{
builtin::{Dictionary, GString, NodePath},
builtin::{Dictionary, GString},
engine::{Node3D, RichTextLabel},
obj::Gd,
};
Expand All @@ -14,22 +14,14 @@ struct Debugger3D {
#[export]
pub title: GString,

#[export(node_path = ["RichTextLabel"])]
pub text_view_path: NodePath,
#[export]
pub text_view: Option<Gd<RichTextLabel>>,

pub debug_data: Dictionary,

text_view: Option<Gd<RichTextLabel>>,

base: Gd<Node3D>,
}

#[godot_script_impl]
impl Debugger3D {
pub fn _ready(&mut self) {
self.text_view = self.base.try_get_node_as(self.text_view_path.clone());
}

pub fn _process(&mut self, _delta: f64) {
let title = &self.title;

Expand Down
49 changes: 19 additions & 30 deletions native/src/scripts/objects/water_jet.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
use std::{cmp::Ordering, ops::DerefMut};

use anyhow::{bail, Result};
use godot::builtin::{
Array, Callable, Dictionary, GString, NodePath, StringName, Variant, Vector3,
};
use godot::builtin::{Array, Callable, Dictionary, GString, StringName, Variant, Vector3};
use godot::engine::object::ConnectFlags;
use godot::engine::{
Area3D, Decal, GpuParticles3D, Node, Node3D, Object, PhysicsRayQueryParameters3D,
Expand All @@ -13,7 +11,7 @@ use godot::global::{clampf, deg_to_rad, randf_range};
use godot::meta::ToGodot;
use godot::obj::bounds::Declarer;
use godot::obj::{Bounds, EngineEnum, Gd, Inherits, InstanceId};
use godot::prelude::GodotClass;
use godot::prelude::{GodotClass, NodePath};
use godot_rust_script::{godot_script_impl, GodotScript};
use itertools::Itertools;

Expand All @@ -26,25 +24,20 @@ use crate::{
#[derive(GodotScript, Debug)]
#[script(base = GpuParticles3D)]
struct WaterJet {
#[export(node_path = ["Array[ShapeCast]"])]
pub impact_casts_path: Array<NodePath>,
/// List of ShapeCast3D nodes to aproximate particle impact.
#[export(node_path = ["ShapeCast3D"])]
pub impact_cast_paths: Array<NodePath>,

impact_casts: Vec<Gd<ShapeCast3D>>,

#[export(node_path = ["Area3D"])]
pub impact_area_path: NodePath,

impact_area: Option<Gd<Area3D>>,

#[export(node_path = ["Decal"])]
pub decal_path: NodePath,
#[export]
pub impact_area: Option<Gd<Area3D>>,

decal: Option<Gd<Decal>>,
#[export]
pub decal: Option<Gd<Decal>>,

#[export(node_path = ["Debugger3D"])]
pub debugger_path: NodePath,

debugger: Option<Gd<Node3D>>,
#[export]
pub debugger: Option<Gd<Node3D>>,

/// Maximum number of decals that will be spawned at an impact point.
#[export(range(min = 1.0, max = 255.0, step = 1.0))]
Expand All @@ -66,6 +59,14 @@ struct Intersection {
impl WaterJet {
const MAX_DISTANCE: f64 = 60.0;

pub fn _ready(&mut self) {
self.impact_casts = self
.impact_cast_paths
.iter_shared()
.map(|path| self.base.get_node_as(path))
.collect();
}

fn impact_area(&self) -> &Gd<Area3D> {
let Some(impact_area) = self.impact_area.as_ref() else {
panic!("Missing impact area!");
Expand Down Expand Up @@ -147,18 +148,6 @@ impl WaterJet {
}))
}

pub fn _ready(&mut self) {
self.impact_casts = self
.impact_casts_path
.iter_shared()
.map(|path| self.base.get_node_as(path))
.collect();

self.impact_area = self.base.try_get_node_as(self.impact_area_path.clone());
self.decal = self.base.try_get_node_as(self.decal_path.clone());
self.debugger = self.base.try_get_node_as(self.debugger_path.clone());
}

pub fn _physics_process(&mut self, delta: f64) {
let mut debugger = self
.debugger
Expand Down
22 changes: 7 additions & 15 deletions native/src/scripts/spawner/fire_spawner.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use godot::builtin::math::ApproxEq;
use godot::builtin::{NodePath, Transform3D, Vector3, Vector3Axis};
use godot::builtin::{Transform3D, Vector3, Vector3Axis};
use godot::classes::{light_3d, FogVolume, Node, OmniLight3D, ShaderMaterial};
use godot::meta::ToGodot;
use godot::obj::Gd;
Expand All @@ -11,25 +11,20 @@ use crate::util::logger;
#[script(base = Node)]
pub struct FireSpawner {
/// Node path to the fog volume for the flames.
#[export(node_path = ["FogVolume"])]
pub fire_path: NodePath,
fire: Option<Gd<FogVolume>>,
#[export]
pub fire: Option<Gd<FogVolume>>,

/// Node path to the fog volume for the smoke.
#[export(node_path = ["FogVolume"])]
pub smoke_path: NodePath,
smoke: Option<Gd<FogVolume>>,
#[export]
pub smoke: Option<Gd<FogVolume>>,

/// Node path to the omni light source of the fire.
#[export(node_path = ["OmniLight3D"])]
pub light_source_path: NodePath,
light_source: Option<Gd<OmniLight3D>>,
#[export]
pub light_source: Option<Gd<OmniLight3D>>,

default_light_energy: f32,

strength: f64,

base: Gd<Node>,
}

#[godot_script_impl]
Expand All @@ -40,9 +35,6 @@ impl FireSpawner {

pub fn _ready(&mut self) {
logger::debug!("Init Fire spawner...");
self.fire = self.base.try_get_node_as(self.fire_path.clone());
self.smoke = self.base.try_get_node_as(self.smoke_path.clone());
self.light_source = self.base.try_get_node_as(self.light_source_path.clone());

if let Some(ref mut fire) = self.fire {
if let Some(material) = fire.get_material() {
Expand Down
22 changes: 6 additions & 16 deletions native/src/scripts/world/solar_setup.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use godot::builtin::{NodePath, Vector3};
use godot::builtin::Vector3;
use godot::engine::{light_3d, DirectionalLight3D, Node3D, Time};
use godot::obj::Gd;
use godot_rust_script::{godot_script_impl, GodotScript};
Expand All @@ -9,14 +9,12 @@ use crate::util::logger;
#[script(base = Node3D)]
struct SolarSetup {
/// Reference to the sun child node.
#[export(node_path = ["DirectionalLight3D"])]
pub sun: NodePath,

sun_ref: Option<Gd<DirectionalLight3D>>,
#[export]
pub sun: Option<Gd<DirectionalLight3D>>,

/// Reference to the moon child node.
#[export(node_path = ["DirectionalLight3D"])]
pub moon: NodePath,
#[export]
pub moon: Option<Gd<DirectionalLight3D>>,

// duration from sun rise to sun set in minutes
#[export(range(min = 1.0, max = 120.0, step = 1.0))]
Expand All @@ -27,21 +25,13 @@ struct SolarSetup {

#[godot_script_impl]
impl SolarSetup {
fn sun(&mut self) -> Option<Gd<DirectionalLight3D>> {
if self.sun_ref.is_none() {
self.sun_ref = self.base.try_get_node_as(self.sun.clone());
}

self.sun_ref.clone()
}

pub fn _physics_process(&mut self, _delta: f64) {
let day_length = self.day_length * 60 * 1000;
let time = Time::singleton().get_ticks_msec() % (day_length * 2);

let sun_pos = time as f32 * (360.0 / (day_length * 2) as f32);

let Some(mut sun) = self.sun() else {
let Some(ref mut sun) = self.sun else {
logger::error!("no sun is assigned to solar setup!");
logger::error!("node path: {}", self.base.get_path());

Expand Down
Loading

0 comments on commit 166e27e

Please sign in to comment.