summaryrefslogtreecommitdiff
path: root/src/main.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/main.rs')
-rw-r--r--src/main.rs81
1 files changed, 35 insertions, 46 deletions
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();
}