Skip to content

Commit

Permalink
Duplicate guide for 0.7
Browse files Browse the repository at this point in the history
  • Loading branch information
leudz committed Jul 10, 2024
1 parent d73a8b5 commit 7743aca
Show file tree
Hide file tree
Showing 42 changed files with 3,365 additions and 0 deletions.
29 changes: 29 additions & 0 deletions .github/workflows/check-deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,29 @@ jobs:
with:
name: guide_0_6
path: guide/0.6/guide_0_6/html
guide_0_7:
runs-on: ubuntu-latest
needs: changes
if: github.event_name == 'push' && needs.changes.outputs.deploy == 'true'
steps:
- uses: actions/checkout@v3
- uses: dtolnay/rust-toolchain@stable

- name: download mdbook
uses: peaceiris/actions-mdbook@v1
with:
mdbook-version: "0.4.34"

- run: cargo install mdbook-linkcheck

- run: mdbook build -d guide_0_7
working-directory: ./guide/0.7

- name: save guide 0.7
uses: actions/upload-artifact@v2
with:
name: guide_0_7
path: guide/0.7/guide_0_7/html
bunny_demo:
runs-on: ubuntu-latest
needs: changes
Expand Down Expand Up @@ -225,6 +248,12 @@ jobs:
with:
name: guide_0_6
path: dist/guide/0.6

- name: load guide 0.7
uses: actions/download-artifact@v2
with:
name: guide_0_7
path: dist/guide/0.7

- name: load bunny_demo
uses: actions/download-artifact@v2
Expand Down
16 changes: 16 additions & 0 deletions guide/0.7/book.toml
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]
36 changes: 36 additions & 0 deletions guide/0.7/src/SUMMARY.md
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)
4 changes: 4 additions & 0 deletions guide/0.7/src/contributors.md
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)
6 changes: 6 additions & 0 deletions guide/0.7/src/fundamentals.md
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.
36 changes: 36 additions & 0 deletions guide/0.7/src/fundamentals/add-components.md
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());
},
);
```
28 changes: 28 additions & 0 deletions guide/0.7/src/fundamentals/add-entity.md
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()));
},
);
```
53 changes: 53 additions & 0 deletions guide/0.7/src/fundamentals/delete-components.md
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);
});
```
25 changes: 25 additions & 0 deletions guide/0.7/src/fundamentals/delete-entity.md
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);
});
```
20 changes: 20 additions & 0 deletions guide/0.7/src/fundamentals/get-and-modify.md
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.
64 changes: 64 additions & 0 deletions guide/0.7/src/fundamentals/iterators.md
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);
}
});
```
31 changes: 31 additions & 0 deletions guide/0.7/src/fundamentals/remove-components.md
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);
},
);
```
Loading

0 comments on commit 7743aca

Please sign in to comment.