Compare commits
No commits in common. "23e8713b6d81b311fc2a0d8c7608e15a8fd24347" and "01d54c88e74e19f478bac959ad3138f5b89a1989" have entirely different histories.
23e8713b6d
...
01d54c88e7
43
src/main.rs
43
src/main.rs
|
@ -1,7 +1,4 @@
|
||||||
use bevy::prelude::*;
|
use bevy::prelude::*;
|
||||||
use bevy::render::pass::ClearColor;
|
|
||||||
use rand::prelude::random;
|
|
||||||
use std::time::Duration;
|
|
||||||
|
|
||||||
const ARENA_WIDTH: u32 = 10;
|
const ARENA_WIDTH: u32 = 10;
|
||||||
const ARENA_HEIGHT: u32 = 10;
|
const ARENA_HEIGHT: u32 = 10;
|
||||||
|
@ -28,23 +25,12 @@ impl Size {
|
||||||
struct SnakeHead;
|
struct SnakeHead;
|
||||||
struct Materials {
|
struct Materials {
|
||||||
head_material: Handle<ColorMaterial>,
|
head_material: Handle<ColorMaterial>,
|
||||||
food_material: Handle<ColorMaterial>,
|
|
||||||
}
|
|
||||||
|
|
||||||
struct Food;
|
|
||||||
|
|
||||||
struct FoodSpawnTimer(Timer);
|
|
||||||
impl Default for FoodSpawnTimer {
|
|
||||||
fn default() -> Self {
|
|
||||||
Self(Timer::new(Duration::from_millis(1000), true))
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn setup(commands: &mut Commands, mut materials: ResMut<Assets<ColorMaterial>>) {
|
fn setup(commands: &mut Commands, mut materials: ResMut<Assets<ColorMaterial>>) {
|
||||||
commands.spawn(Camera2dBundle::default());
|
commands.spawn(Camera2dBundle::default());
|
||||||
commands.insert_resource(Materials {
|
commands.insert_resource(Materials {
|
||||||
head_material: materials.add(Color::rgb(0.7, 0.7, 0.7).into()),
|
head_material: materials.add(Color::rgb(0.7, 0.7, 0.7).into()),
|
||||||
food_material: materials.add(Color::rgb(1.0, 0.0, 1.0).into()),
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -106,42 +92,13 @@ fn position_translation(windows: Res<Windows>, mut q: Query<(&Position, &mut Tra
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn food_spawner(
|
|
||||||
commands: &mut Commands,
|
|
||||||
materials: Res<Materials>,
|
|
||||||
time: Res<Time>,
|
|
||||||
mut timer: Local<FoodSpawnTimer>,
|
|
||||||
) {
|
|
||||||
if timer.0.tick(time.delta_seconds().finished()) {
|
|
||||||
commands
|
|
||||||
.spawn(SpriteBundle {
|
|
||||||
material: materials.food_material.clone(),
|
|
||||||
..Default::default()
|
|
||||||
})
|
|
||||||
.with(Food)
|
|
||||||
.with(Position {
|
|
||||||
x: (random::<f32>() * ARENA_WIDTH as f32) as i32,
|
|
||||||
y: (random::<f32>() * ARENA_HEIGHT as f32) as i32,
|
|
||||||
})
|
|
||||||
.with(Size::square(0.8));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
App::build()
|
App::build()
|
||||||
.add_resource(ClearColor(Color::rgb(0.04, 0.04, 0.04)))
|
|
||||||
.add_resource(WindowDescriptor {
|
|
||||||
title: "Snake!".to_string(),
|
|
||||||
width: 500.0,
|
|
||||||
height: 500.0,
|
|
||||||
..Default::default()
|
|
||||||
})
|
|
||||||
.add_startup_system(setup.system())
|
.add_startup_system(setup.system())
|
||||||
.add_startup_stage("game_setup", SystemStage::single(spawn_snake.system()))
|
.add_startup_stage("game_setup", SystemStage::single(spawn_snake.system()))
|
||||||
.add_system(snake_movement.system())
|
.add_system(snake_movement.system())
|
||||||
.add_system(position_translation.system())
|
.add_system(position_translation.system())
|
||||||
.add_system(size_scaling.system())
|
.add_system(size_scaling.system())
|
||||||
.add_system(food_spawner.system())
|
|
||||||
.add_plugins(DefaultPlugins)
|
.add_plugins(DefaultPlugins)
|
||||||
.run();
|
.run();
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user