First commit

This commit is contained in:
Timothy Warren 2020-11-04 10:09:02 -05:00
commit feb2fc0ce6
4 changed files with 3444 additions and 0 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
/target

3266
Cargo.lock generated Normal file

File diff suppressed because it is too large Load Diff

11
Cargo.toml Normal file
View File

@ -0,0 +1,11 @@
[package]
name = "bevy-snake"
version = "0.1.0"
authors = ["Timothy Warren <twarren@nabancard.com>"]
edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
bevy = "0.2.1"
rand = "0.7.3"

166
src/main.rs Normal file
View File

@ -0,0 +1,166 @@
use bevy::prelude::*;
use bevy::render::pass::ClearColor;
use std::time::Duration;
const ARENA_WIDTH: u32 = 40;
const ARENA_HEIGHT: u32 = 40;
#[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,
}
}
}
#[derive(Default)]
struct SnakeSegment {
next_segment: Option<Entity>
}
struct SnakeHead {
direction: Direction,
next_segment: Entity,
}
struct SnakeMoveTimer(Timer);
struct HeadMaterial(Handle<ColorMaterial>);
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()),
));
}
fn game_setup(mut commands: Commands, head_material: Res<HeadMaterial>) {
commands
.spawn(SpriteComponents {
material: head_material.0,
sprite: Sprite::new(Vec2::new(10.0, 10.0)),
..Default::default()
})
.with(SnakeHead { direction: Direction::Up })
.with(Position { x: 10, y: 10 })
.with(Size::square(0.8));
}
fn main() {
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_stage("game_setup")
.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();
}