ECS test
This commit is contained in:
parent
a1cdb9e643
commit
3d911e8938
5
.gitignore
vendored
5
.gitignore
vendored
@ -8,3 +8,8 @@ Cargo.lock
|
|||||||
|
|
||||||
# These are backup files generated by rustfmt
|
# These are backup files generated by rustfmt
|
||||||
**/*.rs.bk
|
**/*.rs.bk
|
||||||
|
|
||||||
|
|
||||||
|
# Added by cargo
|
||||||
|
|
||||||
|
/target
|
||||||
|
9
Cargo.toml
Normal file
9
Cargo.toml
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
[package]
|
||||||
|
name = "slipwave"
|
||||||
|
version = "0.1.0"
|
||||||
|
edition = "2018"
|
||||||
|
|
||||||
|
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||||
|
|
||||||
|
[dependencies]
|
||||||
|
hypoloop = "0.1.7"
|
22
src/lib.rs
Normal file
22
src/lib.rs
Normal file
@ -0,0 +1,22 @@
|
|||||||
|
pub mod state {
|
||||||
|
pub struct Health(pub i32);
|
||||||
|
pub struct Name(pub &'static str);
|
||||||
|
|
||||||
|
pub struct World {
|
||||||
|
pub health_components: Vec<Option<Health>>,
|
||||||
|
pub name_components: Vec<Option<Name>>,
|
||||||
|
}
|
||||||
|
impl World {
|
||||||
|
pub fn new() -> Self {
|
||||||
|
Self {
|
||||||
|
health_components: Vec::new(),
|
||||||
|
name_components: Vec::new(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn new_entity(&mut self, health: Option<Health>, name: Option<Name>) {
|
||||||
|
self.health_components.push(health);
|
||||||
|
self.name_components.push(name);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
60
src/main.rs
Normal file
60
src/main.rs
Normal file
@ -0,0 +1,60 @@
|
|||||||
|
use hypoloop::core::{State, Loop};
|
||||||
|
use slipwave::state::{World, Health, Name};
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
// create a new sim loop
|
||||||
|
let mut sim = Loop::new();
|
||||||
|
sim.set_update_interval(20);
|
||||||
|
|
||||||
|
// create the game world
|
||||||
|
let mut world = World::new();
|
||||||
|
|
||||||
|
// add entities to the game world
|
||||||
|
// Icarus's health is *not* looking good.
|
||||||
|
world.new_entity(Some(Health(-10)), Some(Name("Icarus")));
|
||||||
|
|
||||||
|
// Prometheus is very healthy.
|
||||||
|
world.new_entity(Some(Health(100)), Some(Name("Prometheus")));
|
||||||
|
|
||||||
|
// Note that Zeus does not have a `Health` component.
|
||||||
|
world.new_entity(None, Some(Name("Zeus")));
|
||||||
|
|
||||||
|
// create a closure containing your update logic
|
||||||
|
let mut tick = move |state: &mut State| {
|
||||||
|
let zip = world
|
||||||
|
.health_components
|
||||||
|
.iter()
|
||||||
|
.zip(world.name_components.iter());
|
||||||
|
|
||||||
|
let with_health_and_name =
|
||||||
|
zip.filter_map(|(health, name): (&Option<Health>, &Option<Name>)| {
|
||||||
|
Some((health.as_ref()?, name.as_ref()?))
|
||||||
|
});
|
||||||
|
|
||||||
|
// health system
|
||||||
|
for (health, name) in with_health_and_name {
|
||||||
|
if health.0 < 0 {
|
||||||
|
println!("{} has perished!", name.0);
|
||||||
|
} else {
|
||||||
|
println!("{} is still alive!", name.0);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// print information about the current tick's timings
|
||||||
|
state.debug_time();
|
||||||
|
};
|
||||||
|
|
||||||
|
// create a closure containing your display logic
|
||||||
|
let mut display = move |state: &mut State| {
|
||||||
|
//
|
||||||
|
};
|
||||||
|
|
||||||
|
// run the simulation with your user-defined update and display logic
|
||||||
|
// initialize the sim (cleans internal clocks, etc.)
|
||||||
|
sim.init();
|
||||||
|
loop {
|
||||||
|
// "step" the sim forward
|
||||||
|
sim.step(&mut tick, &mut display);
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user