summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTom Barrett <tom@tombarrett.xyz>2021-03-21 19:33:05 +0100
committerTom Barrett <tom@tombarrett.xyz>2021-03-21 19:33:05 +0100
commit585d82473ccf566965ded0e50eea1669c3e1cdf3 (patch)
treef63ce7448ad95ae7ac4b2bed11550a315e9db0e6
parent420d0f36adeed7fa254497b49ef91a7574628082 (diff)
moving more to constants
-rw-r--r--src/cell.rs6
-rw-r--r--src/constants.rs11
-rw-r--r--src/main.rs56
3 files changed, 41 insertions, 32 deletions
diff --git a/src/cell.rs b/src/cell.rs
index d5d19e2..fc5d77d 100644
--- a/src/cell.rs
+++ b/src/cell.rs
@@ -69,7 +69,7 @@ impl Cell {
pub fn insert(mut cell_query: Query<(&mut Cell, &mut TextureAtlasSprite)>) {
for (mut cell, mut sprite) in cell_query.iter_mut() {
- if cell.occupant == Occupant::None && cell.y == constants::ROWS - 1 {
+ if cell.occupant == Occupant::None && cell.y == constants::GRID_SIZE - 1 {
cell.occupant = rand::random();
sprite.index = cell.occupant.to_index();
}
@@ -88,7 +88,7 @@ pub fn start_explosion(
}
pub fn check(mut cell_query: Query<(&mut Cell, &mut TextureAtlasSprite)>) {
- let mut cells = [[Cell::default(); constants::COLUMNS]; constants::ROWS];
+ let mut cells = [[Cell::default(); constants::GRID_SIZE]; constants::GRID_SIZE];
for (cell, _) in cell_query.iter_mut() {
cells[cell.x][cell.y] = *cell;
}
@@ -98,7 +98,7 @@ pub fn check(mut cell_query: Query<(&mut Cell, &mut TextureAtlasSprite)>) {
for (i, _) in cells.iter().enumerate() {
let mut c = Vec::new();
- for j in 0..constants::ROWS {
+ for j in 0..constants::GRID_SIZE {
if cells[i][j].occupant == last && last != Occupant::None {
c.push((i, j));
c.push((i, j - 1));
diff --git a/src/constants.rs b/src/constants.rs
index c33e30b..d8effa8 100644
--- a/src/constants.rs
+++ b/src/constants.rs
@@ -1,11 +1,8 @@
-pub const TILE_SCALE: f32 = 3.0;
+pub const TILE_SCALE: f32 = 3.5;
pub const TILE_SIZE: f32 = 16.0;
-pub const TILE_WIDTH: f32 = 16.0;
-pub const BORDER_SIZE: f32 = 3.0;
-pub const SHIFT_X: f32 = 50.0;
-pub const SHIFT_Y: f32 = 50.0;
-pub const COLUMNS: usize = 8;
-pub const ROWS: usize = 8;
+pub const WINDOW_HEIGHT: f32 = 600.0;
+pub const WINDOW_WIDTH: f32 = 800.0;
+pub const GRID_SIZE: usize = 8;
pub const TILESHEET_GREEN: u32 = 0;
pub const TILESHEET_YELLOW: u32 = 1;
diff --git a/src/main.rs b/src/main.rs
index 164ffe3..624aa5b 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -40,7 +40,12 @@ pub fn setup(
let background = asset_server.load("background.png");
let tileset = asset_server.load("tileset.png");
- let atlas = TextureAtlas::from_grid(tileset, Vec2::new(16.0, 16.0), 4, 5);
+ let atlas = TextureAtlas::from_grid(
+ tileset,
+ Vec2::new(constants::TILE_SIZE, constants::TILE_SIZE),
+ 4,
+ 5,
+ );
let atlas_handle = texture_atlases.add(atlas);
commands
@@ -53,7 +58,7 @@ pub fn setup(
y: 0.0,
z: 0.0,
},
- scale: Vec3::splat(3.5),
+ scale: Vec3::splat(constants::TILE_SCALE),
..Default::default()
},
..Default::default()
@@ -67,7 +72,7 @@ pub fn setup(
y: -200.0,
z: 0.0,
},
- scale: Vec3::splat(3.5),
+ scale: Vec3::splat(constants::TILE_SCALE),
..Default::default()
},
..Default::default()
@@ -79,28 +84,28 @@ pub fn setup(
transform: Transform {
translation: Vec3 {
x: 225.0,
- y: -200.0 + (-16.0) * 3.5,
+ y: -200.0 + (-constants::TILE_SIZE) * constants::TILE_SCALE,
z: 0.0,
},
- scale: Vec3::splat(3.5),
+ scale: Vec3::splat(constants::TILE_SCALE),
..Default::default()
},
..Default::default()
});
- for i in 0..constants::COLUMNS {
- for j in 0..constants::ROWS {
+ for i in 0..constants::GRID_SIZE {
+ for j in 0..constants::GRID_SIZE {
commands
.spawn(SpriteSheetBundle {
texture_atlas: atlas_handle.clone(),
sprite: TextureAtlasSprite::new(constants::TILESHEET_NONE1),
transform: Transform {
translation: Vec3 {
- x: ((i as f32) * 16.0 * 3.5) - 320.0,
- y: ((j as f32) * 16.0 * 3.5) - 160.0,
+ x: ((i as f32) * constants::TILE_SIZE * constants::TILE_SCALE) - 320.0,
+ y: ((j as f32) * constants::TILE_SIZE * constants::TILE_SCALE) - 160.0,
z: 1.0,
},
- scale: Vec3::splat(3.5),
+ scale: Vec3::splat(constants::TILE_SCALE),
..Default::default()
},
..Default::default()
@@ -161,13 +166,17 @@ fn cosmonaut_detect_system(
.get_primary()
.and_then(|window| window.cursor_position())
{
- cursor_position.x -= 400.0 - 16.0 * 3.5 * 0.5;
- cursor_position.y -= 300.0 - 16.0 * 3.5 * 0.5;
+ cursor_position.x -=
+ (constants::WINDOW_WIDTH / 2.0) - constants::TILE_SIZE * constants::TILE_SCALE * 0.5;
+ cursor_position.y -=
+ (constants::WINDOW_HEIGHT / 2.0) - constants::TILE_SIZE * constants::TILE_SCALE * 0.5;
for (transform, sprite) in cell_query.iter_mut() {
if transform.translation.x < cursor_position.x
- && transform.translation.x + 16.0 * 3.5 > cursor_position.x
+ && transform.translation.x + constants::TILE_SIZE * constants::TILE_SCALE
+ > cursor_position.x
&& transform.translation.y < cursor_position.y
- && transform.translation.y + 16.0 * 3.5 > cursor_position.y
+ && transform.translation.y + constants::TILE_SIZE * constants::TILE_SCALE
+ > cursor_position.y
&& sprite.index == constants::TILESHEET_COSMONAUT1
{
commands
@@ -191,8 +200,10 @@ fn mouse_system(
.get_primary()
.and_then(|window| window.cursor_position())
{
- cursor_position.x -= 400.0 - 16.0 * 3.5 * 0.5;
- cursor_position.y -= 300.0 - 16.0 * 3.5 * 0.5;
+ cursor_position.x -=
+ (constants::WINDOW_WIDTH / 2.0) - constants::TILE_SIZE * constants::TILE_SCALE * 0.5;
+ cursor_position.y -=
+ (constants::WINDOW_HEIGHT / 2.0) - constants::TILE_SIZE * constants::TILE_SCALE * 0.5;
for (cell, _, mut sprite) in cell_query.iter_mut() {
if cell.selected {
@@ -204,14 +215,15 @@ fn mouse_system(
}
}
- for (mut cell, transform, mut sprite) in cell_query.iter_mut() {
+ for (mut cell, transform, _) in cell_query.iter_mut() {
if transform.translation.x < cursor_position.x
- && transform.translation.x + 16.0 * 3.5 > cursor_position.x
+ && transform.translation.x + constants::TILE_SIZE * constants::TILE_SCALE
+ > cursor_position.x
&& transform.translation.y < cursor_position.y
- && transform.translation.y + 16.0 * 3.5 > cursor_position.y
+ && transform.translation.y + constants::TILE_SIZE * constants::TILE_SCALE
+ > cursor_position.y
{
cell.hovered = true;
- sprite.color.set_a(0.5);
if mouse_button_input.just_pressed(MouseButton::Left) {
cell.selected = true;
}
@@ -273,8 +285,8 @@ pub fn main() {
App::build()
.add_resource(WindowDescriptor {
title: "gems".to_string(),
- width: 800.0,
- height: 600.0,
+ width: constants::WINDOW_WIDTH,
+ height: constants::WINDOW_HEIGHT,
resizable: false,
..Default::default()
})