Start over with newer version of Bevy
This commit is contained in:
parent
676dbf0a96
commit
3218ee8307
1704
Cargo.lock
generated
1704
Cargo.lock
generated
File diff suppressed because it is too large
Load Diff
@ -7,5 +7,5 @@ edition = "2018"
|
|||||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
bevy = "0.2.1"
|
bevy = "0.4.0"
|
||||||
rand = "0.7.3"
|
rand = "0.7.3"
|
||||||
|
183
src/main.rs
183
src/main.rs
@ -1,189 +1,12 @@
|
|||||||
use bevy::prelude::*;
|
use bevy::prelude::*;
|
||||||
use bevy::render::pass::ClearColor;
|
|
||||||
use std::time::Duration;
|
|
||||||
|
|
||||||
const ARENA_WIDTH: u32 = 40;
|
fn setup(commands: &mut Commands) {
|
||||||
const ARENA_HEIGHT: u32 = 40;
|
commands.spawn(Camera2dBundle::default());
|
||||||
|
|
||||||
#[derive(Default, Copy, Clone, Debug, Eq, PartialEq, Hash)]
|
|
||||||
struct Position {
|
|
||||||
x: i32,
|
|
||||||
y: i32,
|
|
||||||
}
|
|
||||||
|
|
||||||
struct Size {
|
|
||||||
width: f32,
|
|
||||||
height: f32,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Size {
|
|
||||||
pub fn square(x: f32) -> Self {
|
|
||||||
Self {
|
|
||||||
width: x,
|
|
||||||
height: x,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(PartialEq, Copy, Clone, Debug)]
|
|
||||||
enum Direction {
|
|
||||||
Left,
|
|
||||||
Up,
|
|
||||||
Right,
|
|
||||||
Down,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Direction {
|
|
||||||
fn opposite(self: &Self) -> Self {
|
|
||||||
match self {
|
|
||||||
Self::Left => Self::Right,
|
|
||||||
Self::Right => Self::Left,
|
|
||||||
Self::Up => Self::Down,
|
|
||||||
Self::Down => Self::Up,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
struct SegmentMaterial(Handle<ColorMaterial>);
|
|
||||||
|
|
||||||
#[derive(Default)]
|
|
||||||
struct SnakeSegment {
|
|
||||||
next_segment: Option<Entity>,
|
|
||||||
}
|
|
||||||
|
|
||||||
struct SnakeHead {
|
|
||||||
direction: Direction,
|
|
||||||
next_segment: Entity,
|
|
||||||
}
|
|
||||||
struct SnakeMoveTimer(Timer);
|
|
||||||
struct HeadMaterial(Handle<ColorMaterial>);
|
|
||||||
|
|
||||||
fn spawn_segment(commands: &mut Commands, material: Handle<ColorMaterial>, position: Position) {
|
|
||||||
commands
|
|
||||||
.spawn(SpriteComponents {
|
|
||||||
material,
|
|
||||||
..Default::default()
|
|
||||||
})
|
|
||||||
.with(SnakeSegment { next_segment: None })
|
|
||||||
.with(position)
|
|
||||||
.with(Size::square(0.65));
|
|
||||||
}
|
|
||||||
|
|
||||||
fn snake_movement(
|
|
||||||
time: Res<Time>,
|
|
||||||
keyboard_input: Res<Input<KeyCode>>,
|
|
||||||
mut snake_timer: ResMut<SnakeMoveTimer>,
|
|
||||||
mut head_positions: Query<(&mut SnakeHead, &mut Position)>,
|
|
||||||
) {
|
|
||||||
snake_timer.0.tick(time.delta_seconds);
|
|
||||||
for (mut head, mut head_pos) in &mut head_positions.iter() {
|
|
||||||
let mut dir: Direction = head.direction;
|
|
||||||
if keyboard_input.pressed(KeyCode::Left) {
|
|
||||||
dir = Direction::Left;
|
|
||||||
}
|
|
||||||
if keyboard_input.pressed(KeyCode::Right) {
|
|
||||||
dir = Direction::Right;
|
|
||||||
}
|
|
||||||
if keyboard_input.pressed(KeyCode::Down) {
|
|
||||||
dir = Direction::Down;
|
|
||||||
}
|
|
||||||
if keyboard_input.pressed(KeyCode::Up) {
|
|
||||||
dir = Direction::Up;
|
|
||||||
}
|
|
||||||
if dir != head.direction.opposite() {
|
|
||||||
head.direction = dir;
|
|
||||||
}
|
|
||||||
if snake_timer.0.finished {
|
|
||||||
match &head.direction {
|
|
||||||
Direction::Left => {
|
|
||||||
head_pos.x -= 1;
|
|
||||||
}
|
|
||||||
Direction::Right => {
|
|
||||||
head_pos.x += 1;
|
|
||||||
}
|
|
||||||
Direction::Up => {
|
|
||||||
head_pos.y -= 1;
|
|
||||||
}
|
|
||||||
Direction::Down => {
|
|
||||||
head_pos.y += 1;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn position_translation(windows: Res<Windows>, mut q: Query<(&Position, &mut Transform)>) {
|
|
||||||
fn convert(p: f32, bound_window: f32, bound_game: f32) -> f32 {
|
|
||||||
p / bound_game * bound_window - (bound_window / 2.)
|
|
||||||
}
|
|
||||||
let window = windows.get_primary().unwrap();
|
|
||||||
for (pos, mut transform) in &mut q.iter() {
|
|
||||||
transform.set_translation(Vec3::new(
|
|
||||||
convert(pos.x as f32, window.width as f32, ARENA_WIDTH as f32),
|
|
||||||
convert(pos.y as f32, window.height as f32, ARENA_HEIGHT as f32),
|
|
||||||
0.0,
|
|
||||||
))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn size_scaling(windows: Res<Windows>, mut q: Query<(&Size, &mut Sprite)>) {
|
|
||||||
for (size, mut sprite) in &mut q.iter() {
|
|
||||||
let window = windows.get_primary().unwrap();
|
|
||||||
sprite.size = Vec2::new(
|
|
||||||
size.width as f32 / ARENA_WIDTH as f32 * window.width as f32,
|
|
||||||
size.height as f32 / ARENA_HEIGHT as f32 * window.height as f32,
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn setup(mut commands: Commands, mut materials: ResMut<Assets<ColorMaterial>>) {
|
|
||||||
commands.spawn(Camera2dComponents::default());
|
|
||||||
commands.insert_resource(HeadMaterial(
|
|
||||||
materials.add(Color::rgb(0.7, 0.7, 0.7).into()),
|
|
||||||
));
|
|
||||||
commands.insert_resource(SegmentMaterial(
|
|
||||||
materials.add(Color::rgb(0.3, 0.3, 0.3).into()),
|
|
||||||
));
|
|
||||||
}
|
|
||||||
|
|
||||||
fn game_setup(
|
|
||||||
mut commands: Commands,
|
|
||||||
head_material: Res<HeadMaterial>,
|
|
||||||
segment_material: Res<SegmentMaterial>,
|
|
||||||
) {
|
|
||||||
spawn_segment(&mut commands, segment_material.0, Position { x: 10, y: 9 });
|
|
||||||
let first_segment = commands.current_entity().unwrap();
|
|
||||||
commands
|
|
||||||
.spawn(SpriteComponents {
|
|
||||||
material: head_material.0,
|
|
||||||
..Default::default()
|
|
||||||
})
|
|
||||||
.with(SnakeHead {
|
|
||||||
direction: Direction::Up,
|
|
||||||
next_segment: first_segment,
|
|
||||||
})
|
|
||||||
.with(Position { x: 10, y: 10 })
|
|
||||||
.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: 2000,
|
|
||||||
height: 2000,
|
|
||||||
..Default::default()
|
|
||||||
})
|
|
||||||
.add_resource(SnakeMoveTimer(Timer::new(
|
|
||||||
Duration::from_millis((150.) as u64),
|
|
||||||
true,
|
|
||||||
)))
|
|
||||||
.add_startup_system(setup.system())
|
.add_startup_system(setup.system())
|
||||||
.add_startup_stage("game_setup")
|
.add_plugins(DefaultPlugins)
|
||||||
.add_startup_system_to_stage("game_setup", game_setup.system())
|
|
||||||
.add_system(snake_movement.system())
|
|
||||||
.add_system(position_translation.system())
|
|
||||||
.add_system(size_scaling.system())
|
|
||||||
.add_default_plugins()
|
|
||||||
.run();
|
.run();
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user