-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add simple Bevy integration, add place-transition arc data
- Loading branch information
Showing
10 changed files
with
607 additions
and
382 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,55 @@ | ||
name: Rust | ||
on: [push, pull_request] | ||
|
||
on: | ||
push: | ||
branches: [ "main" ] | ||
pull_request: | ||
branches: [ "main" ] | ||
|
||
env: | ||
CARGO_TERM_COLOR: always | ||
name: CI | ||
|
||
jobs: | ||
build: | ||
check: | ||
name: check | ||
runs-on: ubuntu-latest | ||
steps: | ||
- uses: actions/checkout@v3 | ||
- uses: dtolnay/rust-toolchain@master | ||
with: | ||
toolchain: stable | ||
- name: Install alsa and udev | ||
run: sudo apt-get update; sudo apt-get install --no-install-recommends libasound2-dev libudev-dev | ||
if: runner.os == 'linux' | ||
- run: cargo check | ||
|
||
test: | ||
name: test | ||
runs-on: ubuntu-latest | ||
steps: | ||
- uses: actions/checkout@v3 | ||
- uses: dtolnay/rust-toolchain@master | ||
with: | ||
toolchain: stable | ||
- name: Install alsa and udev | ||
run: sudo apt-get update; sudo apt-get install --no-install-recommends libasound2-dev libudev-dev | ||
if: runner.os == 'linux' | ||
- run: cargo test | ||
|
||
fmt: | ||
name: rustfmt | ||
runs-on: ubuntu-latest | ||
steps: | ||
- uses: actions/checkout@v3 | ||
- uses: dtolnay/rust-toolchain@master | ||
with: | ||
toolchain: stable | ||
components: rustfmt | ||
- run: cargo fmt --all -- --check | ||
|
||
clippy: | ||
name: clippy | ||
runs-on: ubuntu-latest | ||
steps: | ||
- uses: actions/checkout@v3 | ||
- name: Build | ||
run: cargo build --verbose | ||
- name: Run tests | ||
run: cargo test --verbose --all-features | ||
- uses: actions/checkout@v3 | ||
- uses: dtolnay/rust-toolchain@master | ||
with: | ||
toolchain: stable | ||
components: clippy | ||
- name: Install alsa and udev | ||
run: sudo apt-get update; sudo apt-get install --no-install-recommends libasound2-dev libudev-dev | ||
if: runner.os == 'linux' | ||
- run: cargo clippy -- -D warnings |
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 |
---|---|---|
@@ -1,6 +1,6 @@ | ||
[package] | ||
name = "petnat" | ||
version = "0.0.1" | ||
version = "0.0.2" | ||
authors = ["Nurzhan Sakén <[email protected]>"] | ||
edition = "2021" | ||
description = "A Petri net plugin for Bevy Engine." | ||
|
@@ -11,4 +11,11 @@ keywords = ["bevy", "petri", "petri-net", "state-machine", "gamedev"] | |
categories = ["simulation", "game-development", "science"] | ||
|
||
[dependencies] | ||
# bevy = { version = "0.12", default-features = false } | ||
bevy_ecs = { version = "0.12", default-features = false } | ||
bevy_app = { version = "0.12" } | ||
bevy_utils = { version = "0.12" } | ||
|
||
[dev-dependencies] | ||
bevy = { version = "0.12" } | ||
|
||
[features] |
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,82 @@ | ||
//! A simple Petri net being manipulated via systems. | ||
use bevy::input::common_conditions::input_just_pressed; | ||
use bevy::prelude::*; | ||
use petnat::{NetId, Nn, PetriNet, PetriNetPlugin, Place, Pn, Tn, Token, W}; | ||
use std::any::type_name; | ||
|
||
fn main() { | ||
App::new() | ||
.add_plugins(DefaultPlugins) | ||
// (P0) -\ 1 1 | ||
// >-> |T0| -> (P2) | ||
// (P1) -/ 2 | ||
.add_plugins(PetriNetPlugin::<Nn<0>> { | ||
build: |builder| { | ||
builder | ||
.add_place::<Pn<0>>() | ||
.add_place::<Pn<1>>() | ||
.add_place::<Pn<2>>() | ||
// T0 requires 1 token in P0 and 2 tokens in P1 to be enabled | ||
// and it will produce 1 token in P2 when fired | ||
.add_trans::<Tn<0>, ((Pn<0>, W<1>), (Pn<1>, W<2>)), (Pn<2>, W<1>)>() | ||
}, | ||
}) | ||
.add_systems(Startup, spawn_token::<Nn<0>>) | ||
.add_systems( | ||
Update, | ||
( | ||
// press 1 and 2 to mark `P0` and `P1` | ||
mark::<Nn<0>, Pn<0>>.run_if(input_just_pressed(KeyCode::Key1)), | ||
mark::<Nn<0>, Pn<1>>.run_if(input_just_pressed(KeyCode::Key2)), | ||
// press T to fire `T0` | ||
trans_t0::<Nn<0>>.run_if(input_just_pressed(KeyCode::T)), | ||
print_net::<Nn<0>>, | ||
), | ||
) | ||
.run(); | ||
} | ||
|
||
fn spawn_token<Net: NetId>(mut commands: Commands, net: Res<PetriNet<Net>>) { | ||
// A token can represent the current state of some game object, | ||
// or some sort of resource | ||
commands.spawn(net.spawn_token()); | ||
info!("Spawning a token..."); | ||
} | ||
|
||
fn mark<Net: NetId, P: Place<Net>>(net: Res<PetriNet<Net>>, mut tokens: Query<&mut Token<Net>>) { | ||
for mut token in &mut tokens { | ||
net.mark::<P>(&mut token, 1); | ||
// TODO: better place/trans names | ||
let (_, name) = type_name::<P>() | ||
.rsplit_once(':') | ||
.unwrap_or(("", type_name::<P>())); | ||
info!("{} marked!", name); | ||
} | ||
} | ||
|
||
fn trans_t0<Net: NetId>(net: Res<PetriNet<Net>>, mut tokens: Query<&mut Token<Net>>) { | ||
for mut token in &mut tokens { | ||
// TODO: better handling of change detection | ||
if let Some(()) = net.fire::<Tn<0>>(token.bypass_change_detection()) { | ||
info!("T0 fired!"); | ||
token.set_changed(); | ||
} else { | ||
info!("T0 cannot fire! (Need: 1 in P0 + 2 in P1)"); | ||
} | ||
} | ||
} | ||
|
||
fn print_net<Net: NetId>( | ||
net: Res<PetriNet<Net>>, | ||
tokens: Query<(Entity, &Token<Net>), Changed<Token<Net>>>, | ||
) { | ||
for (id, token) in &tokens { | ||
info!("== TOKEN {:?} STATE ==", id); | ||
info!("P0: {}", token.marks::<Pn<0>>()); | ||
info!("P1: {}", token.marks::<Pn<1>>()); | ||
info!("T0 enabled: {}", net.enabled::<Tn<0>>(token)); | ||
info!("P2: {}", token.marks::<Pn<2>>()); | ||
info!("====================="); | ||
} | ||
} |
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 |
---|---|---|
@@ -1,19 +1,16 @@ | ||
//! A Petri net plugin for [Bevy Engine](https://github.com/bevyengine/bevy). | ||
//#![doc = include_str!("../README.md")] | ||
#![doc = include_str!("../README.md")] | ||
#![deny(missing_docs)] | ||
#![deny(clippy::all)] | ||
|
||
pub mod net; | ||
pub mod plugin; | ||
pub mod token; | ||
pub use crate::net::place::{Place, PlaceId, Pn}; | ||
pub use crate::net::trans::{Arcs, Tn, Trans, TransId, W}; | ||
pub use crate::net::{NetId, Nn, PetriNet, PetriNetBuilder}; | ||
pub use crate::plugin::PetriNetPlugin; | ||
pub use crate::token::Token; | ||
|
||
// todo: | ||
// - petri net resource / component | ||
// - tokens are components, so they can be attached to entities | ||
// - state management via petri nets (total game state & entity states) | ||
// - safe, special cases of petri nets explored | ||
// - workflow patterns | ||
// - one petri net reused by multiple tokens (colored) | ||
mod net; | ||
mod plugin; | ||
mod token; | ||
|
||
#[cfg(test)] | ||
mod tests {} |
Oops, something went wrong.