components.rs 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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, Debug)]
  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. #[derive(Debug)]
  50. pub struct SnakeHead {
  51. pub intention: Direction,
  52. pub direction: Direction,
  53. }
  54. pub struct SnakeSegment;
  55. // snake-related resources
  56. #[derive(Default)]
  57. pub struct SnakeSegments {
  58. pub segments: Vec<Entity>,
  59. }
  60. #[derive(Default)]
  61. pub struct LastTailPosition {
  62. pub pos: Option<Position>,
  63. }
  64. // snake phase labels
  65. #[derive(SystemLabel, Debug, Hash, PartialEq, Eq, Clone)]
  66. pub enum SnakeMovement {
  67. Input,
  68. Movement,
  69. Eating,
  70. Growth,
  71. }
  72. // events
  73. pub struct GrowthEvent;
  74. pub struct GameOverEvent;