components.rs 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. use bevy::prelude::{ColorMaterial, Entity, Handle, SystemLabel};
  2. pub const ARENA_WIDTH: u32 = 10;
  3. pub const ARENA_HEIGHT: u32 = 10;
  4. pub struct SnakeHead {
  5. pub direction: Direction,
  6. }
  7. pub struct Food;
  8. pub struct GrowthEvent;
  9. #[derive(Default, Copy, Clone, Eq, PartialEq, Hash)]
  10. pub struct Position {
  11. pub x: i32,
  12. pub y: i32,
  13. }
  14. pub struct GridSize {
  15. pub width: f32,
  16. pub height: f32,
  17. }
  18. impl GridSize {
  19. pub fn square(x: f32) -> GridSize {
  20. Self {
  21. width: x,
  22. height: x,
  23. }
  24. }
  25. }
  26. pub struct Materials {
  27. pub head_material: Handle<ColorMaterial>,
  28. pub segment_material: Handle<ColorMaterial>,
  29. pub food_material: Handle<ColorMaterial>,
  30. }
  31. #[derive(PartialEq, Copy, Clone)]
  32. pub enum Direction {
  33. Left,
  34. Up,
  35. Right,
  36. Down,
  37. }
  38. impl Direction {
  39. pub fn opposite(self) -> Self {
  40. match self {
  41. Self::Left => Self::Right,
  42. Self::Right => Self::Left,
  43. Self::Up => Self::Down,
  44. Self::Down => Self::Up,
  45. }
  46. }
  47. }
  48. #[derive(SystemLabel, Debug, Hash, PartialEq, Eq, Clone)]
  49. pub enum SnakeMovement {
  50. Input,
  51. Movement,
  52. Eating,
  53. Growth,
  54. }
  55. pub struct SnakeSegment;
  56. #[derive(Default)]
  57. pub struct SnakeSegments {
  58. pub segments: Vec<Entity>,
  59. }