Files
bevy-snake/src/grid.rs
2022-08-21 13:46:08 +02:00

50 lines
1.1 KiB
Rust

use bevy::prelude::*;
use std::ops::{Add, AddAssign};
pub type Index = i16;
pub const SIZE: Index = 17;
pub const SEGMENT_SIZE: f32 = 20.;
#[derive(Component, Clone, Copy, Default, Debug, PartialEq, Eq)]
pub struct Coordinate(pub Index, pub Index);
impl Coordinate {
pub const fn splat(v: Index) -> Self {
Self(v, v)
}
pub const fn in_bounds(self) -> bool {
self.0 >= 0 && self.0 < SIZE && self.1 >= 0 && self.1 < SIZE
}
}
impl Add for Coordinate {
type Output = Self;
fn add(self, rhs: Self) -> Self::Output {
Self(self.0 + rhs.0, self.1 + rhs.1)
}
}
impl AddAssign for Coordinate {
fn add_assign(&mut self, rhs: Self) {
*self = Self(self.0 + rhs.0, self.1 + rhs.1);
}
}
impl From<Coordinate> for Vec2 {
fn from(grid_coordinate: Coordinate) -> Self {
(&grid_coordinate).into()
}
}
impl From<&Coordinate> for Vec2 {
fn from(grid_coordinate: &Coordinate) -> Self {
Self::new(
f32::from(grid_coordinate.0 - SIZE / 2)* SEGMENT_SIZE,
f32::from(grid_coordinate.1 - SIZE / 2) * SEGMENT_SIZE,
)
}
}