Skip to content
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

Scripting Support #99

Closed
wants to merge 3 commits into from
Closed
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
73 changes: 73 additions & 0 deletions examples/custom_component.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
use shipyard::*;
use std::convert::TryInto;

use std::collections::HashMap;

struct Nothing;

struct ScriptSystem {
name: String,
}

fn main() {
// This stores our components by name
let mut components: HashMap<String, CustomComponent> = HashMap::new();

// Insert our custom components
components.insert(
"position".into(),
CustomComponent {
size: 32.try_into().unwrap(),
id: 0,
},
);

components.insert(
"velocity".into(),
CustomComponent {
size: 16.try_into().unwrap(),
id: 1,
},
);

// Create a custom system
let insert_pos_vel_sys = DynamicSystem {
data: (),
system_fn: |_, borrows| {},
borrow_intents: vec![
CustomComponentBorrowIntent {
component: components.get("velocity").unwrap().clone(),
mutation: Mutation::Shared,
},
CustomComponentBorrowIntent {
component: components.get("position").unwrap().clone(),
mutation: Mutation::Unique,
},
],
};

let pos_vel_sys = DynamicSystem {
data: (),
system_fn: |_, borrows| {
// Need to do something like this, but I cant!
let velocities = borrows.get(0).unwrap().downcast::<DynamicView<[u8; 16]>>().unwrap();
let positions = borrows.get(1).unwrap().downcast::<DynamicViewMut<[u8; 32]>>().unwrap();

dbg!(velocities, positions);
},
borrow_intents: vec![
CustomComponentBorrowIntent {
component: components.get("velocity").unwrap().clone(),
mutation: Mutation::Shared,
},
CustomComponentBorrowIntent {
component: components.get("position").unwrap().clone(),
mutation: Mutation::Unique,
},
],
};

let world = World::new();
world.run(insert_pos_vel_sys);
world.run(pos_vel_sys);
}
45 changes: 45 additions & 0 deletions examples/hello_world.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
use shipyard::*;

#[derive(Debug)]
struct Position {
x: f32,
y: f32,
}

struct Velocity {
x: f32,
y: f32,
}

fn position_printer(positions: View<Position>) {
for pos in positions.iter() {
println!("position: {:?}", pos);
}
}

fn velocity_handler(mut positions: ViewMut<Position>, velocities: View<Velocity>) {
for (mut pos, vel) in (&mut positions, &velocities).iter() {
pos.x += vel.x;
pos.y += vel.y;
}
}

fn main() {
let world = World::new();

world.run(
|mut entities: EntitiesViewMut,
mut positions: ViewMut<Position>,
mut velocities: ViewMut<Velocity>| {
entities.add_entity(
(&mut positions, &mut velocities),
(Position { x: 10., y: 10. }, Velocity { x: 10., y: 10. }),
);
},
);

loop {
world.run(velocity_handler);
world.run(position_printer);
}
}
Loading