From a97a1424017ba2b5bdec0bd7473b30acab47f3b9 Mon Sep 17 00:00:00 2001 From: Christian Nieves Date: Mon, 9 Dec 2024 16:28:12 -0600 Subject: [PATCH] working tail --- src/game/main.rs | 4 -- src/snake/main.rs | 179 +++++++++++++++++++++++++++++++++++----------- 2 files changed, 139 insertions(+), 44 deletions(-) diff --git a/src/game/main.rs b/src/game/main.rs index 2be6017..30ef4a0 100644 --- a/src/game/main.rs +++ b/src/game/main.rs @@ -32,10 +32,6 @@ fn change_names(mut query: Query<&mut Name, With>) { } } -fn hello_world() { - println!("hello world!"); -} - pub struct HelloPlugin; impl Plugin for HelloPlugin { diff --git a/src/snake/main.rs b/src/snake/main.rs index 66efd3b..2fc1ad0 100644 --- a/src/snake/main.rs +++ b/src/snake/main.rs @@ -1,9 +1,14 @@ -use bevy::{prelude::*, window::PrimaryWindow}; -use bevy_inspector_egui::quick::WorldInspectorPlugin; +use std::time::Duration; -const SNAKE_HEAD_COLOR: Color = Color::srgb(0.7, 1.0, 0.7); -const ARENA_WIDTH: u32 = 20; -const ARENA_HEIGHT: u32 = 15; +use bevy::{prelude::*, time::common_conditions::on_timer, window::PrimaryWindow}; +use bevy_inspector_egui::quick::WorldInspectorPlugin; +use rand::prelude::random; + +const SNAKE_HEAD_COLOR: Color = Color::srgb(0.5, 0.7, 0.5); +const SNAKE_SEGMENT_COLOR: Color = Color::srgb(0.3, 0.5, 0.3); +const FOOD_COLOR: Color = Color::srgb(0.7, 0.5, 0.7); +const ARENA_WIDTH: u32 = 15; +const ARENA_HEIGHT: u32 = 10; const WINDOW_WIDTH: f32 = 1280.0; #[derive(Eq, PartialEq, Copy, Clone)] @@ -31,6 +36,18 @@ struct SnakeHead { } #[derive(Component)] +struct SnakeSegment; + +#[derive(Resource, Default)] +struct SnakeSegments(Vec); + +#[derive(Resource, Default)] +struct LastTailPosition(Option); + +#[derive(Component)] +struct Food; + +#[derive(Component, Clone, Copy, Eq, PartialEq)] struct Position { x: i32, y: i32, @@ -51,10 +68,7 @@ impl Size { } } -#[derive(Resource)] -struct MovementTimer(Timer); - -fn snake_movement_input(input: Res>, mut heads: Query<(&mut SnakeHead)>) { +fn snake_movement_input(input: Res>, mut heads: Query<&mut SnakeHead>) { if let Some(mut head) = heads.iter_mut().next() { let dir: Direction = if input.pressed(KeyCode::ArrowUp) { Direction::Up @@ -75,24 +89,33 @@ fn snake_movement_input(input: Res>, mut heads: Query<(&mut } fn snake_movement( - input: Res>, - time: Res