-
Notifications
You must be signed in to change notification settings - Fork 51
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
42 changed files
with
3,365 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
[book] | ||
authors = ["Dylan Ancel", "David Komer"] | ||
language = "en" | ||
multilingual = false | ||
src = "src" | ||
title = "Shipyard User's Guide" | ||
|
||
[build] | ||
build-dir = "output" | ||
create-missing = false | ||
|
||
[output.html] | ||
git-repository-url = "https://github.com/leudz/shipyard" | ||
edit-url-template = "https://github.com/leudz/shipyard/edit/master/guide/master/{path}" | ||
|
||
[output.linkcheck] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
# Summary | ||
|
||
[Welcome](./welcome.md) | ||
- [Learn by example](./learn-by-example.md) | ||
- [A lone square](./learn-by-example/a-lone-square.md) | ||
- [Friends](./learn-by-example/friends.md) | ||
- [Spark](./learn-by-example/spark.md) | ||
- [Breather](./learn-by-example/breather.md) | ||
- [Reign](./learn-by-example/reign.md) | ||
- [True Victory](./learn-by-example/true-victory.md) | ||
- [Fundamentals](./fundamentals.md) | ||
- [World](./fundamentals/world.md) | ||
- [Add Entity](./fundamentals/add-entity.md) | ||
- [Delete Entity](./fundamentals/delete-entity.md) | ||
- [Add Components](./fundamentals/add-components.md) | ||
- [Remove Components](./fundamentals/remove-components.md) | ||
- [Delete Components](./fundamentals/delete-components.md) | ||
- [Get and Modify Components](./fundamentals/get-and-modify.md) | ||
- [Iterators](./fundamentals/iterators.md) | ||
- [Uniques](./fundamentals/uniques.md) | ||
- [Systems](./fundamentals/systems.md) | ||
- [Going Further](./going-further.md) | ||
- [Tracking](./going-further/tracking.md) | ||
- [Parallelism](./going-further/parallelism.md) | ||
- [Custom Views](./going-further/custom-views.md) | ||
- [!Send and !Sync Components](./going-further/non-send-sync.md) | ||
- [Performance Tips](./going-further/performance-tips.md) | ||
- [Tracing](./going-further/tracing.md) | ||
- [More Resources](./going-further/more-resources.md) | ||
- [Going Deeper](./going-deeper.md) | ||
- [Sparse Set](./going-deeper/sparse-set.md) | ||
- [Recipes](./recipes.md) | ||
- [Hierarchy](./recipes/hierarchy.md) | ||
- [Seed](./recipes/seed.md) | ||
- [0.4 migration](./recipes/0.4-migration.md) | ||
- [Contributors](./contributors.md) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
# Contributors | ||
|
||
[dakom - David Komer](https://github.com/dakom) | ||
[eldyer](https://github.com/eldyer) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
# Fundamentals | ||
|
||
This section is about learning all basic ECS operations. | ||
|
||
It also acts as a reference that you can come back to.\ | ||
So even if you've just finished building the square eater game, this section can be useful to get a more condensed version. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
# Add Components | ||
|
||
An entity can have any number of components but only one in each storage. | ||
Adding another component of the same type will replace the existing one. | ||
|
||
## World | ||
|
||
```rust, noplaypen | ||
let mut world = World::new(); | ||
let id = world.add_entity(()); | ||
world.add_component(id, Vel::new()); | ||
world.add_component(id, (Pos::new(), Vel::new())); | ||
``` | ||
|
||
## View | ||
|
||
When adding components, the entities storage is only used to check if the [`EntityId`](https://docs.rs/shipyard/latest/shipyard/struct.EntityId.html) is alive. | ||
We don't need exclusive access to the entities storage. | ||
|
||
If you don't need to check if the entity is alive, you can use the [`AddComponent`](https://docs.rs/shipyard/latest/shipyard/trait.AddComponent.html) trait and do without the entities storage entirely. | ||
|
||
```rust, noplaypen | ||
let world = World::new(); | ||
world.run( | ||
|mut entities: EntitiesViewMut, mut vm_pos: ViewMut<Pos>, mut vm_vel: ViewMut<Vel>| { | ||
let id = entities.add_entity((), ()); | ||
entities.add_component(id, &mut vm_pos, Pos::new()); | ||
entities.add_component(id, (&mut vm_pos, &mut vm_vel), (Pos::new(), Vel::new())); | ||
vm_vel.add_component_unchecked(id, Vel::new()); | ||
}, | ||
); | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
# Add Entity | ||
|
||
When an entity is created you will receive a unique handle to it: an [`EntityId`](https://docs.rs/shipyard/latest/shipyard/struct.EntityId.html). | ||
|
||
## World | ||
|
||
```rust, noplaypen | ||
let mut world = World::new(); | ||
let empty_entity = world.add_entity(()); | ||
let single_component = world.add_entity(Pos::new()); | ||
let multiple_components = world.add_entity((Pos::new(), Vel::new())); | ||
``` | ||
|
||
## Views | ||
|
||
```rust, noplaypen | ||
let world = World::new(); | ||
world.run( | ||
|mut entities: EntitiesViewMut, mut vm_pos: ViewMut<Pos>, mut vm_vel: ViewMut<Vel>| { | ||
let empty_entity = entities.add_entity((), ()); | ||
let single_component = entities.add_entity(&mut vm_pos, Pos::new()); | ||
let multiple_components = | ||
entities.add_entity((&mut vm_pos, &mut vm_vel), (Pos::new(), Vel::new())); | ||
}, | ||
); | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
# Delete Components | ||
|
||
Deleting a component will erase it from the storage but will not return it. | ||
|
||
## World | ||
|
||
```rust, noplaypen | ||
let mut world = World::new(); | ||
let id = world.add_entity((Pos::new(), Vel::new())); | ||
world.delete_component::<Vel>(id); | ||
world.delete_component::<(Pos, Vel)>(id); | ||
``` | ||
|
||
#### All Components | ||
|
||
```rust, noplaypen | ||
let mut world = World::new(); | ||
let id = world.add_entity((Pos::new(), Vel::new())); | ||
world.strip(id); | ||
``` | ||
|
||
## View | ||
|
||
We have to import the [`Delete`](https://docs.rs/shipyard/latest/shipyard/trait.Delete.html) trait for multiple components. | ||
|
||
```rust, noplaypen | ||
let world = World::new(); | ||
world.run( | ||
|mut entities: EntitiesViewMut, mut vm_pos: ViewMut<Pos>, mut vm_vel: ViewMut<Vel>| { | ||
let id = entities.add_entity((&mut vm_pos, &mut vm_vel), (Pos::new(), Vel::new())); | ||
vm_pos.delete(id); | ||
(&mut vm_pos, &mut vm_vel).delete(id); | ||
}, | ||
); | ||
``` | ||
|
||
#### All Components | ||
|
||
```rust, noplaypen | ||
let world = World::new(); | ||
world.run(|mut all_storages: AllStoragesViewMut| { | ||
let id = all_storages.add_entity((Pos::new(), Vel::new())); | ||
all_storages.strip(id); | ||
}); | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
# Delete Entity | ||
|
||
Deleting an entity deletes it from the entities storage, while also deleting all its components. | ||
|
||
## World | ||
|
||
```rust, noplaypen | ||
let mut world = World::new(); | ||
let id = world.add_entity(Pos::new()); | ||
world.delete_entity(id); | ||
``` | ||
|
||
## View | ||
|
||
```rust, noplaypen | ||
let world = World::new(); | ||
world.run(|mut all_storages: AllStoragesViewMut| { | ||
let id = all_storages.add_entity(Pos::new()); | ||
all_storages.delete_entity(id); | ||
}); | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
# Get and Modify Components | ||
|
||
To access or update components you can use [`Get::get`](https://docs.rs/shipyard/latest/shipyard/trait.Get.html#tymethod.get). It'll work with both shared and exclusive views. | ||
|
||
```rust, noplaypen | ||
let mut world = World::new(); | ||
let id = world.add_entity((Pos::new(), Vel::new())); | ||
world.run(|mut vm_pos: ViewMut<Pos>, mut vm_vel: ViewMut<Vel>| { | ||
(&mut vm_vel).get(id).unwrap().0 += 1.0; | ||
let (mut i, j) = (&mut vm_pos, &vm_vel).get(id).unwrap(); | ||
i.0 += j.0; | ||
vm_pos[id].0 += 1.0; | ||
}); | ||
``` | ||
|
||
When using a single view, if you are certain an entity has the desired component, you can access it via index. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
# Iterators | ||
|
||
Iteration is one of the most important features of an ECS. | ||
|
||
## World | ||
|
||
```rust, noplaypen | ||
let world = World::new(); | ||
for (i, j) in &mut world.iter::<(&mut Pos, &Vel)>() { | ||
i.0 += j.0; | ||
} | ||
``` | ||
|
||
THe "extra" `&mut` is unfortunate but necessary. | ||
|
||
## Views | ||
|
||
Iteration on views is achieved using [`IntoIter::iter`](https://docs.rs/shipyard/latest/shipyard/trait.IntoIter.html#tymethod.iter). | ||
|
||
```rust, noplaypen | ||
let world = World::new(); | ||
world.run(|mut vm_pos: ViewMut<Pos>, v_vel: View<Vel>| { | ||
for i in vm_pos.iter() { | ||
dbg!(i); | ||
} | ||
for (i, j) in (&mut vm_pos, &v_vel).iter() { | ||
i.0 += j.0; | ||
} | ||
}); | ||
``` | ||
|
||
You can use views in any order. However, using the same combination of views in different positions may yield components in a different order. | ||
You shouldn't expect specific ordering from Shipyard's iterators in general. | ||
|
||
#### With Id | ||
|
||
You can ask an iterator to tell you which entity owns each component by using [`WithId::with_id`](https://docs.rs/shipyard/latest/shipyard/trait.IntoWithId.html#method.with_id): | ||
|
||
```rust, noplaypen | ||
let world = World::new(); | ||
world.run(|v_pos: View<Pos>| { | ||
for (id, i) in v_pos.iter().with_id() { | ||
println!("{:?} belongs to entity {:?}", i, id); | ||
} | ||
}); | ||
``` | ||
|
||
#### Not | ||
|
||
It's possible to filter entities that don't have a certain component using [`Not`](https://docs.rs/shipyard/latest/shipyard/struct.Not.html) by adding `!` in front of the view reference. | ||
|
||
```rust, noplaypen | ||
let world = World::new(); | ||
world.run(|v_pos: View<Pos>, v_vel: View<Vel>| { | ||
for (i, _) in (&v_pos, !&v_vel).iter() { | ||
dbg!(i); | ||
} | ||
}); | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
# Remove Components | ||
|
||
Removing a component will take it out of the storage and return it. | ||
|
||
## World | ||
|
||
```rust, noplaypen | ||
let mut world = World::new(); | ||
let id = world.add_entity((Pos::new(), Vel::new())); | ||
world.remove::<Vel>(id); | ||
world.remove::<(Pos, Vel)>(id); | ||
``` | ||
|
||
## View | ||
|
||
We have to import the [`Remove`](https://docs.rs/shipyard/latest/shipyard/trait.Remove.html) trait for multiple components. | ||
|
||
```rust, noplaypen | ||
let world = World::new(); | ||
world.run( | ||
|mut entities: EntitiesViewMut, mut vm_pos: ViewMut<Pos>, mut vm_vel: ViewMut<Vel>| { | ||
let id = entities.add_entity((&mut vm_pos, &mut vm_vel), (Pos::new(), Vel::new())); | ||
vm_pos.remove(id); | ||
(&mut vm_pos, &mut vm_vel).remove(id); | ||
}, | ||
); | ||
``` |
Oops, something went wrong.