Compare commits

...

2 commits

Author SHA1 Message Date
7574ec20f5 restructure to allow cargo tests 2025-03-15 21:00:20 +01:00
fa10b38f99 make marble creation a weak claim 2025-03-15 20:13:48 +01:00
11 changed files with 90 additions and 50 deletions

View file

@ -6,7 +6,7 @@ use std::{
use serde::{Deserialize, Serialize};
use crate::{marble_engine::board::Board, userdata_dir};
use crate::{marble_engine::board::Board, util::userdata_dir};
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Blueprint {

View file

@ -15,7 +15,6 @@ use crate::{
theme::*,
ui::*,
util::*,
TILE_TEXTURE_SIZE,
};
const HEADER_HEIGHT: i32 = 40;

8
src/lib.rs Normal file
View file

@ -0,0 +1,8 @@
pub mod blueprint;
pub mod editor;
pub mod level;
pub mod marble_engine;
pub mod solution;
pub mod theme;
pub mod ui;
pub mod util;

View file

@ -5,14 +5,7 @@ use std::{
use raylib::prelude::*;
mod blueprint;
mod editor;
mod level;
mod marble_engine;
mod solution;
mod theme;
mod ui;
mod util;
use marble_machinations::*;
use editor::{Editor, ExitState};
use level::{Chapter, Level};
@ -23,8 +16,6 @@ use util::*;
const TITLE_TEXT: &str = concat!("Marble Machinations v", env!("CARGO_PKG_VERSION"));
pub const TILE_TEXTURE_SIZE: f32 = 16.0;
struct Game {
levels: Vec<LevelListEntry>,
level_scroll: usize,

View file

@ -3,12 +3,11 @@ use raylib::prelude::*;
pub mod board;
pub mod pos;
pub mod tile;
use crate::{theme::TILE_TEXTURE_SIZE, ui::draw_usize_small, util::Textures};
use board::Board;
use pos::*;
use tile::*;
use crate::{ui::draw_usize_small, Textures, TILE_TEXTURE_SIZE};
#[derive(Debug)]
pub struct Machine {
board: Board,
@ -261,19 +260,15 @@ impl Machine {
let Some(Tile::Open(OpenTile::Blank, claim)) = self.board.get_mut(pos) else {
unreachable!()
};
*claim = match claim {
Claim::Free => {
claim_positions.push(pos);
Claim::Claimed
}
Claim::Claimed | Claim::Blocked => Claim::Blocked,
_ => unreachable!(),
if claim.claim_indirect() {
claim_positions.push(pos);
}
}
// create new marbles
// new marbles are past old_marbles index, so will not move this step
for (pos, value, dir) in new_marbles {
let Some(Tile::Open(OpenTile::Blank, Claim::Claimed)) = self.board.get_mut(pos) else {
let Some(Tile::Open(OpenTile::Blank, Claim::ClaimedIndirect)) = self.board.get_mut(pos)
else {
continue;
};
self.board.set(pos, Tile::Marble { value, dir });
@ -291,16 +286,9 @@ impl Machine {
continue;
};
if let Tile::Open(_type, claim) = front_tile {
*claim = match claim {
Claim::Free => {
claim_positions.push(front_pos);
Claim::Claimed
}
Claim::ClaimedIndirect => Claim::Claimed,
Claim::BlockedIndirect => Claim::Claimed,
Claim::Claimed => Claim::Blocked,
Claim::Blocked => Claim::Blocked,
};
if claim.claim() {
claim_positions.push(front_pos);
}
} else {
let target_pos = match front_tile {
Tile::Arrow(d) => d.step(front_pos),
@ -312,16 +300,9 @@ impl Machine {
continue;
};
if let Tile::Open(_type, claim) = target_tile {
*claim = match claim {
Claim::Free => {
claim_positions.push(front_pos);
Claim::ClaimedIndirect
}
Claim::ClaimedIndirect => Claim::BlockedIndirect,
Claim::BlockedIndirect => Claim::BlockedIndirect,
Claim::Claimed => Claim::Claimed,
Claim::Blocked => Claim::Blocked,
};
if claim.claim_indirect() {
claim_positions.push(front_pos);
}
}
}
}

View file

@ -1,11 +1,11 @@
use crate::TILE_TEXTURE_SIZE;
use crate::{draw_scaled_texture, Textures};
use super::tile::*;
use super::Pos;
use super::PosInt;
use raylib::prelude::*;
use super::{tile::*, Pos, PosInt};
use crate::{
theme::TILE_TEXTURE_SIZE,
util::{draw_scaled_texture, Textures},
};
#[derive(Debug, Clone, PartialEq)]
pub struct Board {
tiles: Vec<Tile>,

View file

@ -485,3 +485,35 @@ impl Comparison {
}
}
}
impl Claim {
#[must_use]
/// returns `was_free`
pub fn claim(&mut self) -> bool {
let mut was_free = false;
*self = match self {
Claim::Free => {
was_free = true;
Claim::Claimed
}
Claim::ClaimedIndirect | Claim::BlockedIndirect => Claim::Claimed,
Claim::Claimed | Claim::Blocked => Claim::Blocked,
};
was_free
}
#[must_use]
/// returns `was_free`
pub fn claim_indirect(&mut self) -> bool {
let mut was_free = false;
*self = match self {
Claim::Free => {
was_free = true;
Claim::ClaimedIndirect
}
Claim::ClaimedIndirect => Claim::BlockedIndirect,
_ => *self,
};
was_free
}
}

View file

@ -6,7 +6,7 @@ use std::{
use serde::{Deserialize, Serialize};
use crate::{level::Level, userdata_dir};
use crate::{level::Level, util::userdata_dir};
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Solution {

View file

@ -1,5 +1,7 @@
use raylib::prelude::*;
pub const TILE_TEXTURE_SIZE: f32 = 16.0;
pub const BG_WORLD: Color = gray(48);
pub const FG_GRID: Color = gray(64);

View file

@ -1,6 +1,6 @@
use std::ops::Range;
use crate::{draw_scaled_texture, theme::*, MouseInput, Scroll, Textures};
use crate::{theme::*, util::draw_scaled_texture, util::MouseInput, util::Scroll, util::Textures};
use raylib::prelude::*;
#[derive(Debug)]

27
tests/main.rs Normal file
View file

@ -0,0 +1,27 @@
use marble_machinations::marble_engine::{board::Board, Machine};
#[test]
fn creating_marbles_cause_indirect_claim() {
let mut eng = Machine::new_empty();
eng.set_board(Board::parse(
"
I
o 2
B- o
B | |-*-|
|-+o | |
*-| |* -B B-
1 3
I I
",
));
for _ in 0..9 {
eng.step();
}
assert_eq!(eng.output(), [1, 2, 3]);
}