refactor: dont use i64, use instanceid
This commit is contained in:
parent
fbe5d1644b
commit
b6805cef66
@ -1,5 +1,4 @@
|
||||
use super::*;
|
||||
use godot::prelude::*;
|
||||
|
||||
use crate::{BoidProperties, Flock2D};
|
||||
|
||||
@ -14,7 +13,7 @@ pub struct Boid2D {
|
||||
properties: Gd<BoidProperties>,
|
||||
props: BoidProperties,
|
||||
vel: Vec2,
|
||||
flock_id: i64,
|
||||
flock_id: Option<InstanceId>,
|
||||
base: Base<Node2D>,
|
||||
}
|
||||
|
||||
@ -38,15 +37,15 @@ impl Boid2D {
|
||||
#[func]
|
||||
#[inline(always)]
|
||||
/// Get the ID of this boid.
|
||||
pub fn get_id(&self) -> i64 {
|
||||
self.base().instance_id().to_i64()
|
||||
pub fn get_id(&self) -> InstanceId {
|
||||
self.base().instance_id()
|
||||
}
|
||||
|
||||
#[func]
|
||||
#[inline(always)]
|
||||
/// Get the flock ID of this boid.
|
||||
pub fn get_flock_id(&self) -> i64 {
|
||||
self.flock_id
|
||||
pub fn get_flock_id(&self) -> InstanceId {
|
||||
self.flock_id.expect("no flock id set... this is a bug!")
|
||||
}
|
||||
}
|
||||
|
||||
@ -64,7 +63,7 @@ impl INode2D for Boid2D {
|
||||
};
|
||||
let mut flock = flock.bind_mut();
|
||||
flock.register_boid(self.get_id());
|
||||
self.flock_id = flock.get_id();
|
||||
self.flock_id = Some(flock.get_id());
|
||||
}
|
||||
|
||||
fn ready(&mut self) {
|
||||
@ -73,7 +72,7 @@ impl INode2D for Boid2D {
|
||||
|
||||
fn exit_tree(&mut self) {
|
||||
let mut flock: Gd<Flock2D> =
|
||||
Gd::from_instance_id(InstanceId::from_i64(self.get_flock_id()));
|
||||
Gd::from_instance_id(self.get_flock_id());
|
||||
flock.bind_mut().unregister_boid(self.get_id());
|
||||
}
|
||||
}
|
||||
@ -104,7 +103,7 @@ impl Boid for Boid2D {
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
fn get_flock_id(&self) -> i64 {
|
||||
fn get_flock_id(&self) -> InstanceId {
|
||||
self.get_flock_id()
|
||||
}
|
||||
}
|
||||
|
@ -1,5 +1,4 @@
|
||||
use super::*;
|
||||
use godot::prelude::*;
|
||||
|
||||
use crate::{to_glam_vec, BoidProperties, Flock3D};
|
||||
|
||||
@ -14,7 +13,7 @@ pub struct Boid3D {
|
||||
properties: Gd<BoidProperties>,
|
||||
props: BoidProperties,
|
||||
vel: Vec3,
|
||||
flock_id: i64,
|
||||
flock_id: Option<InstanceId>,
|
||||
base: Base<Node3D>,
|
||||
}
|
||||
|
||||
@ -37,15 +36,15 @@ impl Boid3D {
|
||||
#[func]
|
||||
#[inline(always)]
|
||||
/// Get the ID of this boid.
|
||||
pub fn get_id(&self) -> i64 {
|
||||
self.base().instance_id().to_i64()
|
||||
pub fn get_id(&self) -> InstanceId {
|
||||
self.base().instance_id()
|
||||
}
|
||||
|
||||
#[func]
|
||||
#[inline(always)]
|
||||
/// Get the flock ID of this boid.
|
||||
pub fn get_flock_id(&self) -> i64 {
|
||||
self.flock_id
|
||||
pub fn get_flock_id(&self) -> InstanceId {
|
||||
self.flock_id.expect("no flock id found set... this is a bug!")
|
||||
}
|
||||
}
|
||||
|
||||
@ -63,7 +62,7 @@ impl INode3D for Boid3D {
|
||||
};
|
||||
let mut flock = flock.bind_mut();
|
||||
flock.register_boid(self.get_id());
|
||||
self.flock_id = flock.get_id();
|
||||
self.flock_id = Some(flock.get_id());
|
||||
}
|
||||
|
||||
fn ready(&mut self) {
|
||||
@ -72,7 +71,7 @@ impl INode3D for Boid3D {
|
||||
|
||||
fn exit_tree(&mut self) {
|
||||
let mut flock: Gd<Flock3D> =
|
||||
Gd::from_instance_id(InstanceId::from_i64(self.get_flock_id()));
|
||||
Gd::from_instance_id(self.get_flock_id());
|
||||
flock.bind_mut().unregister_boid(self.get_id());
|
||||
}
|
||||
}
|
||||
@ -102,7 +101,7 @@ impl Boid for Boid3D {
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
fn get_flock_id(&self) -> i64 {
|
||||
fn get_flock_id(&self) -> InstanceId {
|
||||
self.get_flock_id()
|
||||
}
|
||||
}
|
||||
|
@ -2,6 +2,7 @@ use std::ops::Sub;
|
||||
use std::sync::Arc;
|
||||
|
||||
use glam::*;
|
||||
use godot::prelude::*;
|
||||
use rayon::prelude::*;
|
||||
|
||||
use crate::FlockProperties;
|
||||
@ -20,7 +21,7 @@ pub trait Boid {
|
||||
fn get_boid_velocity(&self) -> Vec3;
|
||||
fn get_boid_properties(&self) -> &BoidProperties;
|
||||
|
||||
fn get_flock_id(&self) -> i64;
|
||||
fn get_flock_id(&self) -> InstanceId;
|
||||
}
|
||||
|
||||
struct CalcArgs {
|
||||
|
@ -18,20 +18,20 @@ pub struct Flock2D {
|
||||
#[export]
|
||||
/// A target node for the flock to follow.
|
||||
target: Option<Gd<Node2D>>,
|
||||
pub boids: FxIndexMap<i64, Gd<Boid2D>>,
|
||||
pub boids: FxIndexMap<InstanceId, Gd<Boid2D>>,
|
||||
base: Base<Node2D>,
|
||||
}
|
||||
|
||||
impl Flock2D {
|
||||
pub fn register_boid(&mut self, boid_id: i64) {
|
||||
let boid: Gd<Boid2D> = Gd::from_instance_id(InstanceId::from_i64(boid_id));
|
||||
pub fn register_boid(&mut self, boid_id: InstanceId) {
|
||||
let boid: Gd<Boid2D> = Gd::from_instance_id(boid_id);
|
||||
self.boids.insert(boid_id, boid.clone());
|
||||
get_singleton().bind_mut().register_boid_2d(boid_id, boid);
|
||||
let flock_id = self.get_id();
|
||||
godot_print!("[Flock2D:{flock_id}] boid {boid_id} registered");
|
||||
}
|
||||
|
||||
pub fn unregister_boid(&mut self, boid_id: i64) {
|
||||
pub fn unregister_boid(&mut self, boid_id: InstanceId) {
|
||||
self.boids.shift_remove(&boid_id);
|
||||
get_singleton().bind_mut().unregister_boid_2d(boid_id);
|
||||
let flock_id = self.get_id();
|
||||
@ -61,8 +61,8 @@ impl Flock2D {
|
||||
#[func]
|
||||
#[inline(always)]
|
||||
/// Retrieve the ID of this flock.
|
||||
pub fn get_id(&self) -> i64 {
|
||||
self.base().instance_id().to_i64()
|
||||
pub fn get_id(&self) -> InstanceId {
|
||||
self.base().instance_id()
|
||||
}
|
||||
}
|
||||
|
||||
@ -92,7 +92,7 @@ impl Flock for Flock2D {
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
fn get_boids(&self) -> impl Iterator<Item = (&i64, (Vec3, Vec3, BoidProperties))> {
|
||||
fn get_boids(&self) -> impl Iterator<Item = (&InstanceId, (Vec3, Vec3, BoidProperties))> {
|
||||
self.boids.iter().map(|(id, boid)| {
|
||||
let boid = boid.bind();
|
||||
(
|
||||
|
@ -20,20 +20,20 @@ pub struct Flock3D {
|
||||
#[export]
|
||||
/// A target node for the flock to follow.
|
||||
target: Option<Gd<Node3D>>,
|
||||
pub boids: FxIndexMap<i64, Gd<Boid3D>>,
|
||||
pub boids: FxIndexMap<InstanceId, Gd<Boid3D>>,
|
||||
base: Base<Node3D>,
|
||||
}
|
||||
|
||||
impl Flock3D {
|
||||
pub fn register_boid(&mut self, boid_id: i64) {
|
||||
let boid: Gd<Boid3D> = Gd::from_instance_id(InstanceId::from_i64(boid_id));
|
||||
pub fn register_boid(&mut self, boid_id: InstanceId) {
|
||||
let boid: Gd<Boid3D> = Gd::from_instance_id(boid_id);
|
||||
self.boids.insert(boid_id, boid.clone());
|
||||
get_singleton().bind_mut().register_boid_3d(boid_id, boid);
|
||||
let flock_id = self.get_id();
|
||||
godot_print!("[Flock3D:{flock_id}] boid {boid_id} registered");
|
||||
}
|
||||
|
||||
pub fn unregister_boid(&mut self, boid_id: i64) {
|
||||
pub fn unregister_boid(&mut self, boid_id: InstanceId) {
|
||||
self.boids.shift_remove(&boid_id);
|
||||
get_singleton().bind_mut().unregister_boid_3d(boid_id);
|
||||
let flock_id = self.get_id();
|
||||
@ -63,8 +63,8 @@ impl Flock3D {
|
||||
#[func]
|
||||
#[inline(always)]
|
||||
/// Retrieve the ID of this flock.
|
||||
pub fn get_id(&self) -> i64 {
|
||||
self.base().instance_id().to_i64()
|
||||
pub fn get_id(&self) -> InstanceId {
|
||||
self.base().instance_id()
|
||||
}
|
||||
}
|
||||
|
||||
@ -91,7 +91,7 @@ impl Flock for Flock3D {
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
fn get_boids(&self) -> impl Iterator<Item = (&i64, (Vec3, Vec3, BoidProperties))> {
|
||||
fn get_boids(&self) -> impl Iterator<Item = (&InstanceId, (Vec3, Vec3, BoidProperties))> {
|
||||
self.boids.iter().map(|(id, boid)| {
|
||||
let boid = boid.bind();
|
||||
(
|
||||
|
@ -1,4 +1,5 @@
|
||||
use glam::*;
|
||||
use godot::prelude::*;
|
||||
|
||||
use crate::BoidProperties;
|
||||
|
||||
@ -13,6 +14,6 @@ pub use flock_3d::*;
|
||||
pub trait Flock {
|
||||
fn get_flock_properties(&self) -> &FlockProperties;
|
||||
fn get_target_position(&self) -> Option<Vec3>;
|
||||
fn get_boids(&self) -> impl Iterator<Item = (&i64, (Vec3, Vec3, BoidProperties))>;
|
||||
fn get_boids(&self) -> impl Iterator<Item = (&InstanceId, (Vec3, Vec3, BoidProperties))>;
|
||||
fn get_boids_posvel(&self) -> Vec<(Vec3, Vec3)>;
|
||||
}
|
||||
|
@ -9,7 +9,6 @@ use godot::{
|
||||
use indexmap::IndexMap;
|
||||
use rayon::prelude::*;
|
||||
|
||||
mod obstacle;
|
||||
mod boid;
|
||||
mod flock;
|
||||
|
||||
@ -123,53 +122,53 @@ impl INode for BoidsProcess {
|
||||
#[class(init, base=Object)]
|
||||
/// Singleton that holds all boids and flocks and manages them.
|
||||
struct Boids {
|
||||
flocks2d: FxIndexMap<i64, Gd<Flock2D>>,
|
||||
boids2d: FxIndexMap<i64, Gd<Boid2D>>,
|
||||
flocks3d: FxIndexMap<i64, Gd<Flock3D>>,
|
||||
boids3d: FxIndexMap<i64, Gd<Boid3D>>,
|
||||
flocks2d: FxIndexMap<InstanceId, Gd<Flock2D>>,
|
||||
boids2d: FxIndexMap<InstanceId, Gd<Boid2D>>,
|
||||
flocks3d: FxIndexMap<InstanceId, Gd<Flock3D>>,
|
||||
boids3d: FxIndexMap<InstanceId, Gd<Boid3D>>,
|
||||
base: Base<Object>,
|
||||
}
|
||||
|
||||
impl Boids {
|
||||
fn register_flock_2d(&mut self, flock_id: i64) {
|
||||
let flock = Gd::from_instance_id(InstanceId::from_i64(flock_id));
|
||||
fn register_flock_2d(&mut self, flock_id: InstanceId) {
|
||||
let flock = Gd::from_instance_id(flock_id);
|
||||
self.flocks2d.insert(flock_id, flock);
|
||||
godot_print!("[Boids] flock {flock_id} registered");
|
||||
}
|
||||
|
||||
fn unregister_flock_2d(&mut self, flock_id: i64) {
|
||||
fn unregister_flock_2d(&mut self, flock_id: InstanceId) {
|
||||
self.flocks2d.shift_remove(&flock_id);
|
||||
godot_print!("[Boids] flock {flock_id} unregistered");
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
fn register_boid_2d(&mut self, boid_id: i64, boid: Gd<Boid2D>) {
|
||||
fn register_boid_2d(&mut self, boid_id: InstanceId, boid: Gd<Boid2D>) {
|
||||
self.boids2d.insert(boid_id, boid);
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
fn unregister_boid_2d(&mut self, boid_id: i64) {
|
||||
fn unregister_boid_2d(&mut self, boid_id: InstanceId) {
|
||||
self.boids2d.shift_remove(&boid_id);
|
||||
}
|
||||
|
||||
fn register_flock_3d(&mut self, flock_id: i64) {
|
||||
let flock = Gd::from_instance_id(InstanceId::from_i64(flock_id));
|
||||
fn register_flock_3d(&mut self, flock_id: InstanceId) {
|
||||
let flock = Gd::from_instance_id(flock_id);
|
||||
self.flocks3d.insert(flock_id, flock);
|
||||
godot_print!("[Boids] flock {flock_id} registered");
|
||||
}
|
||||
|
||||
fn unregister_flock_3d(&mut self, flock_id: i64) {
|
||||
fn unregister_flock_3d(&mut self, flock_id: InstanceId) {
|
||||
self.flocks3d.shift_remove(&flock_id);
|
||||
godot_print!("[Boids] flock {flock_id} unregistered");
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
fn register_boid_3d(&mut self, boid_id: i64, boid: Gd<Boid3D>) {
|
||||
fn register_boid_3d(&mut self, boid_id: InstanceId, boid: Gd<Boid3D>) {
|
||||
self.boids3d.insert(boid_id, boid);
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
fn unregister_boid_3d(&mut self, boid_id: i64) {
|
||||
fn unregister_boid_3d(&mut self, boid_id: InstanceId) {
|
||||
self.boids3d.shift_remove(&boid_id);
|
||||
}
|
||||
}
|
||||
@ -227,7 +226,7 @@ const fn to_glam_vec(godot_vec: Vector3) -> Vec3 {
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
fn process_boids<F, B>(boids: &mut FxIndexMap<i64, Gd<B>>, flocks: &FxIndexMap<i64, Gd<F>>)
|
||||
fn process_boids<F, B>(boids: &mut FxIndexMap<InstanceId, Gd<B>>, flocks: &FxIndexMap<InstanceId, Gd<F>>)
|
||||
where
|
||||
F: Flock + GodotClass,
|
||||
F: Bounds<Declarer = DeclUser>,
|
||||
@ -268,10 +267,10 @@ where
|
||||
|
||||
#[cfg(feature = "stats")]
|
||||
let time = std::time::Instant::now();
|
||||
let forces: Vec<(i64, Vec3)> = calc_funcs
|
||||
let forces: Vec<(InstanceId, Vec3)> = calc_funcs
|
||||
.into_par_iter()
|
||||
.fold(
|
||||
|| Vec::<(i64, Vec3)>::with_capacity(total_boid_count),
|
||||
|| Vec::<(InstanceId, Vec3)>::with_capacity(total_boid_count),
|
||||
|mut acc, (boid_id, calc_fn)| {
|
||||
let force = calc_fn();
|
||||
acc.push((boid_id, force));
|
||||
@ -279,7 +278,7 @@ where
|
||||
},
|
||||
)
|
||||
.reduce(
|
||||
|| Vec::<(i64, Vec3)>::with_capacity(total_boid_count),
|
||||
|| Vec::<(InstanceId, Vec3)>::with_capacity(total_boid_count),
|
||||
|mut left, mut right| {
|
||||
left.append(&mut right);
|
||||
left
|
||||
|
Loading…
Reference in New Issue
Block a user