1234567891011121314151617181920212223242526272829303132333435363738 |
- use bevy::prelude::*;
- use crate::components as c;
- pub fn setup(mut commands: Commands, mut materials: ResMut<Assets<ColorMaterial>>) {
- commands.spawn_bundle(OrthographicCameraBundle::new_2d());
- commands.insert_resource(c::Materials {
- head_material: materials.add(Color::rgb(0.7, 0.7, 0.7).into()),
- segment_material: materials.add(Color::rgb(0.3, 0.3, 0.3).into()),
- food_material: materials.add(Color::rgb(1.0, 0.0, 1.0).into()),
- });
- }
- pub fn size_scaling(windows: Res<Windows>, mut q: Query<(&c::GridSize, &mut Sprite)>) {
- let window = windows.get_primary().unwrap();
- for (size, mut sprite) in q.iter_mut() {
- sprite.size = Vec2::new(
- size.width / c::ARENA_WIDTH as f32 * window.width() as f32,
- size.height / c::ARENA_HEIGHT as f32 * window.height() as f32,
- );
- }
- }
- pub fn position_translation(windows: Res<Windows>, mut q: Query<(&c::Position, &mut Transform)>) {
- fn convert(pos: f32, bound_window: f32, bound_game: f32) -> f32 {
- let tile_size = bound_window / bound_game;
- pos / bound_game * bound_window - (bound_window / 2.0) + (tile_size / 2.0)
- }
- let window = windows.get_primary().unwrap();
- for (pos, mut tf) in q.iter_mut() {
- tf.translation = Vec3::new(
- convert(pos.x as f32, window.width() as f32, c::ARENA_WIDTH as f32),
- convert(pos.y as f32, window.height() as f32, c::ARENA_HEIGHT as f32),
- 0.0,
- )
- }
- }
|