summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/cell.rs79
-rw-r--r--src/constants.rs50
-rw-r--r--src/main.rs81
3 files changed, 120 insertions, 90 deletions
diff --git a/src/cell.rs b/src/cell.rs
index e4be4ac..6a818c5 100644
--- a/src/cell.rs
+++ b/src/cell.rs
@@ -1,10 +1,48 @@
use crate::constants;
use bevy::prelude::*;
+use bevy::utils::Duration;
use rand::{
distributions::{Distribution, Standard},
Rng,
};
+#[derive(Component)]
+pub struct AnimationTimer {
+ timer: Timer,
+}
+
+impl AnimationTimer {
+ pub fn from_seconds(duration: f32, repeating: bool) -> AnimationTimer {
+ AnimationTimer {
+ timer: Timer::from_seconds(duration, repeating),
+ }
+ }
+
+ pub fn finished(&self) -> bool {
+ self.timer.finished()
+ }
+
+ pub fn tick(&mut self, delta: Duration) -> &Timer {
+ self.timer.tick(delta)
+ }
+
+ pub fn just_finished(&self) -> bool {
+ self.timer.just_finished()
+ }
+
+ pub fn reset(&mut self) {
+ self.timer.reset();
+ }
+
+ pub fn set_duration(&mut self, duration: Duration) {
+ self.timer.set_duration(duration);
+ }
+
+ pub fn repeating(&self) -> bool {
+ self.timer.repeating()
+ }
+}
+
#[derive(Debug, PartialEq, Clone, Copy)]
pub enum Occupant {
None,
@@ -23,7 +61,7 @@ impl Default for Occupant {
}
impl Occupant {
- pub fn to_index(&self) -> u32 {
+ pub fn to_index(&self) -> usize {
match self {
Occupant::Green => constants::TILESHEET_GREEN,
Occupant::Yellow => constants::TILESHEET_YELLOW,
@@ -49,7 +87,7 @@ impl Distribution<Occupant> for Standard {
}
}
-#[derive(Debug, Clone, Default, Copy)]
+#[derive(Debug, Clone, Default, Copy, Component)]
pub struct Cell {
pub x: usize,
pub y: usize,
@@ -73,37 +111,40 @@ impl Cell {
&mut self,
occupant: Occupant,
sprite: &mut TextureAtlasSprite,
- visible: &mut Visible,
+ visibility: &mut Visibility,
) {
self.occupant = occupant;
sprite.index = self.occupant.to_index();
if self.occupant == Occupant::None {
- visible.is_visible = false;
+ visibility.is_visible = false;
} else {
- visible.is_visible = true;
+ visibility.is_visible = true;
}
}
}
-pub fn insert(mut q: Query<(&mut Cell, &mut TextureAtlasSprite, &mut Visible)>) {
- for (mut cell, mut sprite, mut visible) in q.iter_mut() {
+pub fn insert(mut q: Query<(&mut Cell, &mut TextureAtlasSprite, &mut Visibility)>) {
+ for (mut cell, mut sprite, mut visibility) in q.iter_mut() {
if cell.occupant == Occupant::None && cell.y == constants::GRID_SIZE - 1 {
- cell.set_occupant(rand::random(), &mut sprite, &mut visible);
+ cell.set_occupant(rand::random(), &mut sprite, &mut visibility);
}
}
}
-pub fn start_explosion(mut commands: Commands, mut q: Query<(Entity, &Cell), Without<Timer>>) {
+pub fn start_explosion(
+ mut commands: Commands,
+ mut q: Query<(Entity, &Cell), Without<AnimationTimer>>,
+) {
for (entity, cell) in q.iter_mut() {
if cell.occupant == Occupant::Explosion {
commands
.entity(entity)
- .insert(Timer::from_seconds(0.1, true));
+ .insert(AnimationTimer::from_seconds(0.1, true));
}
}
}
-pub fn check(mut q: Query<(&mut Cell, &mut TextureAtlasSprite, &mut Visible)>) {
+pub fn check(mut q: Query<(&mut Cell, &mut TextureAtlasSprite, &mut Visibility)>) {
let mut cells = [[Cell::default(); constants::GRID_SIZE]; constants::GRID_SIZE];
for (cell, _, _) in q.iter_mut() {
cells[cell.x][cell.y] = *cell;
@@ -150,39 +191,39 @@ pub fn check(mut q: Query<(&mut Cell, &mut TextureAtlasSprite, &mut Visible)>) {
for c in connected.iter() {
for (i, j) in c.iter() {
- for (mut cell, mut sprite, mut visible) in q.iter_mut() {
+ for (mut cell, mut sprite, mut visibility) in q.iter_mut() {
if &cell.x == i && &cell.y == j && cell.occupant != Occupant::Explosion {
- cell.set_occupant(Occupant::Explosion, &mut sprite, &mut visible);
+ cell.set_occupant(Occupant::Explosion, &mut sprite, &mut visibility);
}
}
}
}
}
-pub fn falling(mut q: Query<(&mut Cell, &mut TextureAtlasSprite, &mut Visible)>) {
+pub fn falling(mut q: Query<(&mut Cell, &mut TextureAtlasSprite, &mut Visibility)>) {
let mut have_gems = Vec::new();
- for (cell, _sprite, _visible) in q.iter_mut() {
+ for (cell, _sprite, _visibility) in q.iter_mut() {
if cell.occupant != Occupant::None {
have_gems.push(*cell);
}
}
let mut moved_gems = Vec::new();
- for (mut cell, mut sprite, mut visible) in q.iter_mut() {
+ for (mut cell, mut sprite, mut visibility) in q.iter_mut() {
if cell.occupant == Occupant::None {
if let Some(c) = have_gems
.iter()
.find(|&c| (c.x, c.y) == (cell.x, cell.y + 1))
{
- cell.set_occupant(c.occupant, &mut sprite, &mut visible);
+ cell.set_occupant(c.occupant, &mut sprite, &mut visibility);
moved_gems.push(c);
}
}
}
- for (mut cell, mut sprite, mut visible) in q.iter_mut() {
+ for (mut cell, mut sprite, mut visibility) in q.iter_mut() {
if moved_gems.iter().any(|c| (c.x, c.y) == (cell.x, cell.y)) {
- cell.set_occupant(Occupant::None, &mut sprite, &mut visible);
+ cell.set_occupant(Occupant::None, &mut sprite, &mut visibility);
}
}
}
diff --git a/src/constants.rs b/src/constants.rs
index 86bbe5e..af5dc06 100644
--- a/src/constants.rs
+++ b/src/constants.rs
@@ -4,32 +4,32 @@ pub const WINDOW_HEIGHT: f32 = 600.0;
pub const WINDOW_WIDTH: f32 = 800.0;
pub const GRID_SIZE: usize = 8;
-pub const TILESHEET_EXPLOSION1: u32 = 0;
-pub const TILESHEET_EXPLOSION2: u32 = 1;
-pub const TILESHEET_EXPLOSION3: u32 = 2;
-pub const TILESHEET_EXPLOSION4: u32 = 3;
-pub const TILESHEET_EXPLOSION5: u32 = 4;
+pub const TILESHEET_EXPLOSION1: usize = 0;
+pub const TILESHEET_EXPLOSION2: usize = 1;
+pub const TILESHEET_EXPLOSION3: usize = 2;
+pub const TILESHEET_EXPLOSION4: usize = 3;
+pub const TILESHEET_EXPLOSION5: usize = 4;
-pub const TILESHEET_GREEN: u32 = 5;
-pub const TILESHEET_YELLOW: u32 = 6;
-pub const TILESHEET_BLUE: u32 = 7;
-pub const TILESHEET_RED: u32 = 8;
-pub const TILESHEET_PURPLE: u32 = 9;
+pub const TILESHEET_GREEN: usize = 5;
+pub const TILESHEET_YELLOW: usize = 6;
+pub const TILESHEET_BLUE: usize = 7;
+pub const TILESHEET_RED: usize = 8;
+pub const TILESHEET_PURPLE: usize = 9;
-pub const TILESHEET_STAR1: u32 = 10;
-pub const TILESHEET_STAR2: u32 = 11;
-pub const TILESHEET_STAR3: u32 = 12;
-//pub const TILESHEET_X: u32 = 13;
-//pub const TILESHEET_X: u32 = 14;
+pub const TILESHEET_STAR1: usize = 10;
+pub const TILESHEET_STAR2: usize = 11;
+pub const TILESHEET_STAR3: usize = 12;
+//pub const TILESHEET_X: usize = 13;
+//pub const TILESHEET_X: usize = 14;
-pub const TILESHEET_COSMONAUT1: u32 = 15;
-pub const TILESHEET_VISOR1: u32 = 16;
-pub const TILESHEET_VISOR2: u32 = 17;
-//pub const TILESHEET_X: u32 = 18;
-//pub const TILESHEET_X: u32 = 19;
+pub const TILESHEET_COSMONAUT1: usize = 15;
+pub const TILESHEET_VISOR1: usize = 16;
+pub const TILESHEET_VISOR2: usize = 17;
+//pub const TILESHEET_X: usize = 18;
+//pub const TILESHEET_X: usize = 19;
-pub const TILESHEET_COSMONAUT2: u32 = 20;
-pub const TILESHEET_VISOR3: u32 = 21;
-pub const TILESHEET_VISOR4: u32 = 22;
-//pub const TILESHEET_X: u32 = 23;
-//pub const TILESHEET_X: u32 = 24;
+pub const TILESHEET_COSMONAUT2: usize = 20;
+pub const TILESHEET_VISOR3: usize = 21;
+pub const TILESHEET_VISOR4: usize = 22;
+//pub const TILESHEET_X: usize = 23;
+//pub const TILESHEET_X: usize = 24;
diff --git a/src/main.rs b/src/main.rs
index c69d791..d0fb782 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -1,11 +1,11 @@
use bevy::math::Vec3;
use bevy::prelude::*;
-use gems::cell::{self, Cell, Occupant};
+use gems::cell::{self, AnimationTimer, Cell, Occupant};
use gems::constants;
use rand::{thread_rng, Rng};
use std::time::Duration;
-fn star_spawning(mut commands: Commands, time: Res<Time>, mut q: Query<&mut Timer>) {
+fn star_spawning(mut commands: Commands, time: Res<Time>, mut q: Query<&mut AnimationTimer>) {
for mut timer in q.iter_mut() {
if !timer.repeating() {
timer.tick(time.delta());
@@ -17,16 +17,18 @@ fn star_spawning(mut commands: Commands, time: Res<Time>, mut q: Query<&mut Time
sprite: TextureAtlasSprite::new(constants::TILESHEET_STAR1),
transform: Transform {
translation: Vec3::new(
- thread_rng().gen_range(-300..300) as f32,
- thread_rng().gen_range(-200..300) as f32,
0.0,
+ 0.0,
+ //thread_rng().gen_range(-300.0..300.0),
+ //thread_rng().gen_range(-200.0..300.0),
+ 1.15,
),
- scale: Vec3::splat(thread_rng().gen_range(1..3) as f32),
+ //scale: Vec3::splat(thread_rng().gen_range(1.0..3.0)),
..Default::default()
},
..Default::default()
})
- .insert(Timer::from_seconds(0.25, true));
+ .insert(AnimationTimer::from_seconds(0.25, true));
}
}
}
@@ -35,7 +37,6 @@ fn star_spawning(mut commands: Commands, time: Res<Time>, mut q: Query<&mut Time
pub fn setup(
mut commands: Commands,
asset_server: Res<AssetServer>,
- mut materials: ResMut<Assets<ColorMaterial>>,
mut texture_atlases: ResMut<Assets<TextureAtlas>>,
) {
let background = asset_server.load("background.png");
@@ -52,7 +53,7 @@ pub fn setup(
commands.spawn_bundle(OrthographicCameraBundle::new_2d());
commands.spawn_bundle(SpriteBundle {
- material: materials.add(background.into()),
+ texture: background,
transform: Transform {
translation: Vec3::new(50.0, 0.0, 0.0),
scale: Vec3::splat(constants::TILE_SCALE),
@@ -61,9 +62,9 @@ pub fn setup(
..Default::default()
});
commands.spawn_bundle(SpriteBundle {
- material: materials.add(title.into()),
+ texture: title,
transform: Transform {
- translation: Vec3::new(240.0, 200.0, 0.0),
+ translation: Vec3::new(240.0, 200.0, 0.1),
scale: Vec3::splat(2.0),
..Default::default()
},
@@ -73,13 +74,13 @@ pub fn setup(
sprite: TextureAtlasSprite::new(constants::TILESHEET_COSMONAUT1),
texture_atlas: atlas_handle.clone(),
transform: Transform {
- translation: Vec3::new(225.0, -200.0, 0.0),
+ translation: Vec3::new(225.0, -200.0, 0.1),
scale: Vec3::splat(constants::TILE_SCALE),
..Default::default()
},
..Default::default()
});
- commands.spawn_bundle((Timer::from_seconds(1.0, false),));
+ commands.spawn_bundle((AnimationTimer::from_seconds(1.0, false),));
commands.spawn_bundle(SpriteSheetBundle {
sprite: TextureAtlasSprite::new(constants::TILESHEET_COSMONAUT2),
texture_atlas: atlas_handle.clone(),
@@ -87,7 +88,7 @@ pub fn setup(
translation: Vec3::new(
225.0,
-200.0 + (-constants::TILE_SIZE) * constants::TILE_SCALE,
- 0.0,
+ 0.1,
),
scale: Vec3::splat(constants::TILE_SCALE),
..Default::default()
@@ -100,15 +101,12 @@ pub fn setup(
commands
.spawn_bundle(SpriteSheetBundle {
texture_atlas: atlas_handle.clone(),
- visible: Visible {
- is_visible: false,
- is_transparent: true,
- },
+ visibility: Visibility { is_visible: false },
transform: Transform {
translation: Vec3::new(
((i as f32) * constants::TILE_SIZE * constants::TILE_SCALE) - 330.0,
((j as f32) * constants::TILE_SIZE * constants::TILE_SCALE) - 160.0,
- 1.0,
+ 0.1,
),
scale: Vec3::splat(constants::TILE_SCALE),
..Default::default()
@@ -126,12 +124,12 @@ fn animation(
mut q: Query<(
Entity,
Option<&mut Cell>,
- &mut Timer,
+ &mut AnimationTimer,
&mut TextureAtlasSprite,
- &mut Visible,
+ &mut Visibility,
)>,
) {
- for (entity, cell, mut timer, mut sprite, mut visible) in q.iter_mut() {
+ for (entity, cell, mut timer, mut sprite, mut visibility) in q.iter_mut() {
timer.tick(time.delta());
if timer.finished() {
let index = match sprite.index {
@@ -153,8 +151,8 @@ fn animation(
if let Some(index) = index {
sprite.index = index;
} else if let Some(mut cell) = cell {
- cell.set_occupant(Occupant::None, &mut sprite, &mut visible);
- commands.entity(entity).remove::<Timer>();
+ cell.set_occupant(Occupant::None, &mut sprite, &mut visibility);
+ commands.entity(entity).remove::<AnimationTimer>();
} else {
commands.entity(entity).despawn();
}
@@ -190,7 +188,7 @@ fn cosmonaut_detect(
transform: *transform,
..Default::default()
})
- .insert(Timer::from_seconds(0.1, true));
+ .insert(AnimationTimer::from_seconds(0.1, true));
}
}
}
@@ -242,16 +240,14 @@ fn mouse(
for (cell, _, _) in q.iter_mut() {
cells.push(*cell);
}
- if let Some(mut selected) = cells.clone().iter_mut().find(|c| c.selected) {
- if let Some(mut hovered) = cells.iter_mut().find(|c| c.hovered) {
+ if let Some(selected) = cells.clone().iter_mut().find(|c| c.selected) {
+ if let Some(hovered) = cells.iter_mut().find(|c| c.hovered) {
if (selected.x == hovered.x + 1 && selected.y == hovered.y)
|| (selected.x == hovered.x.overflowing_sub(1).0 && selected.y == hovered.y)
|| (selected.y == hovered.y + 1 && selected.x == hovered.x)
|| (selected.y == hovered.y.overflowing_sub(1).0 && selected.x == hovered.x)
{
- let tmp = selected.occupant;
- selected.occupant = hovered.occupant;
- hovered.occupant = tmp;
+ std::mem::swap(&mut selected.occupant, &mut hovered.occupant);
for (mut cell, _, mut sprite) in q.iter_mut() {
if cell.x == selected.x && cell.y == selected.y {
cell.occupant = selected.occupant;
@@ -271,23 +267,8 @@ fn mouse(
}
}
-pub struct GemsPlugin;
-impl Plugin for GemsPlugin {
- fn build(&self, app: &mut AppBuilder) {
- app.add_startup_system(setup.system());
- app.add_system(cell::insert.system());
- app.add_system(cell::falling.system());
- app.add_system(cell::check.system());
- app.add_system(cell::start_explosion.system());
- app.add_system(mouse.system());
- app.add_system(cosmonaut_detect.system());
- app.add_system(animation.system());
- app.add_system(star_spawning.system());
- }
-}
-
pub fn main() {
- App::build()
+ App::new()
.insert_resource(WindowDescriptor {
title: "gems".to_string(),
width: constants::WINDOW_WIDTH,
@@ -296,6 +277,14 @@ pub fn main() {
..Default::default()
})
.add_plugins(DefaultPlugins)
- .add_plugin(GemsPlugin)
+ .add_startup_system(setup)
+ .add_system(cell::insert)
+ .add_system(cell::falling)
+ .add_system(cell::check)
+ .add_system(cell::start_explosion)
+ .add_system(mouse)
+ .add_system(cosmonaut_detect)
+ .add_system(animation)
+ .add_system(star_spawning)
.run();
}