components.rs 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. use bevy::prelude::{ColorMaterial, Entity, Handle, SystemLabel};
  2. // constants
  3. pub const ARENA_WIDTH: u32 = 10;
  4. pub const ARENA_HEIGHT: u32 = 10;
  5. // basic game components
  6. #[derive(Default, Copy, Clone, Eq, PartialEq, Hash)]
  7. pub struct Position {
  8. pub x: i32,
  9. pub y: i32,
  10. }
  11. pub struct GridSize {
  12. pub width: f32,
  13. pub height: f32,
  14. }
  15. impl GridSize {
  16. pub fn square(x: f32) -> GridSize {
  17. Self {
  18. width: x,
  19. height: x,
  20. }
  21. }
  22. }
  23. #[derive(PartialEq, Copy, Clone)]
  24. pub enum Direction {
  25. Left,
  26. Up,
  27. Right,
  28. Down,
  29. }
  30. impl Direction {
  31. pub fn opposite(self) -> Self {
  32. match self {
  33. Self::Left => Self::Right,
  34. Self::Right => Self::Left,
  35. Self::Up => Self::Down,
  36. Self::Down => Self::Up,
  37. }
  38. }
  39. }
  40. // material resource
  41. pub struct Materials {
  42. pub head_material: Handle<ColorMaterial>,
  43. pub segment_material: Handle<ColorMaterial>,
  44. pub food_material: Handle<ColorMaterial>,
  45. }
  46. // food!
  47. pub struct Food;
  48. // snake-related components
  49. pub struct SnakeHead {
  50. pub direction: Direction,
  51. }
  52. pub struct SnakeSegment;
  53. // snake-related resources
  54. #[derive(Default)]
  55. pub struct SnakeSegments {
  56. pub segments: Vec<Entity>,
  57. }
  58. #[derive(Default)]
  59. pub struct LastTailPosition {
  60. pub pos: Option<Position>,
  61. }
  62. // snake phase labels
  63. #[derive(SystemLabel, Debug, Hash, PartialEq, Eq, Clone)]
  64. pub enum SnakeMovement {
  65. Input,
  66. Movement,
  67. Eating,
  68. Growth,
  69. }
  70. // events
  71. pub struct GrowthEvent;
  72. pub struct GameOverEvent;