Documentation
/*
Copyright 2018 Johannes Boczek

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

	http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

extern crate aecs;

use aecs::*;

struct Value {
	val: u16
}

struct CountSystem {
	cnt: u64
}

struct Multiplier {
	amnt: u8
}

impl Component for Value {
	const ID: u8 = 0;
}

impl System<Multiplier> for CountSystem {
	fn handle(&mut self, multiplier: &mut Multiplier, entity: &mut Entity) -> bool {
		let val = entity.get::<Value>()
			.unwrap();
		
		self.cnt += val.val as u64 * multiplier.amnt as u64;
		
		false
	}
	
	fn bitmask(&self) -> u64 {
		Value::BITMASK
	}
}

#[test]
fn test_ecs() {
	let mut world = World::<Multiplier>::new();
	let mut entity = Entity::new();
	let mut entity2 = Entity::new();
	let count = world.register(CountSystem { cnt: 5 });
	let mut multiplier = Multiplier {
		amnt: 1
	};
	
	entity.add(Value { val: 10 });
	entity2.add(Value { val: 3 });
	world.add(entity);
	world.add(entity2);
	
	let total = {
		let guard = count.lock()
			.unwrap();
		
		guard.cnt
	};
	
	assert_eq!(total, 5);
	
	world.dispatch(&mut multiplier);
	
	let total = {
		let guard = count.lock()
			.unwrap();
		
		guard.cnt
	};
	
	assert_eq!(total, 18);
	
	multiplier.amnt = 2;
	world.dispatch(&mut multiplier);
	
	let total = {
		let guard = count.lock()
			.unwrap();
		
		guard.cnt
	};
	
	assert_eq!(total, 44);
}